-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.cpp
More file actions
82 lines (69 loc) · 1.57 KB
/
Parser.cpp
File metadata and controls
82 lines (69 loc) · 1.57 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
#include "StdAfx.h"
#include "Parser.h"
Parser::Parser(void)
{
}
Parser::~Parser(void)
{
}
const int MAX_LINE_LENGTH = 10000;
bool readline(std::ifstream & inputStream, std::string & line)
{
char buf[MAX_LINE_LENGTH];
inputStream.getline(buf, MAX_LINE_LENGTH);
line = buf;
return inputStream.rdstate() == 0;
}
std::string stripComment(std::string & line)
{
size_t pos = line.find('\'');
if (pos != line.npos)
{
if (pos == 0 || line[pos-1] != ' ')
return "";
else
return line.substr(0, pos);
}
return line;
}
std::vector < std::vector< std::string> > Parser::tokenizeFile(const std::string & fileName)
{
std::ifstream inputStream(fileName.c_str());
std::vector < std::vector< std::string> > allTokens;
std::string line;
while(readline(inputStream, line))
{
line = stripComment(line);
if (! line.empty())
{
std::vector<std::string> tokens = tokenize(line);
allTokens.push_back(tokens);
}
}
return allTokens;
}
std::vector<std::string> Parser::tokenize(std::string line)
{
std::vector<std::string> result;
std::istringstream inputStream(line);
while(inputStream.rdstate() == 0)
{
std::string token;
inputStream >> token;
if (! token.empty() )
result.push_back(token);
}
return result;
}
bool Parser::contains(const TokenString & line, const std::string & string) const
{
return std::find(line.begin(), line.end(), string) != line.end();
}
void Parser::strip(TokenString & line, const std::string & string) const
{
TokenString::iterator it = std::remove(line.begin(), line.end(), string);
if (it != line.end())
{
line.erase(it);
}
}