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
|
#include "ScopeGuard.hpp"
#include "Utils.hpp"
#include <stb_c_lexer.h>
#include <filesystem>
#include <string>
#include <string_view>
using namespace std::literals;
namespace fs = std::filesystem;
enum InputOpcode {
IOP_ProcessSingleFile,
IOP_ProcessRecursively,
IOP_COUNT,
};
InputOpcode ParseInputOpcode(std::string_view text) {
if (text == "single"sv) {
return IOP_ProcessSingleFile;
} else if (text == "rec"sv) {
return IOP_ProcessRecursively;
}
return IOP_COUNT;
}
void HandleInputFile(std::string_view source) {
stb_lexer lexer;
char stringStorage[65536];
const char* srcBegin = source.data();
const char* srcEnd = srcBegin + source.length();
stb_c_lexer_init(&lexer, srcBegin, srcEnd, stringStorage, sizeof(stringStorage));
// TODO
}
std::string ReadFileAtOnce(const fs::path& path) {
auto file = Utils::OpenCstdioFile(path, Utils::Read);
if (!file) throw std::runtime_error("Failed to open source file.");
DEFER { fclose(file); };
fseek(file, 0, SEEK_END);
auto fileSize = ftell(file);
rewind(file);
std::string result(fileSize, '\0');
fread(result.data(), fileSize, 1, file);
return result;
}
void HandleArgument(InputOpcode opcode, std::string_view operand) {
switch (opcode) {
case IOP_ProcessSingleFile: {
fs::path filePath(operand);
auto source = ReadFileAtOnce(filePath);
HandleInputFile(source);
} break;
case IOP_ProcessRecursively: {
fs::path startPath(operand);
for (auto& item : fs::directory_iterator(startPath)) {
if (!item.is_regular_file()) {
continue;
}
auto& path = item.path();
auto filename = path.filename().string();
if (filename != ".c" ||
filename != ".cpp")
{
continue;
}
auto source = ReadFileAtOnce(path);
HandleInputFile(source);
}
} break;
case IOP_COUNT: break;
}
}
int main(int argc, char* argv[]) {
// TODO better arg parser
// option 1: use cxxopts and positional arguments
// option 1: take one argument only, being a json objecet
// If no cli is provided (argv[0]), this loop will do nothing
// Otherwise, start with the 2nd element which is the 1st argument
for (int i = 1; i < argc; ++i) {
std::string_view arg(argv[i]);
auto separatorLoc = arg.find(':');
if (separatorLoc != std::string_view::npos) {
auto opcode = ParseInputOpcode(arg.substr(0, separatorLoc));
auto operand = arg.substr(separatorLoc);
HandleArgument(opcode, operand);
}
}
return 0;
}
|