Fixed header dependencies to be fully compatible with the Windows
[wine/multimedia.git] / dlls / ntdll / string.c
blob1ff6b6ee37fd3b71ba46a7aa0a235e8024842128
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 "ntstatus.h"
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winreg.h"
33 #include "winternl.h"
36 /*********************************************************************
37 * _memicmp (NTDLL.@)
39 INT __cdecl NTDLL__memicmp( LPCSTR s1, LPCSTR s2, DWORD len )
41 int ret = 0;
42 while (len--)
44 if ((ret = tolower(*s1) - tolower(*s2))) break;
45 s1++;
46 s2++;
48 return ret;
52 /*********************************************************************
53 * _strupr (NTDLL.@)
55 LPSTR __cdecl _strupr( LPSTR str )
57 LPSTR ret = str;
58 for ( ; *str; str++) *str = toupper(*str);
59 return ret;
63 /*********************************************************************
64 * _strlwr (NTDLL.@)
66 * convert a string in place to lowercase
68 LPSTR __cdecl _strlwr( LPSTR str )
70 LPSTR ret = str;
71 for ( ; *str; str++) *str = tolower(*str);
72 return ret;
76 /*********************************************************************
77 * _ultoa (NTDLL.@)
79 * Converts an unsigned long integer to a string.
81 * RETURNS
82 * Always returns str.
84 * NOTES
85 * Converts value to a '\0' terminated string which is copied to str.
86 * The maximum length of the copied str is 33 bytes.
87 * Does not check if radix is in the range of 2 to 36.
88 * If str is NULL it crashes, as the native function does.
90 char * __cdecl _ultoa(
91 unsigned long value, /* [I] Value to be converted */
92 char *str, /* [O] Destination for the converted value */
93 int radix) /* [I] Number base for conversion */
95 char buffer[33];
96 char *pos;
97 int digit;
99 pos = &buffer[32];
100 *pos = '\0';
102 do {
103 digit = value % radix;
104 value = value / radix;
105 if (digit < 10) {
106 *--pos = '0' + digit;
107 } else {
108 *--pos = 'a' + digit - 10;
109 } /* if */
110 } while (value != 0L);
112 memcpy(str, pos, &buffer[32] - pos + 1);
113 return str;
117 /*********************************************************************
118 * _ltoa (NTDLL.@)
120 * Converts a long integer to a string.
122 * RETURNS
123 * Always returns str.
125 * NOTES
126 * Converts value to a '\0' terminated string which is copied to str.
127 * The maximum length of the copied str is 33 bytes. If radix
128 * is 10 and value is negative, the value is converted with sign.
129 * Does not check if radix is in the range of 2 to 36.
130 * If str is NULL it crashes, as the native function does.
132 char * __cdecl _ltoa(
133 long value, /* [I] Value to be converted */
134 char *str, /* [O] Destination for the converted value */
135 int radix) /* [I] Number base for conversion */
137 unsigned long val;
138 int negative;
139 char buffer[33];
140 char *pos;
141 int digit;
143 if (value < 0 && radix == 10) {
144 negative = 1;
145 val = -value;
146 } else {
147 negative = 0;
148 val = value;
149 } /* if */
151 pos = &buffer[32];
152 *pos = '\0';
154 do {
155 digit = val % radix;
156 val = val / radix;
157 if (digit < 10) {
158 *--pos = '0' + digit;
159 } else {
160 *--pos = 'a' + digit - 10;
161 } /* if */
162 } while (val != 0L);
164 if (negative) {
165 *--pos = '-';
166 } /* if */
168 memcpy(str, pos, &buffer[32] - pos + 1);
169 return str;
173 /*********************************************************************
174 * _itoa (NTDLL.@)
176 * Converts an integer to a string.
178 * RETURNS
179 * Always returns str.
181 * NOTES
182 * Converts value to a '\0' terminated string which is copied to str.
183 * The maximum length of the copied str is 33 bytes. If radix
184 * is 10 and value is negative, the value is converted with sign.
185 * Does not check if radix is in the range of 2 to 36.
186 * If str is NULL it crashes, as the native function does.
188 char * __cdecl _itoa(
189 int value, /* [I] Value to be converted */
190 char *str, /* [O] Destination for the converted value */
191 int radix) /* [I] Number base for conversion */
193 return _ltoa(value, str, radix);
197 /*********************************************************************
198 * _ui64toa (NTDLL.@)
200 * Converts a large unsigned integer to a string.
202 * RETURNS
203 * Always returns str.
205 * NOTES
206 * Converts value to a '\0' terminated string which is copied to str.
207 * The maximum length of the copied str is 65 bytes.
208 * Does not check if radix is in the range of 2 to 36.
209 * If str is NULL it crashes, as the native function does.
211 char * __cdecl _ui64toa(
212 ULONGLONG value, /* [I] Value to be converted */
213 char *str, /* [O] Destination for the converted value */
214 int radix) /* [I] Number base for conversion */
216 char buffer[65];
217 char *pos;
218 int digit;
220 pos = &buffer[64];
221 *pos = '\0';
223 do {
224 digit = value % radix;
225 value = value / radix;
226 if (digit < 10) {
227 *--pos = '0' + digit;
228 } else {
229 *--pos = 'a' + digit - 10;
230 } /* if */
231 } while (value != 0L);
233 memcpy(str, pos, &buffer[64] - pos + 1);
234 return str;
238 /*********************************************************************
239 * _i64toa (NTDLL.@)
241 * Converts a large integer to a string.
243 * RETURNS
244 * Always returns str.
246 * NOTES
247 * Converts value to a '\0' terminated string which is copied to str.
248 * The maximum length of the copied str is 65 bytes. If radix
249 * is 10 and value is negative, the value is converted with sign.
250 * Does not check if radix is in the range of 2 to 36.
251 * If str is NULL it crashes, as the native function does.
253 * DIFFERENCES
254 * - The native DLL converts negative values (for base 10) wrong:
255 * -1 is converted to -18446744073709551615
256 * -2 is converted to -18446744073709551614
257 * -9223372036854775807 is converted to -9223372036854775809
258 * -9223372036854775808 is converted to -9223372036854775808
259 * The native msvcrt _i64toa function and our ntdll _i64toa function
260 * do not have this bug.
262 char * __cdecl _i64toa(
263 LONGLONG value, /* [I] Value to be converted */
264 char *str, /* [O] Destination for the converted value */
265 int radix) /* [I] Number base for conversion */
267 ULONGLONG val;
268 int negative;
269 char buffer[65];
270 char *pos;
271 int digit;
273 if (value < 0 && radix == 10) {
274 negative = 1;
275 val = -value;
276 } else {
277 negative = 0;
278 val = value;
279 } /* if */
281 pos = &buffer[64];
282 *pos = '\0';
284 do {
285 digit = val % radix;
286 val = val / radix;
287 if (digit < 10) {
288 *--pos = '0' + digit;
289 } else {
290 *--pos = 'a' + digit - 10;
291 } /* if */
292 } while (val != 0L);
294 if (negative) {
295 *--pos = '-';
296 } /* if */
298 memcpy(str, pos, &buffer[64] - pos + 1);
299 return str;
303 /*********************************************************************
304 * _atoi64 (NTDLL.@)
306 * Converts a string to a large integer.
308 * PARAMS
309 * str [I] Wstring to be converted
311 * RETURNS
312 * On success it returns the integer value otherwise it returns 0.
314 * NOTES
315 * Accepts: {whitespace} [+|-] {digits}
316 * No check is made for value overflow, only the lower 64 bits are assigned.
317 * If str is NULL it crashes, as the native function does.
319 LONGLONG __cdecl _atoi64( char *str )
321 ULONGLONG RunningTotal = 0;
322 char bMinus = 0;
324 while (*str == ' ' || (*str >= '\011' && *str <= '\015')) {
325 str++;
326 } /* while */
328 if (*str == '+') {
329 str++;
330 } else if (*str == '-') {
331 bMinus = 1;
332 str++;
333 } /* if */
335 while (*str >= '0' && *str <= '9') {
336 RunningTotal = RunningTotal * 10 + *str - '0';
337 str++;
338 } /* while */
340 return bMinus ? -RunningTotal : RunningTotal;
344 /*********************************************************************
345 * _splitpath (NTDLL.@)
347 void __cdecl _splitpath(const char* inpath, char * drv, char * dir,
348 char* fname, char * ext )
350 /* Modified PD code from 'snippets' collection. */
351 char ch, *ptr, *p;
352 char pathbuff[MAX_PATH], *path=pathbuff;
354 strcpy(pathbuff, inpath);
356 /* convert slashes to backslashes for searching */
357 for (ptr = (char*)path; *ptr; ++ptr)
358 if ('/' == *ptr)
359 *ptr = '\\';
361 /* look for drive spec */
362 if ('\0' != (ptr = strchr(path, ':')))
364 ++ptr;
365 if (drv)
367 strncpy(drv, path, ptr - path);
368 drv[ptr - path] = '\0';
370 path = ptr;
372 else if (drv)
373 *drv = '\0';
375 /* find rightmost backslash or leftmost colon */
376 if (NULL == (ptr = strrchr(path, '\\')))
377 ptr = (strchr(path, ':'));
379 if (!ptr)
381 ptr = (char *)path; /* no path */
382 if (dir)
383 *dir = '\0';
385 else
387 ++ptr; /* skip the delimiter */
388 if (dir)
390 ch = *ptr;
391 *ptr = '\0';
392 strcpy(dir, path);
393 *ptr = ch;
397 if (NULL == (p = strrchr(ptr, '.')))
399 if (fname)
400 strcpy(fname, ptr);
401 if (ext)
402 *ext = '\0';
404 else
406 *p = '\0';
407 if (fname)
408 strcpy(fname, ptr);
409 *p = '.';
410 if (ext)
411 strcpy(ext, p);
414 /* Fix pathological case - Win returns ':' as part of the
415 * directory when no drive letter is given.
417 if (drv && drv[0] == ':')
419 *drv = '\0';
420 if (dir)
422 pathbuff[0] = ':';
423 pathbuff[1] = '\0';
424 strcat(pathbuff,dir);
425 strcpy(dir,pathbuff);