Reactos
at master 238 lines 5.2 kB view raw
1/* 2 * PROJECT: ReactOS Applications 3 * LICENSE: LGPL - See COPYING in the top level directory 4 * FILE: base/applications/msconfig_new/stringutils.c 5 * PURPOSE: ANSI & UNICODE String Utility Functions 6 * COPYRIGHT: Copyright 2011-2012 Hermes BELUSCA - MAITO <hermes.belusca@sfr.fr> 7 */ 8 9#include "precomp.h" 10#include "utils.h" 11#include "stringutils.h" 12 13// 14// String formatting 15// 16LPTSTR FormatStringV(LPCTSTR str, va_list args) 17{ 18 LPTSTR lpszString; 19 size_t strLenPlusNull; 20 21 if (!str) return NULL; 22 23 strLenPlusNull = _vsctprintf(str, args) + 1; 24 25 lpszString = (LPTSTR)MemAlloc(0, strLenPlusNull * sizeof(TCHAR)); 26 if (!lpszString) return NULL; 27 28 StringCchVPrintf(lpszString, strLenPlusNull, str, args); 29 30 return lpszString; 31} 32 33LPTSTR FormatString(LPCTSTR str, ...) 34{ 35 LPTSTR lpszString; 36 va_list args; 37 38 va_start(args, str); 39 lpszString = FormatStringV(str, args); 40 va_end(args); 41 42 return lpszString; 43} 44 45// 46// String handling (ANSI <-> Unicode UTF16) 47// 48LPSTR UnicodeToAnsi(LPCWSTR strW) 49{ 50 LPSTR strA; 51 int iNeededChars; 52 53 if (!strW) return NULL; 54 55 iNeededChars = WideCharToMultiByte(CP_ACP, 56 WC_COMPOSITECHECK /* | WC_NO_BEST_FIT_CHARS */, 57 strW, -1, NULL, 0, NULL, NULL); 58 59 strA = (LPSTR)MemAlloc(0, iNeededChars * sizeof(CHAR)); 60 if (!strA) return NULL; 61 62 WideCharToMultiByte(CP_ACP, 63 WC_COMPOSITECHECK /* | WC_NO_BEST_FIT_CHARS */, 64 strW, -1, strA, iNeededChars, NULL, NULL); 65 66 return strA; 67} 68 69LPWSTR AnsiToUnicode(LPCSTR strA) 70{ 71 LPWSTR strW; 72 int iNeededChars; 73 74 if (!strA) return NULL; 75 76 iNeededChars = MultiByteToWideChar(CP_ACP, 77 MB_PRECOMPOSED, 78 strA, -1, NULL, 0); 79 80 strW = (LPWSTR)MemAlloc(0, iNeededChars * sizeof(WCHAR)); 81 if (!strW) return NULL; 82 83 MultiByteToWideChar(CP_ACP, 84 MB_PRECOMPOSED, 85 strA, -1, strW, iNeededChars); 86 87 return strW; 88} 89 90LPSTR DuplicateStringA(LPCSTR str) 91{ 92 LPSTR dupStr; 93 size_t strSizePlusNull; 94 95 if (!str) return NULL; 96 97 strSizePlusNull = strlen(str) + 1; 98 99 dupStr = (LPSTR)MemAlloc(0, strSizePlusNull * sizeof(CHAR)); 100 if (!dupStr) return NULL; 101 102 StringCchCopyA(dupStr, strSizePlusNull, str); 103 104 return dupStr; 105} 106 107LPWSTR DuplicateStringW(LPCWSTR str) 108{ 109 LPWSTR dupStr; 110 size_t strSizePlusNull; 111 112 if (!str) return NULL; 113 114 strSizePlusNull = wcslen(str) + 1; 115 116 dupStr = (LPWSTR)MemAlloc(0, strSizePlusNull * sizeof(WCHAR)); 117 if (!dupStr) return NULL; 118 119 StringCchCopyW(dupStr, strSizePlusNull, str); 120 121 return dupStr; 122} 123 124LPSTR DuplicateStringAEx(LPCSTR str, size_t numOfChars) 125{ 126 LPSTR dupStr; 127 size_t strSize; 128 129 if (!str) return NULL; 130 131 strSize = min(strlen(str), numOfChars); 132 133 dupStr = (LPSTR)MemAlloc(0, (strSize + 1) * sizeof(CHAR)); 134 if (!dupStr) return NULL; 135 136 StringCchCopyNA(dupStr, strSize + 1, str, strSize); 137 dupStr[strSize] = '\0'; 138 139 return dupStr; 140} 141 142LPWSTR DuplicateStringWEx(LPCWSTR str, size_t numOfChars) 143{ 144 LPWSTR dupStr; 145 size_t strSize; 146 147 if (!str) return NULL; 148 149 strSize = min(wcslen(str), numOfChars); 150 151 dupStr = (LPWSTR)MemAlloc(0, (strSize + 1) * sizeof(WCHAR)); 152 if (!dupStr) return NULL; 153 154 StringCchCopyNW(dupStr, strSize + 1, str, strSize); 155 dupStr[strSize] = L'\0'; 156 157 return dupStr; 158} 159 160// 161// String search functions 162// 163/*** 164*wchar_t *wcsstr(string1, string2) - search for string2 in string1 165* (wide strings) 166* 167*Purpose: 168* finds the first occurrence of string2 in string1 (wide strings) 169* 170*Entry: 171* wchar_t *string1 - string to search in 172* wchar_t *string2 - string to search for 173* 174*Exit: 175* returns a pointer to the first occurrence of string2 in 176* string1, or NULL if string2 does not occur in string1 177* 178*Uses: 179* 180*Exceptions: 181* 182*******************************************************************************/ 183LPTSTR FindSubStrI(LPCTSTR str, LPCTSTR strSearch) 184{ 185 LPTSTR cp = (LPTSTR)str; 186 LPTSTR s1, s2; 187 188 if (!*strSearch) 189 return (LPTSTR)str; 190 191 while (*cp) 192 { 193 s1 = cp; 194 s2 = (LPTSTR)strSearch; 195 196 while (*s1 && *s2 && (_totupper(*s1) == _totupper(*s2))) 197 ++s1, ++s2; 198 199 if (!*s2) 200 return cp; 201 202 ++cp; 203 } 204 205 return NULL; 206} 207 208/************************************************************************* 209 * AppendPathSeparator 210 * 211 * Append a backslash ('\') to a path if one doesn't exist. 212 * 213 * PARAMS 214 * lpszPath [I/O] The path to append a backslash to. 215 * 216 * RETURNS 217 * Success: The position of the last backslash in the path. 218 * Failure: NULL, if lpszPath is NULL or the path is too large. 219 */ 220LPTSTR AppendPathSeparator(LPTSTR lpszPath) 221{ 222 size_t iLen = 0; 223 224 if (!lpszPath || (iLen = _tcslen(lpszPath)) >= MAX_PATH) 225 return NULL; 226 227 if (iLen >= 1) 228 { 229 lpszPath += iLen - 1; 230 if (*lpszPath++ != _T('\\')) 231 { 232 *lpszPath++ = _T('\\'); 233 *lpszPath = _T('\0'); 234 } 235 } 236 237 return lpszPath; 238}