-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.cpp
172 lines (154 loc) · 3.8 KB
/
template.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
#include <cassert>
#include <fstream>
#include <sstream>
#include <unordered_map>
#include "template.h"
TemplateError::TemplateError(const std::string &msg)
: m_Message(msg)
{
}
TemplateError::~TemplateError() throw()
{
}
const char *TemplateError::what() const throw()
{
return m_Message.c_str();
}
void EmptyVariableProvider::handle_variable(std::ostream &out, const std::string &name)
{
throw TemplateError("Missing value for variable " + name);
}
static void raise_error(const std::string &filename, int pos,
const std::string &error)
{
std::ostringstream msg;
msg << "Error in " << filename << " character " << pos
<< ": " << error;
throw TemplateError(msg.str());
}
Template::Template(const std::string &filename)
{
std::ifstream source(filename, std::ios::in | std::ios::binary);
if(source.bad())
throw TemplateError("Can't open file: " + filename);
compile(source, filename);
}
Template::Template(std::istream &source, const std::string &filename)
{
compile(source, filename);
}
void Template::compile(std::istream &source, const std::string &filename)
{
std::string current;
enum {
NORMAL,
OPEN_1,
OPEN_2,
VARNAME,
CLOSE_1,
CLOSE_2
} state = NORMAL;
std::istreambuf_iterator<char> i(source), e;
size_t pos = 0;
for(; i != e; ++i, ++pos)
{
char c = *i;
switch(state)
{
case NORMAL:
if(c == '{')
state = OPEN_1;
else
current += c;
break;
case OPEN_1:
if(c == '{')
state = OPEN_2;
else
{
current += '{';
state = NORMAL;
current += c;
}
break;
case OPEN_2:
if(!is_space(c))
{
m_Blocks.push_back(Block(DATA, current));
current = c;
state = VARNAME;
}
break;
case VARNAME:
if(is_space(c))
state = CLOSE_1;
else if(c == '}')
state = CLOSE_2;
else
{
current += c;
break;
}
assert(state != VARNAME);
m_Blocks.push_back(Block(VARIABLE, current));
current.clear();
break;
case CLOSE_1:
if(c == '}')
state = CLOSE_2;
else if(!is_space(c))
raise_error(filename, pos, "expecting }");
break;
case CLOSE_2:
if(c == '}')
state = NORMAL;
else
raise_error(filename, pos, "expecting }");
break;
}
}
if(state == OPEN_1)
{
current += '{';
state = NORMAL;
}
else if(state != NORMAL)
raise_error(filename, pos, "premature end of stream");
if(!current.empty())
m_Blocks.push_back(Block(DATA, current));
}
Template::~Template()
{
}
bool Template::is_space(char c)
{
return c == ' ' || c == '\t' || c == '\n';
}
void Template::render(std::ostream &out, VariableProvider &vars)
{
for(const Block &block : m_Blocks)
{
switch(block.first)
{
case DATA:
out << block.second;
break;
case VARIABLE:
vars.handle_variable(out, block.second);
break;
}
}
}
void Template::render(std::ostream &out,
std::initializer_list<const char*> vars)
{
std::unordered_map<std::string, std::string> map;
assert(vars.size() % 2 == 0);
auto iter = vars.begin();
while(iter != vars.end())
{
const char *key = *iter++;
map[key] = *iter++;
}
render(out, map);
}