Release 960818
[wine/multimedia.git] / misc / lstr.c
blob318e3dab0186c1ffeb947c1ed34bde3a5403b2c8
1 /*
2 * String functions
4 * Copyright 1993 Yngvi Sigurjonsson (yngvi@hafro.is)
5 * Copyright 1996 Marcus Meissner
6 */
8 #include <stdio.h>
9 #include <stdarg.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <ctype.h>
14 #include "windows.h"
15 #include "ldt.h"
16 #include "module.h"
17 #include "stddebug.h"
18 #include "debug.h"
19 #include "xmalloc.h"
20 #include "string32.h"
22 #define ToUpper(c) toupper(c)
23 #define ToLower(c) tolower(c)
26 static const BYTE Oem2Ansi[256] =
27 "\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\244"
28 "\020\021\022\023\266\247\026\027\030\031\032\033\034\035\036\037"
29 "\040\041\042\043\044\045\046\047\050\051\052\053\054\055\056\057"
30 "\060\061\062\063\064\065\066\067\070\071\072\073\074\075\076\077"
31 "\100\101\102\103\104\105\106\107\110\111\112\113\114\115\116\117"
32 "\120\121\122\123\124\125\126\127\130\131\132\133\134\135\136\137"
33 "\140\141\142\143\144\145\146\147\150\151\152\153\154\155\156\157"
34 "\160\161\162\163\164\165\166\167\170\171\172\173\174\175\176\177"
35 "\307\374\351\342\344\340\345\347\352\353\350\357\356\354\304\305"
36 "\311\346\306\364\366\362\373\371\377\326\334\242\243\245\120\203"
37 "\341\355\363\372\361\321\252\272\277\137\254\275\274\241\253\273"
38 "\137\137\137\246\246\246\246\053\053\246\246\053\053\053\053\053"
39 "\053\055\055\053\055\053\246\246\053\053\055\055\246\055\053\055"
40 "\055\055\055\053\053\053\053\053\053\053\053\137\137\246\137\137"
41 "\137\337\137\266\137\137\265\137\137\137\137\137\137\137\137\137"
42 "\137\261\137\137\137\137\367\137\260\225\267\137\156\262\137\137";
44 static const BYTE Ansi2Oem[256] =
45 "\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017"
46 "\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037"
47 "\040\041\042\043\044\045\046\047\050\051\052\053\054\055\056\057"
48 "\060\061\062\063\064\065\066\067\070\071\072\073\074\075\076\077"
49 "\100\101\102\103\104\105\106\107\110\111\112\113\114\115\116\117"
50 "\120\121\122\123\124\125\126\127\130\131\132\133\134\135\136\137"
51 "\140\141\142\143\144\145\146\147\150\151\152\153\154\155\156\157"
52 "\160\161\162\163\164\165\166\167\170\171\172\173\174\175\176\177"
53 "\200\201\054\237\054\137\375\374\210\045\123\074\117\215\216\217"
54 "\220\140\047\042\042\371\055\137\230\231\163\076\157\235\236\131"
55 "\040\255\233\234\017\235\335\025\042\143\246\256\252\055\162\137"
56 "\370\361\375\063\047\346\024\372\054\061\247\257\254\253\137\250"
57 "\101\101\101\101\216\217\222\200\105\220\105\105\111\111\111\111"
58 "\104\245\117\117\117\117\231\170\117\125\125\125\232\131\137\341"
59 "\205\240\203\141\204\206\221\207\212\202\210\211\215\241\214\213"
60 "\144\244\225\242\223\157\224\366\157\227\243\226\201\171\137\230";
62 /* Funny to divide them between user and kernel. */
64 /* IsCharAlpha USER 433 */
65 BOOL16 IsCharAlpha16(CHAR ch)
67 return isalpha(ch); /* This is probably not right for NLS */
70 /* IsCharAlphanumeric USER 434 */
71 BOOL16 IsCharAlphanumeric16(CHAR ch)
73 return isalnum(ch);
76 /* IsCharUpper USER 435 */
77 BOOL16 IsCharUpper16(CHAR ch)
79 return isupper(ch);
82 /* IsCharLower USER 436 */
83 BOOL16 IsCharLower16(CHAR ch)
85 return islower(ch);
88 /***********************************************************************
89 * AnsiUpper (USER.431)
92 /* 16-bit version */
93 SEGPTR WIN16_AnsiUpper( SEGPTR strOrChar )
95 /* I am not sure if the locale stuff works with toupper, but then again
96 I am not sure if the Linux libc locale stuffs works at all */
98 /* uppercase only one char if strOrChar < 0x10000 */
99 if (HIWORD(strOrChar))
101 char *s = PTR_SEG_TO_LIN(strOrChar);
102 while (*s) {
103 *s = ToUpper( *s );
104 s++;
106 return strOrChar;
108 else return (SEGPTR)ToUpper( (int)strOrChar );
111 /* 32-bit version */
112 LPSTR AnsiUpper(LPSTR strOrChar)
114 char *s = strOrChar;
115 /* I am not sure if the locale stuff works with toupper, but then again
116 I am not sure if the Linux libc locale stuffs works at all */
118 while (*s) {
119 *s = ToUpper( *s );
120 s++;
122 return strOrChar;
126 /***********************************************************************
127 * AnsiUpperBuff (USER.437)
129 UINT AnsiUpperBuff(LPSTR str,UINT len)
131 int i;
132 len=(len==0)?65536:len;
134 for(i=0;i<len;i++)
135 str[i]=toupper(str[i]);
136 return i;
139 /***********************************************************************
140 * AnsiLower (USER.432)
143 /* 16-bit version */
144 SEGPTR WIN16_AnsiLower(SEGPTR strOrChar)
146 /* I am not sure if the locale stuff works with toupper, but then again
147 I am not sure if the Linux libc locale stuffs works at all */
149 /* lowercase only one char if strOrChar < 0x10000 */
150 if (HIWORD(strOrChar))
152 char *s = PTR_SEG_TO_LIN( strOrChar );
153 while (*s) {
154 *s = ToLower( *s );
155 s++;
157 return strOrChar;
159 else return (SEGPTR)ToLower( (int)strOrChar );
162 /* 32-bit version */
163 LPSTR AnsiLower(LPSTR strOrChar)
165 char *s = strOrChar;
166 /* I am not sure if the locale stuff works with toupper, but then again
167 I am not sure if the Linux libc locale stuffs works at all */
169 while (*s) {
170 *s = ToLower( *s );
171 s++;
173 return strOrChar;
177 /***********************************************************************
178 * AnsiLowerBuff (USER.438)
180 UINT AnsiLowerBuff(LPSTR str,UINT len)
182 int i;
183 len=(len==0)?65536:len;
184 i=0;
186 for(i=0;i<len;i++)
187 str[i]=tolower(str[i]);
189 return i;
193 /* AnsiNext USER.472 */
194 SEGPTR AnsiNext(SEGPTR current)
196 return (*(char *)PTR_SEG_TO_LIN(current)) ? current + 1 : current;
199 /* AnsiPrev USER.473 */
200 SEGPTR AnsiPrev( SEGPTR start, SEGPTR current)
202 return (current==start)?start:current-1;
206 /* AnsiToOem Keyboard.5 */
207 INT AnsiToOem(LPCSTR lpAnsiStr, LPSTR lpOemStr)
209 dprintf_keyboard(stddeb, "AnsiToOem: %s\n", lpAnsiStr);
210 while(*lpAnsiStr){
211 *lpOemStr++=Ansi2Oem[(unsigned char)(*lpAnsiStr++)];
213 *lpOemStr = 0;
214 return -1;
217 /* OemToAnsi Keyboard.6 */
218 BOOL OemToAnsi(LPCSTR lpOemStr, LPSTR lpAnsiStr)
220 dprintf_keyboard(stddeb, "OemToAnsi: %s\n", lpOemStr);
221 while(*lpOemStr){
222 *lpAnsiStr++=Oem2Ansi[(unsigned char)(*lpOemStr++)];
224 *lpAnsiStr = 0;
225 return -1;
228 /* AnsiToOemBuff Keyboard.134 */
229 void AnsiToOemBuff(LPCSTR lpAnsiStr, LPSTR lpOemStr, UINT nLength)
231 int i;
232 for(i=0;i<nLength;i++)
233 lpOemStr[i]=Ansi2Oem[(unsigned char)(lpAnsiStr[i])];
236 /* OemToAnsi Keyboard.135 */
237 void OemToAnsiBuff(LPCSTR lpOemStr, LPSTR lpAnsiStr, INT nLength)
239 int i;
240 for(i=0;i<nLength;i++)
241 lpAnsiStr[i]=Oem2Ansi[(unsigned char)(lpOemStr[i])];
245 /***********************************************************************
246 * OutputDebugString (KERNEL.115)
248 void OutputDebugString( LPCSTR str )
250 char *module;
251 char *p, *buffer = xmalloc( strlen(str)+1 );
252 /* Remove CRs */
253 for (p = buffer; *str; str++) if (*str != '\r') *p++ = *str;
254 *p = '\0';
255 if ((p > buffer) && (p[-1] == '\n')) p[1] = '\0'; /* Remove trailing \n */
256 module = MODULE_GetModuleName( GetExePtr(GetCurrentTask()) );
257 fprintf( stderr, "OutputDebugString: %s says '%s'\n",
258 module ? module : "???", buffer );
259 free( buffer );
262 /***********************************************************************
263 * CharNextA (USER32.28)
265 LPSTR CharNext32A(LPCSTR x)
267 if (*x) return (LPSTR)(x+1);
268 else return (LPSTR)x;
271 /***********************************************************************
272 * CharNextExA (USER32.29)
274 LPSTR CharNextEx32A(WORD codepage,LPCSTR x,DWORD flags)
276 /* FIXME: add DBCS / codepage stuff */
277 if (*x) return (LPSTR)(x+1);
278 else return (LPSTR)x;
281 /***********************************************************************
282 * CharNextExW (USER32.30)
284 LPWSTR CharNextEx32W(WORD codepage,LPCWSTR x,DWORD flags)
286 /* FIXME: add DBCS / codepage stuff */
287 if (*x) return (LPWSTR)(x+1);
288 else return (LPWSTR)x;
291 /***********************************************************************
292 * CharNextW (USER32.31)
294 LPWSTR CharNext32W(LPCWSTR x)
296 if (*x) return (LPWSTR)(x+1);
297 else return (LPWSTR)x;
300 /***********************************************************************
301 * CharPrevA (USER32.32)
303 LPSTR CharPrev32A(LPCSTR start,LPCSTR x)
305 if (x>start) return (LPSTR)(x-1);
306 else return (LPSTR)x;
309 /***********************************************************************
310 * CharPrevExA (USER32.33)
312 LPSTR CharPrevEx32A(WORD codepage,LPCSTR start,LPCSTR x,DWORD flags)
314 /* FIXME: add DBCS / codepage stuff */
315 if (x>start) return (LPSTR)(x-1);
316 else return (LPSTR)x;
319 /***********************************************************************
320 * CharPrevExW (USER32.34)
322 LPWSTR CharPrevEx32W(WORD codepage,LPCWSTR start,LPCWSTR x,DWORD flags)
324 /* FIXME: add DBCS / codepage stuff */
325 if (x>start) return (LPWSTR)(x-1);
326 else return (LPWSTR)x;
329 /***********************************************************************
330 * CharPrevW (USER32.35)
332 LPWSTR CharPrev32W(LPCWSTR start,LPCWSTR x)
334 if (x>start) return (LPWSTR)(x-1);
335 else return (LPWSTR)x;
338 /***********************************************************************
339 * CharLowerA (USER32.24)
340 * FIXME: handle current locale
342 LPSTR CharLower32A(LPSTR x)
344 LPSTR s;
346 if (HIWORD(x))
348 s=x;
349 while (*s)
351 *s=tolower(*s);
352 s++;
354 return x;
356 else return (LPSTR)tolower(LOWORD(x));
359 /***********************************************************************
360 * CharLowerBuffA (USER32.25)
361 * FIXME: handle current locale
363 DWORD CharLowerBuff32A(LPSTR x,DWORD buflen)
365 DWORD done=0;
367 while (*x && (buflen--))
369 *x=tolower(*x);
370 x++;
371 done++;
373 return done;
376 /***********************************************************************
377 * CharLowerBuffW (USER32.26)
378 * FIXME: handle current locale
380 DWORD CharLowerBuff32W(LPWSTR x,DWORD buflen)
382 DWORD done=0;
384 while (*x && (buflen--))
386 *x=tolower(*x);
387 x++;
388 done++;
390 return done;
393 /***********************************************************************
394 * CharLowerW (USER32.27)
395 * FIXME: handle current locale
397 LPWSTR CharLower32W(LPWSTR x)
399 if (HIWORD(x))
401 LPWSTR s = x;
402 while (*s)
404 *s=tolower(*s);
405 s++;
407 return x;
409 else return (LPWSTR)tolower(LOWORD(x));
412 /***********************************************************************
413 * CharUpperA (USER32.40)
414 * FIXME: handle current locale
416 LPSTR CharUpper32A(LPSTR x)
418 if (HIWORD(x))
420 LPSTR s = x;
421 while (*s)
423 *s=toupper(*s);
424 s++;
426 return x;
428 else return (LPSTR)toupper(LOWORD(x));
431 /***********************************************************************
432 * CharUpperBuffA (USER32.41)
433 * FIXME: handle current locale
435 DWORD CharUpperBuff32A(LPSTR x,DWORD buflen)
437 DWORD done=0;
439 while (*x && (buflen--))
441 *x=toupper(*x);
442 x++;
443 done++;
445 return done;
448 /***********************************************************************
449 * CharUpperBuffW (USER32.42)
450 * FIXME: handle current locale
452 DWORD CharUpperBuff32W(LPWSTR x,DWORD buflen)
454 DWORD done=0;
456 while (*x && (buflen--))
458 *x=toupper(*x);
459 x++;
460 done++;
462 return done;
465 /***********************************************************************
466 * CharUpperW (USER32.43)
467 * FIXME: handle current locale
469 LPWSTR CharUpper32W(LPWSTR x)
471 if (HIWORD(x))
473 LPWSTR s = x;
474 while (*s)
476 *s=toupper(*s);
477 s++;
479 return x;
481 else return (LPWSTR)toupper(LOWORD(x));
484 /***********************************************************************
485 * IsCharAlphaA (USER32.330)
486 * FIXME: handle current locale
488 BOOL32 IsCharAlpha32A(CHAR x)
490 return isalpha(x);
493 /***********************************************************************
494 * IsCharAlphaNumericA (USER32.331)
495 * FIXME: handle current locale
497 BOOL32 IsCharAlphaNumeric32A(CHAR x)
499 return isalnum(x);
502 /***********************************************************************
503 * IsCharAlphaNumericW (USER32.332)
504 * FIXME: handle current locale
506 BOOL32 IsCharAlphaNumeric32W(WCHAR x)
508 return isalnum(x);
511 /***********************************************************************
512 * IsCharAlphaW (USER32.333)
513 * FIXME: handle current locale
515 BOOL32 IsCharAlpha32W(WCHAR x)
517 return isalpha(x);
520 /***********************************************************************
521 * IsCharAlphaW (USER32.334)
522 * FIXME: handle current locale
524 BOOL32 IsCharLower32A(CHAR x)
526 return islower(x);
529 /***********************************************************************
530 * IsCharAlphaW (USER32.335)
531 * FIXME: handle current locale
533 BOOL32 IsCharLower32W(WCHAR x)
535 return islower(x);
538 /***********************************************************************
539 * IsCharAlphaW (USER32.336)
540 * FIXME: handle current locale
542 BOOL32 IsCharUpper32A(CHAR x)
544 return isupper(x);
547 /***********************************************************************
548 * IsCharAlphaW (USER32.337)
549 * FIXME: handle current locale
551 BOOL32 IsCharUpper32W(WCHAR x)
553 return isupper(x);
556 /***********************************************************************
557 * CharToOemA (USER32.36)
559 BOOL32 CharToOem32A(LPSTR s,LPSTR d)
561 if (!s || !d)
562 return TRUE;
563 AnsiToOem(s,d);
564 return TRUE;
567 /***********************************************************************
568 * CharToOemBuffA (USER32.37)
570 BOOL32 CharToOemBuff32A(LPSTR s,LPSTR d,DWORD len)
572 AnsiToOemBuff(s,d,len);
573 return TRUE;
576 /***********************************************************************
577 * CharToOemBuffW (USER32.38)
579 BOOL32 CharToOemBuff32W(LPCWSTR s,LPSTR d,DWORD len)
581 LPSTR x=STRING32_DupUniToAnsi(s);
582 AnsiToOemBuff(x,d,len);
583 return TRUE;
586 /***********************************************************************
587 * CharToOemW (USER32.39)
589 BOOL32 CharToOem32W(LPCWSTR s,LPSTR d)
591 LPSTR x=STRING32_DupUniToAnsi(s);
592 AnsiToOem(x,d);
593 return TRUE;
596 /***********************************************************************
597 * OemToCharA (USER32.401)
599 BOOL32 OemToChar32A(LPSTR s,LPSTR d)
601 OemToAnsi(s,d);
602 return TRUE;
605 /***********************************************************************
606 * OemToCharBuffA (USER32.402)
608 BOOL32 OemToCharBuff32A(LPSTR s,LPSTR d,DWORD len)
610 OemToAnsiBuff(s,d,len);
611 return TRUE;
614 /***********************************************************************
615 * OemToCharBuffW (USER32.403)
617 BOOL32 OemToCharBuff32W(LPCSTR s,LPWSTR d,DWORD len)
619 LPSTR x=(char*)xmalloc(strlen(s));
620 OemToAnsiBuff((LPSTR)s,x,len);
621 STRING32_AnsiToUni(d,x);
622 return TRUE;
625 /***********************************************************************
626 * OemToCharW (USER32.404)
628 BOOL32 OemToChar32W(LPCSTR s,LPWSTR d)
630 LPSTR x=(char*)xmalloc(strlen(s));
631 OemToAnsi((LPSTR)s,x);
632 STRING32_AnsiToUni(d,x);
633 return TRUE;
636 /***********************************************************************
637 * FormatMessageA (KERNEL32.138) Library Version
638 * FIXME: missing wrap,FROM_SYSTEM message-loading,
640 DWORD
641 FormatMessage32A(
642 DWORD dwFlags,
643 LPCVOID lpSource,
644 DWORD dwMessageId,
645 DWORD dwLanguageId,
646 LPSTR lpBuffer,
647 DWORD nSize,
648 LPDWORD args /* va_list *args */
650 LPSTR target,t;
651 DWORD talloced;
652 LPSTR from,f;
653 DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
654 DWORD nolinefeed = 0;
656 dprintf_resource(stddeb,
657 "FormatMessage32A(0x%lx,%p,%ld,0x%lx,%p,%ld,%p)\n",
658 dwFlags,lpSource,dwMessageId,dwLanguageId,lpBuffer,nSize,args
660 if (width)
661 fprintf(stdnimp," - line wrapping not supported.\n");
662 from = NULL;
663 if (dwFlags & FORMAT_MESSAGE_FROM_STRING)
664 from = xstrdup((LPSTR)lpSource);
665 if (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM) {
666 /* gather information from system message tables ... */
667 fprintf(stdnimp," - FORMAT_MESSAGE_FROM_SYSTEM not implemented.\n");
669 if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE) {
670 INT32 bufsize;
672 dwMessageId &= 0xFFFF;
673 bufsize=LoadMessage32A(0,dwMessageId,dwLanguageId,NULL,100);
674 if (bufsize) {
675 from = (char*)xmalloc(bufsize+1);
676 LoadMessage32A(0,dwMessageId,dwLanguageId,from,bufsize+1);
679 target = (char*)xmalloc(100);
680 t = target;
681 talloced= 100;
682 *t = 0;
684 #define ADD_TO_T(c) \
685 *t++=c;\
686 if (t-target == talloced) {\
687 target = (char*)xrealloc(target,talloced*2);\
688 t = target+talloced;\
689 talloced*=2;\
692 if (from) {
693 f=from;
694 while (*f) {
695 if (*f=='%') {
696 int insertnr;
697 char *fmtstr,*sprintfbuf,*x;
698 DWORD *argliststart;
700 f++;
701 if (!*f) {
702 ADD_TO_T('%');
703 continue;
705 switch (*f) {
706 case '1':case '2':case '3':case '4':case '5':
707 case '6':case '7':case '8':case '9':
708 insertnr=*f-'0';
709 switch (f[1]) {
710 case '0':case '1':case '2':case '3':
711 case '4':case '5':case '6':case '7':
712 case '8':case '9':
713 f++;
714 insertnr=insertnr*10+*f-'0';
715 f++;
716 break;
717 default:
718 f++;
719 break;
721 if (*f=='!') {
722 f++;
723 if (NULL!=(x=strchr(f,'!'))) {
724 *x='\0';
725 fmtstr=xmalloc(strlen(f)+2);
726 sprintf(fmtstr,"%%%s",f);
727 f=x+1;
729 } else
730 fmtstr=strdup("%s");
731 if (dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY)
732 argliststart=args+insertnr-1;
733 else
734 /* FIXME: not sure that this is
735 * correct for unix-c-varargs.
737 argliststart=((DWORD*)&args)+insertnr-1;
739 if (fmtstr[strlen(fmtstr)]=='s')
740 sprintfbuf=(char*)xmalloc(strlen((LPSTR)argliststart[0])+1);
741 else
742 sprintfbuf=(char*)xmalloc(100);
743 vsprintf(sprintfbuf,fmtstr,argliststart);
744 x=sprintfbuf;
745 while (*x) {
746 ADD_TO_T(*x++);
748 free(sprintfbuf);
749 free(fmtstr);
750 break;
751 case '0':
752 nolinefeed=1;
753 f++;
754 break;
755 default:ADD_TO_T(*f++)
756 break;
759 } else {
760 ADD_TO_T(*f++)
763 *t='\0';
765 if (!nolinefeed && t[-1]!='\n')
766 ADD_TO_T('\n');
767 talloced = strlen(target)+1;
768 if (nSize && talloced<nSize) {
769 target = (char*)xrealloc(target,nSize);
771 if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
772 /* nSize is the MINIMUM size */
773 *((LPVOID*)lpBuffer) = (LPVOID)LocalAlloc32(GMEM_ZEROINIT,talloced);
774 memcpy(*(LPSTR*)lpBuffer,target,talloced);
775 } else
776 strncpy(lpBuffer,target,nSize);
777 free(target);
778 if (from) free(from);
779 return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
780 strlen(*(LPSTR*)lpBuffer):
781 strlen(lpBuffer);
784 /***********************************************************************
785 * FormatMessageA (KERNEL32.138) Emulator Version
787 DWORD
788 WIN32_FormatMessage32A(DWORD *args) {
789 DWORD dwFlags = args[0];
790 LPCVOID lpSource = (LPCVOID)args[1];
791 DWORD dwMessageId = args[2];
792 DWORD dwLanguageId = args[3];
793 LPSTR lpBuffer = (LPSTR)args[4];
794 DWORD nSize = args[5];
795 DWORD *xargs;
797 /* convert possible varargs to an argument array look-a-like */
799 if (dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY) {
800 xargs=(DWORD*)args[6];
801 } else {
802 /* args[6] is a pointer to a pointer to the start of
803 * a list of arguments.
805 if (args[6])
806 xargs=(DWORD*)(((DWORD*)args[6])[0]);
807 else
808 xargs=NULL;
809 dwFlags|=FORMAT_MESSAGE_ARGUMENT_ARRAY;
811 return FormatMessage32A(
812 dwFlags,
813 lpSource,
814 dwMessageId,
815 dwLanguageId,
816 lpBuffer,
817 nSize,
818 xargs