Added Unicode ctype support.
[wine] / dlls / user / lstr.c
1 /*
2  * USER string functions
3  *
4  * Copyright 1993 Yngvi Sigurjonsson (yngvi@hafro.is)
5  * Copyright 1996 Alexandre Julliard
6  * Copyright 1996 Marcus Meissner
7  */
8
9 #include <ctype.h>
10 #include <stdarg.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13
14 #include "windef.h"
15 #include "winbase.h"
16 #include "winnls.h"
17 #include "wine/winbase16.h"
18 #include "wine/winuser16.h"
19 #include "wine/unicode.h"
20
21 #include "heap.h"
22 #include "ldt.h"
23 #include "debugtools.h"
24
25 DEFAULT_DEBUG_CHANNEL(resource);
26
27 /***********************************************************************
28  *           AnsiToOem16   (KEYBOARD.5)
29  */
30 INT16 WINAPI AnsiToOem16( LPCSTR s, LPSTR d )
31 {
32     CharToOemA( s, d );
33     return -1;
34 }
35
36
37 /***********************************************************************
38  *           OemToAnsi16   (KEYBOARD.6)
39  */
40 INT16 WINAPI OemToAnsi16( LPCSTR s, LPSTR d )
41 {
42     OemToCharA( s, d );
43     return -1;
44 }
45
46
47 /***********************************************************************
48  *           AnsiToOemBuff16   (KEYBOARD.134)
49  */
50 void WINAPI AnsiToOemBuff16( LPCSTR s, LPSTR d, UINT16 len )
51 {
52     if (len != 0) CharToOemBuffA( s, d, len );
53 }
54
55
56 /***********************************************************************
57  *           OemToAnsiBuff16   (KEYBOARD.135)
58  */
59 void WINAPI OemToAnsiBuff16( LPCSTR s, LPSTR d, UINT16 len )
60 {
61     if (len != 0) OemToCharBuffA( s, d, len );
62 }
63
64
65 /***********************************************************************
66  *           AnsiUpper16   (USER.431)
67  */
68 SEGPTR WINAPI AnsiUpper16( SEGPTR strOrChar )
69 {
70     /* uppercase only one char if strOrChar < 0x10000 */
71     if (HIWORD(strOrChar))
72     {
73         CharUpperA( PTR_SEG_TO_LIN(strOrChar) );
74         return strOrChar;
75     }
76     else return toupper((char)strOrChar);
77 }
78
79
80 /***********************************************************************
81  *           AnsiLower16   (USER.432)
82  */
83 SEGPTR WINAPI AnsiLower16( SEGPTR strOrChar )
84 {
85     /* lowercase only one char if strOrChar < 0x10000 */
86     if (HIWORD(strOrChar))
87     {
88         CharLowerA( PTR_SEG_TO_LIN(strOrChar) );
89         return strOrChar;
90     }
91     else return tolower((char)strOrChar);
92 }
93
94
95 /***********************************************************************
96  *           AnsiUpperBuff16   (USER.437)
97  */
98 UINT16 WINAPI AnsiUpperBuff16( LPSTR str, UINT16 len )
99 {
100     CharUpperBuffA( str, len ? len : 65536 );
101     return len;
102 }
103
104
105 /***********************************************************************
106  *           AnsiLowerBuff16   (USER.438)
107  */
108 UINT16 WINAPI AnsiLowerBuff16( LPSTR str, UINT16 len )
109 {
110     CharLowerBuffA( str, len ? len : 65536 );
111     return len;
112 }
113
114
115 /***********************************************************************
116  *           AnsiNext16   (USER.472)
117  */
118 SEGPTR WINAPI AnsiNext16(SEGPTR current)
119 {
120     char *ptr = (char *)PTR_SEG_TO_LIN(current);
121     return current + (CharNextA(ptr) - ptr);
122 }
123
124
125 /***********************************************************************
126  *           AnsiPrev16   (USER.473)
127  */
128 SEGPTR WINAPI AnsiPrev16( LPCSTR start, SEGPTR current )
129 {
130     char *ptr = (char *)PTR_SEG_TO_LIN(current);
131     return current - (ptr - CharPrevA( start, ptr ));
132 }
133
134
135 /***********************************************************************
136  *           CharNextA   (USER32.@)
137  */
138 LPSTR WINAPI CharNextA( LPCSTR ptr )
139 {
140     if (!*ptr) return (LPSTR)ptr;
141     if (IsDBCSLeadByte( ptr[0] ) && ptr[1]) return (LPSTR)(ptr + 2);
142     return (LPSTR)(ptr + 1);
143 }
144
145
146 /***********************************************************************
147  *           CharNextExA   (USER32.@)
148  */
149 LPSTR WINAPI CharNextExA( WORD codepage, LPCSTR ptr, DWORD flags )
150 {
151     if (!*ptr) return (LPSTR)ptr;
152     if (IsDBCSLeadByteEx( codepage, ptr[0] ) && ptr[1]) return (LPSTR)(ptr + 2);
153     return (LPSTR)(ptr + 1);
154 }
155
156
157 /***********************************************************************
158  *           CharNextExW   (USER32.@)
159  */
160 LPWSTR WINAPI CharNextExW( WORD codepage, LPCWSTR ptr, DWORD flags )
161 {
162     /* doesn't make sense, there are no codepages for Unicode */
163     return NULL;
164 }
165
166
167 /***********************************************************************
168  *           CharNextW   (USER32.@)
169  */
170 LPWSTR WINAPI CharNextW(LPCWSTR x)
171 {
172     if (*x) x++;
173
174     return (LPWSTR)x;
175 }
176
177
178 /***********************************************************************
179  *           CharPrevA   (USER32.@)
180  */
181 LPSTR WINAPI CharPrevA( LPCSTR start, LPCSTR ptr )
182 {
183     while (*start && (start < ptr))
184     {
185         LPCSTR next = CharNextA( start );
186         if (next >= ptr) break;
187         start = next;
188     }
189     return (LPSTR)start;
190 }
191
192
193 /***********************************************************************
194  *           CharPrevExA   (USER32.@)
195  */
196 LPSTR WINAPI CharPrevExA( WORD codepage, LPCSTR start, LPCSTR ptr, DWORD flags )
197 {
198     while (*start && (start < ptr))
199     {
200         LPCSTR next = CharNextExA( codepage, start, flags );
201         if (next > ptr) break;
202         start = next;
203     }
204     return (LPSTR)start;
205 }
206
207
208 /***********************************************************************
209  *           CharPrevExW   (USER32.@)
210  */
211 LPSTR WINAPI CharPrevExW( WORD codepage, LPCWSTR start, LPCWSTR ptr, DWORD flags )
212 {
213     /* doesn't make sense, there are no codepages for Unicode */
214     return NULL;
215 }
216
217
218 /***********************************************************************
219  *           CharPrevW   (USER32.@)
220  */
221 LPWSTR WINAPI CharPrevW(LPCWSTR start,LPCWSTR x)
222 {
223     if (x>start) return (LPWSTR)(x-1);
224     else return (LPWSTR)x;
225 }
226
227
228 /***********************************************************************
229  *           CharToOemA   (USER32.@)
230  */
231 BOOL WINAPI CharToOemA( LPCSTR s, LPSTR d )
232 {
233     if ( !s || !d ) return TRUE;
234     return CharToOemBuffA( s, d, strlen( s ) + 1 );
235 }
236
237
238 /***********************************************************************
239  *           CharToOemBuffA   (USER32.@)
240  */
241 BOOL WINAPI CharToOemBuffA( LPCSTR s, LPSTR d, DWORD len )
242 {
243     WCHAR *bufW;
244
245     bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
246     if( bufW )
247     {
248         MultiByteToWideChar( CP_ACP, 0, s, len, bufW, len );
249         WideCharToMultiByte( CP_OEMCP, 0, bufW, len, d, len, NULL, NULL );
250         HeapFree( GetProcessHeap(), 0, bufW );
251     }
252     return TRUE;
253 }
254
255
256 /***********************************************************************
257  *           CharToOemBuffW   (USER32.@)
258  */
259 BOOL WINAPI CharToOemBuffW( LPCWSTR s, LPSTR d, DWORD len )
260 {
261    if ( !s || !d ) return TRUE;
262     WideCharToMultiByte( CP_OEMCP, 0, s, len, d, len, NULL, NULL );
263     return TRUE;
264 }
265
266
267 /***********************************************************************
268  *           CharToOemW   (USER32.@)
269  */
270 BOOL WINAPI CharToOemW( LPCWSTR s, LPSTR d )
271 {
272     return CharToOemBuffW( s, d, strlenW( s ) + 1 );
273 }
274
275
276 /***********************************************************************
277  *           OemToCharA   (USER32.@)
278  */
279 BOOL WINAPI OemToCharA( LPCSTR s, LPSTR d )
280 {
281     return OemToCharBuffA( s, d, strlen( s ) + 1 );
282 }
283
284
285 /***********************************************************************
286  *           OemToCharBuffA   (USER32.@)
287  */
288 BOOL WINAPI OemToCharBuffA( LPCSTR s, LPSTR d, DWORD len )
289 {
290     WCHAR *bufW;
291
292     bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
293     if( bufW )
294     {
295         MultiByteToWideChar( CP_OEMCP, 0, s, len, bufW, len );
296         WideCharToMultiByte( CP_ACP, 0, bufW, len, d, len, NULL, NULL );
297         HeapFree( GetProcessHeap(), 0, bufW );
298     }
299     return TRUE;
300 }
301
302
303 /***********************************************************************
304  *           OemToCharBuffW   (USER32.@)
305  */
306 BOOL WINAPI OemToCharBuffW( LPCSTR s, LPWSTR d, DWORD len )
307 {
308     MultiByteToWideChar( CP_OEMCP, 0, s, len, d, len );
309     return TRUE;
310 }
311
312
313 /***********************************************************************
314  *           OemToCharW   (USER32.@)
315  */
316 BOOL WINAPI OemToCharW( LPCSTR s, LPWSTR d )
317 {
318     return OemToCharBuffW( s, d, strlen( s ) + 1 );
319 }
320
321
322 /***********************************************************************
323  *           IsCharLowerA   (USER.436) (USER32.@)
324  * FIXME: handle current locale
325  */
326 BOOL WINAPI IsCharLowerA(CHAR x)
327 {
328     return islower(x);
329 }
330
331
332 /***********************************************************************
333  *           IsCharLowerW   (USER32.@)
334  */
335 BOOL WINAPI IsCharLowerW(WCHAR x)
336 {
337     return get_char_typeW(x) & C1_LOWER;
338 }
339
340
341 /***********************************************************************
342  *           IsCharUpperA   (USER.435) (USER32.337)
343  * FIXME: handle current locale
344  */
345 BOOL WINAPI IsCharUpperA(CHAR x)
346 {
347     return isupper(x);
348 }
349
350
351 /***********************************************************************
352  *           IsCharUpperW   (USER32.@)
353  */
354 BOOL WINAPI IsCharUpperW(WCHAR x)
355 {
356     return get_char_typeW(x) & C1_UPPER;
357 }
358
359
360 /***********************************************************************
361  *           IsCharAlphaNumericW   (USER32.@)
362  */
363 BOOL WINAPI IsCharAlphaNumericW(WCHAR x)
364 {
365     return get_char_typeW(x) & (C1_ALPHA|C1_DIGIT|C1_LOWER|C1_UPPER);
366 }
367
368
369 /***********************************************************************
370  *           IsCharAlphaW   (USER32.@)
371  */
372 BOOL WINAPI IsCharAlphaW(WCHAR x)
373 {
374     return get_char_typeW(x) & (C1_ALPHA|C1_LOWER|C1_UPPER);
375 }
376
377
378 /***********************************************************************
379  *           FormatMessage16   (USER.606)
380  */
381 DWORD WINAPI FormatMessage16(
382     DWORD   dwFlags,
383     SEGPTR lpSource, /*not always a valid pointer*/
384     WORD   dwMessageId,
385     WORD   dwLanguageId,
386     LPSTR  lpBuffer, /* *((HLOCAL16*)) for FORMAT_MESSAGE_ALLOCATE_BUFFER*/
387     WORD   nSize,
388     LPDWORD args /* va_list *args */
389 ) {
390 #ifdef __i386__
391 /* This implementation is completely dependant on the format of the va_list on x86 CPUs */
392     LPSTR       target,t;
393     DWORD       talloced;
394     LPSTR       from,f;
395     DWORD       width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
396     BOOL        eos = FALSE;
397     LPSTR       allocstring = NULL;
398
399     TRACE("(0x%lx,%lx,%d,0x%x,%p,%d,%p)\n",
400           dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
401         if ((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
402                 && (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)) return 0;
403         if ((dwFlags & FORMAT_MESSAGE_FROM_STRING)
404                 &&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
405                         || (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
406
407     if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK) 
408         FIXME("line wrapping (%lu) not supported.\n", width);
409     from = NULL;
410     if (dwFlags & FORMAT_MESSAGE_FROM_STRING)
411         from = HEAP_strdupA( GetProcessHeap(), 0, PTR_SEG_TO_LIN(lpSource));
412     if (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM) {
413         from = HeapAlloc( GetProcessHeap(),0,200 );
414         sprintf(from,"Systemmessage, messageid = 0x%08x\n",dwMessageId);
415     }
416     if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE) {
417         INT16   bufsize;
418         HINSTANCE16 hinst16 = ((HMODULE)lpSource & 0xffff);
419
420         dwMessageId &= 0xFFFF;
421         bufsize=LoadString16(hinst16,dwMessageId,NULL,0);
422         if (bufsize) {
423             from = HeapAlloc( GetProcessHeap(), 0, bufsize +1);
424             LoadString16(hinst16,dwMessageId,from,bufsize+1);
425         }
426     }
427     target      = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
428     t   = target;
429     talloced= 100;
430
431 #define ADD_TO_T(c) \
432         *t++=c;\
433         if (t-target == talloced) {\
434                 target  = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
435                 t       = target+talloced;\
436                 talloced*=2;\
437         }
438
439     if (from) {
440         f=from;
441         while (*f && !eos) {
442             if (*f=='%') {
443                 int     insertnr;
444                 char    *fmtstr,*x,*lastf;
445                 DWORD   *argliststart;
446
447                 fmtstr = NULL;
448                 lastf = f;
449                 f++;
450                 if (!*f) {
451                     ADD_TO_T('%');
452                     continue;
453                 }
454                 switch (*f) {
455                 case '1':case '2':case '3':case '4':case '5':
456                 case '6':case '7':case '8':case '9':
457                     insertnr=*f-'0';
458                     switch (f[1]) {
459                     case '0':case '1':case '2':case '3':
460                     case '4':case '5':case '6':case '7':
461                     case '8':case '9':
462                         f++;
463                         insertnr=insertnr*10+*f-'0';
464                         f++;
465                         break;
466                     default:
467                         f++;
468                         break;
469                     }
470                     if (*f=='!') {
471                         f++;
472                         if (NULL!=(x=strchr(f,'!'))) {
473                             *x='\0';
474                             fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
475                             sprintf(fmtstr,"%%%s",f);
476                             f=x+1;
477                         } else {
478                             fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
479                             sprintf(fmtstr,"%%%s",f);
480                             f+=strlen(f); /*at \0*/
481                         }
482                     } else
483                         if(!args) 
484                             break;
485                         else
486                           fmtstr=HEAP_strdupA(GetProcessHeap(),0,"%s");
487                     if (args) {
488                         int     sz;
489                         LPSTR   b = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sz = 100);
490                         
491                         argliststart=args+insertnr-1;
492                        
493                         /* CMF - This makes a BIG assumption about va_list */
494                         while (vsnprintf(b, sz, fmtstr, (va_list) argliststart) < 0) {
495                             b = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, b, sz += 100);
496                         }
497                         for (x=b; *x; x++) ADD_TO_T(*x);
498                     } else {
499                         /* NULL args - copy formatstr 
500                          * (probably wrong)
501                          */
502                         while ((lastf<f)&&(*lastf)) {
503                             ADD_TO_T(*lastf++);
504                         }
505                     }
506                     HeapFree(GetProcessHeap(),0,fmtstr);
507                     break;
508                 case '0': /* Just stop processing format string */
509                     eos = TRUE;
510                     f++;
511                     break;
512                 case 'n': /* 16 bit version just outputs 'n' */
513                 default:
514                     ADD_TO_T(*f++);
515                     break;
516                 }
517             } else { /* '\n' or '\r' gets mapped to "\r\n" */
518                 if(*f == '\n' || *f == '\r') {
519                     if (width == 0) {
520                         ADD_TO_T('\r');
521                         ADD_TO_T('\n');
522                         if(*f++ == '\r' && *f == '\n')
523                             f++;
524                     }
525                 } else {
526                     ADD_TO_T(*f++);
527                 }
528             }
529         }
530         *t='\0';
531     }
532     talloced = strlen(target)+1;
533     if (nSize && talloced<nSize) {
534         target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
535     }
536     TRACE("-- %s\n",debugstr_a(target));
537     if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
538         /* nSize is the MINIMUM size */
539         HLOCAL16 h = LocalAlloc16(LPTR,talloced);
540         SEGPTR ptr = LocalLock16(h);
541         allocstring = PTR_SEG_TO_LIN( ptr );
542         memcpy( allocstring,target,talloced);
543         LocalUnlock16( h );
544         *((HLOCAL16*)lpBuffer) = h;
545     } else
546         lstrcpynA(lpBuffer,target,nSize);
547     HeapFree(GetProcessHeap(),0,target);
548     if (from) HeapFree(GetProcessHeap(),0,from);
549     return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ? 
550         strlen(allocstring):
551         strlen(lpBuffer);
552 #else
553         return 0;
554 #endif /* __i386__ */
555 }
556 #undef ADD_TO_T