[PSDK]
[reactos.git] / rostests / rosautotest / tools.cpp
1 /*
2 * PROJECT: ReactOS Automatic Testing Utility
3 * LICENSE: GNU GPLv2 or any later version as published by the Free Software Foundation
4 * PURPOSE: Various helper functions
5 * COPYRIGHT: Copyright 2008-2015 Colin Finck <colin@reactos.org>
6 */
7
8 #include "precomp.h"
9
10 static const char HexCharacters[] = "0123456789ABCDEF";
11
12 /**
13 * Escapes a string according to RFC 1738.
14 * Required for passing parameters to the web service.
15 *
16 * @param Input
17 * Constant pointer to a char array, which contains the input buffer to escape.
18 *
19 * @return
20 * The escaped string as std::string.
21 */
22 string
23 EscapeString(const char* Input)
24 {
25 string ReturnedString;
26
27 do
28 {
29 if(strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~", *Input))
30 {
31 /* It's a character we don't need to escape, just add it to the output string */
32 ReturnedString += *Input;
33 }
34 else
35 {
36 /* We need to escape this character */
37 ReturnedString += '%';
38 ReturnedString += HexCharacters[((UCHAR)*Input >> 4) % 16];
39 ReturnedString += HexCharacters[(UCHAR)*Input % 16];
40 }
41 }
42 while(*++Input);
43
44 return ReturnedString;
45 }
46
47 /**
48 * Escapes a string according to RFC 1738.
49 * Required for passing parameters to the web service.
50 *
51 * @param Input
52 * Pointer to a std::string, which contains the input buffer to escape.
53 *
54 * @return
55 * The escaped string as std::string.
56 */
57 string
58 EscapeString(const string& Input)
59 {
60 return EscapeString(Input.c_str());
61 }
62
63 /**
64 * Determines whether a string contains entirely numeric values.
65 *
66 * @param Input
67 * Constant pointer to a char array containing the input to check.
68 *
69 * @return
70 * true if the string is entirely numeric, false otherwise.
71 */
72 bool
73 IsNumber(const char* Input)
74 {
75 do
76 {
77 if(!isdigit(*Input))
78 return false;
79
80 ++Input;
81 }
82 while(*Input);
83
84 return true;
85 }
86
87 /**
88 * Outputs a string through the standard output and the debug output.
89 * The input string may have LF or CRLF line endings.
90 *
91 * @param InputString
92 * The std::string to output
93 */
94 void
95 StringOut(const string& InputString)
96 {
97 const char* pInput = InputString.c_str();
98 char* OutputString = new char[InputString.size() + 1];
99 char* pOutput = OutputString;
100
101 /* Unify the line endings (the piped output of the tests may use CRLF) */
102 while (*pInput)
103 {
104 /* If this is a CRLF line-ending, only copy a \n to the new string and skip the next character */
105 if (*pInput == '\r' && *(pInput + 1) == '\n')
106 {
107 *pOutput = '\n';
108 ++pInput;
109 }
110 else
111 {
112 *pOutput = *pInput;
113 }
114
115 ++pInput;
116 ++pOutput;
117 }
118
119 *pOutput = 0;
120 OutputDebugStringA(OutputString);
121
122 if (Configuration.DoPrint())
123 cout << OutputString << flush;
124
125 delete[] OutputString;
126 }
127
128 /**
129 * Gets a value from a specified INI file and returns it converted to ASCII.
130 *
131 * @param AppName
132 * Constant pointer to a WCHAR array with the INI section to look in (lpAppName parameter passed to GetPrivateProfileStringW)
133 *
134 * @param KeyName
135 * Constant pointer to a WCHAR array containing the key to look for in the specified section (lpKeyName parameter passed to GetPrivateProfileStringW)
136 *
137 * @param FileName
138 * Constant pointer to a WCHAR array containing the path to the INI file
139 *
140 * @return
141 * Returns the data of the value as std::string or an empty string if no data could be retrieved.
142 */
143 string
144 GetINIValue(PCWCH AppName, PCWCH KeyName, PCWCH FileName)
145 {
146 DWORD Length;
147 PCHAR AsciiBuffer;
148 string ReturnedString;
149 WCHAR Buffer[2048];
150
151 /* Load the value into a temporary Unicode buffer */
152 Length = GetPrivateProfileStringW(AppName, KeyName, NULL, Buffer, sizeof(Buffer) / sizeof(WCHAR), FileName);
153
154 if(Length)
155 {
156 /* Convert the string to ASCII charset */
157 AsciiBuffer = new char[Length + 1];
158 WideCharToMultiByte(CP_ACP, 0, Buffer, Length + 1, AsciiBuffer, Length + 1, NULL, NULL);
159
160 ReturnedString = AsciiBuffer;
161 delete[] AsciiBuffer;
162 }
163
164 return ReturnedString;
165 }
166
167 /**
168 * Converts an ASCII string to a Unicode one.
169 *
170 * @param AsciiString
171 * Constant pointer to a char array containing the ASCII string
172 *
173 * @return
174 * The Unicode string as std::wstring
175 */
176 wstring
177 AsciiToUnicode(const char* AsciiString)
178 {
179 DWORD Length;
180 PWSTR UnicodeString;
181 wstring ReturnString;
182
183 Length = MultiByteToWideChar(CP_ACP, 0, AsciiString, -1, NULL, 0);
184
185 UnicodeString = new WCHAR[Length];
186 MultiByteToWideChar(CP_ACP, 0, AsciiString, -1, UnicodeString, Length);
187 ReturnString = UnicodeString;
188 delete UnicodeString;
189
190 return ReturnString;
191 }
192
193 /**
194 * Converts an ASCII string to a Unicode one.
195 *
196 * @param AsciiString
197 * Pointer to a std::string containing the ASCII string
198 *
199 * @return
200 * The Unicode string as std::wstring
201 */
202 wstring
203 AsciiToUnicode(const string& AsciiString)
204 {
205 return AsciiToUnicode(AsciiString.c_str());
206 }
207
208 /**
209 * Converts a Unicode string to an ASCII one.
210 *
211 * @param UnicodeString
212 * Constant pointer to a WCHAR array containing the Unicode string
213 *
214 * @return
215 * The ASCII string as std::string
216 */
217 string
218 UnicodeToAscii(PCWSTR UnicodeString)
219 {
220 DWORD Length;
221 PCHAR AsciiString;
222 string ReturnString;
223
224 Length = WideCharToMultiByte(CP_ACP, 0, UnicodeString, -1, NULL, 0, NULL, NULL);
225
226 AsciiString = new char[Length];
227 WideCharToMultiByte(CP_ACP, 0, UnicodeString, -1, AsciiString, Length, NULL, NULL);
228 ReturnString = AsciiString;
229 delete AsciiString;
230
231 return ReturnString;
232 }
233
234 /**
235 * Converts a Unicode string to an ASCII one.
236 *
237 * @param UnicodeString
238 * Pointer to a std::wstring containing the Unicode string
239 *
240 * @return
241 * The ASCII string as std::string
242 */
243 string
244 UnicodeToAscii(const wstring& UnicodeString)
245 {
246 return UnicodeToAscii(UnicodeString.c_str());
247 }