Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Transparent Unicode/Ascii using STL

Transparent Unicode/Ascii using STL

Scheduled Pinned Locked Moved C / C++ / MFC
questioncsharpc++javahtml
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Patje
    wrote on last edited by
    #1

    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

    J 1 Reply Last reply
    0
    • P Patje

      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

      J Offline
      J Offline
      John R Shaw
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups