Release 960712
[wine] / win32 / string32.c
1 /*
2  * Unicode string management
3  *
4  * Copyright 1996 Martin von Loewis
5  *
6  * Conversion between Unicode and ISO-8859-1 is inherently lossy,
7  * so the conversion code should be called only if it does not matter
8  * 
9  */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <ctype.h>
14 #include "windows.h"
15 #include "string32.h"
16 #include "xmalloc.h"
17
18 void STRING32_UniToAnsi(LPSTR dest,LPCWSTR src)
19 {
20         static int have_warned=0;
21         while(*src)
22         {
23                 if(*src>255 && !have_warned)
24                 {
25                         fprintf(stderr,"Cannot deal with non-ANSI characters\n");
26                         have_warned=1;
27                 }
28                 *dest++=*src++;
29         }
30         /* copy the terminator */
31         *dest = *src;
32 }
33
34 /* FIXME: we need to use unsigned char here, for if 
35  *        we got chars with the 7th bit set, we will get
36  *        negative integers -> wrong unicode values
37  */
38 void 
39 STRING32_AnsiToUni(LPWSTR dest,LPCSTR src) {
40         unsigned char   *usrc;
41
42         usrc=(unsigned char*)src;
43         while(*usrc)
44                 *dest++=*usrc++;
45         *dest = *usrc;
46 }
47
48 LPSTR STRING32_DupUniToAnsi(LPCWSTR src)
49 {
50         LPSTR dest=xmalloc(lstrlen32W(src)+1);
51         STRING32_UniToAnsi(dest,src);
52         return dest;
53 }
54
55 LPWSTR STRING32_DupAnsiToUni(LPCSTR src)
56 {
57         LPWSTR dest=xmalloc(2*strlen(src)+2);
58         STRING32_AnsiToUni(dest,src);
59         return dest;
60 }
61
62 /* not an API function */
63
64 WCHAR STRING32_tolowerW(WCHAR c)
65 {
66         /* FIXME: Unicode */
67         return tolower(c);
68 }
69
70 LPWSTR
71 STRING32_lstrchrW(LPCWSTR a,WCHAR c) {
72         while(*a) {
73                 if (*a==c)
74                         return a;
75                 a++;
76         }
77         return NULL;
78 }
79
80 LPWSTR
81 STRING32_strdupW(LPCWSTR a) {
82         LPWSTR  b;
83         int     len;
84
85         len=sizeof(WCHAR)*(lstrlen32W(a)+1);
86         b=(LPWSTR)xmalloc(len);
87         memcpy(b,a,len);
88         return b;
89 }