Fixed indentation problems resulting from automatic renaming, other
[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  *           lstrcmp16   (USER.430)
67  */
68 INT16 WINAPI lstrcmp16( LPCSTR str1, LPCSTR str2 )
69 {
70     return (INT16)strcmp( str1, str2 );
71 }
72
73
74 /***********************************************************************
75  *           AnsiUpper16   (USER.431)
76  */
77 SEGPTR WINAPI AnsiUpper16( SEGPTR strOrChar )
78 {
79     /* uppercase only one char if strOrChar < 0x10000 */
80     if (HIWORD(strOrChar))
81     {
82         CharUpperA( PTR_SEG_TO_LIN(strOrChar) );
83         return strOrChar;
84     }
85     else return toupper((char)strOrChar);
86 }
87
88
89 /***********************************************************************
90  *           AnsiLower16   (USER.432)
91  */
92 SEGPTR WINAPI AnsiLower16( SEGPTR strOrChar )
93 {
94     /* lowercase only one char if strOrChar < 0x10000 */
95     if (HIWORD(strOrChar))
96     {
97         CharLowerA( PTR_SEG_TO_LIN(strOrChar) );
98         return strOrChar;
99     }
100     else return tolower((char)strOrChar);
101 }
102
103
104 /***********************************************************************
105  *           AnsiUpperBuff16   (USER.437)
106  */
107 UINT16 WINAPI AnsiUpperBuff16( LPSTR str, UINT16 len )
108 {
109     CharUpperBuffA( str, len ? len : 65536 );
110     return len;
111 }
112
113
114 /***********************************************************************
115  *           AnsiLowerBuff16   (USER.438)
116  */
117 UINT16 WINAPI AnsiLowerBuff16( LPSTR str, UINT16 len )
118 {
119     CharLowerBuffA( str, len ? len : 65536 );
120     return len;
121 }
122
123
124 /***********************************************************************
125  *           AnsiNext16   (USER.472)
126  */
127 SEGPTR WINAPI AnsiNext16(SEGPTR current)
128 {
129     char *ptr = (char *)PTR_SEG_TO_LIN(current);
130     return current + (CharNextA(ptr) - ptr);
131 }
132
133
134 /***********************************************************************
135  *           AnsiPrev16   (USER.473)
136  */
137 SEGPTR WINAPI AnsiPrev16( LPCSTR start, SEGPTR current )
138 {
139     char *ptr = (char *)PTR_SEG_TO_LIN(current);
140     return current - (ptr - CharPrevA( start, ptr ));
141 }
142
143
144 /***********************************************************************
145  *           CharNextA   (USER32.@)
146  */
147 LPSTR WINAPI CharNextA( LPCSTR ptr )
148 {
149     if (!*ptr) return (LPSTR)ptr;
150     if (IsDBCSLeadByte( ptr[0] ) && ptr[1]) return (LPSTR)(ptr + 2);
151     return (LPSTR)(ptr + 1);
152 }
153
154
155 /***********************************************************************
156  *           CharNextExA   (USER32.@)
157  */
158 LPSTR WINAPI CharNextExA( WORD codepage, LPCSTR ptr, DWORD flags )
159 {
160     if (!*ptr) return (LPSTR)ptr;
161     if (IsDBCSLeadByteEx( codepage, ptr[0] ) && ptr[1]) return (LPSTR)(ptr + 2);
162     return (LPSTR)(ptr + 1);
163 }
164
165
166 /***********************************************************************
167  *           CharNextExW   (USER32.@)
168  */
169 LPWSTR WINAPI CharNextExW( WORD codepage, LPCWSTR ptr, DWORD flags )
170 {
171     /* doesn't make sense, there are no codepages for Unicode */
172     return NULL;
173 }
174
175
176 /***********************************************************************
177  *           CharNextW   (USER32.@)
178  */
179 LPWSTR WINAPI CharNextW(LPCWSTR x)
180 {
181     if (*x) x++;
182
183     return (LPWSTR)x;
184 }
185
186
187 /***********************************************************************
188  *           CharPrevA   (USER32.@)
189  */
190 LPSTR WINAPI CharPrevA( LPCSTR start, LPCSTR ptr )
191 {
192     while (*start && (start < ptr))
193     {
194         LPCSTR next = CharNextA( start );
195         if (next >= ptr) break;
196         start = next;
197     }
198     return (LPSTR)start;
199 }
200
201
202 /***********************************************************************
203  *           CharPrevExA   (USER32.@)
204  */
205 LPSTR WINAPI CharPrevExA( WORD codepage, LPCSTR start, LPCSTR ptr, DWORD flags )
206 {
207     while (*start && (start < ptr))
208     {
209         LPCSTR next = CharNextExA( codepage, start, flags );
210         if (next > ptr) break;
211         start = next;
212     }
213     return (LPSTR)start;
214 }
215
216
217 /***********************************************************************
218  *           CharPrevExW   (USER32.@)
219  */
220 LPSTR WINAPI CharPrevExW( WORD codepage, LPCWSTR start, LPCWSTR ptr, DWORD flags )
221 {
222     /* doesn't make sense, there are no codepages for Unicode */
223     return NULL;
224 }
225
226
227 /***********************************************************************
228  *           CharPrevW   (USER32.@)
229  */
230 LPWSTR WINAPI CharPrevW(LPCWSTR start,LPCWSTR x)
231 {
232     if (x>start) return (LPWSTR)(x-1);
233     else return (LPWSTR)x;
234 }
235
236
237 /***********************************************************************
238  *           CharToOemA   (USER32.@)
239  */
240 BOOL WINAPI CharToOemA( LPCSTR s, LPSTR d )
241 {
242     if ( !s || !d ) return TRUE;
243     return CharToOemBuffA( s, d, strlen( s ) + 1 );
244 }
245
246
247 /***********************************************************************
248  *           CharToOemBuffA   (USER32.@)
249  */
250 BOOL WINAPI CharToOemBuffA( LPCSTR s, LPSTR d, DWORD len )
251 {
252     WCHAR *bufW;
253
254     bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
255     if( bufW )
256     {
257         MultiByteToWideChar( CP_ACP, 0, s, len, bufW, len );
258         WideCharToMultiByte( CP_OEMCP, 0, bufW, len, d, len, NULL, NULL );
259         HeapFree( GetProcessHeap(), 0, bufW );
260     }
261     return TRUE;
262 }
263
264
265 /***********************************************************************
266  *           CharToOemBuffW   (USER32.@)
267  */
268 BOOL WINAPI CharToOemBuffW( LPCWSTR s, LPSTR d, DWORD len )
269 {
270    if ( !s || !d ) return TRUE;
271     WideCharToMultiByte( CP_OEMCP, 0, s, len, d, len, NULL, NULL );
272     return TRUE;
273 }
274
275
276 /***********************************************************************
277  *           CharToOemW   (USER32.@)
278  */
279 BOOL WINAPI CharToOemW( LPCWSTR s, LPSTR d )
280 {
281     return CharToOemBuffW( s, d, strlenW( s ) + 1 );
282 }
283
284
285 /***********************************************************************
286  *           OemToCharA   (USER32.@)
287  */
288 BOOL WINAPI OemToCharA( LPCSTR s, LPSTR d )
289 {
290     return OemToCharBuffA( s, d, strlen( s ) + 1 );
291 }
292
293
294 /***********************************************************************
295  *           OemToCharBuffA   (USER32.@)
296  */
297 BOOL WINAPI OemToCharBuffA( LPCSTR s, LPSTR d, DWORD len )
298 {
299     WCHAR *bufW;
300
301     bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
302     if( bufW )
303     {
304         MultiByteToWideChar( CP_OEMCP, 0, s, len, bufW, len );
305         WideCharToMultiByte( CP_ACP, 0, bufW, len, d, len, NULL, NULL );
306         HeapFree( GetProcessHeap(), 0, bufW );
307     }
308     return TRUE;
309 }
310
311
312 /***********************************************************************
313  *           OemToCharBuffW   (USER32.@)
314  */
315 BOOL WINAPI OemToCharBuffW( LPCSTR s, LPWSTR d, DWORD len )
316 {
317     MultiByteToWideChar( CP_OEMCP, 0, s, len, d, len );
318     return TRUE;
319 }
320
321
322 /***********************************************************************
323  *           OemToCharW   (USER32.@)
324  */
325 BOOL WINAPI OemToCharW( LPCSTR s, LPWSTR d )
326 {
327     return OemToCharBuffW( s, d, strlen( s ) + 1 );
328 }
329
330
331 /***********************************************************************
332  *           CharLowerA   (USER32.25)
333  * FIXME: handle current locale
334  */
335 LPSTR WINAPI CharLowerA(LPSTR x)
336 {
337     LPSTR       s;
338
339     if (HIWORD(x))
340     {
341         s=x;
342         while (*s)
343         {
344             *s=tolower(*s);
345             s++;
346         }
347         return x;
348     }
349     else return (LPSTR)tolower((char)(int)x);
350 }
351
352
353 /***********************************************************************
354  *           CharUpperA   (USER32.@)
355  * FIXME: handle current locale
356  */
357 LPSTR WINAPI CharUpperA(LPSTR x)
358 {
359     if (HIWORD(x))
360     {
361         LPSTR s = x;
362         while (*s)
363         {
364             *s=toupper(*s);
365             s++;
366         }
367         return x;
368     }
369     return (LPSTR)toupper((char)(int)x);
370 }
371
372
373 /***********************************************************************
374  *           CharLowerW   (USER32.@)
375  */
376 LPWSTR WINAPI CharLowerW(LPWSTR x)
377 {
378     if (HIWORD(x)) return strlwrW(x);
379     else return (LPWSTR)((UINT)tolowerW(LOWORD(x)));
380 }
381
382
383 /***********************************************************************
384  *           CharUpperW   (USER32.@)
385  * FIXME: handle current locale
386  */
387 LPWSTR WINAPI CharUpperW(LPWSTR x)
388 {
389     if (HIWORD(x)) return struprW(x);
390     else return (LPWSTR)((UINT)toupperW(LOWORD(x)));
391 }
392
393
394 /***********************************************************************
395  *           CharLowerBuffA   (USER32.@)
396  * FIXME: handle current locale
397  */
398 DWORD WINAPI CharLowerBuffA( LPSTR str, DWORD len )
399 {
400     DWORD ret = len;
401     if (!str) return 0; /* YES */
402     for (; len; len--, str++) *str = tolower(*str);
403     return ret;
404 }
405
406
407 /***********************************************************************
408  *           CharLowerBuffW   (USER32.@)
409  */
410 DWORD WINAPI CharLowerBuffW( LPWSTR str, DWORD len )
411 {
412     DWORD ret = len;
413     if (!str) return 0; /* YES */
414     for (; len; len--, str++) *str = tolowerW(*str);
415     return ret;
416 }
417
418
419 /***********************************************************************
420  *           CharUpperBuffA   (USER32.@)
421  * FIXME: handle current locale
422  */
423 DWORD WINAPI CharUpperBuffA( LPSTR str, DWORD len )
424 {
425     DWORD ret = len;
426     if (!str) return 0; /* YES */
427     for (; len; len--, str++) *str = toupper(*str);
428     return ret;
429 }
430
431
432 /***********************************************************************
433  *           CharUpperBuffW   (USER32.@)
434  */
435 DWORD WINAPI CharUpperBuffW( LPWSTR str, DWORD len )
436 {
437     DWORD ret = len;
438     if (!str) return 0; /* YES */
439     for (; len; len--, str++) *str = toupperW(*str);
440     return ret;
441 }
442
443
444 /***********************************************************************
445  *           IsCharLowerA   (USER.436) (USER32.@)
446  * FIXME: handle current locale
447  */
448 BOOL WINAPI IsCharLowerA(CHAR x)
449 {
450     return islower(x);
451 }
452
453
454 /***********************************************************************
455  *           IsCharLowerW   (USER32.@)
456  */
457 BOOL WINAPI IsCharLowerW(WCHAR x)
458 {
459     return get_char_typeW(x) & C1_LOWER;
460 }
461
462
463 /***********************************************************************
464  *           IsCharUpperA   (USER.435) (USER32.337)
465  * FIXME: handle current locale
466  */
467 BOOL WINAPI IsCharUpperA(CHAR x)
468 {
469     return isupper(x);
470 }
471
472
473 /***********************************************************************
474  *           IsCharUpperW   (USER32.@)
475  */
476 BOOL WINAPI IsCharUpperW(WCHAR x)
477 {
478     return get_char_typeW(x) & C1_UPPER;
479 }
480
481
482 /***********************************************************************
483  *           IsCharAlphaNumericW   (USER32.@)
484  */
485 BOOL WINAPI IsCharAlphaNumericW(WCHAR x)
486 {
487     return get_char_typeW(x) & (C1_ALPHA|C1_DIGIT|C1_LOWER|C1_UPPER);
488 }
489
490
491 /***********************************************************************
492  *           IsCharAlphaW   (USER32.@)
493  */
494 BOOL WINAPI IsCharAlphaW(WCHAR x)
495 {
496     return get_char_typeW(x) & (C1_ALPHA|C1_LOWER|C1_UPPER);
497 }
498
499
500 /***********************************************************************
501  *           FormatMessage16   (USER.606)
502  */
503 DWORD WINAPI FormatMessage16(
504     DWORD   dwFlags,
505     SEGPTR lpSource,     /* [in] NOTE: not always a valid pointer */
506     WORD   dwMessageId,
507     WORD   dwLanguageId,
508     LPSTR  lpBuffer,     /* [out] NOTE: *((HLOCAL16*)) for FORMAT_MESSAGE_ALLOCATE_BUFFER*/
509     WORD   nSize,
510     LPDWORD args         /* [in] NOTE: va_list *args */
511 ) {
512 #ifdef __i386__
513 /* This implementation is completely dependant on the format of the va_list on x86 CPUs */
514     LPSTR       target,t;
515     DWORD       talloced;
516     LPSTR       from,f;
517     DWORD       width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
518     BOOL        eos = FALSE;
519     LPSTR       allocstring = NULL;
520
521     TRACE("(0x%lx,%lx,%d,0x%x,%p,%d,%p)\n",
522           dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
523         if ((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
524                 && (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)) return 0;
525         if ((dwFlags & FORMAT_MESSAGE_FROM_STRING)
526                 &&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
527                         || (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
528
529     if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK) 
530         FIXME("line wrapping (%lu) not supported.\n", width);
531     from = NULL;
532     if (dwFlags & FORMAT_MESSAGE_FROM_STRING)
533         from = HEAP_strdupA( GetProcessHeap(), 0, PTR_SEG_TO_LIN(lpSource));
534     if (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM) {
535         from = HeapAlloc( GetProcessHeap(),0,200 );
536         sprintf(from,"Systemmessage, messageid = 0x%08x\n",dwMessageId);
537     }
538     if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE) {
539         INT16   bufsize;
540         HINSTANCE16 hinst16 = ((HMODULE)lpSource & 0xffff);
541
542         dwMessageId &= 0xFFFF;
543         bufsize=LoadString16(hinst16,dwMessageId,NULL,0);
544         if (bufsize) {
545             from = HeapAlloc( GetProcessHeap(), 0, bufsize +1);
546             LoadString16(hinst16,dwMessageId,from,bufsize+1);
547         }
548     }
549     target      = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
550     t   = target;
551     talloced= 100;
552
553 #define ADD_TO_T(c) \
554         *t++=c;\
555         if (t-target == talloced) {\
556                 target  = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
557                 t       = target+talloced;\
558                 talloced*=2;\
559         }
560
561     if (from) {
562         f=from;
563         while (*f && !eos) {
564             if (*f=='%') {
565                 int     insertnr;
566                 char    *fmtstr,*x,*lastf;
567                 DWORD   *argliststart;
568
569                 fmtstr = NULL;
570                 lastf = f;
571                 f++;
572                 if (!*f) {
573                     ADD_TO_T('%');
574                     continue;
575                 }
576                 switch (*f) {
577                 case '1':case '2':case '3':case '4':case '5':
578                 case '6':case '7':case '8':case '9':
579                     insertnr=*f-'0';
580                     switch (f[1]) {
581                     case '0':case '1':case '2':case '3':
582                     case '4':case '5':case '6':case '7':
583                     case '8':case '9':
584                         f++;
585                         insertnr=insertnr*10+*f-'0';
586                         f++;
587                         break;
588                     default:
589                         f++;
590                         break;
591                     }
592                     if (*f=='!') {
593                         f++;
594                         if (NULL!=(x=strchr(f,'!'))) {
595                             *x='\0';
596                             fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
597                             sprintf(fmtstr,"%%%s",f);
598                             f=x+1;
599                         } else {
600                             fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
601                             sprintf(fmtstr,"%%%s",f);
602                             f+=strlen(f); /*at \0*/
603                         }
604                     } else
605                         if(!args) 
606                             break;
607                         else
608                           fmtstr=HEAP_strdupA(GetProcessHeap(),0,"%s");
609                     if (args) {
610                         int     sz;
611                         LPSTR   b = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sz = 100);
612                         
613                         argliststart=args+insertnr-1;
614                        
615                         /* CMF - This makes a BIG assumption about va_list */
616                         while (vsnprintf(b, sz, fmtstr, (va_list) argliststart) < 0) {
617                             b = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, b, sz += 100);
618                         }
619                         for (x=b; *x; x++) ADD_TO_T(*x);
620                     } else {
621                         /* NULL args - copy formatstr 
622                          * (probably wrong)
623                          */
624                         while ((lastf<f)&&(*lastf)) {
625                             ADD_TO_T(*lastf++);
626                         }
627                     }
628                     HeapFree(GetProcessHeap(),0,fmtstr);
629                     break;
630                 case '0': /* Just stop processing format string */
631                     eos = TRUE;
632                     f++;
633                     break;
634                 case 'n': /* 16 bit version just outputs 'n' */
635                 default:
636                     ADD_TO_T(*f++);
637                     break;
638                 }
639             } else { /* '\n' or '\r' gets mapped to "\r\n" */
640                 if(*f == '\n' || *f == '\r') {
641                     if (width == 0) {
642                         ADD_TO_T('\r');
643                         ADD_TO_T('\n');
644                         if(*f++ == '\r' && *f == '\n')
645                             f++;
646                     }
647                 } else {
648                     ADD_TO_T(*f++);
649                 }
650             }
651         }
652         *t='\0';
653     }
654     talloced = strlen(target)+1;
655     if (nSize && talloced<nSize) {
656         target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
657     }
658     TRACE("-- %s\n",debugstr_a(target));
659     if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
660         /* nSize is the MINIMUM size */
661         HLOCAL16 h = LocalAlloc16(LPTR,talloced);
662         SEGPTR ptr = LocalLock16(h);
663         allocstring = PTR_SEG_TO_LIN( ptr );
664         memcpy( allocstring,target,talloced);
665         LocalUnlock16( h );
666         *((HLOCAL16*)lpBuffer) = h;
667     } else
668         lstrcpynA(lpBuffer,target,nSize);
669     HeapFree(GetProcessHeap(),0,target);
670     if (from) HeapFree(GetProcessHeap(),0,from);
671     return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ? 
672         strlen(allocstring):
673         strlen(lpBuffer);
674 #else
675         return 0;
676 #endif /* __i386__ */
677 }
678 #undef ADD_TO_T