CBuild
C++ build system with scripts written in c++
Loading...
Searching...
No Matches
files.cpp
Go to the documentation of this file.
1
20// C++ libraries
21#include "fstream"
22#include "string"
23// Project headers
27/* files.hpp */
28int CBuild::fs::replace(std::string file, std::string token, std::string data) {
29 // Path to tmp file
30 std::string cache;
32 ".tmp";
33 // std::cout << cache << "\n";
34 // Open two files
36 std::ofstream tmp_file(cache);
37 std::ifstream main_file;
38 main_file.open(file);
39 // Number of replaced tokens
40 static int check_num;
41 check_num = 0;
42 // Read and copy file to temp file line by line
43 std::string line;
44 while (std::getline(main_file, line)) {
45 // If token found - replace it and increment counter
46 while (line.find(token) != std::string::npos) {
47 line.replace(line.find(token), token.size(), data);
48 check_num++;
49 }
50 tmp_file << line << "\n";
51 }
52 // Close files
53 main_file.close();
54 tmp_file.close();
55 // Replace file by it's temp version
56 CBuild::fs::remove(file, true);
57 CBuild::fs::move(cache, file);
58 // Return number of replaced element or -1 if nothing has been replaced
59 if (check_num == 0)
60 return -1;
61 return check_num;
62}
63int CBuild::fs::set_var(std::string file, std::string var_type, std::string var_name,
64 std::string data) {
65 // Path to tmp file
66 std::string cache;
68 ".tmp";
69 // std::cout << cache << "\n";
70 // Open two files
72 std::ofstream tmp_file(cache);
73 std::ifstream main_file;
74 main_file.open(file);
75 // Number of replaced tokens
76 static int check_num;
77 check_num = 0;
78 // Read and copy file to temp file line by line
79 std::string line;
80 while (std::getline(main_file, line)) {
81 // We can refer to #define by define
82 if (var_type == std::string("define"))
83 var_type = "#define";
84 // If variable found
85 if ((line.find(var_type) != std::string::npos) &&
86 (line.find(var_name) != std::string::npos)) {
87 // All content before variable
88 std::string prefix = line.substr(0, line.find(var_type));
89 // All content after variable
90 std::string postfix = "";
91 if (var_name != std::string("#define")) {
92 postfix = line.substr(line.find(";"), line.size() - 1);
93 }
94 // Variable
95 std::string var;
96 if (var_type == std::string("#define")) {
97 var = "#define " + var_name + " " + data;
98 } else {
99 var = var_type + " " + var_name + " = " + data;
100 }
101 // Recreate line
102 line = prefix + var + postfix;
103 check_num++;
104 }
105 tmp_file << line << "\n";
106 }
107 // Close files
108 main_file.close();
109 tmp_file.close();
110 // Replace file by it's temp version
111 CBuild::fs::remove(file, true);
112 CBuild::fs::move(cache, file);
113 // Return number of replaced element or -1 if nothing has been replaced
114 if (check_num == 0)
115 return -1;
116 return check_num;
117}
118std::string CBuild::fs::path_to_file(std::string in) {
119 while (in.find("/") != std::string::npos) {
120 in.replace(in.find("/"), std::string("/").size(), ".");
121 }
122 return in;
123}
#define CBUILD_COPY_CACHE_DIR
tmp dir in cache dir
#define CBUILD_CACHE_DIR
Cache directory of CBuild.
File parsing api.
filesystem++ api
bool create(std::vector< std::string > paths, CBuild::fs::type what)
Create element.
bool remove(std::string path, bool force=false)
Delete files or directories.
int set_var(std::string file, std::string var_type, std::string var_name, std::string data)
Set value of variable with type var_type wth name var_name to data This function loocks for variable ...
Definition files.cpp:63
std::string path_to_file(std::string in)
Convert path to filename by replacing '\' by '.'.
Definition files.cpp:118
int replace(std::string file, std::string token, std::string data)
Replace [token] by [data] in [file].
Definition files.cpp:28
@ FILE
Standard file.
bool move(std::string start, std::string end)
Move files or directories, remove starting files or directories.