-
Notifications
You must be signed in to change notification settings - Fork 0
/
PreprocessSAMP.cpp
95 lines (86 loc) · 2.81 KB
/
PreprocessSAMP.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
#include "PreprocessSAMP.hpp"
#include "SimpleCppTextFileHandler/file.hpp"
#include <filesystem>
std::vector<std::string> SplitString(std::string str,std::string delimeter);//just need one def, not whole header
std::vector<std::string> readFiles;
std::filesystem::path cwd;
std::string getQuotesValue(std::string& base, int initialI = 0){
std::string output;
bool fp = false;
for(int i=initialI;i<base.length();i++){
if(fp){
if(base[i] == '\"'){
return(output);
}else{
output += base[i];
}
}else{
if(base[i] == '\"'){
fp = true;
}
}
}
return("");
}
std::vector<std::string> readSAMP(std::string& filename){
for(int i=0;i<readFiles.size();i++){
if(readFiles[i] == filename){
std::cout<<"Preprocessor Error: recursive include of "<<filename<<std::endl;
return(std::vector<std::string>());
}
}
readFiles.push_back(filename);
if(!fileExists((cwd/filename).string())){
std::cout<<"Error: file doesn't exists: "<<filename<<std::endl;
return(std::vector<std::string>());
}
std::string file = readFile((cwd/filename).string());
if(file == ""){
std::cout<<"Error: file contents was empty: "<<filename<<std::endl;
return(std::vector<std::string>());
}
std::vector<std::string> lines = SplitString(file, "\n");
int linesTrue = 0;
std::vector<std::string> output;
for(int i=0;i<lines.size();i++){
if(lines[i].length() > 0 && lines[i][0] == '#'){
if(lines[i].length() > 10 && lines[i].substr(1, 7) == "include"){
std::string quotes = getQuotesValue(lines[i], 9);
if(quotes == ""){
std::cout<<"Preprocessor Error: unable to parse include statement: "<<lines[i]<<" ("<<filename<<"["<<linesTrue<<"])"<<std::endl;
}else{
if(quotes.find("/") != std::string::npos){
std::filesystem::path newFilePos = cwd/quotes;
if(newFilePos.has_filename() && newFilePos.has_parent_path()){
std::string newFilename = newFilePos.filename().string();
cwd = newFilePos.parent_path();
std::vector<std::string> newLines = readSAMP(newFilename);
for(int j=0;j<newLines.size();j++){
output.push_back(newLines[j]);
}
}else{
std::cout<<"Preprocessor Error: \""<<quotes<<"\" is not a valid path"<<" ("<<filename<<"["<<linesTrue<<"])"<<std::endl;
}
}else{
std::vector<std::string> newLines = readSAMP(quotes);
for(int j=0;j<newLines.size();j++){
output.push_back(newLines[j]);
}
}
}
}else{
std::cout<<"Preprocessor Error: unknown preprocessor flag: "<<lines[i]<<" ("<<filename<<"["<<linesTrue<<"])"<<std::endl;
}
}else{
output.push_back(lines[i]);
}
linesTrue++;
}
return(output);
}
std::vector<std::string> preprocessSAMP(std::string& filename){
readFiles.clear();
cwd = std::filesystem::current_path();
std::vector<std::string> read = readSAMP(filename);
return(read);
}