diff options
| author | Alexander Karatarakis <alkarata@microsoft.com> | 2016-09-18 20:50:08 -0700 |
|---|---|---|
| committer | Alexander Karatarakis <alkarata@microsoft.com> | 2016-09-18 20:54:03 -0700 |
| commit | ccca198c1b1730b0241911cb56dc8e3504958b2a (patch) | |
| tree | a2dd9b8b087a09afdcecc5cbb3377bed15127eb2 /toolsrc/src/vcpkg_System.cpp | |
| download | vcpkg-ccca198c1b1730b0241911cb56dc8e3504958b2a.tar.gz vcpkg-ccca198c1b1730b0241911cb56dc8e3504958b2a.zip | |
Initial commit
Diffstat (limited to 'toolsrc/src/vcpkg_System.cpp')
| -rw-r--r-- | toolsrc/src/vcpkg_System.cpp | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/toolsrc/src/vcpkg_System.cpp b/toolsrc/src/vcpkg_System.cpp new file mode 100644 index 000000000..71b4087d2 --- /dev/null +++ b/toolsrc/src/vcpkg_System.cpp @@ -0,0 +1,111 @@ +#include "vcpkg_System.h" +#include <iostream> +#include <Windows.h> +#include <regex> + +namespace fs = std::tr2::sys; + +namespace vcpkg {namespace System +{ + fs::path get_exe_path_of_current_process() + { + wchar_t buf[_MAX_PATH ]; + int bytes = GetModuleFileNameW(nullptr, buf, _MAX_PATH); + if (bytes == 0) + std::abort(); + return fs::path(buf, buf + bytes); + } + + int cmd_execute(const wchar_t* cmd_line) + { + // Basically we are wrapping it in quotes + const std::wstring& actual_cmd_line = Strings::format(LR"###("%s")###", cmd_line); + int exit_code = _wsystem(actual_cmd_line.c_str()); + return exit_code; + } + + exit_code_and_output cmd_execute_and_capture_output(const wchar_t* cmd_line) + { + const std::wstring& actual_cmd_line = Strings::format(LR"###("%s")###", cmd_line); + + std::string output; + char buf[1024]; + auto pipe = _wpopen(actual_cmd_line.c_str(), L"r"); + if (pipe == nullptr) + { + return {1, output}; + } + while (fgets(buf, 1024, pipe)) + { + output.append(buf); + } + if (!feof(pipe)) + { + return {1, output}; + } + auto ec = _pclose(pipe); + return {ec, output}; + } + + void print(const char* message) + { + std::cout << message; + } + + void println(const char* message) + { + print(message); + std::cout << "\n"; + } + + void print(color c, const char* message) + { + HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + + CONSOLE_SCREEN_BUFFER_INFO consoleScreenBufferInfo{}; + GetConsoleScreenBufferInfo(hConsole, &consoleScreenBufferInfo); + auto original_color = consoleScreenBufferInfo.wAttributes; + + SetConsoleTextAttribute(hConsole, static_cast<int>(c) | (original_color & 0xF0)); + std::cout << message; + SetConsoleTextAttribute(hConsole, original_color); + } + + void println(color c, const char* message) + { + print(c, message); + std::cout << "\n"; + } + + std::wstring wdupenv_str(const wchar_t* varname) noexcept + { + std::wstring ret; + wchar_t* buffer; + _wdupenv_s(&buffer, nullptr, varname); + if (buffer != nullptr) + { + ret = buffer; + free(buffer); + } + return ret; + } + + void Stopwatch::start() + { + static_assert(sizeof(start_time) == sizeof(LARGE_INTEGER), ""); + + QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&start_time)); + } + + void Stopwatch::stop() + { + QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&end_time)); + QueryPerformanceFrequency(reinterpret_cast<LARGE_INTEGER*>(&freq)); + } + + double Stopwatch::microseconds() const + { + return (reinterpret_cast<const LARGE_INTEGER*>(&end_time)->QuadPart - + reinterpret_cast<const LARGE_INTEGER*>(&start_time)->QuadPart) * 1000000.0 / reinterpret_cast<const LARGE_INTEGER*>(&freq)->QuadPart; + } +}} |
