ntdll: Add a test for NtNotifyChangeDirectoryFile.
[wine/multimedia.git] / dlls / ntdll / string.c
blobe05f7c33187ef2dc51d21df38a92acfe9eb74b5d
1 /*
2 * NTDLL string functions
4 * Copyright 2000 Alexandre Julliard
5 * Copyright 2000 Jon Griffiths
6 * Copyright 2003 Thomas Mertes
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "config.h"
25 #include <ctype.h>
26 #include <stdarg.h>
27 #include <string.h>
29 #include "windef.h"
30 #include "winternl.h"
33 /*********************************************************************
34 * _memicmp (NTDLL.@)
36 * Compare two blocks of memory as strings, ignoring case.
38 * PARAMS
39 * s1 [I] First string to compare to s2
40 * s2 [I] Second string to compare to s1
41 * len [I] Number of bytes to compare
43 * RETURNS
44 * An integer less than, equal to, or greater than zero indicating that
45 * s1 is less than, equal to or greater than s2 respectively.
47 * NOTES
48 * Any Nul characters in s1 or s2 are ignored. This function always
49 * compares up to len bytes or the first place where s1 and s2 differ.
51 INT __cdecl NTDLL__memicmp( LPCSTR s1, LPCSTR s2, DWORD len )
53 int ret = 0;
54 while (len--)
56 if ((ret = tolower(*s1) - tolower(*s2))) break;
57 s1++;
58 s2++;
60 return ret;
64 /*********************************************************************
65 * _strupr (NTDLL.@)
67 * Convert a string to upper case.
69 * PARAMS
70 * str [I/O] String to convert
72 * RETURNS
73 * str. There is no error return, if str is NULL or invalid, this
74 * function will crash.
76 LPSTR __cdecl _strupr( LPSTR str )
78 LPSTR ret = str;
79 for ( ; *str; str++) *str = toupper(*str);
80 return ret;
84 /*********************************************************************
85 * _strlwr (NTDLL.@)
87 * Convert a string to lowercase
89 * PARAMS
90 * str [I/O] String to convert
92 * RETURNS
93 * str. There is no error return, if str is NULL or invalid, this
94 * function will crash.
96 LPSTR __cdecl _strlwr( LPSTR str )
98 LPSTR ret = str;
99 for ( ; *str; str++) *str = tolower(*str);
100 return ret;
104 /*********************************************************************
105 * _ultoa (NTDLL.@)
107 * Convert an unsigned long integer to a string.
109 * RETURNS
110 * str.
112 * NOTES
113 * - Converts value to a Nul terminated string which is copied to str.
114 * - The maximum length of the copied str is 33 bytes.
115 * - Does not check if radix is in the range of 2 to 36.
116 * - If str is NULL it crashes, as the native function does.
118 char * __cdecl _ultoa(
119 unsigned long value, /* [I] Value to be converted */
120 char *str, /* [O] Destination for the converted value */
121 int radix) /* [I] Number base for conversion */
123 char buffer[33];
124 char *pos;
125 int digit;
127 pos = &buffer[32];
128 *pos = '\0';
130 do {
131 digit = value % radix;
132 value = value / radix;
133 if (digit < 10) {
134 *--pos = '0' + digit;
135 } else {
136 *--pos = 'a' + digit - 10;
137 } /* if */
138 } while (value != 0L);
140 memcpy(str, pos, &buffer[32] - pos + 1);
141 return str;
145 /*********************************************************************
146 * _ltoa (NTDLL.@)
148 * Convert a long integer to a string.
150 * RETURNS
151 * str.
153 * NOTES
154 * - Converts value to a Nul terminated string which is copied to str.
155 * - The maximum length of the copied str is 33 bytes. If radix
156 * is 10 and value is negative, the value is converted with sign.
157 * - Does not check if radix is in the range of 2 to 36.
158 * - If str is NULL it crashes, as the native function does.
160 char * __cdecl _ltoa(
161 long value, /* [I] Value to be converted */
162 char *str, /* [O] Destination for the converted value */
163 int radix) /* [I] Number base for conversion */
165 unsigned long val;
166 int negative;
167 char buffer[33];
168 char *pos;
169 int digit;
171 if (value < 0 && radix == 10) {
172 negative = 1;
173 val = -value;
174 } else {
175 negative = 0;
176 val = value;
177 } /* if */
179 pos = &buffer[32];
180 *pos = '\0';
182 do {
183 digit = val % radix;
184 val = val / radix;
185 if (digit < 10) {
186 *--pos = '0' + digit;
187 } else {
188 *--pos = 'a' + digit - 10;
189 } /* if */
190 } while (val != 0L);
192 if (negative) {
193 *--pos = '-';
194 } /* if */
196 memcpy(str, pos, &buffer[32] - pos + 1);
197 return str;
201 /*********************************************************************
202 * _itoa (NTDLL.@)
204 * Converts an integer to a string.
206 * RETURNS
207 * str.
209 * NOTES
210 * - Converts value to a '\0' terminated string which is copied to str.
211 * - The maximum length of the copied str is 33 bytes. If radix
212 * is 10 and value is negative, the value is converted with sign.
213 * - Does not check if radix is in the range of 2 to 36.
214 * - If str is NULL it crashes, as the native function does.
216 char * __cdecl _itoa(
217 int value, /* [I] Value to be converted */
218 char *str, /* [O] Destination for the converted value */
219 int radix) /* [I] Number base for conversion */
221 return _ltoa(value, str, radix);
225 /*********************************************************************
226 * _ui64toa (NTDLL.@)
228 * Converts a large unsigned integer to a string.
230 * RETURNS
231 * str.
233 * NOTES
234 * - Converts value to a '\0' terminated string which is copied to str.
235 * - The maximum length of the copied str is 65 bytes.
236 * - Does not check if radix is in the range of 2 to 36.
237 * - If str is NULL it crashes, as the native function does.
239 char * __cdecl _ui64toa(
240 ULONGLONG value, /* [I] Value to be converted */
241 char *str, /* [O] Destination for the converted value */
242 int radix) /* [I] Number base for conversion */
244 char buffer[65];
245 char *pos;
246 int digit;
248 pos = &buffer[64];
249 *pos = '\0';
251 do {
252 digit = value % radix;
253 value = value / radix;
254 if (digit < 10) {
255 *--pos = '0' + digit;
256 } else {
257 *--pos = 'a' + digit - 10;
258 } /* if */
259 } while (value != 0L);
261 memcpy(str, pos, &buffer[64] - pos + 1);
262 return str;
266 /*********************************************************************
267 * _i64toa (NTDLL.@)
269 * Converts a large integer to a string.
271 * RETURNS
272 * str.
274 * NOTES
275 * - Converts value to a Nul terminated string which is copied to str.
276 * - The maximum length of the copied str is 65 bytes. If radix
277 * is 10 and value is negative, the value is converted with sign.
278 * - Does not check if radix is in the range of 2 to 36.
279 * - If str is NULL it crashes, as the native function does.
281 * DIFFERENCES
282 * - The native DLL converts negative values (for base 10) wrong:
283 *| -1 is converted to -18446744073709551615
284 *| -2 is converted to -18446744073709551614
285 *| -9223372036854775807 is converted to -9223372036854775809
286 *| -9223372036854775808 is converted to -9223372036854775808
287 * The native msvcrt _i64toa function and our ntdll _i64toa function
288 * do not have this bug.
290 char * __cdecl _i64toa(
291 LONGLONG value, /* [I] Value to be converted */
292 char *str, /* [O] Destination for the converted value */
293 int radix) /* [I] Number base for conversion */
295 ULONGLONG val;
296 int negative;
297 char buffer[65];
298 char *pos;
299 int digit;
301 if (value < 0 && radix == 10) {
302 negative = 1;
303 val = -value;
304 } else {
305 negative = 0;
306 val = value;
307 } /* if */
309 pos = &buffer[64];
310 *pos = '\0';
312 do {
313 digit = val % radix;
314 val = val / radix;
315 if (digit < 10) {
316 *--pos = '0' + digit;
317 } else {
318 *--pos = 'a' + digit - 10;
319 } /* if */
320 } while (val != 0L);
322 if (negative) {
323 *--pos = '-';
324 } /* if */
326 memcpy(str, pos, &buffer[64] - pos + 1);
327 return str;
331 /*********************************************************************
332 * _atoi64 (NTDLL.@)
334 * Convert a string to a large integer.
336 * PARAMS
337 * str [I] String to be converted
339 * RETURNS
340 * Success: The integer value represented by str.
341 * Failure: 0. Note that this cannot be distinguished from a successful
342 * return, if the string contains "0".
344 * NOTES
345 * - Accepts: {whitespace} [+|-] {digits}
346 * - No check is made for value overflow, only the lower 64 bits are assigned.
347 * - If str is NULL it crashes, as the native function does.
349 LONGLONG __cdecl _atoi64( char *str )
351 ULONGLONG RunningTotal = 0;
352 char bMinus = 0;
354 while (*str == ' ' || (*str >= '\011' && *str <= '\015')) {
355 str++;
356 } /* while */
358 if (*str == '+') {
359 str++;
360 } else if (*str == '-') {
361 bMinus = 1;
362 str++;
363 } /* if */
365 while (*str >= '0' && *str <= '9') {
366 RunningTotal = RunningTotal * 10 + *str - '0';
367 str++;
368 } /* while */
370 return bMinus ? -RunningTotal : RunningTotal;
374 /*********************************************************************
375 * _splitpath (NTDLL.@)
377 * Split a path into its component pieces.
379 * PARAMS
380 * inpath [I] Path to split
381 * drv [O] Destination for drive component (e.g. "A:"). Must be at least 3 characters.
382 * dir [O] Destination for directory component. Should be at least MAX_PATH characters.
383 * fname [O] Destination for File name component. Should be at least MAX_PATH characters.
384 * ext [O] Destination for file extension component. Should be at least MAX_PATH characters.
386 * RETURNS
387 * Nothing.
389 void __cdecl _splitpath(const char* inpath, char * drv, char * dir,
390 char* fname, char * ext )
392 const char *p, *end;
394 if (inpath[0] && inpath[1] == ':')
396 if (drv)
398 drv[0] = inpath[0];
399 drv[1] = inpath[1];
400 drv[2] = 0;
402 inpath += 2;
404 else if (drv) drv[0] = 0;
406 /* look for end of directory part */
407 end = NULL;
408 for (p = inpath; *p; p++) if (*p == '/' || *p == '\\') end = p + 1;
410 if (end) /* got a directory */
412 if (dir)
414 memcpy( dir, inpath, end - inpath );
415 dir[end - inpath] = 0;
417 inpath = end;
419 else if (dir) dir[0] = 0;
421 /* look for extension: what's after the last dot */
422 end = NULL;
423 for (p = inpath; *p; p++) if (*p == '.') end = p;
425 if (!end) end = p; /* there's no extension */
427 if (fname)
429 memcpy( fname, inpath, end - inpath );
430 fname[end - inpath] = 0;
432 if (ext) strcpy( ext, end );