Transparent Unicode/Ascii using STL
-
In my application I want to access files, where the underlying character type (ASCII or Unicode) is transparent for the application. Suppose that in a reporting module a report is written to file, like this:
myfile << reportHeader << std::endl; for (all columns) myfile << columnname; myfile << std::endl; for (all records) { for (all columns) myfile << data; myfile << std::endl; }
Now, I want my application to transparently write the report to ASCII or Unicode files, depending on the user specification. Currently, you have to do it like this:
std::ofstream asciiFile; std::wofstream unicodeFile; if (user wants Unicode) { unicodeFile.open ("output.txt"); unicodeFile << reportHeader << std::endl; } else { asciiFile.open ("output.txt"); asciiFile << reportHeader << std::endl; } for (all columns) { if (user wants Unicode) unicodeFile << columnname; else asciiFile << columnName; } ... and so on
The disadvantages seem obvious: - clumsy, unreadable code (especially if the write-to-file logic is spread over several methods) - writing Unicode strings (std::wstring) to an Ascii stream doesn't even work; it produced garbage Therefore, I need a kind of transparent stream, so that I can write the following:
transparentstream myfile; myfile.setMode (ascii or unicode); myfile.open ("output.txt"); myfile << reportHeader << std::endl; for (all columns) myfile << columnname; myfile << std::endl; for (all records) { for (all columns) myfile << data; myfile << std::endl; }
Problems are: - from which stream do I inherit the transparentstream? - how do I define the transparentstream so all defined output operators keep on working? - where do I put the conversion logic? (basic_buf? basic_filebuf?) Of course I want something similar for input, where the stream can find out itself whether the file is Unicode (starts with 0xfffe or 0xfeff) or plain Ascii. And as an additional challenge: is it possible to have such a transparent stream that can do something like this?
transparentstream mystream; mystream.open("http://www.mywebsite.com/mypage.html"); mystream >> ...;
And if this is possible, how do I implement such a stream of buf class? Did some of you already encounter problems trying to mix the STL and Unicode files? How did you solve it? Probably Java and .Net solve this problem in a el
-
In my application I want to access files, where the underlying character type (ASCII or Unicode) is transparent for the application. Suppose that in a reporting module a report is written to file, like this:
myfile << reportHeader << std::endl; for (all columns) myfile << columnname; myfile << std::endl; for (all records) { for (all columns) myfile << data; myfile << std::endl; }
Now, I want my application to transparently write the report to ASCII or Unicode files, depending on the user specification. Currently, you have to do it like this:
std::ofstream asciiFile; std::wofstream unicodeFile; if (user wants Unicode) { unicodeFile.open ("output.txt"); unicodeFile << reportHeader << std::endl; } else { asciiFile.open ("output.txt"); asciiFile << reportHeader << std::endl; } for (all columns) { if (user wants Unicode) unicodeFile << columnname; else asciiFile << columnName; } ... and so on
The disadvantages seem obvious: - clumsy, unreadable code (especially if the write-to-file logic is spread over several methods) - writing Unicode strings (std::wstring) to an Ascii stream doesn't even work; it produced garbage Therefore, I need a kind of transparent stream, so that I can write the following:
transparentstream myfile; myfile.setMode (ascii or unicode); myfile.open ("output.txt"); myfile << reportHeader << std::endl; for (all columns) myfile << columnname; myfile << std::endl; for (all records) { for (all columns) myfile << data; myfile << std::endl; }
Problems are: - from which stream do I inherit the transparentstream? - how do I define the transparentstream so all defined output operators keep on working? - where do I put the conversion logic? (basic_buf? basic_filebuf?) Of course I want something similar for input, where the stream can find out itself whether the file is Unicode (starts with 0xfffe or 0xfeff) or plain Ascii. And as an additional challenge: is it possible to have such a transparent stream that can do something like this?
transparentstream mystream; mystream.open("http://www.mywebsite.com/mypage.html"); mystream >> ...;
And if this is possible, how do I implement such a stream of buf class? Did some of you already encounter problems trying to mix the STL and Unicode files? How did you solve it? Probably Java and .Net solve this problem in a el
My first thought is to encapsulate the ASCII and UNICODE streams with in your own class (or template) then duplicate the methods. It may be as simple defining operators for both char and wchar_t (I dought it thou).
class transparentstream
{
std::ofstream asciiFile;
std::wofstream unicodeFile;
...
};INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen