~ubuntu-branches/ubuntu/wily/opencollada/wily-proposed

« back to all changes in this revision

Viewing changes to COLLADABaseUtils/src/COLLADABUNativeString.cpp

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2015-05-14 17:23:27 UTC
  • Revision ID: package-import@ubuntu.com-20150514172327-f862u8envms01fra
Tags: upstream-0.1.0~20140703.ddf8f47+dfsg1
ImportĀ upstreamĀ versionĀ 0.1.0~20140703.ddf8f47+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (c) 2008-2009 NetAllied Systems GmbH
 
3
 
 
4
    This file is part of COLLADABaseUtils.
 
5
 
 
6
    Licensed under the MIT Open Source License,
 
7
    for details please see LICENSE file or the website
 
8
    http://www.opensource.org/licenses/mit-license.php
 
9
*/
 
10
 
 
11
#include "COLLADABUStableHeaders.h"
 
12
#include "COLLADABUNativeString.h"
 
13
#include "COLLADABUStringUtils.h"
 
14
#include "COLLADABUPlatform.h"
 
15
 
 
16
#include <string>
 
17
 
 
18
#ifdef COLLADABU_OS_WIN
 
19
#include <windows.h>
 
20
#endif
 
21
 
 
22
namespace COLLADABU
 
23
{
 
24
 
 
25
 
 
26
        NativeString::NativeString(const String& str, Encoding encoding ) : std::string()
 
27
        {
 
28
                if ( encoding == ENCODING_NATIVE )
 
29
                {
 
30
                        this->assign(str);
 
31
                }
 
32
                else
 
33
                {
 
34
                        fromWideString(StringUtils::utf8String2WideString(str));
 
35
                }
 
36
        }
 
37
 
 
38
        NativeString::NativeString( const char * s, Encoding encoding /*= ENCODING_NATIVE*/ )
 
39
        {
 
40
                if ( encoding == ENCODING_NATIVE )
 
41
                {
 
42
                        this->assign(s);
 
43
                }
 
44
                else
 
45
                {
 
46
                        fromWideString(StringUtils::utf8String2WideString(String(s)));
 
47
                }
 
48
        }
 
49
 
 
50
 
 
51
        void NativeString::fromWideString( const WideString& wideString )
 
52
        {
 
53
#ifdef COLLADABU_OS_WIN
 
54
                int multibyteLength = WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)wideString.c_str(), -1, 0, 0, 0, 0 );
 
55
 
 
56
                char * dest = new char[ multibyteLength ];
 
57
                WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)wideString.c_str(), -1, dest, multibyteLength, 0, 0 );
 
58
                this->assign( dest );
 
59
                delete[] dest;
 
60
#elif defined (COLLADABU_OS_LINUX) || defined (COLLADABU_OS_MAC)
 
61
//# error check if this really works on linux
 
62
                size_t maxStringLength = wcstombs( 0, wideString.c_str(), 0 ) + 1; // wideString.length()*MB_CUR_MAX + 1;
 
63
                char* dest = new char[ maxStringLength ];
 
64
                wcstombs( dest, wideString.c_str(), maxStringLength );
 
65
 
 
66
                this->assign( dest );
 
67
                delete[] dest;
 
68
#else
 
69
#    error "No StringUtil::toString defined for your platform"
 
70
#endif
 
71
        }
 
72
 
 
73
        WideString NativeString::toWideString() const
 
74
        {
 
75
#ifdef COLLADABU_OS_WIN
 
76
                wchar_t * dest = new wchar_t[ length() + 1 ];
 
77
                int nDest = MultiByteToWideChar ( CP_ACP, 0, c_str(), ( int ) length(),
 
78
                        dest, ( int ) length() );
 
79
                dest[ nDest ] = 0; // null termination
 
80
                WideString returnValue( dest );
 
81
                delete[] dest;
 
82
                return returnValue;
 
83
#elif defined (COLLADABU_OS_LINUX) || defined (COLLADABU_OS_MAC)
 
84
//# error check if this really works on linux
 
85
 
 
86
                size_t maxStringLength = length() + 1;
 
87
                wchar_t* dest = new wchar_t[ maxStringLength ];
 
88
                mbstowcs( dest, this->c_str(), maxStringLength );
 
89
 
 
90
                WideString returnValue( dest );
 
91
 
 
92
                delete[] dest;
 
93
 
 
94
                return returnValue;
 
95
 
 
96
#else
 
97
#    error "Not StringUtil::toWideString defined for your platform"
 
98
                // this functions doesn'nt  seem to work
 
99
                //   return fromUtf8( string );
 
100
#endif
 
101
        }
 
102
 
 
103
 
 
104
        String NativeString::toUtf8String() const
 
105
        {
 
106
                return StringUtils::wideString2utf8String(toWideString());
 
107
        }
 
108
 
 
109
 
 
110
} //namespace COLLADA