-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
74 lines (61 loc) · 1.55 KB
/
utils.cpp
File metadata and controls
74 lines (61 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <fstream>
#include <sstream>
namespace utils
{
std::string replace(std::string str, std::string remove, std::string add)
{
std::string before, after;
size_t pos;
if (str.empty() || remove.empty() || add.empty())
return before;
pos = str.find(remove);
while (pos != std::string::npos)
{
before = str.substr(0, pos);
after = str.substr(pos + remove.size());
str = before + add + after;
pos = str.find(remove, pos + remove.size());
}
return str;
}
bool check_args(int argc, char **argv)
{
if (argc != 4 || !argv[1][0] || !argv[2][0] || !argv[3][0])
return false;
return (true);
}
bool check_file_permissions(std::string input)
{
std::string output;
std::fstream input_file;
std::fstream output_file;
output = input + ".replace";
input_file.open(input.c_str(), std::ios_base::out | std::ios_base::in);
output_file.open(output.c_str(), std::ios_base::out);
return (input_file.is_open() && output_file.is_open());
}
bool validations(int argc, char **argv)
{
std::string input;
if (!check_args(argc, argv))
return false;
input = argv[1];
if (!check_file_permissions(input))
return false;
return true;
}
std::string get_file_content(std::string name)
{
std::fstream file;
std::stringstream get_buffer;
file.open(name.c_str(), std::ios_base::in);
get_buffer << file.rdbuf();
return get_buffer.str();
}
void write_file_content(std::string name, std::string content)
{
std::fstream file;
file.open(name.c_str(), std::ios_base::out);
file << content;
}
}