-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpFile.cpp
More file actions
98 lines (85 loc) · 2.16 KB
/
SimpFile.cpp
File metadata and controls
98 lines (85 loc) · 2.16 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
#include "SimpFile.hpp"
#include <fstream>
#include <string>
#include <exception>
#include <vector>
#include <algorithm>
SimpFile::SimpFile(const std::string& fileName) : currentStream(fileName)
{
if (currentStream)
{
std::string line;
while (std::getline(currentStream, line))
{
parseAndAddLine(line);
}
}
else
{
throw std::invalid_argument("Cannot open file");
}
}
std::vector<Command> SimpFile::commands() const
{
return _commands;
}
std::vector<std::string> SimpFile::getTokens(const std::string& line)
{
std::vector<std::string> result;
static auto findDelimLambda = [](const char c)
{
return (c == ' ' || c == '\t' || c == ',' || c == '\n ' || c == '(' || c == ')');
};
auto currentIter = line.cbegin();
auto nextDelim = currentIter;
while ((nextDelim = std::find_if(currentIter, line.cend(), findDelimLambda)) != line.cend())
{
if (std::distance(currentIter, nextDelim) > 0)
{
auto token = std::string(currentIter, nextDelim);
result.push_back(token);
}
// If same as current
else
{
// Move forward once
nextDelim = std::next(nextDelim);
}
// Move the search forward
currentIter = nextDelim;
}
if (std::distance(currentIter, nextDelim) > 0)
{
auto token = std::string(currentIter, nextDelim);
result.push_back(token);
}
return result;
}
void SimpFile::parseAndAddLine(const std::string& line)
{
auto tokens = getTokens(line);
if (tokens.size() > 0 && (OperationTokens.find(tokens[0]) != OperationTokens.end()))
{
Command command(tokens);
if (command.operation() == Command::Operation::File ||
command.operation() == Command::Operation::ObjectFile)
{
auto fileName = std::get<std::string>(command.parameters());
if (command.operation() == Command::Operation::ObjectFile)
{
_commands.push_back(command);
}
SimpFile childFile(fileName);
auto childCommands = childFile.commands();
_commands.insert(_commands.end(), childCommands.begin(), childCommands.end());
if (command.operation() == Command::Operation::ObjectFile)
{
_commands.push_back(Command{ Command::Operation::ObjectFile, "ENDOFOBJECTFILE"s });
}
}
else
{
_commands.push_back(command);
}
}
}