unicode.cpp

Go to the documentation of this file.
00001 /*
00002 This file is part of hugin.
00003 
00004 hugin is free software: you can redistribute it and/or modify
00005 it under the terms of the GNU General Public License as published by
00006 the Free Software Foundation, either version 2 of the License, or
00007 (at your option) any later version.
00008 
00009 hugin is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with hugin.  If not, see <http://www.gnu.org/licenses/>.
00016 */
00017 
00026 #include <cstring>
00027 #include <iostream>
00028 #include <string>
00029 #include <fstream>
00030 #include <locale>
00031 #include <stdio.h>
00032 
00038 int main(int argc, char* argv[])
00039 {
00040         std::locale de("de_DE.utf8");
00041 
00042         // the global locale is the default value for new streams and sets the C locale.
00043         std::cout << "C locale " << std::locale::global(std::locale("")).name() << std::endl;
00044         std::cout << "C locale " << std::locale::global(de).name() << std::endl;
00045         // This changes the C locale, but has no effect on the C++ streams.
00046         setlocale(LC_ALL, "en_US.utf8");
00047 
00048         std::cout << "C++ locale " << std::cout.getloc().name() << std::endl;
00049         std::cout.imbue(de);
00050         std::cout << "C++ locale " << std::cout.getloc().name() << std::endl;
00051 
00052         float comma = 15.45;
00053 
00054    const char text[] = "olé" ;
00055    const std::string strtext(text);
00056    std::ofstream file("/tmp/file");
00057 
00058    const wchar_t wtext[] = L"olé" ;
00059    const std::wstring wstrtext(wtext);
00060    std::wofstream wfile("/tmp/wfile", std::ios::binary);
00061 
00062    std::cout << "sizeof(char)    : " << sizeof(char) << std::endl ;
00063    std::cout << "text            : " << text << std::endl ;
00064    std::cout << "sizeof(text)    : " << sizeof(text) << std::endl ;
00065    std::cout << "strlen(text)    : " << strlen(text) << std::endl ;
00066    std::cout << "strtext.length(): " << strtext.length() << std::endl;
00067 
00068    std::cout << "text(binary)    :" ;
00069 
00070    for(size_t i = 0, iMax = strlen(text); i < iMax; ++i)
00071    {
00072       std::cout << " " << static_cast<unsigned int>(static_cast<unsigned char>(text[i])) ;
00073    }
00074 
00075    file << text;
00076    file << strtext;
00077    file << comma;
00078 
00079    std::cout << std::endl << comma << std::endl ; // shows the difference between C++ locale
00080    printf("%f\n", comma);       // and C locale
00081 
00082    std::cout << "sizeof(wchar_t) : " << sizeof(wchar_t) << std::endl ;
00083    std::cout << "wtext           : " << wtext << std::endl ;
00084 //   std::cout << "wtext           : UNABLE TO CONVERT NATIVELY." << std::endl ;
00085    std::cout << "sizeof(wtext)   : " << sizeof(wtext) << std::endl ;
00086    std::cout << "wcslen(wtext)   : " << wcslen(wtext) << std::endl ;
00087    std::cout << "wstrtext.length():" << wstrtext.length() << std::endl;
00088 
00089    std::cout << "wtext(binary)   :" ;
00090 
00091    for(size_t i = 0, iMax = wcslen(wtext); i < iMax; ++i)
00092    {
00093       std::cout << " " << static_cast<unsigned int>(static_cast<unsigned short>(wtext[i])) ;
00094    }
00095 
00096    wfile << wtext;
00097    wfile << wstrtext;
00098    wfile << comma;
00099 
00100    std::cout << std::endl << std::endl ;
00101 
00102    return 0;
00103 }

Generated on Thu Oct 2 01:25:40 2014 for Hugintrunk by  doxygen 1.3.9.1