Arkanjo 0.2
A tool for find code duplicated functions in codebases
Loading...
Searching...
No Matches
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
25using json = nlohmann::json;
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
127
133void ensure_file_is_open(const std::ifstream& file, const fs::path& file_name);
134
140std::vector<std::string> read_file_generic(const fs::path& string_path);
141
147void write_file_generic(const fs::path& file_path, const std::vector<std::string>& content);
148
154json read_json(const fs::path& string_path);
155
162std::string format_colored_message(const std::string& message, COLOR color);
163
169bool is_empty_char(char c);
170
176bool is_special_char(char c);
177
184std::vector<std::string> split_string(const std::string& s, char delimiter);
185
186std::string to_uppercase(const std::string input);
187
194std::string hash(const std::string& content);
195
202std::uintmax_t folder_size(const fs::path& folder);
203
204
211std::string format_size(std::uintmax_t bytes);
212
213void open_folder(const std::string& path);
214}; // namespace Utils
215
216struct Wrapped {
217 std::string text;
218 size_t spaces;
220};
221
222Wrapped wrapped(const std::string& text, size_t spaces = 1, bool use_first_line = true);
223std::ostream& operator<<(std::ostream& os, const Wrapped& w);
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
@ 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
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
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: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
const int MKDIR_FLAG
Permission flags used when creating directories (rwx for owner)
Definition utils.hpp:38
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
utility functions
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=1, bool use_first_line=true)
Definition utils.cpp:90
std::ostream & operator<<(std::ostream &os, const Wrapped &w)
Definition utils.cpp:94