0a58850af218d9ddabc61fb26ae6ef12957a9be0
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-2009 Colin Finck <colin@reactos.org>
10 #define DBGPRINT_BUFSIZE 511
11 static const char HexCharacters
[] = "0123456789ABCDEF";
14 * Escapes a string according to RFC 1738.
15 * Required for passing parameters to the web service.
18 * Constant pointer to a char array, which contains the input buffer to escape.
21 * The escaped string as std::string.
24 EscapeString(const char* Input
)
26 string ReturnedString
;
30 if(strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~", *Input
))
32 /* It's a character we don't need to escape, just add it to the output string */
33 ReturnedString
+= *Input
;
37 /* We need to escape this character */
38 ReturnedString
+= '%';
39 ReturnedString
+= HexCharacters
[((UCHAR
)*Input
>> 4) % 16];
40 ReturnedString
+= HexCharacters
[(UCHAR
)*Input
% 16];
45 return ReturnedString
;
49 * Escapes a string according to RFC 1738.
50 * Required for passing parameters to the web service.
53 * Pointer to a std::string, which contains the input buffer to escape.
56 * The escaped string as std::string.
59 EscapeString(const string
& Input
)
61 return EscapeString(Input
.c_str());
65 * Determines whether a string contains entirely numeric values.
68 * Constant pointer to a char array containing the input to check.
71 * true if the string is entirely numeric, false otherwise.
74 IsNumber(const char* Input
)
89 * Outputs a string through the standard output and the debug output.
90 * The string may have LF or CRLF line endings.
93 * The std::string to output
96 StringOut(const string
& String
, bool forcePrint
)
98 char DbgString
[DBGPRINT_BUFSIZE
+ 1];
99 size_t i
, start
= 0, last_newline
= 0, size
= 0, curr_pos
= 0;
102 /* Unify the line endings (the piped output of the tests may use CRLF) */
103 for(i
= 0; i
< String
.size(); i
++)
105 /* If this is a CRLF line-ending, only copy a \n to the new string and skip the next character */
106 if(String
[i
] == '\r' && String
[i
+ 1] == '\n')
113 /* Otherwise copy the string */
114 NewString
+= String
[i
];
117 curr_pos
= NewString
.size();
119 /* Try to print whole lines but obey the 512 bytes chunk size limit*/
120 if(NewString
[curr_pos
- 1] == '\n' || (curr_pos
- start
) == DBGPRINT_BUFSIZE
)
122 if((curr_pos
- start
) >= DBGPRINT_BUFSIZE
)
124 /* No newlines so far, or the string just fits */
125 if(last_newline
<= start
|| ((curr_pos
- start
== DBGPRINT_BUFSIZE
) && NewString
[curr_pos
- 1] == '\n'))
127 size
= curr_pos
- start
;
128 memcpy(DbgString
, NewString
.c_str() + start
, size
);
133 size
= last_newline
- start
;
134 memcpy(DbgString
, NewString
.c_str() + start
, size
);
135 start
= last_newline
;
142 last_newline
= curr_pos
;
146 /* Output the string */
149 size
= curr_pos
- start
;
151 /* Only print if forced to or if the rest is a whole line */
152 if(forcePrint
== true || NewString
[curr_pos
- 1] == '\n')
154 memcpy(DbgString
, NewString
.c_str() + start
, size
);
162 /* Return the remaining chunk */
163 return NewString
.substr(start
, size
);
167 * Gets a value from a specified INI file and returns it converted to ASCII.
170 * Constant pointer to a WCHAR array with the INI section to look in (lpAppName parameter passed to GetPrivateProfileStringW)
173 * Constant pointer to a WCHAR array containing the key to look for in the specified section (lpKeyName parameter passed to GetPrivateProfileStringW)
176 * Constant pointer to a WCHAR array containing the path to the INI file
179 * Returns the data of the value as std::string or an empty string if no data could be retrieved.
182 GetINIValue(PCWCH AppName
, PCWCH KeyName
, PCWCH FileName
)
186 string ReturnedString
;
189 /* Load the value into a temporary Unicode buffer */
190 Length
= GetPrivateProfileStringW(AppName
, KeyName
, NULL
, Buffer
, sizeof(Buffer
) / sizeof(WCHAR
), FileName
);
194 /* Convert the string to ASCII charset */
195 AsciiBuffer
= new char[Length
+ 1];
196 WideCharToMultiByte(CP_ACP
, 0, Buffer
, Length
+ 1, AsciiBuffer
, Length
+ 1, NULL
, NULL
);
198 ReturnedString
= AsciiBuffer
;
202 return ReturnedString
;
206 * Converts an ASCII string to a Unicode one.
209 * Constant pointer to a char array containing the ASCII string
212 * The Unicode string as std::wstring
215 AsciiToUnicode(const char* AsciiString
)
219 wstring ReturnString
;
221 Length
= MultiByteToWideChar(CP_ACP
, 0, AsciiString
, -1, NULL
, 0);
223 UnicodeString
= new WCHAR
[Length
];
224 MultiByteToWideChar(CP_ACP
, 0, AsciiString
, -1, UnicodeString
, Length
);
225 ReturnString
= UnicodeString
;
226 delete UnicodeString
;
232 * Converts an ASCII string to a Unicode one.
235 * Pointer to a std::string containing the ASCII string
238 * The Unicode string as std::wstring
241 AsciiToUnicode(const string
& AsciiString
)
243 return AsciiToUnicode(AsciiString
.c_str());
247 * Converts a Unicode string to an ASCII one.
249 * @param UnicodeString
250 * Constant pointer to a WCHAR array containing the Unicode string
253 * The ASCII string as std::string
256 UnicodeToAscii(PCWSTR UnicodeString
)
262 Length
= WideCharToMultiByte(CP_ACP
, 0, UnicodeString
, -1, NULL
, 0, NULL
, NULL
);
264 AsciiString
= new char[Length
];
265 WideCharToMultiByte(CP_ACP
, 0, UnicodeString
, -1, AsciiString
, Length
, NULL
, NULL
);
266 ReturnString
= AsciiString
;
273 * Converts a Unicode string to an ASCII one.
275 * @param UnicodeString
276 * Pointer to a std::wstring containing the Unicode string
279 * The ASCII string as std::string
282 UnicodeToAscii(const wstring
& UnicodeString
)
284 return UnicodeToAscii(UnicodeString
.c_str());