ArKanjo 0.5.1
A tool for find code duplicated functions in codebases
utils.hpp
Go to the documentation of this file.
1
9#pragma once
10#include <cassert>
11#include <cctype>
12#include <fstream>
13#include <iomanip>
14#include <iostream>
15#include <sstream>
16#include <string>
17#include <sys/stat.h>
18#include <vector>
19#include <sstream>
20
21#include "nlohmann/json.hpp"
22
24
26namespace fs = std::filesystem;
27
28namespace Utils {
29
33const std::string LIMITER_PRINT = "---------------------";
34
38const int MKDIR_FLAG = 0700;
39
58const std::vector<std::string> COLOR_TOKENS_UTILS_LIGTH = {
59 "\033[0m", // RESET
60 "\033[31;2m", // DARK_RED
61 "\033[32;2m", // DARK_GREEN
62 "\033[33;2m", // DARK_YELLOW
63 "\033[34;2m", // DARK_BLUE
64 "\033[35;2m", // DARK_MAGENTA
65 "\033[36;2m", // DARK_CYAN
66 "\033[37;2m", // DARK_GRAY
67 "\033[30;2m", // BLACK
68 "\033[33;2m", // YELLOW
69 "\033[30m", // BLACK
70 "\033[01m", // BOLD
71 "\033[04m", // UNDERLINE
72 "", // NONE
73};
74
91const std::vector<std::string> COLOR_TOKENS_UTILS_DARK = {
92 "\033[00m", // RESET
93 "\033[31m", // RED
94 "\033[32m", // GREEN
95 "\033[33m", // YELLOW
96 "\033[34m", // BLUE
97 "\033[35m", // MAGENTA
98 "\033[36m", // CYAN
99 "\033[37m", // GRAY
100 "\033[97m", // WHITE
101 "\033[33m", // BRIGHT_YELLOW
102 "\033[97m", // WHITE
103 "\033[01m", // BOLD
104 "\033[04m", // UNDERLINE
105 "", // NONE
106};
107
111enum COLOR {
125 NONE
127
133template <typename Stream>
134void ensure_file_is_open(const Stream& file, const fs::path& file_name);
135
141std::string read_file(const fs::path& path);
142
148void write_file(const fs::path& path, const std::string& content);
149
155std::vector<std::string> read_file_with_vector(const fs::path& path);
156
162void write_file_with_vector(const fs::path& path, const std::vector<std::string>& content);
163
169json read_json(const fs::path& string_path);
170
177std::string format_colored_message(const std::string& message, COLOR color);
178
184bool is_empty_char(char c);
185
191bool is_special_char(char c);
192
199std::vector<std::string> split_string(const std::string& s, char delimiter);
200
201std::string to_uppercase(const std::string input);
202
209std::string hash(const std::string& content);
210
217std::uintmax_t folder_size(const fs::path& folder);
218
219
226std::string format_size(std::uintmax_t bytes);
227
228void open_folder(const std::string& path);
229}; // namespace Utils
230
231struct Wrapped {
232 std::string text;
233 size_t spaces;
235};
236
237Wrapped wrapped(const std::string& text, size_t spaces = 1, bool use_first_line = true);
238std::ostream& operator<<(std::ostream& os, const Wrapped& w);
nlohmann::json json
Definition: utils.hpp:28
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
@ BLACK
Black color.
Definition: utils.hpp:122
@ BOLD
Bold text.
Definition: utils.hpp:123
@ BRIGHT_YELLOW
Bright yellow color.
Definition: utils.hpp:121
@ MAGENTA
Magenta color.
Definition: utils.hpp:117
@ GRAY
Gray color.
Definition: utils.hpp:119
@ NONE
Definition: utils.hpp:125
@ WHITE
White color.
Definition: utils.hpp:120
@ RED
Red color.
Definition: utils.hpp:113
@ GREEN
Green color.
Definition: utils.hpp:114
@ YELLOW
Yellow color.
Definition: utils.hpp:115
@ UNDERLINE
Underline text.
Definition: utils.hpp:124
@ BLUE
Blue color.
Definition: utils.hpp:116
@ CYAN
Cyan color.
Definition: utils.hpp:118
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
const std::string LIMITER_PRINT
Constant string used as a visual delimiter/separator in prints.
Definition: utils.hpp:33
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
const int MKDIR_FLAG
Permission flags used when creating directories (rwx for owner)
Definition: utils.hpp:38
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
utility functions
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=1, bool use_first_line=true)
Definition: utils.cpp:103
std::ostream & operator<<(std::ostream &os, const Wrapped &w)
Definition: utils.cpp:107
nlohmann::json json
Definition: utils.hpp:25