Benoetige Hilfe bei boost::utf8-codecvt



  • Hallo,
    ich muss eine UTF-8 kodierte Datei verarbeiten. Habe die Hinweise lt. diesem Thread http://www.c-plusplus.net/forum/viewtopic-var-t-is-210278.html und gemaess http://www.kharchi.eu/wiki/doku.php?id=cpp:std:string#zeichencodeumwandlung_ascii_ucs_und_unicode befolgt, bei der Umsetzung mit Hilfe der Boost-Library stehe ich aber an. Ich bin mir nicht sicher, ob das Ganze ueberhaupt korrekt kodiert ist, habe mit der Boost und UTF-8 Umwandlung in wchar noch ueberhaupt keine Erfahrung. Momentan scheitert es an folgemdem Compiler-Error.

    #include <fstream>
    #include <iostream>
    #include <locale>
    #include <boost/program_options/detail/utf8_codecvt_facet.hpp>
    using namespace boost;
    
    int main() {
      typedef wchar_t ucs4_t;
      std::locale old_locale;
      std::locale 
         utf8_locale(old_locale,
         new 
            utf8_codecvt_facet<ucs4_t>);  // <== error: expected type-specifier
    
      std::wifstream f("funnychars16.ini", std::wifstream::in);
      f.imbue(utf8_locale);
    
      if (f.is_open())
          std::cout << "is open" << std::endl;
      std::wstring wstr;   
      wchar_t wc, dummy; 
      while(f >> wc) {
          f >> dummy;
          f >> wstr;
          std::wcout << wstr << std::endl;  
       } 
       f.close();
       return 0;
    }
    

    Kann es jemand ansehen, der Erfahrung mit UTF-8 in C++ hat ?? Ist die Loesung ueberhaupt korrekt ? Die Beschreibung in der Boost-Doku ist ziemlich duerftig und kryptisch. Oder kennt jemand eine funktionierende Implementierung ??? Bitte um Hilfe, ich bin schon ziemlich am Ende 😞

    Danke im Voraus fuer Hilfe
    Gruss, Diogenes



  • Der Namespace ist falsch. utf8_codecvt_facet liegt in boost::program_options::detail.



  • Danke !!!

    Jetzt habe ich "nur mehr" einen Linker Error in utf8_codecvt_facet.hpp:

    // maximum lenght of a multibyte string
    #define MB_LENGTH_MAX 8
    
    BOOST_UTF8_BEGIN_NAMESPACE
    
    struct BOOST_UTF8_DECL utf8_codecvt_facet :
        public std::codecvt<wchar_t, char, std::mbstate_t>   /*<== /usr/include/boost/detail/utf8_codecvt_facet.hpp:118: undefined reference to `vtable for boost::program_options::detail::utf8_codecvt_facet' */
    
    {
    public:
        explicit utf8_codecvt_facet(std::size_t no_locale_manage=0)
            : std::codecvt<wchar_t, char, std::mbstate_t>(no_locale_manage) 
        {}
    .............
    

    Kann ich Dich damit nochmals belaestigen ?? Fehlt mir da irgendeine Linker-Option ??

    Vollstaendigkeitshalber, hier ist noch das korrigeirte Programm:

    #include <fstream>
    #include <iostream>
    #include <locale>
    #include <boost/program_options/detail/utf8_codecvt_facet.hpp>
    using namespace boost::program_options::detail;
    
    int main() {
      typedef wchar_t ucs4_t;
      std::locale old_locale;
    
      std::locale utf8_locale(old_locale, new 
         utf8_codecvt_facet/*<ucs4_t>*/);
    
      std::wifstream f("funnychars16.ini", std::wifstream::in);
      f.imbue(utf8_locale);
    
      if (f.is_open())
          std::cout << "is open" << std::endl;
      std::wstring wstr;   
      wchar_t wc, dummy; 
      while(f >> wc) {
          f >> dummy;
          f >> wstr;
          std::wcout << wstr << std::endl;  
       } 
       f.close();
       return 0;
    }
    


  • Mußt noch die program_options-Library dazu linken. Hier gibts genaue Infos. Wenn du den MSVC benutzt, linkt der automatisch.



  • Ich bin mit Linuix/ubuntu + gcc unterwegs ... sorry, habe ich ich vorhin vergessen zu erwaehnen.



  • Aber habe schon natuerlich das Pendant fuer Linux gefunden. DANKE fuer die Hilfe !!!


Anmelden zum Antworten