From: Angus Leeming Date: Thu, 25 Sep 2003 23:01:43 +0000 (+0000) Subject: Rewrite ReplaceEnvironmentPath in a sane manner. X-Git-Tag: 1.6.10~16032 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=15d71955d994e0a462f4501c7e49f0c9732d4189;p=features.git 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 --- diff --git a/src/frontends/xforms/ChangeLog b/src/frontends/xforms/ChangeLog index 3d140cb9af..0a7296217f 100644 --- a/src/frontends/xforms/ChangeLog +++ b/src/frontends/xforms/ChangeLog @@ -1,3 +1,8 @@ +2003-09-25 Angus Leeming + + * FormFiledialog.C (regexMatch): moved here from lstrings.[Ch] because + this is the only place using this 'massaged' regex. + 2003-09-25 Angus Leeming * FormExternal.C (update, apply): InsetExternal::Params:: diff --git a/src/frontends/xforms/FormFiledialog.C b/src/frontends/xforms/FormFiledialog.C index 1234f636f1..dbacd3d329 100644 --- a/src/frontends/xforms/FormFiledialog.C +++ b/src/frontends/xforms/FormFiledialog.C @@ -28,6 +28,7 @@ #include "lyx_forms.h" #include +#include #include #include @@ -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() { diff --git a/src/support/ChangeLog b/src/support/ChangeLog index 1ba1d463c5..8347f3a4d2 100644 --- a/src/support/ChangeLog +++ b/src/support/ChangeLog @@ -1,3 +1,8 @@ +2003-09-25 Angus Leeming + + * filetools.C (ReplaceEnvironmentPath): rewrite to use boost::regex. + * lstrings.[Ch]: (regexMatch): removed. + 2003-09-25 Angus Leeming * translator.h (add): new member function. diff --git a/src/support/filetools.C b/src/support/filetools.C index 26199c185f..b2503fd6a3 100644 --- a/src/support/filetools.C +++ b/src/support/filetools.C @@ -37,7 +37,7 @@ #include "support/std_sstream.h" #include -#include +#include #include #include @@ -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? - -// 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 $ - - // 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 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; - } + // 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]*)\\}"; - if (!result) { - // no correct variable name - result1 += CompareString + res1 + EndString; - result0 = split(res0, res1, CompareChar); - result1 += res1; - continue; - } + boost::regex re("(.*)" + valid_var + "(.*)"); + boost::smatch what; - 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; + 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); } - return result1; + return result; } diff --git a/src/support/lstrings.C b/src/support/lstrings.C index 9a234bdc5a..70dd845294 100644 --- a/src/support/lstrings.C +++ b/src/support/lstrings.C @@ -19,7 +19,6 @@ #include "lyxlib.h" #include "tostr.h" -#include #include #include @@ -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); diff --git a/src/support/lstrings.h b/src/support/lstrings.h index 0725ff3f49..d85dca7a4e 100644 --- a/src/support/lstrings.h +++ b/src/support/lstrings.h @@ -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);