Got rid of GetSysColorPen.
[wine] / unicode / string.c
1 /*
2  * Unicode string manipulation functions
3  *
4  * Copyright 2000 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <limits.h>
22
23 #include "wine/unicode.h"
24
25 int strcmpiW( const WCHAR *str1, const WCHAR *str2 )
26 {
27     for (;;)
28     {
29         int ret = toupperW(*str1) - toupperW(*str2);
30         if (ret || !*str1) return ret;
31         str1++;
32         str2++;
33     }
34 }
35
36 int strncmpiW( const WCHAR *str1, const WCHAR *str2, int n )
37 {
38     int ret = 0;
39     for ( ; n > 0; n--, str1++, str2++)
40         if ((ret = toupperW(*str1) - toupperW(*str2)) || !*str1) break;
41     return ret;
42 }
43
44 WCHAR *strstrW( const WCHAR *str, const WCHAR *sub )
45 {
46     while (*str)
47     {
48         const WCHAR *p1 = str, *p2 = sub;
49         while (*p1 && *p2 && *p1 == *p2) { p1++; p2++; }
50         if (!*p2) return (WCHAR *)str;
51         str++;
52     }
53     return NULL;
54 }
55
56 /* strtolW and strtoulW implementation based on the GNU C library code */
57 /* Copyright (C) 1991,92,94,95,96,97,98,99,2000,2001 Free Software Foundation, Inc. */
58
59 long int strtolW( const WCHAR *nptr, WCHAR **endptr, int base )
60 {
61   int negative;
62   register unsigned long int cutoff;
63   register unsigned int cutlim;
64   register unsigned long int i;
65   register const WCHAR *s;
66   register WCHAR c;
67   const WCHAR *save, *end;
68   int overflow;
69
70   if (base < 0 || base == 1 || base > 36) return 0;
71
72   save = s = nptr;
73
74   /* Skip white space.  */
75   while (isspaceW (*s))
76     ++s;
77   if (!*s) goto noconv;
78
79   /* Check for a sign.  */
80   negative = 0;
81   if (*s == '-')
82     {
83       negative = 1;
84       ++s;
85     }
86   else if (*s == '+')
87     ++s;
88
89   /* Recognize number prefix and if BASE is zero, figure it out ourselves.  */
90   if (*s == '0')
91     {
92       if ((base == 0 || base == 16) && toupperW(s[1]) == 'X')
93         {
94           s += 2;
95           base = 16;
96         }
97       else if (base == 0)
98         base = 8;
99     }
100   else if (base == 0)
101     base = 10;
102
103   /* Save the pointer so we can check later if anything happened.  */
104   save = s;
105   end = NULL;
106
107   cutoff = ULONG_MAX / (unsigned long int) base;
108   cutlim = ULONG_MAX % (unsigned long int) base;
109
110   overflow = 0;
111   i = 0;
112   c = *s;
113   for (;c != '\0'; c = *++s)
114   {
115       if (s == end)
116           break;
117       if (c >= '0' && c <= '9')
118           c -= '0';
119       else if (isalphaW (c))
120           c = toupperW (c) - 'A' + 10;
121       else
122           break;
123       if ((int) c >= base)
124           break;
125       /* Check for overflow.  */
126       if (i > cutoff || (i == cutoff && c > cutlim))
127           overflow = 1;
128       else
129       {
130           i *= (unsigned long int) base;
131           i += c;
132       }
133   }
134
135   /* Check if anything actually happened.  */
136   if (s == save)
137     goto noconv;
138
139   /* Store in ENDPTR the address of one character
140      past the last character we converted.  */
141   if (endptr != NULL)
142     *endptr = (WCHAR *)s;
143
144   /* Check for a value that is within the range of
145      `unsigned LONG int', but outside the range of `LONG int'.  */
146   if (overflow == 0
147       && i > (negative
148               ? -((unsigned long int) (LONG_MIN + 1)) + 1
149               : (unsigned long int) LONG_MAX))
150     overflow = 1;
151
152   if (overflow)
153     {
154       return negative ? LONG_MIN : LONG_MAX;
155     }
156
157   /* Return the result of the appropriate sign.  */
158   return negative ? -i : i;
159
160 noconv:
161   /* We must handle a special case here: the base is 0 or 16 and the
162      first two characters are '0' and 'x', but the rest are no
163      hexadecimal digits.  This is no error case.  We return 0 and
164      ENDPTR points to the `x`.  */
165   if (endptr != NULL)
166     {
167       if (save - nptr >= 2 && toupperW (save[-1]) == 'X'
168           && save[-2] == '0')
169         *endptr = (WCHAR *)&save[-1];
170       else
171         /*  There was no number to convert.  */
172         *endptr = (WCHAR *)nptr;
173     }
174
175   return 0L;
176 }
177
178
179 unsigned long int strtoulW( const WCHAR *nptr, WCHAR **endptr, int base )
180 {
181   int negative;
182   register unsigned long int cutoff;
183   register unsigned int cutlim;
184   register unsigned long int i;
185   register const WCHAR *s;
186   register WCHAR c;
187   const WCHAR *save, *end;
188   int overflow;
189
190   if (base < 0 || base == 1 || base > 36) return 0;
191
192   save = s = nptr;
193
194   /* Skip white space.  */
195   while (isspaceW (*s))
196     ++s;
197   if (!*s) goto noconv;
198
199   /* Check for a sign.  */
200   negative = 0;
201   if (*s == '-')
202     {
203       negative = 1;
204       ++s;
205     }
206   else if (*s == '+')
207     ++s;
208
209   /* Recognize number prefix and if BASE is zero, figure it out ourselves.  */
210   if (*s == '0')
211     {
212       if ((base == 0 || base == 16) && toupperW(s[1]) == 'X')
213         {
214           s += 2;
215           base = 16;
216         }
217       else if (base == 0)
218         base = 8;
219     }
220   else if (base == 0)
221     base = 10;
222
223   /* Save the pointer so we can check later if anything happened.  */
224   save = s;
225   end = NULL;
226
227   cutoff = ULONG_MAX / (unsigned long int) base;
228   cutlim = ULONG_MAX % (unsigned long int) base;
229
230   overflow = 0;
231   i = 0;
232   c = *s;
233   for (;c != '\0'; c = *++s)
234   {
235       if (s == end)
236           break;
237       if (c >= '0' && c <= '9')
238           c -= '0';
239       else if (isalphaW (c))
240           c = toupperW (c) - 'A' + 10;
241       else
242           break;
243       if ((int) c >= base)
244           break;
245       /* Check for overflow.  */
246       if (i > cutoff || (i == cutoff && c > cutlim))
247           overflow = 1;
248       else
249       {
250           i *= (unsigned long int) base;
251           i += c;
252       }
253   }
254
255   /* Check if anything actually happened.  */
256   if (s == save)
257     goto noconv;
258
259   /* Store in ENDPTR the address of one character
260      past the last character we converted.  */
261   if (endptr != NULL)
262     *endptr = (WCHAR *)s;
263
264   if (overflow)
265     {
266       return ULONG_MAX;
267     }
268
269   /* Return the result of the appropriate sign.  */
270   return negative ? -i : i;
271
272 noconv:
273   /* We must handle a special case here: the base is 0 or 16 and the
274      first two characters are '0' and 'x', but the rest are no
275      hexadecimal digits.  This is no error case.  We return 0 and
276      ENDPTR points to the `x`.  */
277   if (endptr != NULL)
278     {
279       if (save - nptr >= 2 && toupperW (save[-1]) == 'X'
280           && save[-2] == '0')
281         *endptr = (WCHAR *)&save[-1];
282       else
283         /*  There was no number to convert.  */
284         *endptr = (WCHAR *)nptr;
285     }
286
287   return 0L;
288 }