forked from kact929/kagent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
98 lines (81 loc) · 2.87 KB
/
utils.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
/*
Copyright (C) 2024 [email protected]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <sys/stat.h>
#include <array>
#include <random>
#include <filesystem>
#include <boost/log/trivial.hpp>
#include "utils.h"
std::string get_random_string(size_t n)
{
std::string output{};
output.resize(n);
{
// list(map(lambda n: chr(n + ord('A')), range(26)))
std::array<char, 52> charset {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
};
std::default_random_engine rng{std::random_device{}()};
std::uniform_int_distribution<> dist(0, charset.size() - 1);
for (auto& c : output) {
c = dist(rng);
}
}
return output;
}
std::vector<char> read_file(int fd, ssize_t size)
{
std::vector<char> buffer{};
if (size == -1) {
struct stat status{};
if (::fstat(fd, &status) == -1) {
return {};
}
size = status.st_size;
}
buffer.resize(size);
if (::read(fd, buffer.data(), buffer.size()) != size) {
return {};
}
return buffer;
}
std::vector<char> read_file(const std::string& filename, ssize_t size)
{
UniqueFD fd{::open(filename.c_str(), O_RDONLY)};
if (not fd) {
BOOST_LOG_TRIVIAL(error) << "Failed to opend file " << filename << " " << strerror(errno);
return {};
}
auto buffer = read_file(fd.get(), size);
if (buffer.empty()) {
BOOST_LOG_TRIVIAL(error) << "Failed to read file " << filename << " " << strerror(errno);
}
return buffer;
}
bool write_file(const std::string& filename, const std::string& content)
{
auto fd = UniqueFD{::open(filename.c_str(), O_WRONLY)};
if (not fd) {
BOOST_LOG_TRIVIAL(error) << "Unable open " << filename << " " << strerror(errno);
return false;
}
if (::write(fd.get(), content.data(), content.size()) != content.size()) {
BOOST_LOG_TRIVIAL(error) << "Failed to write content to " << filename << " " << strerror(errno);
return false;
}
return true;
}