ArKanjo 0.5.1
A tool for find code duplicated functions in codebases
utils.cpp
Go to the documentation of this file.
2
4
5template <typename Stream>
6void Utils::ensure_file_is_open(const Stream& file, const fs::path& file_name) {
7 if (!file.is_open()) {
8 throw std::runtime_error("Attempted to open file: " + file_name.string() + " but a Error ocurred. Check if the file exist.");
9 }
10}
11
12std::string Utils::read_file(const fs::path& path) {
13 std::ifstream file(path);
14 std::stringstream buffer;
15 buffer << file.rdbuf();
16 return buffer.str();
17}
18
19void Utils::write_file(const fs::path& path, const std::string& content) {
20 fs::create_directories(path.parent_path());
21 std::ofstream file(path, std::ios::out | std::ios::trunc);
22 ensure_file_is_open(file, path);
23 file << content;
24}
25
26std::vector<std::string> Utils::read_file_with_vector(const fs::path& path) {
27 std::ifstream filein;
28 std::string line;
29 std::vector<std::string> ret;
30 filein.open(path);
31 ensure_file_is_open(filein, path);
32 while (getline(filein, line)) {
33 ret.push_back(line);
34 }
35 filein.close();
36 return ret;
37}
38
39void Utils::write_file_with_vector(const fs::path& path, const std::vector<std::string>& content) {
40 std::ofstream fileout;
41 fs::create_directories(path.parent_path());
42 fileout.open(path);
43
44 for (const auto& line : content) {
45 fileout << line << '\n';
46 }
47 fileout.close();
48}
49
50json Utils::read_json(const fs::path& string_path) {
51 std::ifstream json_file(string_path.string(), std::ifstream::binary);
52 ensure_file_is_open(json_file, string_path);
53 json j_read;
54 json_file >> j_read;
55 return j_read;
56}
57
58std::string Utils::format_colored_message(const std::string& message, COLOR color) {
59 if (color == COLOR::NONE) {
60 return message;
61 }
62 if (Config::config().theme == config::Theme::Dark) {
63 return COLOR_TOKENS_UTILS_DARK[color] + message + COLOR_TOKENS_UTILS_DARK[RESET];
64 } else {
66 }
67}
68
70 if (c == ' ') {
71 return true;
72 }
73 if (c <= 20) {
74 return true;
75 }
76 return false;
77}
78
80 unsigned char uc = static_cast<unsigned char>(c);
81 return !(std::isalnum(uc) || c == '_');
82}
83
84std::vector<std::string> Utils::split_string(const std::string& s, char delimiter) {
85 std::string cur_token;
86 std::vector<std::string> ret;
87 for (auto c : s) {
88 if (c == delimiter) {
89 if (!cur_token.empty()) {
90 ret.push_back(cur_token);
91 }
92 cur_token = "";
93 } else {
94 cur_token.push_back(c);
95 }
96 }
97 if (!cur_token.empty()) {
98 ret.push_back(cur_token);
99 }
100 return ret;
101}
102
103Wrapped wrapped(const std::string& text, size_t spaces, bool use_first_line) {
104 return {text, spaces, use_first_line};
105}
106
107std::ostream& operator<<(std::ostream& os, const Wrapped& w) {
108 std::string indent(w.spaces, ' ');
109 bool first_line = w.use_first_line;
110 size_t max_line = 80 - w.spaces;
111 std::string word;
112 std::string current_line;
113 std::istringstream stream(w.text);
114 while (stream >> word) {
115 if (current_line.empty()) {
116 current_line = word;
117 } else if (current_line.length() + 1 + word.length() > max_line) {
118 if (first_line) {
119 os << current_line << "\n";
120 first_line = false;
121 } else {
122 os << indent << current_line << "\n";
123 }
124 current_line = word;
125 } else {
126 current_line += " " + word;
127 }
128 }
129 if (!current_line.empty()) {
130 if (first_line) {
131 os << current_line << "\n";
132 } else {
133 os << indent << current_line << "\n";
134 }
135 }
136 return os;
137}
138
139std::string Utils::to_uppercase(const std::string input) {
140 std::string output = input;
141 for (char &c : output) {
142 c = static_cast<char>(
143 std::toupper(static_cast<unsigned char>(c))
144 );
145 }
146 return output;
147}
148
149std::string Utils::hash(const std::string& content) {
150 std::hash<std::string> hasher;
151 size_t hash = hasher(content);
152
153 std::stringstream ss;
154 ss << std::hex << hash;
155
156 std::string hash_str = ss.str();
157 if (hash_str.length() > 12) {
158 hash_str = hash_str.substr(0, 12);
159 }
160
161 return hash_str;
162}
163
164std::uintmax_t Utils::folder_size(const fs::path& folder) {
165 std::uintmax_t size = 0;
166
167 if (!fs::exists(folder)) return 0;
168
169 std::error_code ec;
170 for (auto& p : fs::recursive_directory_iterator(folder, fs::directory_options::skip_permission_denied, ec)) {
171 if (ec) continue;
172
173 if (fs::is_regular_file(p.status(ec)) && !ec) {
174 size += fs::file_size(p, ec);
175 }
176 }
177
178 return size;
179}
180
181std::string Utils::format_size(std::uintmax_t bytes) {
182 double size = static_cast<double>(bytes);
183 const char* units[] = {"B", "KB", "MB", "GB", "TB"};
184 int unit = 0;
185
186 while (size >= 1024 && unit < 4) {
187 size /= 1024;
188 ++unit;
189 }
190
191 std::ostringstream out;
192 out << std::fixed << std::setprecision(2) << size << units[unit];
193 return out.str();
194}
195
196void Utils::open_folder(const std::string& path) {
197#ifdef _WIN32
198 std::system(("explorer \"" + path + "\"").c_str());
199#elif __APPLE__
200 std::system(("open \"" + path + "\"").c_str());
201#else
202 std::system(("xdg-open \"" + path + "\"").c_str());
203#endif
204}
static Config & config()
Gets the singleton configuration instance.
Definition: config.cpp:68
Configuration management interface.
nlohmann::json json
std::string to_uppercase(const std::string input)
Definition: utils.cpp:139
const std::vector< std::string > COLOR_TOKENS_UTILS_LIGTH
ANSI color codes for light terminal backgrounds.
Definition: utils.hpp:58
std::string format_colored_message(const std::string &message, COLOR color)
Formats a message with ANSI color codes.
Definition: utils.cpp:58
std::vector< std::string > split_string(const std::string &s, char delimiter)
Splits a string by a delimiter into tokens.
Definition: utils.cpp:84
void write_file_with_vector(const fs::path &path, const std::vector< std::string > &content)
Writes content to a file at specified path.
Definition: utils.cpp:39
bool is_special_char(char c)
Checks if a character is special (non-alphanumeric and not underscore)
Definition: utils.cpp:79
COLOR
Enumeration of available colors for formatted messages.
Definition: utils.hpp:111
@ RESET
Reset to default color.
Definition: utils.hpp:112
@ NONE
Definition: utils.hpp:125
void write_file(const fs::path &path, const std::string &content)
Writes content to a file at specified path.
Definition: utils.cpp:19
const std::vector< std::string > COLOR_TOKENS_UTILS_DARK
ANSI color codes for dark terminal backgrounds.
Definition: utils.hpp:91
void open_folder(const std::string &path)
Definition: utils.cpp:196
std::string read_file(const fs::path &path)
Reads a file into string.
Definition: utils.cpp:12
json read_json(const fs::path &string_path)
Reads and parses a JSON file, uses nlohmann json library.
Definition: utils.cpp:50
std::vector< std::string > read_file_with_vector(const fs::path &path)
Reads a file line by line into a vector of strings.
Definition: utils.cpp:26
bool is_empty_char(char c)
Checks if a character is considered empty/whitespace.
Definition: utils.cpp:69
std::string format_size(std::uintmax_t bytes)
Formats a byte size into a human-readable string.
Definition: utils.cpp:181
std::uintmax_t folder_size(const fs::path &folder)
Calculates the total size of all regular files in a folder.
Definition: utils.cpp:164
void ensure_file_is_open(const Stream &file, const fs::path &file_name)
Ensures that a file stream is successfully opened.
Definition: utils.cpp:6
std::string hash(const std::string &content)
Computes a simple hash of a string.
Definition: utils.cpp:149
std::string text
Definition: utils.hpp:232
bool use_first_line
Definition: utils.hpp:234
size_t spaces
Definition: utils.hpp:233
Wrapped wrapped(const std::string &text, size_t spaces, bool use_first_line)
Definition: utils.cpp:103
std::ostream & operator<<(std::ostream &os, const Wrapped &w)
Definition: utils.cpp:107
Defines utility functions used across all files.