-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemulator.cpp
189 lines (156 loc) · 4.7 KB
/
emulator.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include "emulator.h"
#include <fstream>
#include <iostream>
#include <string>
#include <sys/stat.h>
#include <unistd.h>
namespace chimera
{
namespace test
{
//==============================================================================
bool exists_file(const std::string &name)
{
std::ifstream f(name.c_str());
return f.good();
}
//==============================================================================
std::vector<std::string> split(const std::string &buffer,
const std::string &separator)
{
std::string line;
std::vector<std::string> lines;
for (std::size_t i = 0; i < buffer.size(); ++i)
{
if (buffer.compare(i, separator.size(), separator))
{
line.push_back(buffer[i]);
}
else
{
lines.push_back(line);
line.resize(0);
}
}
if (line.size())
lines.push_back(line);
return lines;
}
//==============================================================================
std::vector<const char *> convertArgs(const std::vector<std::string> &args)
{
std::vector<const char *> argv;
for (const auto &arg : args)
argv.push_back(arg.data());
return argv;
}
//==============================================================================
Emulator::Emulator()
{
// Do nothing
}
//==============================================================================
bool Emulator::Run()
{
std::vector<std::string> args;
args.push_back("");
args.push_back("-p=" + GetBuildPath());
if (!binding_.empty())
args.push_back("-b=" + binding_);
if (!config_filepath_.empty())
args.push_back("-c=" + config_filepath_);
if (!modulename_.empty())
args.push_back("-m=" + modulename_);
for (const auto &path : sources_)
{
const auto abs_path = GetExamplesDirPath() + path;
// TODO: assumed path is a relative path
if (!exists_file(abs_path))
{
continue;
}
else
{
args.push_back(abs_path);
}
}
std::vector<const char *> argv = convertArgs(args);
return chimera::run(static_cast<int>(argv.size()), argv.data()) == 0;
}
//==============================================================================
bool Emulator::Run(const std::string &args)
{
std::vector<std::string> args_splits = split(args, " ");
std::vector<const char *> argv;
argv.push_back("");
for (auto &arg : args_splits)
argv.push_back(arg.data());
return chimera::run(static_cast<int>(argv.size()), argv.data()) == 0;
}
//==============================================================================
bool Emulator::RunHelp()
{
int argc = 2;
std::vector<const char *> argv;
argv.push_back("");
argv.push_back("--help");
return chimera::run(argc, argv.data()) == 0;
}
//==============================================================================
bool Emulator::RunHelpList()
{
int argc = 2;
std::vector<const char *> argv;
argv.push_back("");
argv.push_back("--help-list");
return chimera::run(argc, argv.data()) == 0;
}
//==============================================================================
bool Emulator::RunVersion()
{
int argc = 2;
std::vector<const char *> argv;
argv.push_back("");
argv.push_back("--version");
return chimera::run(argc, argv.data()) == 0;
}
//==============================================================================
void Emulator::SetSource(const std::string &filename)
{
sources_.clear();
sources_.push_back(filename);
}
//==============================================================================
void Emulator::SetSources(const std::vector<std::string> &filenames)
{
sources_ = filenames;
}
//==============================================================================
void Emulator::SetModuleName(const std::string &name)
{
modulename_ = name;
}
//==============================================================================
void Emulator::SetBinding(const std::string &name)
{
binding_ = name;
}
//==============================================================================
void Emulator::SetConfigurationFile(const std::string &path)
{
config_filepath_ = GetExamplesDirPath() + path;
}
//==============================================================================
const std::string &Emulator::GetExamplesDirPath()
{
static auto path = std::string(EXAMPLES_PATH);
return path;
}
//==============================================================================
const std::string &Emulator::GetBuildPath()
{
static auto path = std::string(BUILD_PATH);
return path;
}
} // namespace test
} // namespace chimera