-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
executable file
·189 lines (157 loc) · 5.18 KB
/
utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include "utils.h"
#include <sstream>
#include <iomanip>
using namespace std;
wstring systemErrorText(DWORD errorCode, DWORD langId)
{
HLOCAL pRes = 0;
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_IGNORE_INSERTS
| FORMAT_MESSAGE_FROM_SYSTEM, /* dwFlags */
NULL, /* lpSource */
errorCode, /* dwMessageId */
langId, /* dwLanguageId */
reinterpret_cast<LPTSTR>(&pRes), /* lpBuffer */
0, /* nSize */
0 /* Arguments */
) == 0)
{
wostringstream ss;
ss << L"[Could not find a description for error 0x"
<< hex << setiosflags(ios_base::uppercase) << errorCode << L"]";
return ss.str();
}
else
{
wstring res(reinterpret_cast<LPTSTR>(pRes));
LocalFree(pRes);
return res;
}
}
wstring COMErrorText(HRESULT hr)
{
if (HRESULT_FACILITY(hr) == FACILITY_WINDOWS)
hr = HRESULT_CODE(hr);
HLOCAL pRes = 0;
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_IGNORE_INSERTS
| FORMAT_MESSAGE_FROM_SYSTEM, /* dwFlags */
NULL, /* lpSource */
hr, /* dwMessageId */
0, /* dwLanguageId */
reinterpret_cast<LPTSTR>(&pRes), /* lpBuffer */
0, /* nSize */
0 /* Arguments */
) == 0)
{
wostringstream ss;
ss << L"[Could not find a description for error 0x"
<< hex << setiosflags(ios_base::uppercase) << hr << L"]";
return ss.str();
}
else
{
wstring res(reinterpret_cast<LPTSTR>(pRes));
LocalFree(pRes);
return res;
}
}
void quoteAsCommandComponent(wstring & arg)
{
/* Check if we need to add quotes. */
if (arg.find_first_of(L" \"") == wstring::npos)
return;
/* First we escape all double quotes. */
wstring::size_type p = 0;
while (true)
{
p = arg.find(L"\"", p);
if (p == wstring::npos)
break;
arg.replace(p, 1, L"\\\"");
++p;
}
/*
* Now quote a backslash if there is any at the end. Otherwise it will
* quote the closing quote we are going to add. Note that other backslashes
* are either already originally escaped or they have literal meaning.
*/
if (arg.length() > 0 && arg[arg.length() - 1] == L'\\')
arg += L'\\';
arg.insert(0, L"\"");
arg += L'"';
}
/*
* Creates all the missing directories, making it possible to create a file
* with the specified path and an arbitrary name.
*/
void ensurePathExists(const std::wstring & path)
throw(exceptions::systemError)
{
wstring p = path;
/* Replace all forward slashes with backslashes. */
for (wstring::size_type i = p.find(L'/');
i != wstring::npos;
i = p.find(L'/', i))
p.replace(i, 1, 1, L'\\');
/*
* Index of the next character after previous backslash or of the start
* of the path.
*/
wstring::size_type i = 0;
/* An index of the next backslash. */
wstring::size_type j = p.find(L'\\');
/* Skip the drive letter if any. */
if (j == 2 && p[1] == L':')
{
i = j;
j = p.find(L'\\', j + 1);
}
/* Quit if there is nothing to do. */
if (j == wstring::npos
&& i == p.size() - 1)
return;
/* Make sure all the path components exist. */
while (true)
{
/* Skip over sequences of backslashes, '.' and '..' entries. */
if (!(i == j
|| (i + 1 == j
&& p[i] != L'.')
|| (i + 2 == j
&& p[i] != L'.'
&& p[i + 1] != L'.'))
)
{
if (j != wstring::npos)
p[j] = L'\0';
BOOL res = CreateDirectoryW
(p.c_str() /* lpPathName */,
0 /* lpSecurityAttributes */
);
if (!res)
{
DWORD errorCode = GetLastError();
if (errorCode != ERROR_ALREADY_EXISTS)
throw exceptions::systemError(errorCode);
}
if (j != wstring::npos)
p[j] = L'\\';
}
if (j == wstring::npos)
break;
i = j + 1;
j = p.find(L'\\', i);
}
}
wstring getDirectory(const wstring & path) throw()
{
if (path.empty())
return path;
wstring::size_type i = path.find_last_of(L"\\/");
if (i == path.size() - 1)
return path;
if (i == wstring::npos)
return L"";
return path.substr(0, i);
}