Release 960331
[wine.git] / win32 / string32.c
blob66e243e2ec45f5d00adbe56cc245ed4091839162
1 /*
2 * Unicode string management
4 * Copyright 1996 Martin von Loewis
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 */
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <ctype.h>
14 #include "string32.h"
15 #include "xmalloc.h"
17 int
18 STRING32_UniLen(LPCWSTR s)
20 int i;
21 for(i=0;*s;s++)
22 i++;
23 return i;
26 void STRING32_UniToAnsi(LPSTR dest,LPCWSTR src)
28 static int have_warned=0;
29 while(*src)
31 if(*src>255 && !have_warned)
33 fprintf(stderr,"Cannot deal with non-ANSI characters\n");
34 have_warned=1;
36 *dest++=*src++;
38 /* copy the terminator */
39 *dest = *src;
42 /* FIXME: we need to use unsigned char here, for if
43 * we got chars with the 7th bit set, we will get
44 * negative integers -> wrong unicode values
46 void
47 STRING32_AnsiToUni(LPWSTR dest,LPCSTR src) {
48 unsigned char *usrc;
50 usrc=(unsigned char*)src;
51 while(*usrc)
52 *dest++=*usrc++;
53 *dest = *usrc;
56 LPSTR STRING32_DupUniToAnsi(LPCWSTR src)
58 LPSTR dest=xmalloc(STRING32_UniLen(src)+1);
59 STRING32_UniToAnsi(dest,src);
60 return dest;
63 LPWSTR STRING32_DupAnsiToUni(LPCSTR src)
65 LPWSTR dest=xmalloc(2*strlen(src)+2);
66 STRING32_AnsiToUni(dest,src);
67 return dest;
70 DWORD STRING32_lstrlenW(LPCWSTR str)
72 int len;
73 for(len=0;*str;str++)
74 len++;
75 return len;
78 /* not an API function */
80 WCHAR STRING32_tolowerW(WCHAR c)
82 /* FIXME: Unicode */
83 return tolower(c);
86 int STRING32_lstrcmpniW(LPCWSTR a,LPCWSTR b,DWORD len)
88 while(len--)
90 WCHAR c1,c2;
91 c1 = STRING32_tolowerW(*a);
92 c2 = STRING32_tolowerW(*b);
93 if(c1<c2)return -1;
94 if(c1>c2)return 1;
95 if(c1==0 && c2==0)return 0;
96 if(c1==0)return -1;
97 if(c2==0)return 1;
98 a++;
99 b++;
101 return 0;
105 STRING32_lstrcmpW(LPCWSTR a,LPCWSTR b) {
106 WCHAR diff;
108 while(*a && *b) {
109 diff=*a-*b;
110 if (diff) return diff;
111 a++;
112 b++;
114 if (*a) return *a;
115 if (*b) return -*b;
116 return 0;
119 LPWSTR
120 STRING32_lstrchrW(LPCWSTR a,WCHAR c) {
121 while(*a) {
122 if (*a==c)
123 return a;
124 a++;
126 return NULL;
129 LPWSTR
130 STRING32_strdupW(LPCWSTR a) {
131 LPWSTR b;
132 int len;
134 len=sizeof(WCHAR)*(STRING32_UniLen(a)+1);
135 b=(LPWSTR)xmalloc(len);
136 memcpy(b,a,len);
137 return b;