Arkanjo 0.1
A tool for find code duplicated functions in codebases
Loading...
Searching...
No Matches
utils.cpp
Go to the documentation of this file.
1#include "utils.hpp"
2
3#ifdef _WIN32
4 #include "windows_utils.hpp"
5#elif __linux__
6 #include "linux_utils.hpp"
7#elif __APPLE__
8 #include "apple_utils.hpp"
9#endif
10
11void Utils::ensure_file_is_open(std::ifstream &file, string file_name){
12 if(!file.is_open()){
13 cout << "Attempted to open file: " << file_name << " ";
14 cout << "but a Error ocurred. Check if the file exist." << endl;
15 exit(0);
16 }
17}
18
19vector<string> Utils::read_file_generic(string string_path){
20 std::ifstream filein;
21 string line;
22 vector<string> ret;
23 filein.open(string_path);
24 ensure_file_is_open(filein,string_path);
25 while(getline(filein,line)){
26 ret.push_back(line);
27 }
28 filein.close();
29 return ret;
30}
31
32void Utils::write_file_generic(string file_path, vector<string> content){
33 std::ofstream fileout;
35 fileout.open(file_path);
36
37 for(auto line : content){
38 fileout << line << '\n';
39 }
40 fileout.close();
41}
42
44 vector<string> parents;
45 for(size_t i = 0; i < file_path.size(); i++){
46 if(file_path[i] == '/'){
47 string s = "";
48 for(size_t j = 0; j < i; j++){
49 s += file_path[j];
50 }
51 parents.push_back(s);
52 }
53 }
54 for(auto folder : parents){
55 const char *cfolder = folder.c_str();
56 mkdir(cfolder,MKDIR_FLAG);
57 }
58}
59
60json Utils::read_json(string string_path){
61 ifstream json_file(string_path,std::ifstream::binary);
62 ensure_file_is_open(json_file,string_path);
63 json j_read;
64 json_file >> j_read;
65 return j_read;
66}
67
68bool Utils::does_file_exist(string file_path){
69 if (FILE *file = fopen(file_path.c_str(), "r")) {
70 fclose(file);
71 return true;
72 } else {
73 return false;
74 }
75}
76
77bool Utils::is_regular_file(string path){
78 struct stat path_stat;
79 auto path_c_str = path.c_str();
80 stat(path_c_str,&path_stat);
81 return S_ISREG(path_stat.st_mode);
82}
83
84string Utils::format_colored_message(string message, COLOR color){
86 return COLOR_TOKENS_UTILS_DARK[color] + message + COLOR_TOKENS_UTILS_DARK[RESET];
87 }
88 else{
90 }
91}
92
94 if(c == ' '){
95 return true;
96 }
97 if(c <= 20){
98 return true;
99 }
100 return false;
101}
102
104 if(c >= 'a' && c <= 'z'){
105 return false;
106 }
107 if(c >= 'A' && c <= 'Z'){
108 return false;
109 }
110 if(c >= '0' && c <= '9'){
111 return false;
112 }
113 if(c == '_'){
114 return false;
115 }
116 return true;
117}
118
119vector<string> Utils::split_string(string s, char delimiter){
120 string cur_token;
121 vector<string> ret;
122 for(auto c : s){
123 if(c == delimiter){
124 if(!cur_token.empty()){
125 ret.push_back(cur_token);
126 }
127 cur_token = "";
128 }else{
129 cur_token.push_back(c);
130 }
131 }
132 if(!cur_token.empty()){
133 ret.push_back(cur_token);
134 }
135 return ret;
136}
return ret
Definition sum.c:3
nlohmann::json json
Definition function.hpp:25
Linux-specific utility functions.
bool is_bg_color_dark()
Determines if terminal background color is dark.
json read_json(string string_path)
Reads and parses a JSON file, uses nlohmann json library.
Definition utils.cpp:60
vector< string > split_string(string s, char delimiter)
Splits a string by a delimiter into tokens.
Definition utils.cpp:119
bool is_special_char(char c)
Checks if a character is special (non-alphanumeric and not underscore)
Definition utils.cpp:103
COLOR
Enumeration of available colors for formatted messages.
Definition utils.hpp:103
@ RESET
Reset to default color.
Definition utils.hpp:104
bool is_regular_file(string path)
Determines if a path refers to a regular file.
Definition utils.cpp:77
const vector< string > COLOR_TOKENS_UTILS_LIGTH
ANSI color codes for light terminal backgrounds.
Definition utils.hpp:56
void create_parents_folder_of_file_path(string file_path)
Creates all parent directories for a given file path.
Definition utils.cpp:43
bool does_file_exist(string file_path)
Checks if a file exists at the given path.
Definition utils.cpp:68
string format_colored_message(string message, COLOR color)
Formats a message with ANSI color codes.
Definition utils.cpp:84
bool is_empty_char(char c)
Checks if a character is considered empty/whitespace.
Definition utils.cpp:93
void ensure_file_is_open(std::ifstream &file, string file_name)
Ensures that a file stream is successfully opened.
Definition utils.cpp:11
vector< string > read_file_generic(string string_path)
Reads a file line by line into a vector of strings.
Definition utils.cpp:19
const int MKDIR_FLAG
Permission flags used when creating directories (rwx for owner)
Definition utils.hpp:38
const vector< string > COLOR_TOKENS_UTILS_DARK
ANSI color codes for dark terminal backgrounds.
Definition utils.hpp:86
void write_file_generic(string file_path, vector< string > content)
Writes content to a file at specified path.
Definition utils.cpp:32
Defines utility functions used across all files.
Windows-specific utility functions.