Append .DRV to name only if no extension present.
[wine/hacks.git] / dlls / user / lstr.c
blob07325a85778fcd80d15872ecc01e052c359c0e41
1 /*
2 * USER string functions
4 * Copyright 1993 Yngvi Sigurjonsson (yngvi@hafro.is)
5 * Copyright 1996 Alexandre Julliard
6 * Copyright 1996 Marcus Meissner
7 */
9 #include <ctype.h>
10 #include <stdarg.h>
11 #include <stdlib.h>
12 #include <stdio.h>
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"
21 #include "heap.h"
22 #include "ldt.h"
23 #include "debugtools.h"
25 DEFAULT_DEBUG_CHANNEL(resource);
27 /***********************************************************************
28 * AnsiToOem16 (KEYBOARD.5)
30 INT16 WINAPI AnsiToOem16( LPCSTR s, LPSTR d )
32 CharToOemA( s, d );
33 return -1;
37 /***********************************************************************
38 * OemToAnsi16 (KEYBOARD.6)
40 INT16 WINAPI OemToAnsi16( LPCSTR s, LPSTR d )
42 OemToCharA( s, d );
43 return -1;
47 /***********************************************************************
48 * AnsiToOemBuff16 (KEYBOARD.134)
50 void WINAPI AnsiToOemBuff16( LPCSTR s, LPSTR d, UINT16 len )
52 if (len != 0) CharToOemBuffA( s, d, len );
56 /***********************************************************************
57 * OemToAnsiBuff16 (KEYBOARD.135)
59 void WINAPI OemToAnsiBuff16( LPCSTR s, LPSTR d, UINT16 len )
61 if (len != 0) OemToCharBuffA( s, d, len );
65 /***********************************************************************
66 * AnsiUpper16 (USER.431)
68 SEGPTR WINAPI AnsiUpper16( SEGPTR strOrChar )
70 /* uppercase only one char if strOrChar < 0x10000 */
71 if (HIWORD(strOrChar))
73 CharUpperA( PTR_SEG_TO_LIN(strOrChar) );
74 return strOrChar;
76 else return toupper((char)strOrChar);
80 /***********************************************************************
81 * AnsiLower16 (USER.432)
83 SEGPTR WINAPI AnsiLower16( SEGPTR strOrChar )
85 /* lowercase only one char if strOrChar < 0x10000 */
86 if (HIWORD(strOrChar))
88 CharLowerA( PTR_SEG_TO_LIN(strOrChar) );
89 return strOrChar;
91 else return tolower((char)strOrChar);
95 /***********************************************************************
96 * AnsiUpperBuff16 (USER.437)
98 UINT16 WINAPI AnsiUpperBuff16( LPSTR str, UINT16 len )
100 CharUpperBuffA( str, len ? len : 65536 );
101 return len;
105 /***********************************************************************
106 * AnsiLowerBuff16 (USER.438)
108 UINT16 WINAPI AnsiLowerBuff16( LPSTR str, UINT16 len )
110 CharLowerBuffA( str, len ? len : 65536 );
111 return len;
115 /***********************************************************************
116 * AnsiNext16 (USER.472)
118 SEGPTR WINAPI AnsiNext16(SEGPTR current)
120 char *ptr = (char *)PTR_SEG_TO_LIN(current);
121 return current + (CharNextA(ptr) - ptr);
125 /***********************************************************************
126 * AnsiPrev16 (USER.473)
128 SEGPTR WINAPI AnsiPrev16( LPCSTR start, SEGPTR current )
130 char *ptr = (char *)PTR_SEG_TO_LIN(current);
131 return current - (ptr - CharPrevA( start, ptr ));
135 /***********************************************************************
136 * CharNextA (USER32.@)
138 LPSTR WINAPI CharNextA( LPCSTR ptr )
140 if (!*ptr) return (LPSTR)ptr;
141 if (IsDBCSLeadByte( ptr[0] ) && ptr[1]) return (LPSTR)(ptr + 2);
142 return (LPSTR)(ptr + 1);
146 /***********************************************************************
147 * CharNextExA (USER32.@)
149 LPSTR WINAPI CharNextExA( WORD codepage, LPCSTR ptr, DWORD flags )
151 if (!*ptr) return (LPSTR)ptr;
152 if (IsDBCSLeadByteEx( codepage, ptr[0] ) && ptr[1]) return (LPSTR)(ptr + 2);
153 return (LPSTR)(ptr + 1);
157 /***********************************************************************
158 * CharNextExW (USER32.@)
160 LPWSTR WINAPI CharNextExW( WORD codepage, LPCWSTR ptr, DWORD flags )
162 /* doesn't make sense, there are no codepages for Unicode */
163 return NULL;
167 /***********************************************************************
168 * CharNextW (USER32.@)
170 LPWSTR WINAPI CharNextW(LPCWSTR x)
172 if (*x) x++;
174 return (LPWSTR)x;
178 /***********************************************************************
179 * CharPrevA (USER32.@)
181 LPSTR WINAPI CharPrevA( LPCSTR start, LPCSTR ptr )
183 while (*start && (start < ptr))
185 LPCSTR next = CharNextA( start );
186 if (next >= ptr) break;
187 start = next;
189 return (LPSTR)start;
193 /***********************************************************************
194 * CharPrevExA (USER32.@)
196 LPSTR WINAPI CharPrevExA( WORD codepage, LPCSTR start, LPCSTR ptr, DWORD flags )
198 while (*start && (start < ptr))
200 LPCSTR next = CharNextExA( codepage, start, flags );
201 if (next > ptr) break;
202 start = next;
204 return (LPSTR)start;
208 /***********************************************************************
209 * CharPrevExW (USER32.@)
211 LPSTR WINAPI CharPrevExW( WORD codepage, LPCWSTR start, LPCWSTR ptr, DWORD flags )
213 /* doesn't make sense, there are no codepages for Unicode */
214 return NULL;
218 /***********************************************************************
219 * CharPrevW (USER32.@)
221 LPWSTR WINAPI CharPrevW(LPCWSTR start,LPCWSTR x)
223 if (x>start) return (LPWSTR)(x-1);
224 else return (LPWSTR)x;
228 /***********************************************************************
229 * CharToOemA (USER32.@)
231 BOOL WINAPI CharToOemA( LPCSTR s, LPSTR d )
233 if ( !s || !d ) return TRUE;
234 return CharToOemBuffA( s, d, strlen( s ) + 1 );
238 /***********************************************************************
239 * CharToOemBuffA (USER32.@)
241 BOOL WINAPI CharToOemBuffA( LPCSTR s, LPSTR d, DWORD len )
243 WCHAR *bufW;
245 bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
246 if( bufW )
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 );
252 return TRUE;
256 /***********************************************************************
257 * CharToOemBuffW (USER32.@)
259 BOOL WINAPI CharToOemBuffW( LPCWSTR s, LPSTR d, DWORD len )
261 if ( !s || !d ) return TRUE;
262 WideCharToMultiByte( CP_OEMCP, 0, s, len, d, len, NULL, NULL );
263 return TRUE;
267 /***********************************************************************
268 * CharToOemW (USER32.@)
270 BOOL WINAPI CharToOemW( LPCWSTR s, LPSTR d )
272 return CharToOemBuffW( s, d, strlenW( s ) + 1 );
276 /***********************************************************************
277 * OemToCharA (USER32.@)
279 BOOL WINAPI OemToCharA( LPCSTR s, LPSTR d )
281 return OemToCharBuffA( s, d, strlen( s ) + 1 );
285 /***********************************************************************
286 * OemToCharBuffA (USER32.@)
288 BOOL WINAPI OemToCharBuffA( LPCSTR s, LPSTR d, DWORD len )
290 WCHAR *bufW;
292 bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
293 if( bufW )
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 );
299 return TRUE;
303 /***********************************************************************
304 * OemToCharBuffW (USER32.@)
306 BOOL WINAPI OemToCharBuffW( LPCSTR s, LPWSTR d, DWORD len )
308 MultiByteToWideChar( CP_OEMCP, 0, s, len, d, len );
309 return TRUE;
313 /***********************************************************************
314 * OemToCharW (USER32.@)
316 BOOL WINAPI OemToCharW( LPCSTR s, LPWSTR d )
318 return OemToCharBuffW( s, d, strlen( s ) + 1 );
322 /***********************************************************************
323 * CharLowerA (USER32.25)
324 * FIXME: handle current locale
326 LPSTR WINAPI CharLowerA(LPSTR x)
328 LPSTR s;
330 if (HIWORD(x))
332 s=x;
333 while (*s)
335 *s=tolower(*s);
336 s++;
338 return x;
340 else return (LPSTR)tolower((char)(int)x);
344 /***********************************************************************
345 * CharUpperA (USER32.@)
346 * FIXME: handle current locale
348 LPSTR WINAPI CharUpperA(LPSTR x)
350 if (HIWORD(x))
352 LPSTR s = x;
353 while (*s)
355 *s=toupper(*s);
356 s++;
358 return x;
360 return (LPSTR)toupper((char)(int)x);
364 /***********************************************************************
365 * CharLowerW (USER32.@)
367 LPWSTR WINAPI CharLowerW(LPWSTR x)
369 if (HIWORD(x)) return strlwrW(x);
370 else return (LPWSTR)((UINT)tolowerW(LOWORD(x)));
374 /***********************************************************************
375 * CharUpperW (USER32.@)
376 * FIXME: handle current locale
378 LPWSTR WINAPI CharUpperW(LPWSTR x)
380 if (HIWORD(x)) return struprW(x);
381 else return (LPWSTR)((UINT)toupperW(LOWORD(x)));
385 /***********************************************************************
386 * CharLowerBuffA (USER32.@)
387 * FIXME: handle current locale
389 DWORD WINAPI CharLowerBuffA( LPSTR str, DWORD len )
391 DWORD ret = len;
392 if (!str) return 0; /* YES */
393 for (; len; len--, str++) *str = tolower(*str);
394 return ret;
398 /***********************************************************************
399 * CharLowerBuffW (USER32.@)
401 DWORD WINAPI CharLowerBuffW( LPWSTR str, DWORD len )
403 DWORD ret = len;
404 if (!str) return 0; /* YES */
405 for (; len; len--, str++) *str = tolowerW(*str);
406 return ret;
410 /***********************************************************************
411 * CharUpperBuffA (USER32.@)
412 * FIXME: handle current locale
414 DWORD WINAPI CharUpperBuffA( LPSTR str, DWORD len )
416 DWORD ret = len;
417 if (!str) return 0; /* YES */
418 for (; len; len--, str++) *str = toupper(*str);
419 return ret;
423 /***********************************************************************
424 * CharUpperBuffW (USER32.@)
426 DWORD WINAPI CharUpperBuffW( LPWSTR str, DWORD len )
428 DWORD ret = len;
429 if (!str) return 0; /* YES */
430 for (; len; len--, str++) *str = toupperW(*str);
431 return ret;
435 /***********************************************************************
436 * IsCharLowerA (USER.436) (USER32.@)
437 * FIXME: handle current locale
439 BOOL WINAPI IsCharLowerA(CHAR x)
441 return islower(x);
445 /***********************************************************************
446 * IsCharLowerW (USER32.@)
448 BOOL WINAPI IsCharLowerW(WCHAR x)
450 return get_char_typeW(x) & C1_LOWER;
454 /***********************************************************************
455 * IsCharUpperA (USER.435) (USER32.337)
456 * FIXME: handle current locale
458 BOOL WINAPI IsCharUpperA(CHAR x)
460 return isupper(x);
464 /***********************************************************************
465 * IsCharUpperW (USER32.@)
467 BOOL WINAPI IsCharUpperW(WCHAR x)
469 return get_char_typeW(x) & C1_UPPER;
473 /***********************************************************************
474 * IsCharAlphaNumericW (USER32.@)
476 BOOL WINAPI IsCharAlphaNumericW(WCHAR x)
478 return get_char_typeW(x) & (C1_ALPHA|C1_DIGIT|C1_LOWER|C1_UPPER);
482 /***********************************************************************
483 * IsCharAlphaW (USER32.@)
485 BOOL WINAPI IsCharAlphaW(WCHAR x)
487 return get_char_typeW(x) & (C1_ALPHA|C1_LOWER|C1_UPPER);
491 /***********************************************************************
492 * FormatMessage16 (USER.606)
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 */
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;
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;
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);
529 if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE) {
530 INT16 bufsize;
531 HINSTANCE16 hinst16 = ((HMODULE)lpSource & 0xffff);
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);
540 target = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100);
541 t = target;
542 talloced= 100;
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;\
552 if (from) {
553 f=from;
554 while (*f && !eos) {
555 if (*f=='%') {
556 int insertnr;
557 char *fmtstr,*x,*lastf;
558 DWORD *argliststart;
560 fmtstr = NULL;
561 lastf = f;
562 f++;
563 if (!*f) {
564 ADD_TO_T('%');
565 continue;
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;
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*/
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);
604 argliststart=args+insertnr-1;
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);
610 for (x=b; *x; x++) ADD_TO_T(*x);
611 } else {
612 /* NULL args - copy formatstr
613 * (probably wrong)
615 while ((lastf<f)&&(*lastf)) {
616 ADD_TO_T(*lastf++);
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;
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++;
638 } else {
639 ADD_TO_T(*f++);
643 *t='\0';
645 talloced = strlen(target)+1;
646 if (nSize && talloced<nSize) {
647 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
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__ */
669 #undef ADD_TO_T