]> git.lyx.org Git - lyx.git/blob - src/Variables.C
update libtool
[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-2001 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         // We want to use const_iterator (Lgb)
24         Vars::iterator cit = vars_.find(var);
25         if (cit != vars_.end()) 
26                 vars_.erase(var);
27         vars_[var] = val;;
28 }
29
30
31 string const Variables::get(string const & var) const
32 {
33   Vars::const_iterator cit = vars_.find(var);
34   if (cit != vars_.end()) 
35     return (*cit).second;
36   else 
37     return string();
38 }
39
40
41 bool Variables::set(string const & var) const
42 {
43    Vars::const_iterator cit = vars_.find(var);
44    return  (cit != vars_.end()); 
45 }
46
47
48 string const Variables::expand(string const & s) const
49 {
50   string str(s);
51   LRegex reg("\\$\\{\\(.*\\)\\}");
52
53   if (!reg.exact_match(str))
54     return str;
55
56   LRegex::MatchPair match;
57   string var;
58   
59   do {
60     match = reg.first_match(str);
61     var = str.substr(match.first,match.second);
62     // we correct the match to take ${} in account.
63     str.replace(match.first - 2, match.second + 3, get(var));
64   } while (reg.exact_match(str));
65
66   return str;
67 }
68
69 #ifdef TEST
70
71 #include <iostream>
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