23 string Replace(
const string& input,
const string& find,
const string& replace) {
24 string result = input;
26 while ((start_pos = result.find(find, start_pos)) != string::npos) {
27 result.replace(start_pos, find.length(), replace);
28 start_pos += replace.length();
37 return Replace(path,
" ",
"\\ ");
41 SECURITY_ATTRIBUTES security_attributes = {};
42 security_attributes.nLength =
sizeof(SECURITY_ATTRIBUTES);
43 security_attributes.bInheritHandle = TRUE;
47 CreateFileA(
"NUL", GENERIC_READ,
48 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
49 &security_attributes, OPEN_EXISTING, 0, NULL);
50 if (nul == INVALID_HANDLE_VALUE)
51 Fatal(
"couldn't open nul");
53 HANDLE stdout_read, stdout_write;
54 if (!CreatePipe(&stdout_read, &stdout_write, &security_attributes, 0))
55 Win32Fatal(
"CreatePipe");
57 if (!SetHandleInformation(stdout_read, HANDLE_FLAG_INHERIT, 0))
58 Win32Fatal(
"SetHandleInformation");
60 PROCESS_INFORMATION process_info = {};
61 STARTUPINFOA startup_info = {};
62 startup_info.cb =
sizeof(STARTUPINFOA);
63 startup_info.hStdInput = nul;
64 startup_info.hStdError = ::GetStdHandle(STD_ERROR_HANDLE);
65 startup_info.hStdOutput = stdout_write;
66 startup_info.dwFlags |= STARTF_USESTDHANDLES;
68 if (!CreateProcessA(NULL, (
char*)command.c_str(), NULL, NULL,
71 &startup_info, &process_info)) {
72 Win32Fatal(
"CreateProcess");
75 if (!CloseHandle(nul) ||
76 !CloseHandle(stdout_write)) {
77 Win32Fatal(
"CloseHandle");
85 if (!::
ReadFile(stdout_read, buf,
sizeof(buf), &read_len, NULL) &&
86 GetLastError() != ERROR_BROKEN_PIPE) {
87 Win32Fatal(
"ReadFile");
89 output->append(buf, read_len);
93 if (WaitForSingleObject(process_info.hProcess, INFINITE) == WAIT_FAILED)
94 Win32Fatal(
"WaitForSingleObject");
96 if (!GetExitCodeProcess(process_info.hProcess, &exit_code))
97 Win32Fatal(
"GetExitCodeProcess");
99 if (!CloseHandle(stdout_read) ||
100 !CloseHandle(process_info.hProcess) ||
101 !CloseHandle(process_info.hThread)) {
102 Win32Fatal(
"CloseHandle");
int ReadFile(const string &path, string *contents, string *err)
Read a file to a string (in text mode: with CRLF conversion on Windows).
int Run(const string &command, string *output)
Start a process and gather its raw output.
void Fatal(const char *msg,...)
Log a fatal message and exit.
string EscapeForDepfile(const string &path)