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