-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplates.cpp
35 lines (32 loc) · 1020 Bytes
/
templates.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
#include "templates.hpp"
#include <stdexcept>
#include <fstream>
#include <streambuf>
std::string
mt940::read_file_to_string(std::string const &filename) {
std::ifstream t(filename);
if(!t)
throw std::runtime_error("cannot open “"+filename+"”");
return std::string(std::istreambuf_iterator<char>(t),std::istreambuf_iterator<char>());
}
std::string mt940::replace_all_substrings(
std::string subject,
std::string const &search,
std::string const &replace) {
size_t pos{0};
while ((pos = subject.find(search, pos)) != std::string::npos) {
subject.replace(pos, search.length(), replace);
pos += replace.length();
}
return subject;
}
std::string
mt940::replace_template_file(
std::string const &filename,
replacement_map const &replacements) {
std::string result{read_file_to_string(filename)};
for(auto replacement : replacements) {
result = replace_all_substrings(result,"${"+replacement.first+"}",replacement.second);
}
return result;
}