/
util.cpp
125 lines (102 loc) · 3.17 KB
/
util.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
#include "stdafx.h"
#include "util.h"
//convert wide string(WCHAR*)to multibytestring(char*)
errno_t util::wide2char(LPWSTR wstr, char* cstr) {
size_t wLen = 0;
errno_t err = 0;
//setlocale(LC_ALL, "japanese");
err = wcstombs_s(&wLen, cstr, 255, wstr, _TRUNCATE);
return err;
}
//convert multibytestring(char*) to wide string(WCHAR*)
errno_t util::char2wide(const char* cstr, LPWSTR wstr) {
size_t wLen = 0;
errno_t err = 0;
//setlocale(LC_ALL, "japanese");
err = mbstowcs_s(&wLen, wstr, 255, cstr, _TRUNCATE);
return err;
}
std::string util::removeStrRegex(std::string& str, const char* pattern) {
std::regex re(pattern);
std::string ret = std::regex_replace(str, re, "");
return ret;
}
void util::strReplace(std::string& str, const std::string& from, const std::string& to) {
std::string::size_type pos = 0;
while (pos = str.find(from, pos), pos != std::string::npos) {
str.replace(pos, from.length(), to);
pos += to.length();
}
}
std::string util::trim(const std::string& string, const char* trimCharacterList)
{
std::string result;
std::string::size_type left = string.find_first_not_of(trimCharacterList);
if (left != std::string::npos)
{
std::string::size_type right = string.find_last_not_of(trimCharacterList);
result = string.substr(left, right - left + 1);
}
return result;
}
void util::removeChars(std::string& str, const char* chars) {
for (size_t c = str.find_first_of(chars); c != std::string::npos; c = c = str.find_first_of(chars)) {
str.erase(c, 1);
}
}
bool util::create_cmd_process(TCHAR* command, std::string& outbuf) {
outbuf = "";
//create pipe
HANDLE readPipe;
HANDLE writePipe;
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(sa);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
if (CreatePipe(&readPipe, &writePipe, &sa, 0) == 0) {
//MessageBox(0, _TEXT("could'nt create pipe"), _TEXT("error"), MB_OK);
return false;
}
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
ZeroMemory(&pi, sizeof(pi));
si.cb = sizeof(si);
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
si.hStdOutput = writePipe;
si.hStdError = writePipe;
si.wShowWindow = SW_HIDE;
if (CreateProcess(NULL, command, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi) == 0) {
//MessageBox(0, _TEXT("could'nt create process"), _TEXT("error"), MB_OK);
return false;
}
HANDLE childProcess = pi.hProcess;
CloseHandle(pi.hThread);
char readBuf[1025];
bool end = false;
do {
DWORD totalLen, len;
if (WaitForSingleObject(childProcess, 100) == WAIT_OBJECT_0)
end = true;
if (PeekNamedPipe(readPipe, NULL, 0, NULL, &totalLen, NULL) == 0)
break;
if (0 < totalLen) {
if (ReadFile(readPipe, readBuf, sizeof(readBuf) - 1, &len, NULL) == 0)
return false;
readBuf[len] = 0;
outbuf += readBuf;
if (totalLen>len) //poocess has ended,but data is remained.so we continue this loop
end = false;
}
} while (end == false);
if (CloseHandle(writePipe) == 0) {
//MessageBox(0, _TEXT("could'nt close pipe"), _TEXT("error"), MB_OK);
return false;
}
if (CloseHandle(readPipe) == 0) {
//MessageBox(0, _TEXT("could'nt close pipe"), _TEXT("error"), MB_OK);
return false;
}
CloseHandle(pi.hProcess);
return true;
}