]> git.lyx.org Git - lyx.git/blob - src/Variables.C
Removed Options->LaTeX and all related code. New Variables.[Ch] files (not
[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 void Variables::set(string const &var, string const &val)
21 {
22   Vars::const_iterator cit = vars_.find(var);
23   if (cit != vars_.end()) 
24     vars_.erase(var);
25   vars_[var] = val;;
26 }
27
28
29 string Variables::get(string const &var) const
30 {
31   Vars::const_iterator cit = vars_.find(var);
32   if (cit != vars_.end()) 
33     return (*cit).second;
34   else 
35     return string();
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 string Variables::expand(string str) const
45 {
46   LRegex reg("\\$\\{\\(.*\\)\\}");
47
48   if (!reg.exact_match(str))
49     return str;
50
51   LRegex::MatchPair match;
52   string var;
53   
54   do {
55     match = reg.first_match(str);
56     var = str.substr(match.first,match.second);
57     // we correct the match to take ${} in account.
58     str.replace(match.first - 2, match.second + 3, get(var));
59   } while (reg.exact_match(str));
60
61   return str;
62 }
63
64 #ifdef TEST
65
66 #include <iostream>
67 using std::endl;
68 using std::cout;
69
70 int main() {
71   Variables vars;
72   vars.set("x", "hello");
73   vars.set("y", "world");
74   cout << vars.expand("${x}") << endl;
75 }
76
77 #endif
78