string.h (5988B)
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 21#ifndef WINPR_CRT_STRING_H 22#define WINPR_CRT_STRING_H 23 24#include <wchar.h> 25#include <string.h> 26#include <winpr/winpr.h> 27#include <winpr/wtypes.h> 28 29#ifdef __cplusplus 30extern "C" 31{ 32#endif 33 34 WINPR_API BOOL winpr_str_append(const char* what, char* buffer, size_t size, 35 const char* separator); 36 37#ifndef _WIN32 38 39#define CSTR_LESS_THAN 1 40#define CSTR_EQUAL 2 41#define CSTR_GREATER_THAN 3 42 43#define CP_ACP 0 44#define CP_OEMCP 1 45#define CP_MACCP 2 46#define CP_THREAD_ACP 3 47#define CP_SYMBOL 42 48#define CP_UTF7 65000 49#define CP_UTF8 65001 50 51#define MB_PRECOMPOSED 0x00000001 52#define MB_COMPOSITE 0x00000002 53#define MB_USEGLYPHCHARS 0x00000004 54#define MB_ERR_INVALID_CHARS 0x00000008 55 56 WINPR_API char* _strdup(const char* strSource); 57 WINPR_API WCHAR* _wcsdup(const WCHAR* strSource); 58 59 WINPR_API int _stricmp(const char* string1, const char* string2); 60 WINPR_API int _strnicmp(const char* string1, const char* string2, size_t count); 61 62 WINPR_API int _wcscmp(const WCHAR* string1, const WCHAR* string2); 63 WINPR_API int _wcsncmp(const WCHAR* string1, const WCHAR* string2, size_t count); 64 65 WINPR_API size_t _wcslen(const WCHAR* str); 66 WINPR_API size_t _wcsnlen(const WCHAR* str, size_t maxNumberOfElements); 67 68 WINPR_API WCHAR* _wcsstr(const WCHAR* str, const WCHAR* strSearch); 69 70 WINPR_API WCHAR* _wcschr(const WCHAR* str, WCHAR c); 71 WINPR_API WCHAR* _wcsrchr(const WCHAR* str, WCHAR c); 72 73 WINPR_API char* strtok_s(char* strToken, const char* strDelimit, char** context); 74 WINPR_API WCHAR* wcstok_s(WCHAR* strToken, const WCHAR* strDelimit, WCHAR** context); 75 76#else 77 78#define _wcscmp wcscmp 79#define _wcsncmp wcsncmp 80#define _wcslen wcslen 81#define _wcsnlen wcsnlen 82#define _wcsstr wcsstr 83#define _wcschr wcschr 84#define _wcsrchr wcsrchr 85 86#endif /* _WIN32 */ 87 88#if !defined(_WIN32) || defined(_UWP) 89 90 WINPR_API LPSTR CharUpperA(LPSTR lpsz); 91 WINPR_API LPWSTR CharUpperW(LPWSTR lpsz); 92 93#ifdef UNICODE 94#define CharUpper CharUpperW 95#else 96#define CharUpper CharUpperA 97#endif 98 99 WINPR_API DWORD CharUpperBuffA(LPSTR lpsz, DWORD cchLength); 100 WINPR_API DWORD CharUpperBuffW(LPWSTR lpsz, DWORD cchLength); 101 102#ifdef UNICODE 103#define CharUpperBuff CharUpperBuffW 104#else 105#define CharUpperBuff CharUpperBuffA 106#endif 107 108 WINPR_API LPSTR CharLowerA(LPSTR lpsz); 109 WINPR_API LPWSTR CharLowerW(LPWSTR lpsz); 110 111#ifdef UNICODE 112#define CharLower CharLowerW 113#else 114#define CharLower CharLowerA 115#endif 116 117 WINPR_API DWORD CharLowerBuffA(LPSTR lpsz, DWORD cchLength); 118 WINPR_API DWORD CharLowerBuffW(LPWSTR lpsz, DWORD cchLength); 119 120#ifdef UNICODE 121#define CharLowerBuff CharLowerBuffW 122#else 123#define CharLowerBuff CharLowerBuffA 124#endif 125 126 WINPR_API BOOL IsCharAlphaA(CHAR ch); 127 WINPR_API BOOL IsCharAlphaW(WCHAR ch); 128 129#ifdef UNICODE 130#define IsCharAlpha IsCharAlphaW 131#else 132#define IsCharAlpha IsCharAlphaA 133#endif 134 135 WINPR_API BOOL IsCharAlphaNumericA(CHAR ch); 136 WINPR_API BOOL IsCharAlphaNumericW(WCHAR ch); 137 138#ifdef UNICODE 139#define IsCharAlphaNumeric IsCharAlphaNumericW 140#else 141#define IsCharAlphaNumeric IsCharAlphaNumericA 142#endif 143 144 WINPR_API BOOL IsCharUpperA(CHAR ch); 145 WINPR_API BOOL IsCharUpperW(WCHAR ch); 146 147#ifdef UNICODE 148#define IsCharUpper IsCharUpperW 149#else 150#define IsCharUpper IsCharUpperA 151#endif 152 153 WINPR_API BOOL IsCharLowerA(CHAR ch); 154 WINPR_API BOOL IsCharLowerW(WCHAR ch); 155 156#ifdef UNICODE 157#define IsCharLower IsCharLowerW 158#else 159#define IsCharLower IsCharLowerA 160#endif 161 162 WINPR_API int lstrlenA(LPCSTR lpString); 163 WINPR_API int lstrlenW(LPCWSTR lpString); 164 165#ifdef UNICODE 166#define lstrlen lstrlenW 167#else 168#define lstrlen lstrlenA 169#endif 170 171 WINPR_API int lstrcmpA(LPCSTR lpString1, LPCSTR lpString2); 172 WINPR_API int lstrcmpW(LPCWSTR lpString1, LPCWSTR lpString2); 173 174#ifdef UNICODE 175#define lstrcmp lstrcmpW 176#else 177#define lstrcmp lstrcmpA 178#endif 179 180#endif 181 182#ifndef _WIN32 183 184#define sprintf_s snprintf 185#define _snprintf snprintf 186#define _scprintf(...) snprintf(NULL, 0, __VA_ARGS__) 187 188#define _scprintf(...) snprintf(NULL, 0, __VA_ARGS__) 189 190 /* Unicode Conversion */ 191 192 WINPR_API int MultiByteToWideChar(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr, 193 int cbMultiByte, LPWSTR lpWideCharStr, int cchWideChar); 194 195 WINPR_API int WideCharToMultiByte(UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr, 196 int cchWideChar, LPSTR lpMultiByteStr, int cbMultiByte, 197 LPCSTR lpDefaultChar, LPBOOL lpUsedDefaultChar); 198 199#endif 200 201 /* Extended API */ 202 203 WINPR_API int ConvertToUnicode(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr, 204 int cbMultiByte, LPWSTR* lpWideCharStr, int cchWideChar); 205 206 WINPR_API int ConvertFromUnicode(UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr, 207 int cchWideChar, LPSTR* lpMultiByteStr, int cbMultiByte, 208 LPCSTR lpDefaultChar, LPBOOL lpUsedDefaultChar); 209 210 WINPR_API void ByteSwapUnicode(WCHAR* wstr, int length); 211 212 WINPR_API int ConvertLineEndingToLF(char* str, int size); 213 WINPR_API char* ConvertLineEndingToCRLF(const char* str, int* size); 214 215 WINPR_API char* StrSep(char** stringp, const char* delim); 216 217 WINPR_API INT64 GetLine(char** lineptr, size_t* size, FILE* stream); 218 219#ifdef __cplusplus 220} 221#endif 222 223#endif /* WINPR_CRT_STRING_H */