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 */
36 #define TAG WINPR_TAG("crt")
38 char* _strdup(const char* strSource)
42 if (strSource == NULL)
45 strDestination = strdup(strSource);
47 if (strDestination == NULL)
48 WLog_ERR(TAG,"strdup");
50 return strDestination;
53 WCHAR* _wcsdup(const WCHAR* strSource)
55 WCHAR* strDestination;
57 if (strSource == NULL)
60 #if defined(__APPLE__) && defined(__MACH__) || defined(ANDROID) || defined(sun)
61 strDestination = malloc(wcslen((wchar_t*)strSource));
63 if (strDestination != NULL)
64 wcscpy((wchar_t*)strDestination, (const wchar_t*)strSource);
67 strDestination = (WCHAR*) wcsdup((wchar_t*) strSource);
70 if (strDestination == NULL)
71 WLog_ERR(TAG,"wcsdup");
73 return strDestination;
76 int _stricmp(const char* string1, const char* string2)
78 return strcasecmp(string1, string2);
81 int _strnicmp(const char* string1, const char* string2, size_t count)
83 return strncasecmp(string1, string2, count);
86 /* _wcscmp -> wcscmp */
88 int _wcscmp(const WCHAR* string1, const WCHAR* string2)
90 while (*string1 && (*string1 == *string2))
96 return *string1 - *string2;
99 /* _wcslen -> wcslen */
101 size_t _wcslen(const WCHAR* str)
103 WCHAR* p = (WCHAR*) str;
114 /* _wcschr -> wcschr */
116 WCHAR* _wcschr(const WCHAR* str, WCHAR c)
118 WCHAR* p = (WCHAR*) str;
120 while (*p && (*p != c))
123 return ((*p == c) ? p : NULL);
126 char* strtok_s(char* strToken, const char* strDelimit, char** context)
128 return strtok_r(strToken, strDelimit, context);
131 WCHAR* wcstok_s(WCHAR* strToken, const WCHAR* strDelimit, WCHAR** context)
138 while (*strToken && _wcschr(strDelimit, *strToken))
144 nextToken = strToken++;
146 while (*strToken && !(_wcschr(strDelimit, *strToken)))
156 /* Windows API Sets - api-ms-win-core-string-l2-1-0.dll
157 * http://msdn.microsoft.com/en-us/library/hh802935/
160 LPSTR CharUpperA(LPSTR lpsz)
168 length = strlen(lpsz);
177 if ((c >= 'a') && (c <= 'z'))
184 for (i = 0; i < length; i++)
186 if ((lpsz[i] >= 'a') && (lpsz[i] <= 'z'))
187 lpsz[i] = lpsz[i] - 32;
193 LPWSTR CharUpperW(LPWSTR lpsz)
195 WLog_ERR(TAG, "CharUpperW unimplemented!");
196 return (LPWSTR) NULL;
199 DWORD CharUpperBuffA(LPSTR lpsz, DWORD cchLength)
206 for (i = 0; i < cchLength; i++)
208 if ((lpsz[i] >= 'a') && (lpsz[i] <= 'z'))
209 lpsz[i] = lpsz[i] - 32;
215 DWORD CharUpperBuffW(LPWSTR lpsz, DWORD cchLength)
219 for (i = 0; i < cchLength; i++)
221 lpsz[i] = WINPR_TOUPPERW(lpsz[i]);
227 LPSTR CharLowerA(LPSTR lpsz)
235 length = strlen(lpsz);
244 if ((c >= 'A') && (c <= 'Z'))
251 for (i = 0; i < length; i++)
253 if ((lpsz[i] >= 'A') && (lpsz[i] <= 'Z'))
254 lpsz[i] = lpsz[i] + 32;
260 LPWSTR CharLowerW(LPWSTR lpsz)
262 WLog_ERR(TAG, "CharLowerW unimplemented!");
263 return (LPWSTR) NULL;
266 DWORD CharLowerBuffA(LPSTR lpsz, DWORD cchLength)
273 for (i = 0; i < cchLength; i++)
275 if ((lpsz[i] >= 'A') && (lpsz[i] <= 'Z'))
276 lpsz[i] = lpsz[i] + 32;
282 DWORD CharLowerBuffW(LPWSTR lpsz, DWORD cchLength)
286 for (i = 0; i < cchLength; i++)
288 lpsz[i] = WINPR_TOLOWERW(lpsz[i]);
294 BOOL IsCharAlphaA(CHAR ch)
296 if (((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z')))
302 BOOL IsCharAlphaW(WCHAR ch)
304 WLog_ERR(TAG, "IsCharAlphaW unimplemented!");
308 BOOL IsCharAlphaNumericA(CHAR ch)
310 if (((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z')) ||
311 ((ch >= '0') && (ch <= '9')))
317 BOOL IsCharAlphaNumericW(WCHAR ch)
319 WLog_ERR(TAG, "IsCharAlphaNumericW unimplemented!");
323 BOOL IsCharUpperA(CHAR ch)
325 if ((ch >= 'A') && (ch <= 'Z'))
331 BOOL IsCharUpperW(WCHAR ch)
333 WLog_ERR(TAG, "IsCharUpperW unimplemented!");
337 BOOL IsCharLowerA(CHAR ch)
339 if ((ch >= 'a') && (ch <= 'z'))
345 BOOL IsCharLowerW(WCHAR ch)
347 WLog_ERR(TAG, "IsCharLowerW unimplemented!");
351 int lstrlenA(LPCSTR lpString)
353 return strlen(lpString);
356 int lstrlenW(LPCWSTR lpString)
363 p = (LPWSTR) lpString;
371 int lstrcmpA(LPCSTR lpString1, LPCSTR lpString2)
373 return strcmp(lpString1, lpString2);
376 int lstrcmpW(LPCWSTR lpString1, LPCWSTR lpString2)
378 while (*lpString1 && (*lpString1 == *lpString2))
384 return *lpString1 - *lpString2;
389 int ConvertLineEndingToLF(char* str, int size)
397 pInput = pOutput = str;
401 if ((pInput[0] == '\r') && (pInput[1] == '\n'))
408 *pOutput++ = *pInput++;
412 status = pOutput - str;
417 char* ConvertLineEndingToCRLF(const char* str, int* size)
438 newStr = (char*) malloc(*size + (count * 2) + 1);
448 if ((*pInput == '\n') && ((pInput > str) && (pInput[-1] != '\r')))
455 *pOutput++ = *pInput;
461 *size = pOutput - newStr;