Read the input files

1

Read the input files

2
@Add(global elements)
@put(globals)
@End(global elements)

The Inputs class

3

The Inputs class

4
@def(globals)
@Put(inputs prereqs)
class Inputs {
public:
@Put(inputs elements)
private:
@Put(private inputs elements)
};
@end(globals)
5
@Def(includes)
#include <string>
@End(includes)
6
@Def(inputs elements)
void read_line(std::string &line);
@End(inputs elements)
7
@add(globals)
void Inputs::read_line(std::string &line) {
@put(inputs read line)
}
@end(globals)
8
@Def(inputs prereqs)
struct No_More_Lines {};
@End(inputs prereqs)
9
@def(inputs read line)
throw No_More_Lines {};
@end(inputs read line)

Reading everything

10

Reading everything

11
@add(globals)
Inputs inputs;
@end(globals)
12
@add(globals)
@Put(needed by read_sources)
void read_sources() {
@put(read sources)
}
@end(globals)
13
@Def(read source files)
read_sources();
@End(read source files)
14
@Add(inputs elements)
void clear() {
@Put(clear inputs)
}
@End(inputs elements)
15
@def(read sources) {
inputs.clear();
clear_frags();
@Put(additional read vars)
std::string line;
bool skip_spaces { false };
try { for (;;) {
inputs.read_line(line);
@Put(process line)
} }
catch (const No_More_Lines &) {}
eval_metas();
} @end(read sources)

What is a file?

16

What is a file?

Input class

17

Input class

18
@Add(inputs prereqs)
@Put(open input prereqs)
class Open_Input {
public:
@Put(open input elements)
private:
@Put(private open input els)
};
@End(inputs prereqs)
19
@Def(open input prereqs)
@Put(input prereqs)
class Input {
public:
@Put(input elements)
const std::string prev;
};
@End(open input prereqs)
20
@Add(includes)
#include <fstream>
@End(includes)
21
@Def(private open input els)
std::string path_;
std::ifstream file_;
@End(private open input els)
22
@Def(open input elements)
Open_Input(const std::string &path):
path_ { path },
file_ { path.c_str() }
{}
@End(open input elements)
23
@Add(open input elements)
const std::string &path() const {
return path_;
}
@End(open input elements)
24
@Add(open input elements)
Open_Input(const Open_Input &) = delete;
Open_Input(Open_Input &&) = default;
@End(open input elements)
25
@Add(open input elements)
Open_Input &operator=(const Open_Input &) = delete;
Open_Input &operator=(Open_Input &&) = default;
@End(open input elements)
26
@Def(input elements)
Input(const std::string &prev = {}): prev { prev } { }
@End(input elements)
27
@Add(input elements)
Input(const Input &) = delete;
Input(Input &&) = default;
@End(input elements)
28
@Add(input elements)
Input &operator=(const Input &) = delete;
@End(input elements)
29
@Add(open input elements)
void read_line(std::string &line) {
if (file_.is_open()) {
@put(get line)
}
throw No_More_Lines {};
}
@end(open input elements)
30
@def(get line)
if (std::getline(file_, line)) {
@Put(line read)
return;
}
file_.close();
@end(get line)
31
@Def(private inputs elements)
std::vector<std::string> roots_;
std::vector<std::string>::const_iterator current_path_;
@End(private inputs elements)
32
@Add(private inputs elements)
std::vector<Open_Input> open_;
std::map<std::string, Input> used_;
@End(private inputs elements)
33
@rep(inputs read line)
for (;;) {
@put(push next path)
try {
open_.back().read_line(line);
return;
}
catch (const No_More_Lines &) {}
@put(save open input)
open_.pop_back();
}
throw No_More_Lines {};
@end(inputs read line)
34
@def(push next path)
if (open_.empty()) {
if (current_path_ != roots_.end()) {
push(*current_path_++);
} else {
break;
}
}
@end(push next path)
35
@def(save open input)
auto &f { used_.find(open_.back().path())->second };
if (f.blocks.empty()) {
f.blocks.push_back({
RS::header, { "EMPTY FILE" }, {}
});
}
@end(save open input)