]> git.lyx.org Git - lyx.git/blob - src/Variables.C
Angus insetindex patch + protect patch from Dekel
[lyx.git] / src / Variables.C
1 /* This file is part of
2 * ====================================================== 
3
4 *           LyX, The Document Processor
5 *        
6 *           Copyright 1995 Matthias Ettrich
7 *           Copyright 1995-2000 the LyX Team.
8 *
9 * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation "Variables.h"
15 #endif
16
17 #include "Variables.h"
18 #include "support/LRegex.h"
19
20
21 void Variables::set(string const & var, string const & val)
22 {
23   Vars::const_iterator cit = vars_.find(var);
24   if (cit != vars_.end()) 
25     vars_.erase(var);
26   vars_[var] = val;;
27 }
28
29
30 string Variables::get(string const & var) const
31 {
32   Vars::const_iterator cit = vars_.find(var);
33   if (cit != vars_.end()) 
34     return (*cit).second;
35   else 
36     return string();
37 }
38
39
40 bool Variables::isset(string const & var) const
41 {
42    Vars::const_iterator cit = vars_.find(var);
43    return  (cit != vars_.end()); 
44 }
45
46
47 string Variables::expand(string const & s) const
48 {
49   string str(s);
50   LRegex reg("\\$\\{\\(.*\\)\\}");
51
52   if (!reg.exact_match(str))
53     return str;
54
55   LRegex::MatchPair match;
56   string var;
57   
58   do {
59     match = reg.first_match(str);
60     var = str.substr(match.first,match.second);
61     // we correct the match to take ${} in account.
62     str.replace(match.first - 2, match.second + 3, get(var));
63   } while (reg.exact_match(str));
64
65   return str;
66 }
67
68 #ifdef TEST
69
70 #include <iostream>
71 using std::endl;
72 using std::cout;
73
74 int main() {
75   Variables vars;
76   vars.set("x", "hello");
77   vars.set("y", "world");
78   cout << vars.expand("${x}") << endl;
79 }
80
81 #endif
82