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.
17 Change History (most recent first):
19 $Log: StringServices.cpp,v $
20 Revision 1.2 2009/06/02 18:43:57 herscher
21 <rdar://problem/3948252> Allow component consumers to pass in null strings
23 Revision 1.1 2009/05/26 04:43:54 herscher
24 <rdar://problem/3948252> COM component that can be used with any .NET language and VB.
29 #include "StringServices.h"
30 #include <DebugServices.h>
37 std::string
& outString
42 char * utf8String
= NULL
;
43 OSStatus err
= kNoErr
;
49 TCHAR
* utf16String
= NULL
;
52 utf16String
= OLE2T( inString
);
53 require_action( utf16String
!= NULL
, exit
, err
= kUnknownErr
);
55 if ( wcslen( utf16String
) > 0 )
57 size
= (size_t) WideCharToMultiByte( CP_UTF8
, 0, utf16String
, ( int ) wcslen( utf16String
), NULL
, 0, NULL
, NULL
);
58 err
= translate_errno( size
!= 0, GetLastError(), kUnknownErr
);
59 require_noerr( err
, exit
);
63 utf8String
= new char[ size
+ 1 ];
70 require_action( utf8String
!= NULL
, exit
, err
= kNoMemoryErr
);
71 size
= (size_t) WideCharToMultiByte( CP_UTF8
, 0, utf16String
, ( int ) wcslen( utf16String
), utf8String
, (int) size
, NULL
, NULL
);
72 err
= translate_errno( size
!= 0, GetLastError(), kUnknownErr
);
73 require_noerr( err
, exit
);
75 // have to add the trailing 0 because WideCharToMultiByte doesn't do it,
76 // although it does return the correct size
78 utf8String
[size
] = '\0';
79 outString
= utf8String
;
85 if ( utf8String
!= NULL
)
90 return ( !err
) ? TRUE
: FALSE
;
97 const char * inString
,
101 wchar_t * unicode
= NULL
;
108 n
= MultiByteToWideChar( CP_UTF8
, 0, inString
, -1, NULL
, 0 );
114 unicode
= new wchar_t[ n
];
121 require_action( unicode
, exit
, err
= ERROR_INSUFFICIENT_BUFFER
);
123 n
= MultiByteToWideChar( CP_UTF8
, 0, inString
, -1, unicode
, n
);
131 if ( unicode
!= NULL
)
136 return ( !err
) ? TRUE
: FALSE
;
143 const void * inArray
,
152 VariantClear( outVariant
);
153 outVariant
->vt
= VT_ARRAY
|VT_UI1
;
154 outVariant
->parray
= SafeArrayCreateVector( VT_UI1
, 0, ( ULONG
) inArrayLen
);
155 require_action( outVariant
->parray
, exit
, ok
= FALSE
);
156 hr
= SafeArrayAccessData( outVariant
->parray
, (LPVOID
*)&buf
);
157 require_action( hr
== S_OK
, exit
, ok
= FALSE
);
158 memcpy( buf
, inArray
, inArrayLen
);
159 hr
= SafeArrayUnaccessData( outVariant
->parray
);
160 require_action( hr
== S_OK
, exit
, ok
= FALSE
);
172 std::vector
< BYTE
> & outArray
175 SAFEARRAY
* psa
= NULL
;
181 require_action( V_VT( inVariant
) == ( VT_ARRAY
|VT_UI1
), exit
, ok
= FALSE
);
182 psa
= V_ARRAY( inVariant
);
183 require_action( psa
, exit
, ok
= FALSE
);
184 require_action( SafeArrayGetDim( psa
) == 1, exit
, ok
= FALSE
);
185 hr
= SafeArrayAccessData( psa
, ( LPVOID
* )&pData
);
186 require_action( hr
== S_OK
, exit
, ok
= FALSE
);
187 cElements
= psa
->rgsabound
[0].cElements
;
188 outArray
.reserve( cElements
);
189 outArray
.assign( cElements
, 0 );
190 memcpy( &outArray
[ 0 ], pData
, cElements
);
191 SafeArrayUnaccessData( psa
);