]> git.lyx.org Git - lyx.git/blob - src/Variables.cpp
infrastructure for 'graceful asserts'
[lyx.git] / src / Variables.cpp
1 /**
2  * \file Variables.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "Variables.h"
15 #include "support/LRegex.h"
16
17 using namespace std;
18
19 void Variables::set(string const & var, string const & val)
20 {
21         // We want to use const_iterator (Lgb)
22         Vars::iterator cit = vars_.find(var);
23         if (cit != vars_.end())
24                 vars_.erase(var);
25         vars_[var] = val;;
26 }
27
28
29 string const Variables::get(string const & var) const
30 {
31         Vars::const_iterator cit = vars_.find(var);
32         if (cit != vars_.end())
33                 return cit->second;
34         else
35                 return string();
36 }
37
38
39 bool Variables::isSet(string const & var) const
40 {
41         Vars::const_iterator cit = vars_.find(var);
42         return (cit != vars_.end());
43 }
44
45
46 string const Variables::expand(string const & s) const
47 {
48         string str(s);
49         LRegex reg("\\$\\{\\(.*\\)\\}");
50
51         if (!reg.exact_match(str))
52                 return str;
53
54         LRegex::MatchPair match;
55         string var;
56
57         do {
58                 match = reg.first_match(str);
59                 var = str.substr(match.first,match.second);
60                 // we correct the match to take ${} in account.
61                 str.replace(match.first - 2, match.second + 3, get(var));
62         } while (reg.exact_match(str));
63
64         return str;
65 }
66
67 #ifdef TEST
68
69 #include <iostream>
70
71 namespace lyx {
72
73 int main() {
74         Variables vars;
75         vars.set("x", "hello");
76         vars.set("y", "world");
77         cout << vars.expand("${x}") << endl;
78 }
79
80 #endif
81
82
83 } // namespace lyx