-
Notifications
You must be signed in to change notification settings - Fork 0
/
smith.cpp~
110 lines (106 loc) · 2.82 KB
/
smith.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
#include "forge.cpp"
#define dash_x "x"
#define dash_s "s"
#define dash "-"
using namespace std;
int getLine(char **line, FILE *file);
int main(int argc, char *argv[]){
int ch = 0;
bool sanity = false;//run sanity check
bool xTern = false;//specify extra filename
bool verbose = false;// verbose mode, gives more details.
char *fileName= "lib.conf";
char *fileOut = "out.h";
char *line;
FILE *conf;
const char *usage = "Usage: ./pacSmith -(sx) [lib.conf]";
const char *welcome = "Running libPacSmith, the libPacForge extender.";
while( (ch = getopt(argc, argv, "svx:")) != -1) {
switch (ch) {
case 'x':
xTern = true;
if(optarg==0){
cout<<usage<<endl;
exit(EXIT_FAILURE);
}
fileName = optarg;
break;
case 's':
sanity = true;
fileOut = "out.h";
break;
case 'v':
verbose = true;
break;
default:
cout<<usage<<endl;
}
}
if((conf = fopen(fileName, "r")) == NULL){
if(errno==ENOENT){
cout<<"file: "<<fileName<<" not found"<<endl;
}else{
perror("File error: ");
}
exit(EXIT_FAILURE);
}
if(verbose){
cout<<welcome<<endl;
cout<<"You have enabled verbose mode,";
cout<<"full details will be provided during runtime"<<endl;
if(xTern)
cout<<"You have elected to use a custom config file: "<<fileName<<endl;
else{
cout<<"You have elected to use the default configuration file: ";
cout<<"lib.conf"<<endl;
}
if(sanity){
cout<<"You have furthermore selected test-run mode, ";
cout<<"which writes output to out.h"<<endl;
//we write to a different file
}
}
int ret = 0;
int i=0;
while(!(ret = getLine(&line, conf))){
if(verbose){
cout<<"Line #"<<i<<": " <<line<<endl;
}
//now that we have a line, we process it, keeping track of any variables.
//we'll use a map, mapping from string to a pair<value, type>
}
//now that the preamble is out of the way
//we want to read in a line at a time, and scan each line.
//we can match variables later
fclose(conf);
}
//reads until a newline is hit, and allocates
int getLine(char **lineOut, FILE *file){
char *line;
fpos_t init_pos;
int lineLen = 0;
if(fgetpos(file, &init_pos)){
perror("line read error: ");
exit(EXIT_FAILURE);
}
char c = '\0';
while((c=fgetc(file))!='\n'){
lineLen++;
if(lineLen>1000){
return -1;//we catch this and indicate that an invalid line was struck
}
}
if(fsetpos(file, &init_pos)){
perror("line read error: ");
exit(EXIT_FAILURE);
}
line = (char*) malloc(sizeof(char)*(lineLen+1));
int i = 0;
while((c=fgetc(file))!='\n'){
line[i] = c;
i++;
}
line[++i] = '\0';
*lineOut = line;
return 0;
}