about summary refs log tree commit diff stats
path: root/freevars.cpp
blob: 8c3eda4c94882420857498f94ac5d2dc2fe2b6b4 (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
46
#include "freevars.h"
#include <fstream>
#include <cstdlib>

freevars::freevars()
{
    vars = new std::map<std::string, std::vector<std::string>* >();
}

void freevars::addVar(std::string name, std::string filename)
{
    std::vector<std::string>* eltlist = new std::vector<std::string>();
    
    std::ifstream infile(filename.c_str());
    if (infile)
    {
        std::string line;
        
        while (getline(infile, line))
        {
            eltlist->push_back(line);
        }
    } else {
        eltlist->push_back("");
    }
    
    (*vars)[name] = eltlist;
}

std::string freevars::parse(std::string in)
{
    std::string res(in);
    
    for (std::map<std::string, std::vector<std::string>* >::iterator it = vars->begin(); it != vars->end(); it++)
    {
        std::string tofind = "$" + it->first + "$";
        size_t fpos = res.find(tofind);
        if (fpos != std::string::npos)
        {
            int r = rand() % it->second->size();
            res.replace(fpos, tofind.length(), (*it->second)[r], 0, std::string::npos);
        }
    }
    
    return res;
}