2000-07-18 11:06:04 +00:00
|
|
|
/* This file is part of
|
|
|
|
* ======================================================
|
|
|
|
*
|
|
|
|
* LyX, The Document Processor
|
|
|
|
*
|
|
|
|
* Copyright 1995 Matthias Ettrich
|
|
|
|
* Copyright 1995-2000 the LyX Team.
|
|
|
|
*
|
|
|
|
* ====================================================== */
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation "Variables.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "Variables.h"
|
|
|
|
#include "support/LRegex.h"
|
|
|
|
|
2000-07-18 17:45:27 +00:00
|
|
|
|
|
|
|
void Variables::set(string const & var, string const & val)
|
2000-07-18 11:06:04 +00:00
|
|
|
{
|
|
|
|
Vars::const_iterator cit = vars_.find(var);
|
|
|
|
if (cit != vars_.end())
|
|
|
|
vars_.erase(var);
|
|
|
|
vars_[var] = val;;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
string const Variables::get(string const & var) const
|
2000-07-18 11:06:04 +00:00
|
|
|
{
|
|
|
|
Vars::const_iterator cit = vars_.find(var);
|
|
|
|
if (cit != vars_.end())
|
|
|
|
return (*cit).second;
|
|
|
|
else
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
|
2000-07-18 17:45:27 +00:00
|
|
|
|
2000-07-18 11:06:04 +00:00
|
|
|
bool Variables::isset(string const & var) const
|
|
|
|
{
|
|
|
|
Vars::const_iterator cit = vars_.find(var);
|
|
|
|
return (cit != vars_.end());
|
|
|
|
}
|
|
|
|
|
2000-07-18 17:45:27 +00:00
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
string const Variables::expand(string const & s) const
|
2000-07-18 11:06:04 +00:00
|
|
|
{
|
2000-07-18 17:45:27 +00:00
|
|
|
string str(s);
|
2000-07-18 11:06:04 +00:00
|
|
|
LRegex reg("\\$\\{\\(.*\\)\\}");
|
|
|
|
|
|
|
|
if (!reg.exact_match(str))
|
|
|
|
return str;
|
|
|
|
|
|
|
|
LRegex::MatchPair match;
|
|
|
|
string var;
|
|
|
|
|
|
|
|
do {
|
|
|
|
match = reg.first_match(str);
|
|
|
|
var = str.substr(match.first,match.second);
|
|
|
|
// we correct the match to take ${} in account.
|
|
|
|
str.replace(match.first - 2, match.second + 3, get(var));
|
|
|
|
} while (reg.exact_match(str));
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TEST
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
using std::endl;
|
|
|
|
using std::cout;
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
Variables vars;
|
|
|
|
vars.set("x", "hello");
|
|
|
|
vars.set("y", "world");
|
|
|
|
cout << vars.expand("${x}") << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|