root/OpenSceneGraph/trunk/src/osgDB/ConvertUTF.cpp @ 13041

Revision 13041, 5.1 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 2008 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#include <osgDB/ConvertUTF>
15#include <osg/Notify>
16
17#include <string.h>
18#include <wchar.h>
19
20#if defined(WIN32) && !defined(__CYGWIN__)
21#define WIN32_LEAN_AND_MEAN
22#include <windows.h>
23#endif
24
25namespace osgDB
26{
27
28std::string convertUTF16toUTF8(const std::wstring& s){return convertUTF16toUTF8(s.c_str(), s.length());}
29std::string convertUTF16toUTF8(const wchar_t* s){return convertUTF16toUTF8(s, wcslen(s));}
30
31std::wstring convertUTF8toUTF16(const std::string& s){return convertUTF8toUTF16(s.c_str(), s.length());}
32std::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));}
39
40std::string convertUTF16toUTF8(const wchar_t* source, unsigned sourceLength)
41{
42#if defined(WIN32) && !defined(__CYGWIN__)
43    if (sourceLength == 0)
44    {
45        return std::string();
46    }
47
48    int destLen = WideCharToMultiByte(CP_UTF8, 0, source, sourceLength, 0, 0, 0, 0);
49    if (destLen <= 0)
50    {
51        OSG_WARN << "Cannot convert UTF-16 string to UTF-8." << std::endl;
52        return std::string();
53    }
54
55    std::string sDest(destLen, '\0');
56    destLen = WideCharToMultiByte(CP_UTF8, 0, source, sourceLength, &sDest[0], destLen, 0, 0);
57
58    if (destLen <= 0)
59    {
60        OSG_WARN << "Cannot convert UTF-16 string to UTF-8." << std::endl;
61        return std::string();
62    }
63
64    return sDest;
65#else
66    //TODO: Implement for other platforms
67    OSG_WARN << "ConvertUTF16toUTF8 not implemented." << std::endl;
68    return std::string();
69#endif
70}
71
72std::wstring convertUTF8toUTF16(const char* source, unsigned sourceLength)
73{
74#if defined(WIN32) && !defined(__CYGWIN__)
75    if (sourceLength == 0)
76    {
77        return std::wstring();
78    }
79
80    int destLen = MultiByteToWideChar(CP_UTF8, 0, source, sourceLength, 0, 0);
81    if (destLen <= 0)
82    {
83        OSG_WARN << "Cannot convert UTF-8 string to UTF-16." << std::endl;
84        return std::wstring();
85    }
86
87    std::wstring sDest(destLen, L'\0');
88    destLen = MultiByteToWideChar(CP_UTF8, 0, source, sourceLength, &sDest[0], destLen);
89
90    if (destLen <= 0)
91    {
92        OSG_WARN << "Cannot convert UTF-8 string to UTF-16." << std::endl;
93        return std::wstring();
94    }
95
96    return sDest;
97#else
98    //TODO: Implement for other platforms
99    OSG_WARN << "ConvertUTF8toUTF16 not implemented." << std::endl;
100    return std::wstring();
101#endif
102}
103
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_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_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
131}
132
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_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, &sDest[0], destLen, 0, 0);
153    if (destLen <= 0)
154    {
155        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}
Note: See TracBrowser for help on using the browser.