libstdc++/9027: codecvt example from The C++ Programming Language does not work.

peturr02@ru.is peturr02@ru.is
Fri Dec 20 07:16:00 GMT 2002


>Number:         9027
>Category:       libstdc++
>Synopsis:       codecvt example from The C++ Programming Language does not work.
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Fri Dec 20 07:16:14 PST 2002
>Closed-Date:
>Last-Modified:
>Originator:     peturr02@ru.is
>Release:        gcc-3.2.1
>Organization:
>Environment:
Red Hat Linux 8.0 on i686; glibc-2.2.93
>Description:
In appendix D of The C++ Programming Language, 3rd edition <http://www.research.att.com/~bs/3rd_loc.pdf> Stroustrup describes a codecvt facet that converts input to uppercase. 

This example doesn't work on gcc-3.2.1, as basic_filebuf<char> never calls codecvt<char, char, mbstate_t>::in().
>How-To-Repeat:
See attachment for a modified version of the example.
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: text/plain; name="charupper.cc"
Content-Disposition: inline; filename="charupper.cc"

#include <locale>
#include <algorithm>
#include <cstddef>
#include <cstdio>
#include <string>
#include <fstream>
#include <iostream>
using namespace std;

class Cvt_to_upper : public codecvt<char, char, mbstate_t>
{
	typedef codecvt<char, char, mbstate_t> Base;

public:
	explicit Cvt_to_upper(size_t refs = 0)
		: Base(refs)
		{
		}

protected:
	result do_in(state_type&,
		     const extern_type* from, const extern_type* from_end,
		     const extern_type*& from_next,
		     intern_type* to, intern_type* to_end,
		     intern_type*& to_next) const
		{
			while (from < from_end && to < to_end)
				*to++ = toupper(*from++);

			to_next = to;
			from_next = from;
			return from == from_end ? ok : partial;
		}

	bool do_always_noconv() const throw()
		{
			return false;
		}
};

int main()
{
	locale loc1;
	locale loc (loc1, new Cvt_to_upper);
	
	char name[] = "charupperXXXXXX";
	mktemp(name);

	string str ("abcdefghijklmnopqrstuvwxyz");
	string tmp;

	{
		ofstream out (name);
		copy(str.begin(), str.end(),
		     ostreambuf_iterator<char>(out));
	}
	{
		ifstream in;
		in.imbue(loc);
		in.open(name);
		copy(istreambuf_iterator<char>(in),
		     istreambuf_iterator<char>(),
		     back_inserter(tmp));
	}

	remove(name);

	cout << tmp << endl;
	assert(tmp.size() == str.size());
	for (size_t i = 0; i < tmp.size(); ++i)
		assert(isupper(tmp[i]));

	return 0;
}



More information about the Gcc-prs mailing list