- improve some parameter checking in WSAIoctl
[wine.git] / include / wine / unicode.h
blob98ad0fb919d7d1a04d74af8bc17a4e27ab5e9053
1 /*
2 * Wine internal Unicode definitions
4 * Copyright 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #ifndef __WINE_UNICODE_H
22 #define __WINE_UNICODE_H
24 #include <stdarg.h>
26 #include <windef.h>
27 #include <winbase.h>
28 #include <winnls.h>
30 /* code page info common to SBCS and DBCS */
31 struct cp_info
33 unsigned int codepage; /* codepage id */
34 unsigned int char_size; /* char size (1 or 2 bytes) */
35 WCHAR def_char; /* default char value (can be double-byte) */
36 WCHAR def_unicode_char; /* default Unicode char value */
37 const char *name; /* code page name */
40 struct sbcs_table
42 struct cp_info info;
43 const WCHAR *cp2uni; /* code page -> Unicode map */
44 const unsigned char *uni2cp_low; /* Unicode -> code page map */
45 const unsigned short *uni2cp_high;
48 struct dbcs_table
50 struct cp_info info;
51 const WCHAR *cp2uni; /* code page -> Unicode map */
52 const unsigned char *cp2uni_leadbytes;
53 const unsigned short *uni2cp_low; /* Unicode -> code page map */
54 const unsigned short *uni2cp_high;
55 unsigned char lead_bytes[12]; /* lead bytes ranges */
58 union cptable
60 struct cp_info info;
61 struct sbcs_table sbcs;
62 struct dbcs_table dbcs;
65 extern const union cptable *wine_cp_get_table( unsigned int codepage );
66 extern const union cptable *wine_cp_enum_table( unsigned int index );
68 extern int wine_cp_mbstowcs( const union cptable *table, int flags,
69 const char *src, int srclen,
70 WCHAR *dst, int dstlen );
71 extern int wine_cp_wcstombs( const union cptable *table, int flags,
72 const WCHAR *src, int srclen,
73 char *dst, int dstlen, const char *defchar, int *used );
74 extern int wine_utf8_wcstombs( const WCHAR *src, int srclen, char *dst, int dstlen );
75 extern int wine_utf8_mbstowcs( int flags, const char *src, int srclen, WCHAR *dst, int dstlen );
77 extern int wine_get_sortkey( int flags, const WCHAR *src, int srclen, char *dst, int dstlen );
79 extern int strcmpiW( const WCHAR *str1, const WCHAR *str2 );
80 extern int strncmpiW( const WCHAR *str1, const WCHAR *str2, int n );
81 extern WCHAR *strstrW( const WCHAR *str, const WCHAR *sub );
82 extern long int strtolW( const WCHAR *nptr, WCHAR **endptr, int base );
83 extern unsigned long int strtoulW( const WCHAR *nptr, WCHAR **endptr, int base );
84 extern int sprintfW( WCHAR *str, const WCHAR *format, ... );
85 extern int snprintfW( WCHAR *str, size_t len, const WCHAR *format, ... );
86 extern int vsprintfW( WCHAR *str, const WCHAR *format, va_list valist );
87 extern int vsnprintfW( WCHAR *str, size_t len, const WCHAR *format, va_list valist );
89 static inline int is_dbcs_leadbyte( const union cptable *table, unsigned char ch )
91 return (table->info.char_size == 2) && (table->dbcs.cp2uni_leadbytes[ch]);
94 static inline WCHAR tolowerW( WCHAR ch )
96 extern const WCHAR wine_casemap_lower[];
97 return ch + wine_casemap_lower[wine_casemap_lower[ch >> 8] + (ch & 0xff)];
100 static inline WCHAR toupperW( WCHAR ch )
102 extern const WCHAR wine_casemap_upper[];
103 return ch + wine_casemap_upper[wine_casemap_upper[ch >> 8] + (ch & 0xff)];
106 /* the character type contains the C1_* flags in the low 12 bits */
107 /* and the C2_* type in the high 4 bits */
108 static inline unsigned short get_char_typeW( WCHAR ch )
110 extern const unsigned short wine_wctype_table[];
111 return wine_wctype_table[wine_wctype_table[ch >> 8] + (ch & 0xff)];
114 inline static int iscntrlW( WCHAR wc )
116 return get_char_typeW(wc) & C1_CNTRL;
119 inline static int ispunctW( WCHAR wc )
121 return get_char_typeW(wc) & C1_PUNCT;
124 inline static int isspaceW( WCHAR wc )
126 return get_char_typeW(wc) & C1_SPACE;
129 inline static int isdigitW( WCHAR wc )
131 return get_char_typeW(wc) & C1_DIGIT;
134 inline static int isxdigitW( WCHAR wc )
136 return get_char_typeW(wc) & C1_XDIGIT;
139 inline static int islowerW( WCHAR wc )
141 return get_char_typeW(wc) & C1_LOWER;
144 inline static int isupperW( WCHAR wc )
146 return get_char_typeW(wc) & C1_UPPER;
149 inline static int isalnumW( WCHAR wc )
151 return get_char_typeW(wc) & (C1_ALPHA|C1_DIGIT|C1_LOWER|C1_UPPER);
154 inline static int isalphaW( WCHAR wc )
156 return get_char_typeW(wc) & (C1_ALPHA|C1_LOWER|C1_UPPER);
159 inline static int isgraphW( WCHAR wc )
161 return get_char_typeW(wc) & (C1_ALPHA|C1_PUNCT|C1_DIGIT|C1_LOWER|C1_UPPER);
164 inline static int isprintW( WCHAR wc )
166 return get_char_typeW(wc) & (C1_ALPHA|C1_BLANK|C1_PUNCT|C1_DIGIT|C1_LOWER|C1_UPPER);
169 /* some useful string manipulation routines */
171 static inline unsigned int strlenW( const WCHAR *str )
173 #if defined(__i386__) && defined(__GNUC__)
174 int dummy, res;
175 __asm__ __volatile__( "cld\n\t"
176 "repnz\n\t"
177 "scasw\n\t"
178 "notl %0"
179 : "=c" (res), "=&D" (dummy)
180 : "0" (0xffffffff), "1" (str), "a" (0) );
181 return res - 1;
182 #else
183 const WCHAR *s = str;
184 while (*s) s++;
185 return s - str;
186 #endif
189 static inline WCHAR *strcpyW( WCHAR *dst, const WCHAR *src )
191 #if defined(__i386__) && defined(__GNUC__)
192 int dummy1, dummy2, dummy3;
193 __asm__ __volatile__( "cld\n"
194 "1:\tlodsw\n\t"
195 "stosw\n\t"
196 "testw %%ax,%%ax\n\t"
197 "jne 1b"
198 : "=&S" (dummy1), "=&D" (dummy2), "=&a" (dummy3)
199 : "0" (src), "1" (dst)
200 : "memory" );
201 #else
202 WCHAR *p = dst;
203 while ((*p++ = *src++));
204 #endif
205 return dst;
208 static inline int strcmpW( const WCHAR *str1, const WCHAR *str2 )
210 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
211 return *str1 - *str2;
214 static inline int strncmpW( const WCHAR *str1, const WCHAR *str2, int n )
216 if (n <= 0) return 0;
217 while ((--n > 0) && *str1 && (*str1 == *str2)) { str1++; str2++; }
218 return *str1 - *str2;
221 static inline WCHAR *strncpyW( WCHAR *str1, const WCHAR *str2, int n )
223 WCHAR *ret = str1;
224 while (n-- > 0) if (!(*str1++ = *str2++)) break;
225 while (n-- > 0) *str1++ = 0;
226 return ret;
229 static inline WCHAR *strcatW( WCHAR *dst, const WCHAR *src )
231 strcpyW( dst + strlenW(dst), src );
232 return dst;
235 static inline WCHAR *strchrW( const WCHAR *str, WCHAR ch )
237 for ( ; *str; str++) if (*str == ch) return (WCHAR *)str;
238 return NULL;
241 static inline WCHAR *strrchrW( const WCHAR *str, WCHAR ch )
243 WCHAR *ret = NULL;
244 for ( ; *str; str++) if (*str == ch) ret = (WCHAR *)str;
245 return ret;
248 static inline WCHAR *strpbrkW( const WCHAR *str, const WCHAR *accept )
250 for ( ; *str; str++) if (strchrW( accept, *str )) return (WCHAR *)str;
251 return NULL;
254 static inline WCHAR *strlwrW( WCHAR *str )
256 WCHAR *ret = str;
257 while ((*str = tolowerW(*str))) str++;
258 return ret;
261 static inline WCHAR *struprW( WCHAR *str )
263 WCHAR *ret = str;
264 while ((*str = toupperW(*str))) str++;
265 return ret;
268 static inline long int atolW( const WCHAR *str )
270 return strtolW( str, (WCHAR **)0, 10 );
273 static inline int atoiW( const WCHAR *str )
275 return (int)atolW( str );
278 #endif /* __WINE_UNICODE_H */