F
FabioAng
I implemented a codecvt facet (extracted from TCPL) but while it works well
with 'ifstream' it doesn't work at all with 'stringstream'.
Am I missing anything ? I tested it with MSVC 7.1
The code below shows what I'm trying to do:
#include <stdio.h>
#ifdef _WIN32
#include <io.h>
#else
#include <stdlib.h>
#endif
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
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(int argc, char* argv[])
{
locale loc1;
locale loc (loc1, new Cvt_to_upper);
stringstream oss;
oss.imbue(loc);
char name[] = "charupperXXXXXX";
mktemp(name);
string lower_str("abcd");
string upper_str;
{
ofstream out(name);
out << lower_str;
oss << lower_str;
}
{
ifstream in;
in.imbue(loc);
in.open(name);
in >> upper_str;
cout << "using ifstream: " << upper_str << std::endl;
oss >> upper_str;
cout << "using stringstream: " << upper_str << std::endl;
}
remove(name);
}
with 'ifstream' it doesn't work at all with 'stringstream'.
Am I missing anything ? I tested it with MSVC 7.1
The code below shows what I'm trying to do:
#include <stdio.h>
#ifdef _WIN32
#include <io.h>
#else
#include <stdlib.h>
#endif
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
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(int argc, char* argv[])
{
locale loc1;
locale loc (loc1, new Cvt_to_upper);
stringstream oss;
oss.imbue(loc);
char name[] = "charupperXXXXXX";
mktemp(name);
string lower_str("abcd");
string upper_str;
{
ofstream out(name);
out << lower_str;
oss << lower_str;
}
{
ifstream in;
in.imbue(loc);
in.open(name);
in >> upper_str;
cout << "using ifstream: " << upper_str << std::endl;
oss >> upper_str;
cout << "using stringstream: " << upper_str << std::endl;
}
remove(name);
}