-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathalgorithm.cpp
More file actions
66 lines (54 loc) · 1.66 KB
/
algorithm.cpp
File metadata and controls
66 lines (54 loc) · 1.66 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
#pragma once
#include <algorithm>
#include <codecvt>
#include <array>
#include "zero/base/algorithm.h"
#ifdef _WIN32
#define pclose _pclose
#define popen _popen
#endif
namespace zero {
static std::wstring_convert<std::codecvt_utf8<wchar_t>, 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 <typename To, typename From>
struct Converter {
static typename std::enable_if<std::is_same<From, To>::value, bool>::type Convert(const From &from) {
return from;
}
};
template <typename From>
struct Converter<std::string, From> {
static std::string Convert(From &from) {
return std::to_string(from);
}
};
template <typename From>
struct Converter<double, From> {
static std::string Convert(From &from) {
return std::to_string(from);
}
};
template <typename To, typename From>
typename To lexical_cast(const From& from) {
return Converter<To, From>::Convert(from);
}
std::string Execute(const std::string &command, const bool error) {
std::array<char, 1024> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> 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);
}
}