string.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /**
  2. * WinPR: Windows Portable Runtime
  3. * String Manipulation (CRT)
  4. *
  5. * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
  6. * Copyright 2016 David PHAM-VAN <d.phamvan@inuvika.com>
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #ifndef WINPR_CRT_STRING_H
  21. #define WINPR_CRT_STRING_H
  22. #include <wchar.h>
  23. #include <string.h>
  24. #include <stdarg.h>
  25. #include <winpr/winpr.h>
  26. #include <winpr/wtypes.h>
  27. #ifdef __cplusplus
  28. extern "C"
  29. {
  30. #endif
  31. #ifndef _WIN32
  32. #define CSTR_LESS_THAN 1
  33. #define CSTR_EQUAL 2
  34. #define CSTR_GREATER_THAN 3
  35. #define CP_ACP 0
  36. #define CP_OEMCP 1
  37. #define CP_MACCP 2
  38. #define CP_THREAD_ACP 3
  39. #define CP_SYMBOL 42
  40. #define CP_UTF7 65000
  41. #define CP_UTF8 65001
  42. #define MB_PRECOMPOSED 0x00000001
  43. #define MB_COMPOSITE 0x00000002
  44. #define MB_USEGLYPHCHARS 0x00000004
  45. #define MB_ERR_INVALID_CHARS 0x00000008
  46. #define _TRUNCATE ((size_t)-1)
  47. WINPR_API char* _strdup(const char* strSource);
  48. WINPR_API WCHAR* _wcsdup(const WCHAR* strSource);
  49. WINPR_API int _stricmp(const char* string1, const char* string2);
  50. WINPR_API int _strnicmp(const char* string1, const char* string2, size_t count);
  51. WINPR_API int _wcscmp(const WCHAR* string1, const WCHAR* string2);
  52. WINPR_API int _wcsicmp(const WCHAR* string1, const WCHAR* string2);
  53. WINPR_API int _wcsnicmp(const WCHAR* string1, const WCHAR* string2, size_t count);
  54. WINPR_API int _wcsncmp(const WCHAR* string1, const WCHAR* string2, size_t count);
  55. WINPR_API size_t _wcslen(const WCHAR* str);
  56. WINPR_API size_t _wcsnlen(const WCHAR* str, size_t maxNumberOfElements);
  57. WINPR_API WCHAR* _wcschr(const WCHAR* str, WCHAR c);
  58. WINPR_API WCHAR* _wcsrchr(const WCHAR* str, WCHAR c);
  59. WINPR_API char* strtok_s(char* strToken, const char* strDelimit, char** context);
  60. WINPR_API WCHAR* wcstok_s(WCHAR* strToken, const WCHAR* strDelimit, WCHAR** context);
  61. #define stricmp _stricmp
  62. #define strnicmp _strnicmp
  63. #define strcpy_s(dst, size, src) strcpy(dst, src)
  64. #define strncpy_s(a, b, c, d) strncpy(a, c, d)
  65. #else
  66. #define _wcscmp wcscmp
  67. #define _wcslen wcslen
  68. #define _wcsnlen wcsnlen
  69. #define _wcschr wcschr
  70. #define _wcsrchr wcsrchr
  71. #endif
  72. #if !defined(_WIN32) || defined(_UWP)
  73. WINPR_API LPSTR CharUpperA(LPSTR lpsz);
  74. WINPR_API LPWSTR CharUpperW(LPWSTR lpsz);
  75. #ifdef UNICODE
  76. #define CharUpper CharUpperW
  77. #else
  78. #define CharUpper CharUpperA
  79. #endif
  80. WINPR_API DWORD CharUpperBuffA(LPSTR lpsz, DWORD cchLength);
  81. WINPR_API DWORD CharUpperBuffW(LPWSTR lpsz, DWORD cchLength);
  82. #ifdef UNICODE
  83. #define CharUpperBuff CharUpperBuffW
  84. #else
  85. #define CharUpperBuff CharUpperBuffA
  86. #endif
  87. WINPR_API LPSTR CharLowerA(LPSTR lpsz);
  88. WINPR_API LPWSTR CharLowerW(LPWSTR lpsz);
  89. #ifdef UNICODE
  90. #define CharLower CharLowerW
  91. #else
  92. #define CharLower CharLowerA
  93. #endif
  94. WINPR_API DWORD CharLowerBuffA(LPSTR lpsz, DWORD cchLength);
  95. WINPR_API DWORD CharLowerBuffW(LPWSTR lpsz, DWORD cchLength);
  96. #ifdef UNICODE
  97. #define CharLowerBuff CharLowerBuffW
  98. #else
  99. #define CharLowerBuff CharLowerBuffA
  100. #endif
  101. WINPR_API BOOL IsCharAlphaA(CHAR ch);
  102. WINPR_API BOOL IsCharAlphaW(WCHAR ch);
  103. #ifdef UNICODE
  104. #define IsCharAlpha IsCharAlphaW
  105. #else
  106. #define IsCharAlpha IsCharAlphaA
  107. #endif
  108. WINPR_API BOOL IsCharAlphaNumericA(CHAR ch);
  109. WINPR_API BOOL IsCharAlphaNumericW(WCHAR ch);
  110. #ifdef UNICODE
  111. #define IsCharAlphaNumeric IsCharAlphaNumericW
  112. #else
  113. #define IsCharAlphaNumeric IsCharAlphaNumericA
  114. #endif
  115. WINPR_API BOOL IsCharUpperA(CHAR ch);
  116. WINPR_API BOOL IsCharUpperW(WCHAR ch);
  117. #ifdef UNICODE
  118. #define IsCharUpper IsCharUpperW
  119. #else
  120. #define IsCharUpper IsCharUpperA
  121. #endif
  122. WINPR_API BOOL IsCharLowerA(CHAR ch);
  123. WINPR_API BOOL IsCharLowerW(WCHAR ch);
  124. #ifdef UNICODE
  125. #define IsCharLower IsCharLowerW
  126. #else
  127. #define IsCharLower IsCharLowerA
  128. #endif
  129. WINPR_API int lstrlenA(LPCSTR lpString);
  130. WINPR_API int lstrlenW(LPCWSTR lpString);
  131. #ifdef UNICODE
  132. #define lstrlen lstrlenW
  133. #else
  134. #define lstrlen lstrlenA
  135. #endif
  136. WINPR_API int lstrcmpA(LPCSTR lpString1, LPCSTR lpString2);
  137. WINPR_API int lstrcmpW(LPCWSTR lpString1, LPCWSTR lpString2);
  138. #ifdef UNICODE
  139. #define lstrcmp lstrcmpW
  140. #else
  141. #define lstrcmp lstrcmpA
  142. #endif
  143. #endif
  144. #ifndef _WIN32
  145. #define sprintf_s snprintf
  146. #define _snprintf snprintf
  147. #define _vsnprintf vsnprintf
  148. #define _scprintf(...) snprintf(NULL, 0, __VA_ARGS__)
  149. #if 0
  150. #define _vscprintf(...) vsnprintf(NULL, 0, __VA_ARGS__)
  151. #else
  152. WINPR_API int _vscprintf(const char* format, va_list pargs);
  153. #endif
  154. WINPR_API int _memicmp(const void* first, const void* last, unsigned int count);
  155. WINPR_API VOID OutputDebugStringA(LPCSTR lpOutputString);
  156. WINPR_API VOID OutputDebugStringW(LPCSTR lpOutputString);
  157. WINPR_API int _vscwprintf(const WCHAR* format, va_list arglist);
  158. WINPR_API int _scwprintf(const WCHAR* format, ...);
  159. //TODO: check its validity.
  160. WINPR_API int _vsnprintf_s(char* dst, size_t sz_in_byte, size_t max_count, const char* format, va_list arglist);
  161. //TODO: check its validity.
  162. WINPR_API int _vsnwprintf_s(WCHAR* dst, size_t sz_in_byte, size_t max_count, const WCHAR* format, va_list arglist);
  163. //TODO: check its validity.
  164. WINPR_API int wvsprintfA(LPSTR, LPCSTR, va_list arglist);
  165. //TODO: check its validity.
  166. WINPR_API int wvsprintfW(LPWSTR, LPCWSTR, va_list arglist);
  167. #ifdef UNICODE
  168. #define wvsprintf wvsprintfW
  169. #else
  170. #define wvsprintf wvsprintfA
  171. #endif
  172. WINPR_API int wsprintfA(LPSTR, LPCSTR, ...);
  173. #define fprintf_s fprintf
  174. #define swprintf_s swprintf
  175. #define sscanf_s sscanf
  176. #ifdef UNICODE
  177. #define OutputDebugString OutputDebugStringW
  178. #else
  179. #define OutputDebugString OutputDebugStringA
  180. #endif
  181. /* Unicode Conversion */
  182. WINPR_API int MultiByteToWideChar(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr,
  183. int cbMultiByte, LPWSTR lpWideCharStr, int cchWideChar);
  184. WINPR_API int WideCharToMultiByte(UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr,
  185. int cchWideChar, LPSTR lpMultiByteStr, int cbMultiByte,
  186. LPCSTR lpDefaultChar, LPBOOL lpUsedDefaultChar);
  187. #endif
  188. /* Extended API */
  189. WINPR_API int ConvertToUnicode(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr,
  190. int cbMultiByte, LPWSTR* lpWideCharStr, int cchWideChar);
  191. WINPR_API int ConvertFromUnicode(UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr,
  192. int cchWideChar, LPSTR* lpMultiByteStr, int cbMultiByte,
  193. LPCSTR lpDefaultChar, LPBOOL lpUsedDefaultChar);
  194. WINPR_API void ByteSwapUnicode(WCHAR* wstr, int length);
  195. WINPR_API int ConvertLineEndingToLF(char* str, int size);
  196. WINPR_API char* ConvertLineEndingToCRLF(const char* str, int* size);
  197. WINPR_API char* StrSep(char** stringp, const char* delim);
  198. WINPR_API INT64 GetLine(char** lineptr, size_t* size, FILE* stream);
  199. #ifdef __cplusplus
  200. }
  201. #endif
  202. #endif /* WINPR_CRT_STRING_H */