-
Notifications
You must be signed in to change notification settings - Fork 0
/
miniParser.cpp
137 lines (99 loc) · 2.86 KB
/
miniParser.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
#include <iostream>
#include <string>
#include "miniParser.hpp"
char FileSource::nextChar()
{
if(file.eof()) return character = std::char_traits<char>::eof();//EOF;
file.get(character);
return character;
}
char Lexer::skipWhitespaces()
{
while(!source.isEOF() && isspace(source.getCharacter())) source.nextChar();
return source.getCharacter();
}
std::string Lexer::getString()
{
std::string ret;
skipWhitespaces();
while(!source.isEOF() && !isspace(source.getCharacter()))
{
ret = ret + source.getCharacter();
source.nextChar();
}
if(source.isEOF()) return "\0";
return ret;
}
double Lexer::getNumber()
{
char c = source.getCharacter();
int i = c - '0';
while(isDigit(c = source.nextChar()))
{
i *= 10;
i += (c - '0');
}
if(c != '.') return (double)i;
double d = 0, j = 10;
while(isDigit(c = source.nextChar()))
{
double temp = (c - '0') / j;
d += temp;
j *= 10;
}
return i+d;
}
void MiniParser::parse()
{
std::string token = lex.getString();
if(token != "NODES") return;
lex.getString();
std::ofstream verts;
verts.open("ver.txt", std::ios::trunc);
int j = 0;
while((token = lex.getString()) != "\0" && (token) != ")")
{
std::cout << token << " = " << j << std::endl;
verts << j << '\n';
vert_map[token] = j++;
for(int i=0;i<4;++i) lex.getString();
}
verts.close();
//-----------------------------------------------------
token = lex.getString();
if(token != "LINKS") return;
lex.getString();
std::ofstream links;
links.open("edges.txt", std::ios::trunc);
while((token = lex.getString()) != "\0" && token != ")")
{
for(int i=0;i<2;++i) token = lex.getString();
std::cout << "Link: "<< token << " ";
links << vert_map[token] << ' ';
links << vert_map[token = lex.getString()] << '\n';
std::cout << token << std::endl;
for(int i=0;i<11;++i) lex.getString();
}
links.close();
//-----------------------------------------------------
token = lex.getString();
if(token != "DEMANDS") return;
lex.getString();
std::ofstream demands;
demands.open("demands.txt", std::ios::trunc);
while((token = lex.getString()) != "\0" && token != ")")
{
for(int i=0;i<2;++i) token = lex.getString();
std::cout << "Demand: "<< token << " ";
demands << vert_map[token] << ' ';
demands << vert_map[token = lex.getString()] << ' ';
std::cout << token << " ";
for(int i=0;i<2;++i) lex.getString();
token = lex.getString();
token.erase(token.end()-3, token.end());
demands << token << '\n';
std::cout << token << std::endl;
for(int i=0;i<1;++i) lex.getString();
}
demands.close();
}