1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 #ifndef _STRCONVERT_H_ 24 #define _STRCONVERT_H_ 25 26 #include <windows.h> 27 28 #ifdef NDEBUG 29 #define STRCONVERT_H_HAD_NDEBUG 30 #undef NDEBUG 31 #endif 32 #if OSL_DEBUG_LEVEL == 0 33 #define NDEBUG 34 #endif 35 #include <assert.h> 36 37 #ifdef __cplusplus 38 extern "C"{ 39 #endif 40 41 int AllocNecessarySpaceAndCopyWStr2Str( LPCWSTR lpcwstrString, LPSTR* lppStr ); 42 int AllocSpaceAndCopyWStr2Str( LPCWSTR lpcwstrString, DWORD nWCharsToCopy, LPSTR* lppStr ); 43 int CalcLenDblNullTerminatedWStr( LPCWSTR lpcwstrString ); 44 int CalcLenDblNullTerminatedStr( LPCSTR lpcstrString ); 45 void FreeSpaceStr( LPSTR lpszString ); 46 47 /* WC2MB allocates a sufficient amount of memory on stack and converts 48 the wide char parameter to multi byte string using the actual code 49 page. 50 51 @Param: wcStr - a wide char string 52 mbStr - the corresponding multi byte string 53 54 NOTE: due to the use of _alloca, this must be a macro and no function 55 */ 56 57 #define WC2MB( wcStr, mbStr ) \ 58 if( wcStr ) \ 59 { \ 60 int needed = WideCharToMultiByte( CP_ACP, 0, wcStr, -1, NULL, 0, NULL, NULL ); \ 61 if( needed > 0 ) \ 62 { \ 63 int copied; \ 64 mbStr = _alloca( needed * sizeof( CHAR ) ); \ 65 copied = WideCharToMultiByte( CP_ACP, 0, wcStr, -1, mbStr, needed, NULL, NULL ); \ 66 assert( copied == needed ); \ 67 } \ 68 } 69 70 71 /* WideCharListGetMultiByteLength 72 calculates the needed length of a corresponding the multi byte string 73 list for a wide char string list. 74 75 @Param: cp - the code page to use for conversion. 76 wcList - a double '\0' terminated wide char string list. 77 */ 78 79 int WideCharListGetMultiByteLength( UINT codepage, LPCWSTR wcList ); 80 81 /* WideCharListToMultiByteList 82 converts a double '\0' terminated list of wide char strings to a 83 multi byte string list. 84 85 @Param: cp - the code page to use for conversion. 86 wcList - a double '\0' terminated wide char string list. 87 mbList - a double '\0' terminated multi byte string list. 88 dwSize - size of buffer for multi byte string list. 89 */ 90 91 int WideCharListToMultiByteList( UINT codepage, LPCWSTR wcList, LPSTR mbList, DWORD dwSize ); 92 93 94 /* WCL2MBL allocates a sufficient amount of memory on stack and converts 95 the wide char list parameter to multi byte string list using the actual 96 code page. 97 98 @Param: wcList - a wide char string list 99 mbList - the corresponding multi byte string list 100 101 NOTE: due to the use of _alloca, this must be a macro and no function 102 */ 103 104 #define WCL2MBL( wcList, mbList ) \ 105 if( wcList ) \ 106 { \ 107 int needed = WideCharListGetMultiByteLength( CP_ACP, wcList ); \ 108 if( needed > 0 ) \ 109 { \ 110 int copied; \ 111 mbList = _alloca( needed * sizeof( CHAR ) ); \ 112 copied = WideCharListToMultiByteList( CP_ACP, wcList, mbList, needed ); \ 113 assert( copied == needed ); \ 114 } \ 115 } 116 117 #ifdef __cplusplus 118 } 119 #endif 120 121 // Restore NDEBUG state 122 #ifdef STRCONVERT_H_HAD_NDEBUG 123 #define NDEBUG 124 #else 125 #undef NDEBUG 126 #endif 127 128 #endif 129