1 /*
2  * Wide character string functions
3  *
4  * Copyright (C) 2010-2021, Joachim Metz <joachim.metz@gmail.com>
5  *
6  * Refer to AUTHORS for acknowledgements.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #if !defined( _WIDE_STRING_H )
23 #define _WIDE_STRING_H
24 
25 #include "common.h"
26 #include "memory.h"
27 #include "types.h"
28 
29 #if defined( HAVE_WCHAR_H ) || defined( WINAPI )
30 #include <wchar.h>
31 #endif
32 
33 #if defined( __cplusplus )
34 extern "C" {
35 #endif
36 
37 /* Intermediate version of the macro required
38  * for correct evaluation predefined string
39  */
40 #define _WIDE_STRING_INTERMEDIATE( string ) \
41 	L ## string
42 
43 #define _WIDE_STRING( string ) \
44 	_WIDE_STRING_INTERMEDIATE( string )
45 
46 /* String allocation
47  */
48 #define wide_string_allocate( size ) \
49 	(wchar_t *) memory_allocate( sizeof( wchar_t ) * ( size ) )
50 
51 /* String reallocation
52  */
53 #define wide_string_reallocate( string, size ) \
54 	(wchar_t *) memory_reallocate( string, ( sizeof( wchar_t ) * ( size ) ) )
55 
56 /* String length
57  */
58 #if defined( HAVE_WCSLEN ) || defined( WINAPI )
59 #define wide_string_length( string ) \
60 	wcslen( string )
61 #endif
62 
63 /* String compare
64  */
65 #if defined( HAVE_WMEMCMP )
66 #define wide_string_compare( string1, string2, size ) \
67 	wmemcmp( (void *) string1, (void *) string2, size )
68 
69 #elif defined( HAVE_WCSNCMP ) || defined( WINAPI )
70 #define wide_string_compare( string1, string2, size ) \
71 	wcsncmp( string1, string2, size )
72 #endif
73 
74 /* Caseless string compare
75  */
76 #if defined( _MSC_VER ) || ( defined( __BORLANDC__ ) && ( __BORLANDC__ >= 0x0551 ) )
77 #define wide_string_compare_no_case( string1, string2, size ) \
78 	_wcsnicmp( string1, string2, size )
79 
80 #elif ( defined( WINAPI ) && !defined( __CYGWIN__ ) ) || defined( HAVE_WCSNICMP )
81 #define wide_string_compare_no_case( string1, string2, size ) \
82 	wcsnicmp( string1, string2, size )
83 
84 #elif defined( HAVE_WCSNCASECMP )
85 #define wide_string_compare_no_case( string1, string2, size ) \
86 	wcsncasecmp( string1, string2, size )
87 
88 #elif defined( HAVE_WCSCASECMP )
89 #define wide_string_compare_no_case( string1, string2, size ) \
90 	wcscasecmp( string1, string2 )
91 #endif
92 
93 /* String copy
94  */
95 #if defined( HAVE_WMEMCPY )
96 #define wide_string_copy( destination, source, size ) \
97 	(wchar_t *) wmemcpy( (void *) destination, (void *) source, size )
98 
99 #elif defined( HAVE_WCSNCPY ) || defined( WINAPI )
100 #define wide_string_copy( destination, source, size ) \
101 	wcsncpy( destination, source, size )
102 #endif
103 
104 /* String character search
105  */
106 #if defined( HAVE_WMEMCHR )
107 #define wide_string_search_character( string, character, size ) \
108 	(wchar_t *) wmemchr( (void *) string, (wchar_t) character, size )
109 
110 #elif defined( HAVE_WCSCHR ) || defined( WINAPI )
111 #define wide_string_search_character( string, character, size ) \
112 	wcschr( string, (wchar_t) character )
113 
114 #endif
115 
116 /* String reverse character search
117  */
118 #if defined( HAVE_WMEMRCHR )
119 #define wide_string_search_character_reverse( string, character, size ) \
120 	(wchar_t *) wmemrchr( (void *) string, (wchar_t) character, size )
121 
122 #elif defined( HAVE_WCSRCHR ) || defined( WINAPI )
123 /* (void)(size) is used to suppress unused variable warnings */
124 #define wide_string_search_character_reverse( string, character, size ) \
125 	wcsrchr( string, (wchar_t) character ); (void)(size)
126 #endif
127 
128 /* String sub-string search
129  */
130 #if defined( HAVE_WCSSTR ) || defined( WINAPI )
131 #define wide_string_search_string( string, substring, size ) \
132 	wcsstr( string, substring )
133 
134 #endif
135 
136 /* String formatted print (snwprintf)
137  */
138 #if defined( _MSC_VER )
139 #define wide_string_snwprintf( target, size, ... ) \
140 	swprintf_s( target, size, __VA_ARGS__ )
141 
142 #elif defined( __BORLANDC__ ) && ( __BORLANDC__ < 0x0560 )
143 #define wide_string_snwprintf \
144 	snwprintf
145 
146 #elif defined( WINAPI )
147 #define wide_string_snwprintf( target, size, ... ) \
148 	snwprintf( target, size, __VA_ARGS__ )
149 
150 #elif defined( HAVE_SWPRINTF )
151 #define wide_string_snwprintf( target, size, ... ) \
152 	swprintf( target, size, __VA_ARGS__ )
153 #endif
154 
155 /* Variable arguments formatted print to string function (vsnwprintf)
156  */
157 #if defined( __BORLANDC__ ) && ( __BORLANDC__ < 0x0560 )
158 #define wide_string_vsnwprintf \
159 	_vsnwprintf
160 
161 #elif defined( WINAPI )
162 #define wide_string_vsnwprintf( string, size, format, ... ) \
163 	_vsnwprintf( string, size, format, __VA_ARGS__ )
164 
165 #elif defined( HAVE_VSWPRINTF )
166 #define wide_string_vsnwprintf( string, size, format, ... ) \
167 	vswprintf( string, size, format, __VA_ARGS__ )
168 #endif
169 
170 #if defined( __cplusplus )
171 }
172 #endif
173 
174 #endif /* !defined( _WIDE_STRING_H ) */
175 
176