-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexer.cpp
More file actions
101 lines (91 loc) · 2.07 KB
/
Copy pathLexer.cpp
File metadata and controls
101 lines (91 loc) · 2.07 KB
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
#include <cstdlib>
#include <iostream>
#include <fstream>
#include "Lexer.hpp"
std::vector<Vertex> & Lexer::readFile(std::string const filename)
{
std::string line;
std::ifstream file(filename.c_str());
std::vector<Vertex> *ret = new std::vector<Vertex>;
if(file.is_open())
{
while (std::getline(file, line))
{
if (Lexer::_checkFormat(line))
Lexer::_lexLine(line, ret);
}
file.close();
}
else
std::cerr << "Unable to open file" << std::endl;
return *ret;
}
void Lexer::_lexLine(std::string const line, std::vector<Vertex> * ret)
{
std::stringstream tmp;
unsigned int i = 0;
unsigned int j;
unsigned int k = 0;
while (k < line.size() - 1)
{
std::stringstream *tmp = new std::stringstream;
while (line[i] != '(' && i < line.size())
i++;
i++;
for (j = i; line[j] != ')' && j < line.size(); j++)
{
*tmp << line[j];
}
ret->push_back(Lexer::_getVertex(tmp->str()));
k = j;
i = j;
delete tmp;
}
}
Vertex Lexer::_getVertex(std::string const tmp)
{
double nb[3];
unsigned int i = 0;
unsigned int j = 0;
unsigned int k = 0;
//double prct;
for (i = 0; i < tmp.size(); i++)
{
for (j = i; tmp[j] != ',' && j < tmp.size(); j++);
nb[k] = atof((tmp.substr(i, j - i)).c_str());
k++;
i = j;
}
/*for ( i = 0; i < 4; i++)
{
if (nb[i] > 800)
{
prct = (nb[i] / 20000);
std::cout << " prct = " << std::endl;
nb[i] = 800 * prct;
std::cout << "nb[" << i << "] = " << nb[i] << std::endl;
}
}*/
Vertex ret(nb[0], nb[1], nb[2]);
return ret;
}
bool Lexer::_checkFormat(std::string const line)
{
int crochet1 = 0;
int crochet2 = 0;
int virgule = 0;
unsigned int i = 0;
while (i < line.size())
{
if (line[i] == '(' && crochet1 == crochet2)
crochet1++;
if (line[i] == ',' && crochet1 > 0 && crochet1 == crochet2 + 1)
virgule++;
if (line[i] == ')' && crochet1 == crochet2 + 1 && virgule / 2 == crochet1 && virgule % 2 == 0)
crochet2++;
i++;
}
if (crochet1 == crochet2 && crochet1 > 0)
return 1;
return 0;
}