xref: /aoo42x/main/sal/inc/systools/win32/StrConvert.h (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir #ifndef _STRCONVERT_H_
28*cdf0e10cSrcweir #define _STRCONVERT_H_
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include <windows.h>
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #ifdef NDEBUG
33*cdf0e10cSrcweir #define STRCONVERT_H_HAD_NDEBUG
34*cdf0e10cSrcweir #undef NDEBUG
35*cdf0e10cSrcweir #endif
36*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL == 0
37*cdf0e10cSrcweir #define NDEBUG
38*cdf0e10cSrcweir #endif
39*cdf0e10cSrcweir #include <assert.h>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #ifdef __cplusplus
42*cdf0e10cSrcweir extern "C"{
43*cdf0e10cSrcweir #endif
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir int  AllocNecessarySpaceAndCopyWStr2Str( LPCWSTR lpcwstrString, LPSTR* lppStr );
46*cdf0e10cSrcweir int  AllocSpaceAndCopyWStr2Str( LPCWSTR lpcwstrString, DWORD nWCharsToCopy, LPSTR* lppStr );
47*cdf0e10cSrcweir int  CalcLenDblNullTerminatedWStr( LPCWSTR lpcwstrString );
48*cdf0e10cSrcweir int  CalcLenDblNullTerminatedStr( LPCSTR lpcstrString );
49*cdf0e10cSrcweir void FreeSpaceStr( LPSTR lpszString );
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir /* WC2MB allocates a sufficient amount of memory on stack and converts
52*cdf0e10cSrcweir    the wide char parameter to multi byte string using the actual code
53*cdf0e10cSrcweir    page.
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir    @Param: wcStr - a wide char string
56*cdf0e10cSrcweir            mbStr - the corresponding multi byte string
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir    NOTE: due to the use of _alloca, this must be a macro and no function
59*cdf0e10cSrcweir */
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir #define WC2MB( wcStr, mbStr ) \
62*cdf0e10cSrcweir if( wcStr ) \
63*cdf0e10cSrcweir { \
64*cdf0e10cSrcweir     int needed = WideCharToMultiByte( CP_ACP, 0, wcStr, -1, NULL, 0, NULL, NULL ); \
65*cdf0e10cSrcweir     if( needed > 0 ) \
66*cdf0e10cSrcweir     { \
67*cdf0e10cSrcweir         int copied; \
68*cdf0e10cSrcweir         mbStr = _alloca( needed * sizeof( CHAR ) ); \
69*cdf0e10cSrcweir         copied = WideCharToMultiByte( CP_ACP, 0, wcStr, -1, mbStr, needed, NULL, NULL ); \
70*cdf0e10cSrcweir         assert( copied == needed ); \
71*cdf0e10cSrcweir     } \
72*cdf0e10cSrcweir }
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir /* WideCharListGetMultiByteLength
76*cdf0e10cSrcweir    calculates the needed length of a corresponding the multi byte string
77*cdf0e10cSrcweir    list for a wide char string list.
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir    @Param: cp - the code page to use for convertion.
80*cdf0e10cSrcweir            wcList - a double '\0' terminated wide char string list.
81*cdf0e10cSrcweir */
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir int WideCharListGetMultiByteLength( UINT codepage, LPCWSTR wcList );
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir /* WideCharListToMultiByteList
86*cdf0e10cSrcweir    converts a double '\0' terminated list of wide char strings to a
87*cdf0e10cSrcweir    multi byte string list.
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir    @Param: cp - the code page to use for convertion.
90*cdf0e10cSrcweir            wcList - a double '\0' terminated wide char string list.
91*cdf0e10cSrcweir            mbList - a double '\0' terminated multi byte string list.
92*cdf0e10cSrcweir            dwSize - size of buffer for multi byte string list.
93*cdf0e10cSrcweir */
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir int WideCharListToMultiByteList( UINT codepage, LPCWSTR wcList, LPSTR mbList, DWORD dwSize );
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir /* WCL2MBL allocates a sufficient amount of memory on stack and converts
99*cdf0e10cSrcweir    the wide char list parameter to multi byte string list using the actual
100*cdf0e10cSrcweir    code page.
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir    @Param: wcList - a wide char string list
103*cdf0e10cSrcweir            mbList - the corresponding multi byte string list
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir    NOTE: due to the use of _alloca, this must be a macro and no function
106*cdf0e10cSrcweir */
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir #define WCL2MBL( wcList, mbList ) \
109*cdf0e10cSrcweir if( wcList ) \
110*cdf0e10cSrcweir { \
111*cdf0e10cSrcweir     int needed = WideCharListGetMultiByteLength( CP_ACP, wcList ); \
112*cdf0e10cSrcweir     if( needed > 0 ) \
113*cdf0e10cSrcweir     { \
114*cdf0e10cSrcweir         int copied; \
115*cdf0e10cSrcweir         mbList = _alloca( needed * sizeof( CHAR ) ); \
116*cdf0e10cSrcweir         copied = WideCharListToMultiByteList( CP_ACP, wcList, mbList, needed ); \
117*cdf0e10cSrcweir         assert( copied == needed ); \
118*cdf0e10cSrcweir     } \
119*cdf0e10cSrcweir }
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir #ifdef __cplusplus
122*cdf0e10cSrcweir }
123*cdf0e10cSrcweir #endif
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir // Restore NDEBUG state
126*cdf0e10cSrcweir #ifdef STRCONVERT_H_HAD_NDEBUG
127*cdf0e10cSrcweir #define NDEBUG
128*cdf0e10cSrcweir #else
129*cdf0e10cSrcweir #undef NDEBUG
130*cdf0e10cSrcweir #endif
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir #endif
133