2
* WinPR: Windows Portable Runtime
3
* String Manipulation (CRT)
5
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
7
* Licensed under the Apache License, Version 2.0 (the "License");
8
* you may not use this file except in compliance with the License.
9
* You may obtain a copy of the License at
11
* http://www.apache.org/licenses/LICENSE-2.0
13
* Unless required by applicable law or agreed to in writing, software
14
* distributed under the License is distributed on an "AS IS" BASIS,
15
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
* See the License for the specific language governing permissions and
17
* limitations under the License.
27
#include <winpr/crt.h>
29
/* String Manipulation (CRT): http://msdn.microsoft.com/en-us/library/f0151s4x.aspx */
33
char* _strdup(const char* strSource)
37
if (strSource == NULL)
40
strDestination = strdup(strSource);
42
if (strDestination == NULL)
45
return strDestination;
48
WCHAR* _wcsdup(const WCHAR* strSource)
50
WCHAR* strDestination;
52
if (strSource == NULL)
56
strDestination = wsdup(strSource);
57
#elif defined(__APPLE__) && defined(__MACH__) || defined(ANDROID)
58
strDestination = malloc(wcslen((wchar_t*)strSource));
60
if (strDestination != NULL)
61
wcscpy((wchar_t*)strDestination, (const wchar_t*)strSource);
63
strDestination = (WCHAR*) wcsdup((wchar_t*) strSource);
66
if (strDestination == NULL)
69
return strDestination;
72
int _stricmp(const char* string1, const char* string2)
74
return strcasecmp(string1, string2);
77
int _strnicmp(const char* string1, const char* string2, size_t count)
79
return strncasecmp(string1, string2, count);
82
/* _wcscmp -> wcscmp */
84
int _wcscmp(const WCHAR* string1, const WCHAR* string2)
86
while (*string1 && (*string1 == *string2))
92
return *string1 - *string2;
95
/* _wcslen -> wcslen */
97
size_t _wcslen(const WCHAR* str)
99
WCHAR* p = (WCHAR*) str;
110
/* _wcschr -> wcschr */
112
WCHAR* _wcschr(const WCHAR* str, WCHAR c)
114
WCHAR* p = (WCHAR*) str;
116
while (*p && (*p != c))
119
return ((*p == c) ? p : NULL);
122
char* strtok_s(char* strToken, const char* strDelimit, char** context)
124
return strtok_r(strToken, strDelimit, context);
127
WCHAR* wcstok_s(WCHAR* strToken, const WCHAR* strDelimit, WCHAR** context)
134
while (*strToken && _wcschr(strDelimit, *strToken))
140
nextToken = strToken++;
142
while (*strToken && !(_wcschr(strDelimit, *strToken)))
153
/* Windows API Sets - api-ms-win-core-string-l2-1-0.dll
154
* http://msdn.microsoft.com/en-us/library/hh802935/
157
LPSTR CharUpperA(LPSTR lpsz)
162
length = strlen(lpsz);
172
if ((c >= 'a') && (c <= 'z'))
180
for (i = 0; i < length; i++)
182
if ((lpsz[i] >= 'a') && (lpsz[i] <= 'z'))
183
lpsz[i] = lpsz[i] - 32;
189
LPWSTR CharUpperW(LPWSTR lpsz)
191
fprintf(stderr, "CharUpperW unimplemented!\n");
193
return (LPWSTR) NULL;
196
DWORD CharUpperBuffA(LPSTR lpsz, DWORD cchLength)
203
for (i = 0; i < cchLength; i++)
205
if ((lpsz[i] >= 'a') && (lpsz[i] <= 'z'))
206
lpsz[i] = lpsz[i] - 32;
212
DWORD CharUpperBuffW(LPWSTR lpsz, DWORD cchLength)
216
unsigned int wc, uwc;
218
p = (unsigned char*) lpsz;
220
for (i = 0; i < cchLength; i++)
222
wc = (unsigned int) (*p);
223
wc += (unsigned int) (*(p + 1)) << 8;
230
*(p + 1) = (uwc >> 8) & 0xFF;
239
LPSTR CharLowerA(LPSTR lpsz)
244
length = strlen(lpsz);
254
if ((c >= 'A') && (c <= 'Z'))
262
for (i = 0; i < length; i++)
264
if ((lpsz[i] >= 'A') && (lpsz[i] <= 'Z'))
265
lpsz[i] = lpsz[i] + 32;
271
LPWSTR CharLowerW(LPWSTR lpsz)
273
fprintf(stderr, "CharLowerW unimplemented!\n");
275
return (LPWSTR) NULL;
278
DWORD CharLowerBuffA(LPSTR lpsz, DWORD cchLength)
285
for (i = 0; i < cchLength; i++)
287
if ((lpsz[i] >= 'A') && (lpsz[i] <= 'Z'))
288
lpsz[i] = lpsz[i] + 32;
294
DWORD CharLowerBuffW(LPWSTR lpsz, DWORD cchLength)
298
unsigned int wc, uwc;
300
p = (unsigned char*) lpsz;
302
for (i = 0; i < cchLength; i++)
304
wc = (unsigned int) (*p);
305
wc += (unsigned int) (*(p + 1)) << 8;
312
*(p + 1) = (uwc >> 8) & 0xFF;
321
BOOL IsCharAlphaA(CHAR ch)
323
if (((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z')))
329
BOOL IsCharAlphaW(WCHAR ch)
331
fprintf(stderr, "IsCharAlphaW unimplemented!\n");
335
BOOL IsCharAlphaNumericA(CHAR ch)
337
if (((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z')) ||
338
((ch >= '0') && (ch <= '9')))
344
BOOL IsCharAlphaNumericW(WCHAR ch)
346
fprintf(stderr, "IsCharAlphaNumericW unimplemented!\n");
350
BOOL IsCharUpperA(CHAR ch)
352
if ((ch >= 'A') && (ch <= 'Z'))
358
BOOL IsCharUpperW(WCHAR ch)
360
fprintf(stderr, "IsCharUpperW unimplemented!\n");
364
BOOL IsCharLowerA(CHAR ch)
366
if ((ch >= 'a') && (ch <= 'z'))
372
BOOL IsCharLowerW(WCHAR ch)
374
fprintf(stderr, "IsCharLowerW unimplemented!\n");
378
int lstrlenA(LPCSTR lpString)
380
return strlen(lpString);
383
int lstrlenW(LPCWSTR lpString)
390
p = (LPWSTR) lpString;
398
int lstrcmpA(LPCSTR lpString1, LPCSTR lpString2)
400
return strcmp(lpString1, lpString2);
403
int lstrcmpW(LPCWSTR lpString1, LPCWSTR lpString2)
405
while (*lpString1 && (*lpString1 == *lpString2))
411
return *lpString1 - *lpString2;