]> git.lyx.org Git - features.git/commitdiff
Rewrite ReplaceEnvironmentPath in a sane manner.
authorAngus Leeming <leeming@lyx.org>
Thu, 25 Sep 2003 23:01:43 +0000 (23:01 +0000)
committerAngus Leeming <leeming@lyx.org>
Thu, 25 Sep 2003 23:01:43 +0000 (23:01 +0000)
Get rid of lstrings.[Ch]'s regexMatch.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@7830 a592a061-630c-0410-9148-cb99ea01b6c8

src/frontends/xforms/ChangeLog
src/frontends/xforms/FormFiledialog.C
src/support/ChangeLog
src/support/filetools.C
src/support/lstrings.C
src/support/lstrings.h

index 3d140cb9af1a3c894ba03a8a9982c93465e77c9c..0a7296217fd83dc4a7b50c1b55cb4ecc0d4fa573 100644 (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::
index 1234f636f1f9d6a18d7f1b7802d3a0a98f5593a6..dbacd3d32939e85fb5dda0ce38e2b00650e4cd1b 100644 (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()
 {
index 1ba1d463c53589b6155f6535f5165c6e32c1c248..8347f3a4d29d87ed0f6031201352293629e1ded6 100644 (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.
index 26199c185fa5b847e2490218aec42426bc259999..b2503fd6a349f97132f703b8fed12d05d3926489 100644 (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?
-
-// 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;
 }
 
 
index 9a234bdc5a8cc0ee04ba77fc9f068d6e843c09d6..70dd84529405c9ba2e0843fe2e67e2bf0d3fcece 100644 (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);
index 0725ff3f490b21d8a17b80a82f6f14d437d02927..d85dca7a4e716d3afdea584ecc036d4d0d7c744e 100644 (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);