ReactOS 0.4.15-dev-8222-g9164419
stringutils.c
Go to the documentation of this file.
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//
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
34{
35 LPTSTR lpszString;
37
39 lpszString = FormatStringV(str, args);
40 va_end(args);
41
42 return lpszString;
43}
44
45//
46// String handling (ANSI <-> Unicode UTF16)
47//
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
63 WC_COMPOSITECHECK /* | WC_NO_BEST_FIT_CHARS */,
64 strW, -1, strA, iNeededChars, NULL, NULL);
65
66 return strA;
67}
68
70{
72 int iNeededChars;
73
74 if (!strA) return NULL;
75
76 iNeededChars = MultiByteToWideChar(CP_ACP,
78 strA, -1, NULL, 0);
79
80 strW = (LPWSTR)MemAlloc(0, iNeededChars * sizeof(WCHAR));
81 if (!strW) return NULL;
82
85 strA, -1, strW, iNeededChars);
86
87 return strW;
88}
89
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
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
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
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*******************************************************************************/
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 */
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}
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
char * va_list
Definition: acmsvcex.h:78
#define va_end(ap)
Definition: acmsvcex.h:90
#define va_start(ap, A)
Definition: acmsvcex.h:91
PVOID MemAlloc(IN DWORD dwFlags, IN SIZE_T dwBytes)
Definition: utils.c:33
#define CHAR(Char)
#define NULL
Definition: types.h:112
#define CP_ACP
Definition: compat.h:109
#define MAX_PATH
Definition: compat.h:34
#define WideCharToMultiByte
Definition: compat.h:111
#define MultiByteToWideChar
Definition: compat.h:110
#define WC_COMPOSITECHECK
Definition: unicode.h:43
#define _totupper
Definition: tchar.h:1509
#define _vsctprintf
Definition: tchar.h:544
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
POINT cp
Definition: magnifier.c:59
struct S1 s1
struct S2 s2
WCHAR strW[12]
Definition: clipboard.c:2029
char strA[12]
Definition: clipboard.c:2028
#define min(a, b)
Definition: monoChain.cc:55
#define L(x)
Definition: ntvdm.h:50
const WCHAR * str
#define args
Definition: format.c:66
LPTSTR AppendPathSeparator(LPTSTR lpszPath)
Definition: stringutils.c:220
LPTSTR FormatString(LPCTSTR str,...)
Definition: stringutils.c:33
LPWSTR DuplicateStringWEx(LPCWSTR str, size_t numOfChars)
Definition: stringutils.c:142
LPWSTR AnsiToUnicode(LPCSTR strA)
Definition: stringutils.c:69
LPTSTR FormatStringV(LPCTSTR str, va_list args)
Definition: stringutils.c:16
LPWSTR DuplicateStringW(LPCWSTR str)
Definition: stringutils.c:107
LPSTR DuplicateStringAEx(LPCSTR str, size_t numOfChars)
Definition: stringutils.c:124
LPSTR DuplicateStringA(LPCSTR str)
Definition: stringutils.c:90
LPSTR UnicodeToAnsi(LPCWSTR strW)
Definition: stringutils.c:48
LPTSTR FindSubStrI(LPCTSTR str, LPCTSTR strSearch)
Definition: stringutils.c:183
#define StringCchVPrintf
Definition: strsafe.h:482
STRSAFEAPI StringCchCopyNA(STRSAFE_LPSTR pszDest, size_t cchDest, STRSAFE_LPCSTR pszSrc, size_t cchToCopy)
Definition: strsafe.h:230
STRSAFEAPI StringCchCopyW(STRSAFE_LPWSTR pszDest, size_t cchDest, STRSAFE_LPCWSTR pszSrc)
Definition: strsafe.h:149
STRSAFEAPI StringCchCopyA(STRSAFE_LPSTR pszDest, size_t cchDest, STRSAFE_LPCSTR pszSrc)
Definition: strsafe.h:145
STRSAFEAPI StringCchCopyNW(STRSAFE_LPWSTR pszDest, size_t cchDest, STRSAFE_LPCWSTR pszSrc, size_t cchToCopy)
Definition: strsafe.h:236
Definition: match.c:390
#define _T(x)
Definition: vfdio.h:22
#define MB_PRECOMPOSED
Definition: winnls.h:281
char TCHAR
Definition: xmlstorage.h:189
const char * LPCSTR
Definition: xmlstorage.h:183
char * LPSTR
Definition: xmlstorage.h:182
__wchar_t WCHAR
Definition: xmlstorage.h:180
WCHAR * LPWSTR
Definition: xmlstorage.h:184
const CHAR * LPCTSTR
Definition: xmlstorage.h:193
CHAR * LPTSTR
Definition: xmlstorage.h:192
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
#define _tcslen
Definition: xmlstorage.h:198
char CHAR
Definition: xmlstorage.h:175