forked from adzm/mssqlPipe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.h
More file actions
264 lines (214 loc) · 4.65 KB
/
util.h
File metadata and controls
264 lines (214 loc) · 4.65 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#pragma once
#include <string>
#include <algorithm>
#include <nowide/convert.hpp>
using nowide::widen;
using nowide::narrow;
inline std::string ToLower(std::string str)
{
std::transform(str.begin(), str.end(), str.begin(), towlower);
return str;
}
inline int icmp(const std::string& a, const std::string& b)
{
return _stricmp(a.c_str(), b.c_str());
}
inline int icmp(const char* a, const std::string& b)
{
return _stricmp(a, b.c_str());
}
inline int icmp(const std::string& a, const char* b)
{
return _stricmp(a.c_str(), b);
}
inline int icmp(const char* a, const char* b)
{
return _stricmp(a, b);
}
template<typename A, typename B>
bool iequals(A&& a, B&& b)
{
return 0 == icmp(std::forward<A>(a), std::forward<B>(b));
}
struct iless_predicate
{
template<typename A, typename B>
bool operator()(A&& a, B&& b) const
{
return icmp(std::forward<A>(a), std::forward<B>(b)) < 0;
}
};
inline std::ostream& operator<<(std::ostream& o, const _bstr_t& bstr)
{
const char* str = (const char*)bstr;
if (str) {
return o << str;
}
else {
return o;
}
}
/****/
inline std::string escape(std::string str)
{
size_t quoteCount = count(begin(str), end(str), '\'');
if (!quoteCount) {
return str;
}
std::string q;
q.reserve(str.size() + quoteCount);
for (auto c : str) {
q.push_back(c);
if (c == '\'') {
q.push_back('\'');
}
}
return q;
}
/****/
inline std::vector<std::string> make_argv(int argc, wchar_t** argv)
{
std::vector<std::string> args;
args.reserve(argc);
for (int i = 0; i < argc; ++i) {
args.push_back(narrow(argv[i]));
}
return args;
}
inline std::vector<std::string> make_argv(const char* cmdLine)
{
auto wcmdLine = widen(cmdLine);
int argc = 0;
wchar_t** argv = ::CommandLineToArgvW(wcmdLine.c_str(), &argc);
std::vector<std::string> args;
args.reserve(argc);
for (int i = 0; i < argc; ++i) {
args.push_back(narrow(argv[i]));
}
::LocalFree(argv);
return args;
}
inline std::vector<const char*> make_argv_ptrs(const std::vector<std::string>& args)
{
std::vector<const char*> argv;
argv.reserve(args.size());
for (auto&& arg : args) {
argv.push_back(arg.c_str());
}
return argv;
}
/****/
inline std::string make_guid()
{
wchar_t guid[39] = { 0 };
GUID guidRaw = { 0 };
::CoCreateGuid(&guidRaw);
::StringFromGUID2(guidRaw, guid, _countof(guid));
return narrow(guid, _countof(guid) - 1);
}
template<typename _IIID>
void ComIssueError(HRESULT hr, const _com_ptr_t<_IIID>& p)
{
_com_issue_errorex(hr, p.GetInterfacePtr(), p.GetIID());
}
template<typename T>
void ComIssueError(HRESULT hr, const T* p)
{
_com_issue_errorex(hr, p, __uuidof(p));
}
inline void ComIssueError(HRESULT hr)
{
_com_issue_error(hr);
}
template<typename _IIID>
void ComEnsure(HRESULT hr, const _com_ptr_t<_IIID>& p)
{
if (FAILED(hr)) {
ComIssueError(hr, p);
}
}
template<typename T>
void ComEnsure(HRESULT hr, const T* p)
{
if (FAILED(hr)) {
ComIssueError(hr, p);
}
}
inline void ComEnsure(HRESULT hr)
{
if (FAILED(hr)) {
ComIssueError(hr);
}
}
/****/
struct CoInit
{
CoInit(DWORD dwCoInit = COINIT_MULTITHREADED)
{
::CoInitializeEx(NULL, dwCoInit);
}
~CoInit()
{
::CoUninitialize();
}
};
/****/
template <typename char_type,
typename traits = std::char_traits<char_type> >
class basic_teebuf :
public std::basic_streambuf<char_type, traits>
{
public:
typedef typename traits::int_type int_type;
basic_teebuf(std::basic_streambuf<char_type, traits>* sb1,
std::basic_streambuf<char_type, traits>* sb2)
: sb1(sb1)
, sb2(sb2)
{
}
private:
virtual int sync() override
{
int const r1 = sb1->pubsync();
int const r2 = sb2->pubsync();
return r1 == 0 && r2 == 0 ? 0 : -1;
}
virtual int_type overflow(int_type c) override
{
int_type const eof = traits::eof();
if (traits::eq_int_type(c, eof))
{
return traits::not_eof(c);
}
else
{
char_type const ch = traits::to_char_type(c);
int_type const r1 = sb1->sputc(ch);
int_type const r2 = sb2->sputc(ch);
return
traits::eq_int_type(r1, eof) ||
traits::eq_int_type(r2, eof) ? eof : c;
}
}
private:
std::basic_streambuf<char_type, traits>* sb1;
std::basic_streambuf<char_type, traits>* sb2;
};
typedef basic_teebuf<char> teebuf;
typedef basic_teebuf<char> wteebuf;
template <typename char_type,
typename traits = std::char_traits<char_type> >
class basic_teestream : public std::basic_ostream<char_type, traits>
{
public:
basic_teestream(std::basic_ostream<char_type, traits>& o1, std::basic_ostream<char_type, traits>& o2)
: std::basic_ostream<char_type, traits>(&tbuf)
, tbuf(o1.rdbuf(), o2.rdbuf())
{
}
protected:
basic_teebuf<char_type, traits> tbuf;
};
typedef basic_teestream<char> teestream;
typedef basic_teestream<char> wteestream;
/****/