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

Revision 9475, 3.0 kB (checked in by robert, 5 years ago)

From Andy Skinner, fixes for Solaris build

RevLine 
[9124]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
[9343]17#include <string.h>
[9475]18#include <wchar.h>
[9343]19
[9124]20#if defined(WIN32) && !defined(__CYGWIN__)
21#define WIN32_LEAN_AND_MEAN
22#include <windows.h>
23#endif
24
25namespace osgDB
26{
27
[9343]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
[9124]34std::string convertUTF16toUTF8(const wchar_t* source, unsigned sourceLength)
35{
36#if defined(WIN32) && !defined(__CYGWIN__)
37    if (sourceLength == 0)
38    {
39        return std::string();
40    }
41
42    int destLen = WideCharToMultiByte(CP_UTF8, 0, source, sourceLength, 0, 0, 0, 0);
43    if (destLen <= 0)
44    {
45        osg::notify(osg::WARN) << "Cannot convert UTF-16 string to UTF-8." << std::endl;
46        return std::string();
47    }
48
49    std::string sDest(destLen, '\0');
50    destLen = WideCharToMultiByte(CP_UTF8, 0, source, sourceLength, &sDest[0], destLen, 0, 0);
51
52    if (destLen <= 0)
53    {
54        osg::notify(osg::WARN) << "Cannot convert UTF-16 string to UTF-8." << std::endl;
55        return std::string();
56    }
57
58    return sDest;
59#else
60    //TODO: Implement for other platforms
61    osg::notify(osg::WARN) << "ConvertUTF16toUTF8 not implemented." << std::endl;
62    return std::string();
63#endif
64}
65
66std::wstring convertUTF8toUTF16(const char* source, unsigned sourceLength)
67{
68#if defined(WIN32) && !defined(__CYGWIN__)
69    if (sourceLength == 0)
70    {
71        return std::wstring();
72    }
73
74    int destLen = MultiByteToWideChar(CP_UTF8, 0, source, sourceLength, 0, 0);
75    if (destLen <= 0)
76    {
77        osg::notify(osg::WARN) << "Cannot convert UTF-8 string to UTF-16." << std::endl;
78        return std::wstring();
79    }
80
81    std::wstring sDest(destLen, L'\0');
82    destLen = MultiByteToWideChar(CP_UTF8, 0, source, sourceLength, &sDest[0], destLen);
83
84    if (destLen <= 0)
85    {
86        osg::notify(osg::WARN) << "Cannot convert UTF-8 string to UTF-16." << std::endl;
87        return std::wstring();
88    }
89
90    return sDest;
91#else
92    //TODO: Implement for other platforms
93    osg::notify(osg::WARN) << "ConvertUTF8toUTF16 not implemented." << std::endl;
94    return std::wstring();
95#endif
96}
97
98}
Note: See TracBrowser for help on using the browser.