Rewrite ReplaceEnvironmentPath in a sane manner.

Get rid of lstrings.[Ch]'s regexMatch.


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@7830 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Angus Leeming 2003-09-25 23:01:43 +00:00
parent 2a6a261cd1
commit 15d71955d9
6 changed files with 46 additions and 108 deletions

View File

@ -1,3 +1,8 @@
2003-09-25 Angus Leeming <leeming@lyx.org>
* FormFiledialog.C (regexMatch): moved here from lstrings.[Ch] because
this is the only place using this 'massaged' regex.
2003-09-25 Angus Leeming <leeming@lyx.org>
* FormExternal.C (update, apply): InsetExternal::Params::

View File

@ -28,6 +28,7 @@
#include "lyx_forms.h"
#include <boost/bind.hpp>
#include <boost/regex.hpp>
#include <algorithm>
#include <map>
@ -62,8 +63,8 @@ using lyx::support::GetEnvPath;
using lyx::support::LyXReadLink;
using lyx::support::MakeAbsPath;
using lyx::support::OnlyFilename;
using lyx::support::regexMatch;
using lyx::support::split;
using lyx::support::subst;
using lyx::support::suffixIs;
using lyx::support::trim;
@ -197,6 +198,25 @@ int FileDialog::Private::minw_ = 0;
int FileDialog::Private::minh_ = 0;
namespace {
bool regexMatch(string const & a, string const & pattern)
{
// We massage the pattern a bit so that the usual
// shell pattern we all are used to will work.
// One nice thing about using a real regex is that
// things like "*.*[^~]" will work also.
// build the regex string.
string regex = subst(pattern, ".", "\\.");
regex = subst(regex, "*", ".*");
boost::regex reg(regex);
return boost::regex_match(a, reg);
}
} // namespace anon
// Reread: updates dialog list to match class directory
void FileDialog::Private::Reread()
{

View File

@ -1,3 +1,8 @@
2003-09-25 Angus Leeming <leeming@lyx.org>
* filetools.C (ReplaceEnvironmentPath): rewrite to use boost::regex.
* lstrings.[Ch]: (regexMatch): removed.
2003-09-25 Angus Leeming <leeming@lyx.org>
* translator.h (add): new member function.

View File

@ -37,7 +37,7 @@
#include "support/std_sstream.h"
#include <boost/assert.hpp>
#include <boost/cregex.hpp>
#include <boost/regex.hpp>
#include <cctype>
#include <cstdlib>
@ -737,96 +737,25 @@ string const GetFileContents(string const & fname)
}
//
// Search ${...} as Variable-Name inside the string and replace it with
// the denoted environmentvariable
// Allow Variables according to
// variable := '$' '{' [A-Za-z_]{[A-Za-z_0-9]*} '}'
//
// Search the string for ${...} and replace the ... with the value of the
// denoted environment variable
string const ReplaceEnvironmentPath(string const & path)
{
//
// CompareChar: Environment variables starts with this character
// PathChar: Next path component start with this character
// while CompareChar found do:
// Split String with PathChar
// Search Environmentvariable
// if found: Replace Strings
//
char const CompareChar = '$';
char const FirstChar = '{';
char const EndChar = '}';
char const UnderscoreChar = '_';
string EndString; EndString += EndChar;
string FirstString; FirstString += FirstChar;
string CompareString; CompareString += CompareChar;
string const RegExp("*}*"); // Exist EndChar inside a String?
// A valid environment variable is defined as
// $\{[A-Za-z_][A-Za-z_0-9]*\}
string const valid_var = "[$]\\{([A-Za-z_][A-Za-z_0-9]*)\\}";
// first: Search for a '$' - Sign.
//string copy(path);
string result1; //(copy); // for split-calls
string result0 = split(path, result1, CompareChar);
while (!result0.empty()) {
string copy1(result0); // contains String after $
boost::regex re("(.*)" + valid_var + "(.*)");
boost::smatch what;
// Check, if there is an EndChar inside original String.
if (!regexMatch(copy1, RegExp)) {
// No EndChar inside. So we are finished
result1 += CompareString + result0;
result0.erase();
continue;
string result = path;
while (1) {
regex_match(result, what, re, boost::match_partial);
if (!what[0].matched)
break;
result = what.str(1) + GetEnv(what.str(2)) + what.str(3);
}
string res1;
string res0 = split(copy1, res1, EndChar);
// Now res1 holds the environmentvariable
// First, check, if Contents is ok.
if (res1.empty()) { // No environmentvariable. Continue Loop.
result1 += CompareString + FirstString;
result0 = res0;
continue;
}
// check contents of res1
char const * res1_contents = res1.c_str();
if (*res1_contents != FirstChar) {
// Again No Environmentvariable
result1 += CompareString;
result0 = res0;
}
// Check for variable names
// Situation ${} is detected as "No Environmentvariable"
char const * cp1 = res1_contents + 1;
bool result = isalpha(*cp1) || (*cp1 == UnderscoreChar);
++cp1;
while (*cp1 && result) {
result = isalnum(*cp1) ||
(*cp1 == UnderscoreChar);
++cp1;
}
if (!result) {
// no correct variable name
result1 += CompareString + res1 + EndString;
result0 = split(res0, res1, CompareChar);
result1 += res1;
continue;
}
string env(GetEnv(res1_contents + 1));
if (!env.empty()) {
// Congratulations. Environmentvariable found
result1 += env;
} else {
result1 += CompareString + res1 + EndString;
}
// Next $-Sign?
result0 = split(res0, res1, CompareChar);
result1 += res1;
}
return result1;
return result;
}

View File

@ -19,7 +19,6 @@
#include "lyxlib.h"
#include "tostr.h"
#include <boost/regex.hpp>
#include <boost/tokenizer.hpp>
#include <algorithm>
@ -389,21 +388,6 @@ int tokenPos(string const & a, char delim, string const & tok)
}
bool regexMatch(string const & a, string const & pattern)
{
// We massage the pattern a bit so that the usual
// shell pattern we all are used to will work.
// One nice thing about using a real regex is that
// things like "*.*[^~]" will work also.
// build the regex string.
string regex(pattern);
regex = subst(regex, ".", "\\.");
regex = subst(regex, "*", ".*");
boost::regex reg(regex);
return boost::regex_match(a, reg);
}
string const subst(string const & a, char oldchar, char newchar)
{
string tmp(a);

View File

@ -141,11 +141,6 @@ string const token(string const & a, char delim, int n);
int tokenPos(string const & a, char delim, string const & tok);
/** Compares a string and a (simple) regular expression
The only element allowed is "*" for any string of characters
*/
bool regexMatch(string const & a, string const & pattern);
/// Substitute all \a oldchar with \a newchar
string const subst(string const & a, char oldchar, char newchar);