-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
72 lines (58 loc) · 1.74 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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <locale>
#include "Util.h"
#include "parser/Parser.h"
#include "constructor/Constructor.h"
/*
main function
the entry point of the program
*/
int main(int argc, char *argv[]){
bool error = false;
for(int i=1;i<argc;i++){
//open argv[i] file
std::string arg(argv[i]);
std::string out = splitString(arg, '.')[0] + ".cpp";
std::ifstream src;
src.open(arg.c_str());
//if the src file successfully opened
if(src.is_open()){
std::ofstream dest;
std::vector<std::string> input;
std::vector<std::string> output;
//get all lines from src file and place them in a std::vector<std::string>
std::string line;
while(getline(src, line)){
input.push_back(line);
}
//try-catch to catch errors in parser and constructor functions
try{
//run the lines from the src file through the parser function
//run the parser output through the constructor function
//save constructor output to output std::vector<std::string>
output = constructor(parser(input));
//open dest file if there have been no errors
dest.open(out.c_str());
}catch(const std::exception&){
//close src file and return EXIT_FAILURE if an exception is caught
src.close();
std::cout << "Error caught" << std::endl;
return EXIT_FAILURE;
}
//write output to new file
for(int x=0;x<output.size();x++){
dest << output.at(x) + "\n";
}
//close both src and dest files
src.close();
dest.close();
//error message if input file can't be found
}else{
std::cout << "File " << arg << " missing or not found." << std::endl;
}
}
}