1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2009 Apple Computer, Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 #include "StringServices.h"
22 #include <DebugServices.h>
36 std::string
& outString
46 char * utf8String
= NULL
;
48 OSStatus err
= kNoErr
;
57 TCHAR
* utf16String
= NULL
;
61 utf16String
= OLE2T( inString
);
63 require_action( utf16String
!= NULL
, exit
, err
= kUnknownErr
);
67 if ( wcslen( utf16String
) > 0 )
71 size
= (size_t) WideCharToMultiByte( CP_UTF8
, 0, utf16String
, ( int ) wcslen( utf16String
), NULL
, 0, NULL
, NULL
);
73 err
= translate_errno( size
!= 0, GetLastError(), kUnknownErr
);
75 require_noerr( err
, exit
);
83 utf8String
= new char[ size
+ 1 ];
97 require_action( utf8String
!= NULL
, exit
, err
= kNoMemoryErr
);
99 size
= (size_t) WideCharToMultiByte( CP_UTF8
, 0, utf16String
, ( int ) wcslen( utf16String
), utf8String
, (int) size
, NULL
, NULL
);
101 err
= translate_errno( size
!= 0, GetLastError(), kUnknownErr
);
103 require_noerr( err
, exit
);
107 // have to add the trailing 0 because WideCharToMultiByte doesn't do it,
109 // although it does return the correct size
113 utf8String
[size
] = '\0';
115 outString
= utf8String
;
126 if ( utf8String
!= NULL
)
130 delete [] utf8String
;
136 return ( !err
) ? TRUE
: FALSE
;
150 const char * inString
,
158 wchar_t * unicode
= NULL
;
169 n
= MultiByteToWideChar( CP_UTF8
, 0, inString
, -1, NULL
, 0 );
181 unicode
= new wchar_t[ n
];
195 require_action( unicode
, exit
, err
= ERROR_INSUFFICIENT_BUFFER
);
199 n
= MultiByteToWideChar( CP_UTF8
, 0, inString
, -1, unicode
, n
);
214 if ( unicode
!= NULL
)
224 return ( !err
) ? TRUE
: FALSE
;
238 const void * inArray
,
256 VariantClear( outVariant
);
258 outVariant
->vt
= VT_ARRAY
|VT_UI1
;
260 outVariant
->parray
= SafeArrayCreateVector( VT_UI1
, 0, ( ULONG
) inArrayLen
);
262 require_action( outVariant
->parray
, exit
, ok
= FALSE
);
264 hr
= SafeArrayAccessData( outVariant
->parray
, (LPVOID
*)&buf
);
266 require_action( hr
== S_OK
, exit
, ok
= FALSE
);
268 memcpy( buf
, inArray
, inArrayLen
);
270 hr
= SafeArrayUnaccessData( outVariant
->parray
);
272 require_action( hr
== S_OK
, exit
, ok
= FALSE
);
293 std::vector
< BYTE
> & outArray
298 if ( V_VT( inVariant
) == VT_BSTR
)
300 BSTR bstr
= V_BSTR( inVariant
);
303 BSTRToUTF8( bstr
, utf8
);
305 outArray
.reserve( utf8
.size() );
306 outArray
.assign( utf8
.begin(), utf8
.end() );
308 else if ( V_VT( inVariant
) == VT_ARRAY
)
310 SAFEARRAY
* psa
= NULL
;
315 psa
= V_ARRAY( inVariant
);
317 require_action( psa
, exit
, ok
= FALSE
);
319 require_action( SafeArrayGetDim( psa
) == 1, exit
, ok
= FALSE
);
321 hr
= SafeArrayAccessData( psa
, ( LPVOID
* )&pData
);
323 require_action( hr
== S_OK
, exit
, ok
= FALSE
);
325 cElements
= psa
->rgsabound
[0].cElements
;
327 outArray
.reserve( cElements
);
329 outArray
.assign( cElements
, 0 );
331 memcpy( &outArray
[ 0 ], pData
, cElements
);
333 SafeArrayUnaccessData( psa
);