2003-08-23 00:17:00 +00:00
|
|
|
|
/**
|
|
|
|
|
* \file Variables.C
|
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
|
*
|
|
|
|
|
* \author Lars Gullik Bj<EFBFBD>nnes
|
|
|
|
|
* \author Jean-Marc Lasgouttes
|
|
|
|
|
*
|
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
|
*/
|
2000-07-18 11:06:04 +00:00
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#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
|
|
|
|
{
|
2000-11-15 03:22:08 +00:00
|
|
|
|
// We want to use const_iterator (Lgb)
|
|
|
|
|
Vars::iterator cit = vars_.find(var);
|
2002-03-21 17:27:08 +00:00
|
|
|
|
if (cit != vars_.end())
|
2000-11-15 03:22:08 +00:00
|
|
|
|
vars_.erase(var);
|
|
|
|
|
vars_[var] = val;;
|
2000-07-18 11:06:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
|
string const Variables::get(string const & var) const
|
2000-07-18 11:06:04 +00:00
|
|
|
|
{
|
2001-06-29 06:40:08 +00:00
|
|
|
|
Vars::const_iterator cit = vars_.find(var);
|
2002-03-21 17:27:08 +00:00
|
|
|
|
if (cit != vars_.end())
|
2001-07-12 11:11:10 +00:00
|
|
|
|
return cit->second;
|
2002-03-21 17:27:08 +00:00
|
|
|
|
else
|
2001-06-29 06:40:08 +00:00
|
|
|
|
return string();
|
2000-07-18 11:06:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-07-18 17:45:27 +00:00
|
|
|
|
|
2001-06-29 06:40:08 +00:00
|
|
|
|
bool Variables::isSet(string const & var) const
|
2000-07-18 11:06:04 +00:00
|
|
|
|
{
|
2001-06-29 06:40:08 +00:00
|
|
|
|
Vars::const_iterator cit = vars_.find(var);
|
2002-03-21 17:27:08 +00:00
|
|
|
|
return (cit != vars_.end());
|
2000-07-18 11:06:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
{
|
2001-06-29 06:40:08 +00:00
|
|
|
|
string str(s);
|
|
|
|
|
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;
|
2000-07-18 11:06:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef TEST
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lyx {
|
2000-07-18 11:06:04 +00:00
|
|
|
|
using std::endl;
|
|
|
|
|
using std::cout;
|
|
|
|
|
|
|
|
|
|
int main() {
|
2001-06-29 06:40:08 +00:00
|
|
|
|
Variables vars;
|
|
|
|
|
vars.set("x", "hello");
|
|
|
|
|
vars.set("y", "world");
|
|
|
|
|
cout << vars.expand("${x}") << endl;
|
2000-07-18 11:06:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|