#pragma once #include #include #include #include "zero/base/algorithm.h" #ifdef _WIN32 #define pclose _pclose #define popen _popen #endif namespace zero { static std::wstring_convert, wchar_t> g_converter; std::string Lower(const std::string &input) { std::string output; output.resize(input.size()); std::transform(input.begin(), input.end(), output.begin(), ::tolower); return output; } template struct Converter { static typename std::enable_if::value, bool>::type Convert(const From &from) { return from; } }; template struct Converter { static std::string Convert(From &from) { return std::to_string(from); } }; template struct Converter { static std::string Convert(From &from) { return std::to_string(from); } }; template typename To lexical_cast(const From& from) { return Converter::Convert(from); } std::string Execute(const std::string &command, const bool error) { std::array buffer; std::string result; std::unique_ptr pipe(popen(command.data(), "r"), pclose); if (pipe) { while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) { result += buffer.data(); } } else if (error) { throw std::runtime_error(std::string("popen ") + command + " failed!"); } return result; } std::string Convert(const std::wstring& input) { return g_converter.to_bytes(input); } std::wstring Convert(const std::string& input) { return g_converter.from_bytes(input); } }