LyX 1.5.0 is released
[lyx.git] / src / Variables.cpp
blob46e542a3211b45e4909dc50c3f45b5bcf46e2807
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.
6 * \author Lars Gullik Bjønnes
7 * \author Jean-Marc Lasgouttes
9 * Full author contact details are available in file CREDITS.
12 #include <config.h>
14 #include "Variables.h"
15 #include "support/LRegex.h"
18 void Variables::set(string const & var, string const & val)
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;;
28 string const Variables::get(string const & var) const
30 Vars::const_iterator cit = vars_.find(var);
31 if (cit != vars_.end())
32 return cit->second;
33 else
34 return string();
38 bool Variables::isSet(string const & var) const
40 Vars::const_iterator cit = vars_.find(var);
41 return (cit != vars_.end());
45 string const Variables::expand(string const & s) const
47 string str(s);
48 LRegex reg("\\$\\{\\(.*\\)\\}");
50 if (!reg.exact_match(str))
51 return str;
53 LRegex::MatchPair match;
54 string var;
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));
63 return str;
66 #ifdef TEST
68 #include <iostream>
71 namespace lyx {
72 using std::endl;
73 using std::cout;
75 int main() {
76 Variables vars;
77 vars.set("x", "hello");
78 vars.set("y", "world");
79 cout << vars.expand("${x}") << endl;
82 #endif
85 } // namespace lyx