-
Notifications
You must be signed in to change notification settings - Fork 0
/
switch.cpp
130 lines (122 loc) · 4.03 KB
/
switch.cpp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <algorithm>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std; // 汚染がひどいのであまりやってはいけない
namespace fs = std::filesystem;
string getImplStr(string str);
int change_file(string file_name, string sign, bool commentOut);
int file_copy_write(string fromPath, string toPath);
pair<int, string> count_leading_whitespace(const string& str);
int main(int argc, char* argv[]) {
const vector<string> out_extensions = { ".ts", ".scss" };
if (argc != 4) {
cerr << "./switch.exe <dir> <tauri | web> <on | off>" << endl;
return 1;
}
vector<string> path_arr;
for (const fs::directory_entry& i : fs::recursive_directory_iterator(argv[1])) {
const string path = i.path( ).string( );
const string ext = i.path( ).extension( ).string( );
if (i.is_regular_file( ))
// if (is_text(path))
if (count(begin(out_extensions), end(out_extensions), ext) > 0)
path_arr.push_back(path);
}
const bool commentOut = (!strcmp(argv[3], "on")) ? true : false;
for (string item : path_arr) {
change_file(item, argv[2], commentOut);
cout << item << endl;
}
}
int change_file(string file_name, string sign, bool commentOut) {
string line;
ifstream filein(file_name); //File to read from
if (!filein.is_open( )) {
cout << "Error opening files!" << endl;
return 1;
} else {
string tmp_file_name = file_name + ".tmp";
ofstream fileout(tmp_file_name); //Temporary file
if (fileout.is_open( )) {
bool isComment = false;
pair<int, string> slashBeforeSpaces;
while (getline(filein, line)) {
string line_impl = getImplStr(line);
// cout << line_impl << ":" << sign << endl;
if (line_impl == "// " + sign) {
isComment = true;
slashBeforeSpaces = count_leading_whitespace(line);
fileout << line;
} else if (line_impl == "// " + sign + " end") {
isComment = false;
fileout << line;
} else if (isComment) {
if (commentOut)
fileout << slashBeforeSpaces.second + line.substr(slashBeforeSpaces.first + 3);
else {
fileout << slashBeforeSpaces.second + "// " + line.substr(slashBeforeSpaces.first);
}
} else
fileout << line;
fileout << "\n";
}
filein.close( );
fileout.close( );
file_copy_write(tmp_file_name, file_name);
fs::remove(tmp_file_name);
}
}
return 0;
}
int file_copy_write(string fromPath, string toPath) {
ifstream fromFile(fromPath, ios::binary);
if (!fromFile.is_open( )) {
cerr << "Failed to open the original file.\n";
return 1;
}
ofstream toFile(toPath, ios::binary | ios::trunc);
if (!toFile.is_open( )) {
cerr << "Failed to create the copy file.\n";
return 1;
}
const auto fileSize = fs::file_size(fromPath);
const size_t bufferSize = 4096;
char buffer[bufferSize];
size_t remainingBytes = fileSize;
while (remainingBytes > 0) {
const size_t readBytes = min(bufferSize, remainingBytes);
fromFile.read(buffer, readBytes);
toFile.write(buffer, readBytes);
remainingBytes -= readBytes;
}
fromFile.close( );
toFile.close( );
return 0;
}
bool isBothSpace(const char& lhs, const char& rhs) {
return lhs == rhs && iswspace(lhs);
}
string getImplStr(string str) {
if (!str.empty( )) {
auto it = unique(str.begin( ), str.end( ), isBothSpace);
str.erase(it, str.end( ));
if (str[0] == ' ')
str.erase(str.begin( ));
if (str[str.size( ) - 1] == ' ')
str.erase(str.size( ) - 1);
}
return str;
}
pair<int, string> count_leading_whitespace(const string& str) {
int count = 0;
for (const char& c : str)
if (c == ' ')
count++;
else
break;
return { count, str.substr(0, count) };
}