blob: ca69f6c386d85bb0012a584299c38e942154464b (
plain) (
blame)
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
|
#include "freevars.h"
#include <fstream>
freevars::freevars()
{
vars = new map<string, vector<string>* >();
}
void freevars::addVar(string name, string filename)
{
vector<string>* eltlist = new vector<string>();
ifstream infile(filename.c_str());
if (infile)
{
string line;
while (getline(infile, line))
{
eltlist->push_back(line);
}
} else {
eltlist->push_back("");
}
(*vars)[name] = eltlist;
}
string freevars::parse(string in)
{
string res(in);
for (map<string, vector<string>* >::iterator it = vars->begin(); it != vars->end(); it++)
{
string tofind = "$" + it->first + "$";
size_t fpos = res.find(tofind);
if (fpos != string::npos)
{
int r = rand() % it->second->size();
res.replace(fpos, tofind.length(), (*it->second)[r], 0, string::npos);
}
}
return res;
}
|