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