ReactOS 0.4.15-dev-7958-gcd0bb1a
tools.cpp
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Automatic Testing Utility
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: Various helper functions
5 * COPYRIGHT: Copyright 2008-2015 Colin Finck (colin@reactos.org)
6 */
7
8#include "precomp.h"
9
10#define DBGPRINT_BUFSIZE 511
11static const char HexCharacters[] = "0123456789ABCDEF";
12
23string
24EscapeString(const char* Input)
25{
26 string ReturnedString;
27
28 do
29 {
30 if(strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~", *Input))
31 {
32 /* It's a character we don't need to escape, just add it to the output string */
33 ReturnedString += *Input;
34 }
35 else
36 {
37 /* We need to escape this character */
38 ReturnedString += '%';
39 ReturnedString += HexCharacters[((UCHAR)*Input >> 4) % 16];
40 ReturnedString += HexCharacters[(UCHAR)*Input % 16];
41 }
42 }
43 while(*++Input);
44
45 return ReturnedString;
46}
47
58string
59EscapeString(const string& Input)
60{
61 return EscapeString(Input.c_str());
62}
63
73bool
74IsNumber(const char* Input)
75{
76 do
77 {
78 if(!isdigit(*Input))
79 return false;
80
81 ++Input;
82 }
83 while(*Input);
84
85 return true;
86}
87
95string
96StringOut(const string& String, bool forcePrint)
97{
98 char DbgString[DBGPRINT_BUFSIZE + 1];
99 size_t i, start = 0, last_newline = 0, size = 0, curr_pos = 0;
100 string NewString;
101
102 /* Unify the line endings (the piped output of the tests may use CRLF) */
103 for(i = 0; i < String.size(); i++)
104 {
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')
107 {
108 NewString += '\n';
109 ++i;
110 }
111 else
112 {
113 /* Otherwise copy the string */
114 NewString += String[i];
115 }
116
117 curr_pos = NewString.size();
118
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)
121 {
122 if((curr_pos - start) >= DBGPRINT_BUFSIZE)
123 {
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'))
126 {
127 size = curr_pos - start;
128 memcpy(DbgString, NewString.c_str() + start, size);
129 start = curr_pos;
130 }
131 else
132 {
133 size = last_newline - start;
134 memcpy(DbgString, NewString.c_str() + start, size);
135 start = last_newline;
136 }
137
138 DbgString[size] = 0;
139 DbgPrint("%s", DbgString);
140 }
141
142 last_newline = curr_pos;
143 }
144 }
145
146 size = curr_pos - start;
147
148 /* Only print if forced to or if the rest is a whole line */
149 if(forcePrint == true || NewString[curr_pos - 1] == '\n')
150 {
151 /* Output the whole string */
152 if(Configuration.DoPrint())
153 cout << NewString << flush;
154
155 memcpy(DbgString, NewString.c_str() + start, size);
156 DbgString[size] = 0;
157 DbgPrint("%s", DbgString);
158
159 NewString.clear();
160 return NewString;
161 }
162
163 /* Output full lines only */
164 if(Configuration.DoPrint())
165 cout << NewString.substr(0, start) << flush;
166
167 /* Return the remaining chunk */
168 return NewString.substr(start, size);
169}
170
186string
188{
190 PCHAR AsciiBuffer;
191 string ReturnedString;
192 WCHAR Buffer[2048];
193
194 /* Load the value into a temporary Unicode buffer */
196
197 if(Length)
198 {
199 /* Convert the string to ASCII charset */
200 AsciiBuffer = new char[Length + 1];
201 WideCharToMultiByte(CP_ACP, 0, Buffer, Length + 1, AsciiBuffer, Length + 1, NULL, NULL);
202
203 ReturnedString = AsciiBuffer;
204 delete[] AsciiBuffer;
205 }
206
207 return ReturnedString;
208}
209
219wstring
220AsciiToUnicode(const char* AsciiString)
221{
224 wstring ReturnString;
225
226 Length = MultiByteToWideChar(CP_ACP, 0, AsciiString, -1, NULL, 0);
227
229 MultiByteToWideChar(CP_ACP, 0, AsciiString, -1, UnicodeString, Length);
230 ReturnString = UnicodeString;
231 delete UnicodeString;
232
233 return ReturnString;
234}
235
245wstring
246AsciiToUnicode(const string& AsciiString)
247{
248 return AsciiToUnicode(AsciiString.c_str());
249}
250
260string
262{
264 PCHAR AsciiString;
265 string ReturnString;
266
268
269 AsciiString = new char[Length];
270 WideCharToMultiByte(CP_ACP, 0, UnicodeString, -1, AsciiString, Length, NULL, NULL);
271 ReturnString = AsciiString;
272 delete AsciiString;
273
274 return ReturnString;
275}
276
286string
288{
289 return UnicodeToAscii(UnicodeString.c_str());
290}
#define isdigit(c)
Definition: acclib.h:68
char * strchr(const char *String, int ch)
Definition: utclib.c:501
Definition: bufpool.h:45
void clear()
Definition: _string.h:421
_Self substr(size_type __pos=0, size_type __n=npos) const
Definition: _string.h:1022
const _CharT * c_str() const
Definition: _string.h:949
size_type size() const
Definition: _string.h:400
static CHAR AppName[MAX_PATH]
Definition: dem.c:252
#define NULL
Definition: types.h:112
#define CP_ACP
Definition: compat.h:109
#define WideCharToMultiByte
Definition: compat.h:111
#define MultiByteToWideChar
Definition: compat.h:110
INT WINAPI GetPrivateProfileStringW(LPCWSTR section, LPCWSTR entry, LPCWSTR def_val, LPWSTR buffer, UINT len, LPCWSTR filename)
Definition: profile.c:1142
unsigned long DWORD
Definition: ntddk_ex.h:95
GLuint start
Definition: gl.h:1545
GLsizeiptr size
Definition: glext.h:5919
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define DbgPrint
Definition: hal.h:12
#define cout
Definition: iostream.cpp:38
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
static const char HexCharacters[]
Definition: tools.cpp:11
string StringOut(const string &String, bool forcePrint)
Definition: tools.cpp:96
string GetINIValue(PCWCH AppName, PCWCH KeyName, PCWCH FileName)
Definition: tools.cpp:187
string UnicodeToAscii(PCWSTR UnicodeString)
Definition: tools.cpp:261
bool IsNumber(const char *Input)
Definition: tools.cpp:74
string EscapeString(const char *Input)
Definition: tools.cpp:24
wstring AsciiToUnicode(const char *AsciiString)
Definition: tools.cpp:220
#define DBGPRINT_BUFSIZE
Definition: tools.cpp:10
CONST WCHAR * PCWCH
Definition: ntbasedef.h:411
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
@ Input
Definition: arc.h:84
int flush
Definition: zlib.h:309
uint16_t * PWSTR
Definition: typedefs.h:56
const uint16_t * PCWSTR
Definition: typedefs.h:57
char * PCHAR
Definition: typedefs.h:51
_Must_inspect_result_ _In_ WDFDEVICE _In_ PCUNICODE_STRING KeyName
Definition: wdfdevice.h:2699
_Must_inspect_result_ _In_ WDFDEVICE _In_ WDFSTRING String
Definition: wdfdevice.h:2433
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_INTERRUPT_CONFIG Configuration
Definition: wdfinterrupt.h:374
unsigned char UCHAR
Definition: xmlstorage.h:181
__wchar_t WCHAR
Definition: xmlstorage.h:180