]> git.lyx.org Git - lyx.git/blob - src/Variables.cpp
Fixed some lines that were too long. It compiled afterwards.
[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
18 void Variables::set(string const & var, string const & val)
19 {
20         // We want to use const_iterator (Lgb)
21         Vars::iterator cit = vars_.find(var);
22         if (cit != vars_.end())
23                 vars_.erase(var);
24         vars_[var] = val;;
25 }
26
27
28 string const Variables::get(string const & var) const
29 {
30         Vars::const_iterator cit = vars_.find(var);
31         if (cit != vars_.end())
32                 return cit->second;
33         else
34                 return string();
35 }
36
37
38 bool Variables::isSet(string const & var) const
39 {
40         Vars::const_iterator cit = vars_.find(var);
41         return (cit != vars_.end());
42 }
43
44
45 string const Variables::expand(string const & s) const
46 {
47         string str(s);
48         LRegex reg("\\$\\{\\(.*\\)\\}");
49
50         if (!reg.exact_match(str))
51                 return str;
52
53         LRegex::MatchPair match;
54         string var;
55
56         do {
57                 match = reg.first_match(str);
58                 var = str.substr(match.first,match.second);
59                 // we correct the match to take ${} in account.
60                 str.replace(match.first - 2, match.second + 3, get(var));
61         } while (reg.exact_match(str));
62
63         return str;
64 }
65
66 #ifdef TEST
67
68 #include <iostream>
69
70
71 namespace lyx {
72 using std::endl;
73 using std::cout;
74
75 int main() {
76         Variables vars;
77         vars.set("x", "hello");
78         vars.set("y", "world");
79         cout << vars.expand("${x}") << endl;
80 }
81
82 #endif
83
84
85 } // namespace lyx