Changeset 11111

Show
Ignore:
Timestamp:
02/25/10 18:07:34 (4 years ago)
Author:
robert
Message:

From Michael Platings, "There is a function, convertStringFromCurrentCodePageToUTF8() that would be useful outside the FBX plugin so it belongs in osgDB. I've attached ConvertUTF and ConvertUTF.cpp with the function added for you to commit at your convenience.
"

Location:
OpenSceneGraph/trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/include/osgDB/ConvertUTF

    r9343 r11111  
    3939extern OSGDB_EXPORT std::wstring convertUTF8toUTF16(const char* s); 
    4040 
     41extern OSGDB_EXPORT std::string convertStringFromCurrentCodePageToUTF8(const char* source, unsigned sourceLength); 
     42extern OSGDB_EXPORT std::string convertStringFromUTF8toCurrentCodePage(const char* source, unsigned sourceLength); 
     43 
     44extern OSGDB_EXPORT std::string convertStringFromCurrentCodePageToUTF8(const std::string& s); 
     45extern OSGDB_EXPORT std::string convertStringFromCurrentCodePageToUTF8(const char* s); 
     46 
     47extern OSGDB_EXPORT std::string convertStringFromUTF8toCurrentCodePage(const std::string& s); 
     48extern OSGDB_EXPORT std::string convertStringFromUTF8toCurrentCodePage(const char* s); 
     49 
    4150} 
    4251 
  • OpenSceneGraph/trunk/src/osgDB/ConvertUTF.cpp

    r9475 r11111  
    3131std::wstring convertUTF8toUTF16(const std::string& s){return convertUTF8toUTF16(s.c_str(), s.length());} 
    3232std::wstring convertUTF8toUTF16(const char* s){return convertUTF8toUTF16(s, strlen(s));} 
     33 
     34std::string convertStringFromCurrentCodePageToUTF8(const std::string& s){return convertStringFromCurrentCodePageToUTF8(s.c_str(), s.length());} 
     35std::string convertStringFromCurrentCodePageToUTF8(const char* s){return convertStringFromCurrentCodePageToUTF8(s, strlen(s));} 
     36 
     37std::string convertStringFromUTF8toCurrentCodePage(const std::string& s){return convertStringFromUTF8toCurrentCodePage(s.c_str(), s.length());} 
     38std::string convertStringFromUTF8toCurrentCodePage(const char* s){return convertStringFromUTF8toCurrentCodePage(s, strlen(s));} 
    3339 
    3440std::string convertUTF16toUTF8(const wchar_t* source, unsigned sourceLength) 
     
    96102} 
    97103 
     104std::string convertStringFromCurrentCodePageToUTF8(const char* source, unsigned sourceLength) 
     105{ 
     106#if defined(WIN32) && !defined(__CYGWIN__) 
     107    if (sourceLength == 0) 
     108    { 
     109        return std::string(); 
     110    } 
     111 
     112    int utf16Length = MultiByteToWideChar(CP_ACP, 0, source, sourceLength, 0, 0); 
     113    if (utf16Length <= 0) 
     114    { 
     115        osg::notify(osg::WARN) << "Cannot convert multi-byte string to UTF-8." << std::endl; 
     116        return std::string(); 
     117    } 
     118 
     119    std::wstring sUTF16(utf16Length, L'\0'); 
     120    utf16Length = MultiByteToWideChar(CP_ACP, 0, source, sourceLength, &sUTF16[0], utf16Length); 
     121    if (utf16Length <= 0) 
     122    { 
     123        osg::notify(osg::WARN) << "Cannot convert multi-byte string to UTF-8." << std::endl; 
     124        return std::string(); 
     125    } 
     126 
     127    return convertUTF16toUTF8(sUTF16); 
     128#else 
     129    return source; 
     130#endif 
    98131} 
    99132 
     133std::string convertStringFromUTF8toCurrentCodePage(const char* source, unsigned sourceLength) 
     134{ 
     135#if defined(WIN32) && !defined(__CYGWIN__) 
     136    if (sourceLength == 0) 
     137    { 
     138        return std::string(); 
     139    } 
     140 
     141    std::wstring utf16 = convertUTF8toUTF16(source, sourceLength); 
     142    sourceLength = utf16.length(); 
     143 
     144    int destLen = WideCharToMultiByte(CP_ACP, 0, utf16.c_str(), sourceLength, 0, 0, 0, 0); 
     145    if (destLen <= 0) 
     146    { 
     147        osg::notify(osg::WARN) << "Cannot convert multi-byte string to UTF-8." << std::endl; 
     148        return std::string(); 
     149    } 
     150 
     151    std::string sDest(destLen, '\0'); 
     152    destLen = WideCharToMultiByte(CP_ACP, 0, utf16.c_str(), sourceLength, 0, 0, 0, 0); 
     153    if (destLen <= 0) 
     154    { 
     155        osg::notify(osg::WARN) << "Cannot convert multi-byte string to UTF-8." << std::endl; 
     156        return std::string(); 
     157    } 
     158 
     159    return sDest; 
     160#else 
     161    return source; 
     162#endif 
     163} 
     164 
     165} 
     166