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