Another minor change, but this should almost get us to the point that we
[lyx.git] / src / Variables.cpp
blob931c2ed22410544207470a221056028e84f3b3ea
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"
17 using namespace std;
19 void Variables::set(string const & var, string const & val)
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;;
29 string const Variables::get(string const & var) const
31 Vars::const_iterator cit = vars_.find(var);
32 if (cit != vars_.end())
33 return cit->second;
34 else
35 return string();
39 bool Variables::isSet(string const & var) const
41 Vars::const_iterator cit = vars_.find(var);
42 return (cit != vars_.end());
46 string const Variables::expand(string const & s) const
48 string str(s);
49 LRegex reg("\\$\\{\\(.*\\)\\}");
51 if (!reg.exact_match(str))
52 return str;
54 LRegex::MatchPair match;
55 string var;
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));
64 return str;
67 #ifdef TEST
69 #include <iostream>
71 namespace lyx {
73 int main() {
74 Variables vars;
75 vars.set("x", "hello");
76 vars.set("y", "world");
77 cout << vars.expand("${x}") << endl;
80 #endif
83 } // namespace lyx