-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
303 lines (268 loc) · 6.28 KB
/
main.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <sysexits.h>
#include <filesystem>
enum class Language { C, Cpp };
//convert String to lowercase
std::string strToLower(std::string in)
{
std::ranges::transform(in.begin(), in.end(), in.begin(),
[](const unsigned char c) { return std::tolower(c); });
return in;
}
std::string getHeaderName(const std::string_view &filePath)
{
//separate filename
const auto begin {filePath.find_last_of("/\\") + 1};
std::string name {filePath.substr(begin)};
for (char &c: name)
{
//replace all non alphadigits with underscore
if (!std::isalnum(c))
c = '_';
//make everything Uppercase
else
c = static_cast<char>(std::toupper(c));
}
//add _H to the End
name += "_H";
return name;
}
//removes directory and type from name and converts it to a valid c name
std::string getCFileName(const std::string &filePath)
{
//separate filename
const auto begin {filePath.find_last_of("/\\") + 1};
const auto end {filePath.find_last_of('.')};
std::string name {filePath.substr(begin, end - begin)};
//add underscore if first char is a digit
if (std::isdigit(name[0]))
name.insert(0, "_");
//replace characters not allowed in C names with underscore
for (char &c: name)
{
if (!std::isalnum(c))
c = '_';
}
return name;
}
//get filename with extension
std::string getFilename(const std::string &filePath)
{
//separate filename
const auto begin {filePath.find_last_of("/\\") + 1};
return filePath.substr(begin);
}
std::string getFileEnding(const enum Language lang)
{
switch (lang)
{
case Language::C:
return ".h";
case Language::Cpp:
return ".hpp";
default:
return "";
}
}
std::string transformCharC(const char in)
{
std::string out {};
switch (in)
{
case '\n':
out.append("\\n\"\n\"");
break;
case '\t':
out.append("\\t");
break;
case '\v':
out.append("\\v");
break;
case '\b':
out.append("\\b");
break;
case '\r':
out.append("\\r");
break;
case '\f':
out.append("\\f");
break;
case '\a':
out.append("\\a");
break;
case '\\':
out.append(R"(\\)");
break;
case '?':
out.append("\\?");
break;
case '\'':
out.append("\\'");
break;
case '\"':
out.append(R"(\")");
break;
default:
if (std::isprint(in))
out.push_back(in);
else
{
std::stringstream tmp;
tmp << "\\x" << std::hex << static_cast<const int>(in) << "\"\"";
out.append(tmp.str());
}
break;
}
return out;
}
void transformFileCpp(std::ifstream &in, std::ofstream &out, std::string const &path)
{
out << "//\n"
"// Created by fileString: https://github.com/TjarkG/fileString\n"
"//\n"
"\n#ifndef " + getHeaderName(path) +
"\n#define " + getHeaderName(path) +
"\n\n#include <string>\n\n"
"inline const std::string " + getCFileName(path) + " {\"";
char c {};
while (!in.eof())
{
in.get(c);
out << transformCharC(c);
}
in.close();
out << "\"};\n\n#endif //" + getHeaderName(path) + "\n";
}
void transformFileC(std::ifstream &in, std::ofstream &out, std::string const &path)
{
out << "//\n"
"// Created by fileString: https://github.com/TjarkG/fileString\n"
"//\n"
"\n#ifndef " + getHeaderName(path) +
"\n#define " + getHeaderName(path) +
"\n\n"
"const char " + getCFileName(path) + "[] = \"";
char c {};
while (!in.eof())
{
in.get(c);
out << transformCharC(c);
}
in.close();
out << "\";\n\n#endif //" + getHeaderName(path) + "\n";
}
void printHelp()
{
//Yes, this would be the exact use case of this program... chickens and eggs
std::cout << "How to use: fileString [Options] [Input Files]\n\n"
"Options:\n"
"-h Show this help message\n"
"-v Verbose output, prints output paths\n"
"-l Language, followed by language specifier (C, C++ or cpp), default is C\n"
"-o Output directory (default: equal to Input Directory)\n";
}
int main(int argc, char **argv)
{
bool verbose {false};
bool help {argc == 1}; //show help if no arguments are supplied
enum Language lang {Language::C};
std::string outDir {};
std::vector<std::string> inFiles {};
//read in arguments
for (int i = 1; i < argc; ++i)
{
//make argument lowercase
const auto argument {strToLower(argv[i])};
if (argument == "-h")
help = true;
else if (argument == "-v")
verbose = true;
else if (argument == "-l")
{
++i;
if (i == argc)
{
std::cerr << "Error: -l argument without language specifier\n";
exit(EX_USAGE);
}
const auto next {strToLower(argv[i])};
if (next == "cpp" || next == "c++")
lang = Language::Cpp;
else if (next == "c")
lang = Language::C;
else
{
std::cerr << "Error: " << argv[i] << " is not a valid language specifier\n";
exit(EX_USAGE);
}
} else if (argument == "-o")
{
++i;
if (i == argc)
{
std::cerr << "Error: -o argument without Output Path\n";
exit(EX_USAGE);
}
std::filesystem::file_status const s {std::filesystem::status(argv[i])};
if (std::filesystem::is_directory(s))
outDir = argv[i];
else
{
std::cerr << "Error: " << argv[i] << " is not a directory\n";
exit(EX_IOERR);
}
} else
inFiles.emplace_back(argv[i]);
}
//print user info
if (help)
printHelp();
if (verbose)
{
switch (lang)
{
case Language::C:
std::cout << "Language Mode: C\n";
break;
case Language::Cpp:
std::cout << "Language Mode: C++\n";
break;
}
}
//transform files
for (const auto &inPath: inFiles)
{
std::ifstream inFile {inPath};
if (inFile.is_open())
{
std::string outPath {};
if (outDir.empty())
outPath = inPath + getFileEnding(lang);
else
outPath = outDir + "/" + getFilename(inPath) + getFileEnding(lang);
std::ofstream outFile {outPath};
if (outFile.is_open())
{
if (lang == Language::Cpp)
transformFileCpp(inFile, outFile, inPath);
else
transformFileC(inFile, outFile, inPath);
if (verbose)
std::cout << "Output Path: " << outPath << "\n";
} else
{
std::cerr << "Error: Unable to open/create file " << outPath << "\n";
exit(EX_IOERR);
}
} else
{
std::cerr << "Error: Unable to open input file " << inPath << "\n";
exit(EX_IOERR);
}
}
return 0;
}