Fixed the displaying of the FOURCC codes in _dump_pixelformat.
[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  *           CharLowerA   (USER32.25)
324  * FIXME: handle current locale
325  */
326 LPSTR WINAPI CharLowerA(LPSTR x)
327 {
328     LPSTR       s;
329
330     if (HIWORD(x))
331     {
332         s=x;
333         while (*s)
334         {
335             *s=tolower(*s);
336             s++;
337         }
338         return x;
339     }
340     else return (LPSTR)tolower((char)(int)x);
341 }
342
343
344 /***********************************************************************
345  *           CharUpperA   (USER32.@)
346  * FIXME: handle current locale
347  */
348 LPSTR WINAPI CharUpperA(LPSTR x)
349 {
350     if (HIWORD(x))
351     {
352         LPSTR s = x;
353         while (*s)
354         {
355             *s=toupper(*s);
356             s++;
357         }
358         return x;
359     }
360     return (LPSTR)toupper((char)(int)x);
361 }
362
363
364 /***********************************************************************
365  *           CharLowerW   (USER32.@)
366  */
367 LPWSTR WINAPI CharLowerW(LPWSTR x)
368 {
369     if (HIWORD(x)) return strlwrW(x);
370     else return (LPWSTR)((UINT)tolowerW(LOWORD(x)));
371 }
372
373
374 /***********************************************************************
375  *           CharUpperW   (USER32.@)
376  * FIXME: handle current locale
377  */
378 LPWSTR WINAPI CharUpperW(LPWSTR x)
379 {
380     if (HIWORD(x)) return struprW(x);
381     else return (LPWSTR)((UINT)toupperW(LOWORD(x)));
382 }
383
384
385 /***********************************************************************
386  *           CharLowerBuffA   (USER32.@)
387  * FIXME: handle current locale
388  */
389 DWORD WINAPI CharLowerBuffA( LPSTR str, DWORD len )
390 {
391     DWORD ret = len;
392     if (!str) return 0; /* YES */
393     for (; len; len--, str++) *str = tolower(*str);
394     return ret;
395 }
396
397
398 /***********************************************************************
399  *           CharLowerBuffW   (USER32.@)
400  */
401 DWORD WINAPI CharLowerBuffW( LPWSTR str, DWORD len )
402 {
403     DWORD ret = len;
404     if (!str) return 0; /* YES */
405     for (; len; len--, str++) *str = tolowerW(*str);
406     return ret;
407 }
408
409
410 /***********************************************************************
411  *           CharUpperBuffA   (USER32.@)
412  * FIXME: handle current locale
413  */
414 DWORD WINAPI CharUpperBuffA( LPSTR str, DWORD len )
415 {
416     DWORD ret = len;
417     if (!str) return 0; /* YES */
418     for (; len; len--, str++) *str = toupper(*str);
419     return ret;
420 }
421
422
423 /***********************************************************************
424  *           CharUpperBuffW   (USER32.@)
425  */
426 DWORD WINAPI CharUpperBuffW( LPWSTR str, DWORD len )
427 {
428     DWORD ret = len;
429     if (!str) return 0; /* YES */
430     for (; len; len--, str++) *str = toupperW(*str);
431     return ret;
432 }
433
434
435 /***********************************************************************
436  *           IsCharLowerA   (USER.436) (USER32.@)
437  * FIXME: handle current locale
438  */
439 BOOL WINAPI IsCharLowerA(CHAR x)
440 {
441     return islower(x);
442 }
443
444
445 /***********************************************************************
446  *           IsCharLowerW   (USER32.@)
447  */
448 BOOL WINAPI IsCharLowerW(WCHAR x)
449 {
450     return get_char_typeW(x) & C1_LOWER;
451 }
452
453
454 /***********************************************************************
455  *           IsCharUpperA   (USER.435) (USER32.337)
456  * FIXME: handle current locale
457  */
458 BOOL WINAPI IsCharUpperA(CHAR x)
459 {
460     return isupper(x);
461 }
462
463
464 /***********************************************************************
465  *           IsCharUpperW   (USER32.@)
466  */
467 BOOL WINAPI IsCharUpperW(WCHAR x)
468 {
469     return get_char_typeW(x) & C1_UPPER;
470 }
471
472
473 /***********************************************************************
474  *           IsCharAlphaNumericW   (USER32.@)
475  */
476 BOOL WINAPI IsCharAlphaNumericW(WCHAR x)
477 {
478     return get_char_typeW(x) & (C1_ALPHA|C1_DIGIT|C1_LOWER|C1_UPPER);
479 }
480
481
482 /***********************************************************************
483  *           IsCharAlphaW   (USER32.@)
484  */
485 BOOL WINAPI IsCharAlphaW(WCHAR x)
486 {
487     return get_char_typeW(x) & (C1_ALPHA|C1_LOWER|C1_UPPER);
488 }
489
490
491 /***********************************************************************
492  *           FormatMessage16   (USER.606)
493  */
494 DWORD WINAPI FormatMessage16(
495     DWORD   dwFlags,
496     SEGPTR lpSource, /*not always a valid pointer*/
497     WORD   dwMessageId,
498     WORD   dwLanguageId,
499     LPSTR  lpBuffer, /* *((HLOCAL16*)) for FORMAT_MESSAGE_ALLOCATE_BUFFER*/
500     WORD   nSize,
501     LPDWORD args /* va_list *args */
502 ) {
503 #ifdef __i386__
504 /* This implementation is completely dependant on the format of the va_list on x86 CPUs */
505     LPSTR       target,t;
506     DWORD       talloced;
507     LPSTR       from,f;
508     DWORD       width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
509     BOOL        eos = FALSE;
510     LPSTR       allocstring = NULL;
511
512     TRACE("(0x%lx,%lx,%d,0x%x,%p,%d,%p)\n",
513           dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args);
514         if ((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
515                 && (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)) return 0;
516         if ((dwFlags & FORMAT_MESSAGE_FROM_STRING)
517                 &&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
518                         || (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
519
520     if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK) 
521         FIXME("line wrapping (%lu) not supported.\n", width);
522     from = NULL;
523     if (dwFlags & FORMAT_MESSAGE_FROM_STRING)
524         from = HEAP_strdupA( GetProcessHeap(), 0, PTR_SEG_TO_LIN(lpSource));
525     if (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM) {
526         from = HeapAlloc( GetProcessHeap(),0,200 );
527         sprintf(from,"Systemmessage, messageid = 0x%08x\n",dwMessageId);
528     }
529     if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE) {
530         INT16   bufsize;
531         HINSTANCE16 hinst16 = ((HMODULE)lpSource & 0xffff);
532
533         dwMessageId &= 0xFFFF;
534         bufsize=LoadString16(hinst16,dwMessageId,NULL,0);
535         if (bufsize) {
536             from = HeapAlloc( GetProcessHeap(), 0, bufsize +1);
537             LoadString16(hinst16,dwMessageId,from,bufsize+1);
538         }
539     }
540     target      = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
541     t   = target;
542     talloced= 100;
543
544 #define ADD_TO_T(c) \
545         *t++=c;\
546         if (t-target == talloced) {\
547                 target  = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
548                 t       = target+talloced;\
549                 talloced*=2;\
550         }
551
552     if (from) {
553         f=from;
554         while (*f && !eos) {
555             if (*f=='%') {
556                 int     insertnr;
557                 char    *fmtstr,*x,*lastf;
558                 DWORD   *argliststart;
559
560                 fmtstr = NULL;
561                 lastf = f;
562                 f++;
563                 if (!*f) {
564                     ADD_TO_T('%');
565                     continue;
566                 }
567                 switch (*f) {
568                 case '1':case '2':case '3':case '4':case '5':
569                 case '6':case '7':case '8':case '9':
570                     insertnr=*f-'0';
571                     switch (f[1]) {
572                     case '0':case '1':case '2':case '3':
573                     case '4':case '5':case '6':case '7':
574                     case '8':case '9':
575                         f++;
576                         insertnr=insertnr*10+*f-'0';
577                         f++;
578                         break;
579                     default:
580                         f++;
581                         break;
582                     }
583                     if (*f=='!') {
584                         f++;
585                         if (NULL!=(x=strchr(f,'!'))) {
586                             *x='\0';
587                             fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
588                             sprintf(fmtstr,"%%%s",f);
589                             f=x+1;
590                         } else {
591                             fmtstr=HeapAlloc(GetProcessHeap(),0,strlen(f)+2);
592                             sprintf(fmtstr,"%%%s",f);
593                             f+=strlen(f); /*at \0*/
594                         }
595                     } else
596                         if(!args) 
597                             break;
598                         else
599                           fmtstr=HEAP_strdupA(GetProcessHeap(),0,"%s");
600                     if (args) {
601                         int     sz;
602                         LPSTR   b = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sz = 100);
603                         
604                         argliststart=args+insertnr-1;
605                        
606                         /* CMF - This makes a BIG assumption about va_list */
607                         while (vsnprintf(b, sz, fmtstr, (va_list) argliststart) < 0) {
608                             b = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, b, sz += 100);
609                         }
610                         for (x=b; *x; x++) ADD_TO_T(*x);
611                     } else {
612                         /* NULL args - copy formatstr 
613                          * (probably wrong)
614                          */
615                         while ((lastf<f)&&(*lastf)) {
616                             ADD_TO_T(*lastf++);
617                         }
618                     }
619                     HeapFree(GetProcessHeap(),0,fmtstr);
620                     break;
621                 case '0': /* Just stop processing format string */
622                     eos = TRUE;
623                     f++;
624                     break;
625                 case 'n': /* 16 bit version just outputs 'n' */
626                 default:
627                     ADD_TO_T(*f++);
628                     break;
629                 }
630             } else { /* '\n' or '\r' gets mapped to "\r\n" */
631                 if(*f == '\n' || *f == '\r') {
632                     if (width == 0) {
633                         ADD_TO_T('\r');
634                         ADD_TO_T('\n');
635                         if(*f++ == '\r' && *f == '\n')
636                             f++;
637                     }
638                 } else {
639                     ADD_TO_T(*f++);
640                 }
641             }
642         }
643         *t='\0';
644     }
645     talloced = strlen(target)+1;
646     if (nSize && talloced<nSize) {
647         target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
648     }
649     TRACE("-- %s\n",debugstr_a(target));
650     if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
651         /* nSize is the MINIMUM size */
652         HLOCAL16 h = LocalAlloc16(LPTR,talloced);
653         SEGPTR ptr = LocalLock16(h);
654         allocstring = PTR_SEG_TO_LIN( ptr );
655         memcpy( allocstring,target,talloced);
656         LocalUnlock16( h );
657         *((HLOCAL16*)lpBuffer) = h;
658     } else
659         lstrcpynA(lpBuffer,target,nSize);
660     HeapFree(GetProcessHeap(),0,target);
661     if (from) HeapFree(GetProcessHeap(),0,from);
662     return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ? 
663         strlen(allocstring):
664         strlen(lpBuffer);
665 #else
666         return 0;
667 #endif /* __i386__ */
668 }
669 #undef ADD_TO_T