gnome-commander r2360 - in trunk: . src src/dialogs



Author: epiotr
Date: Tue Dec 16 19:01:18 2008
New Revision: 2360
URL: http://svn.gnome.org/viewvc/gnome-commander?rev=2360&view=rev

Log:
regex: move common code to gnome-cmd-data.h file

Added:
   trunk/src/gnome-cmd-regex.h
Modified:
   trunk/ChangeLog
   trunk/src/Makefile.am
   trunk/src/dialogs/gnome-cmd-advrename-regex-dialog.cc
   trunk/src/dialogs/gnome-cmd-advrename-regex-dialog.h
   trunk/src/gnome-cmd-advrename-dialog.cc
   trunk/src/gnome-cmd-advrename-dialog.h
   trunk/src/gnome-cmd-data.cc
   trunk/src/gnome-cmd-data.h
   trunk/src/gnome-cmd-xml-config.cc

Modified: trunk/src/Makefile.am
==============================================================================
--- trunk/src/Makefile.am	(original)
+++ trunk/src/Makefile.am	Tue Dec 16 19:01:18 2008
@@ -67,6 +67,7 @@
 	gnome-cmd-prepare-copy-dialog.h gnome-cmd-prepare-copy-dialog.cc \
 	gnome-cmd-prepare-move-dialog.h gnome-cmd-prepare-move-dialog.cc \
 	gnome-cmd-prepare-xfer-dialog.h gnome-cmd-prepare-xfer-dialog.cc \
+	gnome-cmd-regex.h \
 	gnome-cmd-remote-dialog.h gnome-cmd-remote-dialog.cc \
 	gnome-cmd-quicksearch-popup.h gnome-cmd-quicksearch-popup.cc \
 	gnome-cmd-rename-dialog.h gnome-cmd-rename-dialog.cc \

Modified: trunk/src/dialogs/gnome-cmd-advrename-regex-dialog.cc
==============================================================================
--- trunk/src/dialogs/gnome-cmd-advrename-regex-dialog.cc	(original)
+++ trunk/src/dialogs/gnome-cmd-advrename-regex-dialog.cc	Tue Dec 16 19:01:18 2008
@@ -29,7 +29,7 @@
 using namespace std;
 
 
-static void response_callback (GtkDialog *dialog, int response_id, GnomeCmdAdvrenameDialog::Regex *rx)
+static void response_callback (GtkDialog *dialog, int response_id, GnomeCmd::RegexReplace *rx)
 {
     switch (response_id)
     {
@@ -50,7 +50,7 @@
 }
 
 
-gboolean gnome_cmd_advrename_regex_dialog_new (const gchar *title, GtkWindow *parent, GnomeCmdAdvrenameDialog::Regex *rx)
+gboolean gnome_cmd_advrename_regex_dialog_new (const gchar *title, GtkWindow *parent, GnomeCmd::RegexReplace *rx)
 {
     GtkWidget *dialog = gtk_dialog_new_with_buttons (title, parent,
                                                      GtkDialogFlags (GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
@@ -82,7 +82,7 @@
 
     entry = gtk_entry_new ();
     gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
-    gtk_entry_set_text (GTK_ENTRY (entry), rx->from.c_str());
+    gtk_entry_set_text (GTK_ENTRY (entry), rx->pattern.c_str());
     g_object_set_data (G_OBJECT (dialog), "pattern", entry);
     gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
     gtk_table_attach_defaults (GTK_TABLE (table), entry, 1, 2, 0, 1);
@@ -93,7 +93,7 @@
 
     entry = gtk_entry_new ();
     gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
-    gtk_entry_set_text (GTK_ENTRY (entry), rx->to.c_str());
+    gtk_entry_set_text (GTK_ENTRY (entry), rx->replacement.c_str());
     g_object_set_data (G_OBJECT (dialog), "replace", entry);
     gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
     gtk_table_attach_defaults (GTK_TABLE (table), entry, 1, 2, 1, 2);
@@ -104,7 +104,7 @@
 
     check = gtk_check_button_new_with_mnemonic (_("_Match case"));
     g_object_set_data (G_OBJECT (dialog), "match_case", check);
-    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check), rx ? rx->case_sensitive : FALSE);
+    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check), rx ? rx->match_case : FALSE);
     gtk_container_add (GTK_CONTAINER (align), check);
 
 #if !GLIB_CHECK_VERSION (2, 14, 0)

Modified: trunk/src/dialogs/gnome-cmd-advrename-regex-dialog.h
==============================================================================
--- trunk/src/dialogs/gnome-cmd-advrename-regex-dialog.h	(original)
+++ trunk/src/dialogs/gnome-cmd-advrename-regex-dialog.h	Tue Dec 16 19:01:18 2008
@@ -22,7 +22,8 @@
 #define __GNOME_CMD_ADVRENAME_REGEX_DIALOG_H__
 
 #include "gnome-cmd-advrename-dialog.h"
+#include "gnome-cmd-regex.h"
 
-gboolean gnome_cmd_advrename_regex_dialog_new (const gchar *title, GtkWindow *parent, GnomeCmdAdvrenameDialog::Regex *rx);
+gboolean gnome_cmd_advrename_regex_dialog_new (const gchar *title, GtkWindow *parent, GnomeCmd::RegexReplace *rx);
 
 #endif // __GNOME_CMD_ADVRENAME_REGEX_DIALOG_H__

Modified: trunk/src/gnome-cmd-advrename-dialog.cc
==============================================================================
--- trunk/src/gnome-cmd-advrename-dialog.cc	(original)
+++ trunk/src/gnome-cmd-advrename-dialog.cc	Tue Dec 16 19:01:18 2008
@@ -589,13 +589,13 @@
 
         for (gboolean valid_iter=gtk_tree_model_get_iter_first (cfg.regexes, &i); valid_iter; valid_iter=gtk_tree_model_iter_next (cfg.regexes, &i))
         {
-            Regex *r;
+            GnomeCmd::RegexReplace *r;
 
             gtk_tree_model_get (cfg.regexes, &i,
                                 COL_REGEX, &r,
                                 -1);
             if (r)                            //  ignore null regex patterns
-                cfg.default_profile.regexes.push_back(GnomeCmdData::AdvrenameConfig::Profile::Regex(r->from, r->to, r->case_sensitive));
+                cfg.default_profile.regexes.push_back(*r);
         }
     }
 
@@ -691,7 +691,7 @@
 
 void GnomeCmdAdvrenameDialog::Private::on_regex_add_btn_clicked (GtkButton *button, GnomeCmdAdvrenameDialog *dialog)
 {
-    Regex *rx = new Regex;
+    GnomeCmd::RegexReplace *rx = new GnomeCmd::RegexReplace;
 
     if (gnome_cmd_advrename_regex_dialog_new (_("Add Rule"), GTK_WINDOW (dialog), rx))
     {
@@ -701,9 +701,9 @@
         gtk_list_store_set (GTK_LIST_STORE (dialog->defaults.regexes), &i,
                             COL_REGEX, rx,
                             COL_MALFORMED_REGEX, !*rx,
-                            COL_PATTERN, rx->from.c_str(),
-                            COL_REPLACE, rx->to.c_str(),
-                            COL_MATCH_CASE, rx->case_sensitive ? _("Yes") : _("No"),
+                            COL_PATTERN, rx->pattern.c_str(),
+                            COL_REPLACE, rx->replacement.c_str(),
+                            COL_MATCH_CASE, rx->match_case ? _("Yes") : _("No"),
                             -1);
 
         dialog->update_new_filenames();
@@ -724,7 +724,7 @@
 
     if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (tree_view), NULL, &i))
     {
-        Regex *rx = NULL;
+        GnomeCmd::RegexReplace *rx = NULL;
 
         gtk_tree_model_get (dialog->defaults.regexes, &i, COL_REGEX, &rx, -1);
 
@@ -733,9 +733,9 @@
             gtk_list_store_set (GTK_LIST_STORE (dialog->defaults.regexes), &i,
                                 COL_REGEX, rx,
                                 COL_MALFORMED_REGEX, !*rx,
-                                COL_PATTERN, rx->from.c_str(),
-                                COL_REPLACE, rx->to.c_str(),
-                                COL_MATCH_CASE, rx->case_sensitive ? _("Yes") : _("No"),
+                                COL_PATTERN, rx->pattern.c_str(),
+                                COL_REPLACE, rx->replacement.c_str(),
+                                COL_MATCH_CASE, rx->match_case ? _("Yes") : _("No"),
                                 -1);
 
             dialog->update_new_filenames();
@@ -751,7 +751,7 @@
 
     if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (tree_view), NULL, &i))
     {
-        Regex *rx = NULL;
+        GnomeCmd::RegexReplace *rx = NULL;
 
         gtk_tree_model_get (dialog->defaults.regexes, &i, COL_REGEX, &rx, -1);
         gtk_list_store_remove (GTK_LIST_STORE (dialog->defaults.regexes), &i);
@@ -773,7 +773,7 @@
 
     for (gboolean valid_iter=gtk_tree_model_get_iter_first (dialog->defaults.regexes, &i); valid_iter; valid_iter=gtk_tree_model_iter_next (dialog->defaults.regexes, &i))
     {
-        Regex *rx = NULL;
+        GnomeCmd::RegexReplace *rx = NULL;
 
         gtk_tree_model_get (dialog->defaults.regexes, &i, COL_REGEX, &rx, -1);
         delete rx;
@@ -1454,11 +1454,11 @@
     char buff[256];
     GtkTreeIter i;
 
-    vector<Regex *> rx;
+    vector<GnomeCmd::RegexReplace *> rx;
 
     for (gboolean valid_iter=gtk_tree_model_get_iter_first (defaults.regexes, &i); valid_iter; valid_iter=gtk_tree_model_iter_next (defaults.regexes, &i))
     {
-        Regex *r;
+        GnomeCmd::RegexReplace *r;
 
         gtk_tree_model_get (defaults.regexes, &i,
                             COL_REGEX, &r,
@@ -1485,9 +1485,9 @@
 
         gchar *fname = g_strdup (buff);
 
-        for (vector<Regex *>::iterator j=rx.begin(); j!=rx.end(); ++j)
+        for (vector<GnomeCmd::RegexReplace *>::iterator j=rx.begin(); j!=rx.end(); ++j)
         {
-            Regex *&r = *j;
+            GnomeCmd::RegexReplace *&r = *j;
 
             gchar *prev_fname = fname;
 

Modified: trunk/src/gnome-cmd-advrename-dialog.h
==============================================================================
--- trunk/src/gnome-cmd-advrename-dialog.h	(original)
+++ trunk/src/gnome-cmd-advrename-dialog.h	Tue Dec 16 19:01:18 2008
@@ -21,11 +21,6 @@
 #ifndef __GNOME_CMD_ADVRENAME_DIALOG_H__
 #define __GNOME_CMD_ADVRENAME_DIALOG_H__
 
-
-#if !GLIB_CHECK_VERSION (2, 14, 0)
-#include <regex.h>
-#endif
-
 #include "gnome-cmd-data.h"
 #include "gnome-cmd-file-list.h"
 
@@ -62,39 +57,6 @@
 
     GtkTreeModel *files;
 
-    class Regex
-    {
-#if GLIB_CHECK_VERSION (2, 14, 0)
-        GRegex *re;
-#else
-        regex_t re;
-        regmatch_t pmatch;
-#endif
-        gboolean malformed_pattern;
-
-      public:
-
-        std::string from;
-        std::string to;
-        gboolean case_sensitive;
-
-        Regex(): malformed_pattern(TRUE), case_sensitive(FALSE)                                  {}
-        Regex(const gchar *pattern, const gchar *replacement, gboolean case_sensitive);
-        ~Regex();
-
-        void assign(const gchar *pattern, const gchar *replacement, gboolean case_sensitive);
-#if GLIB_CHECK_VERSION (2, 14, 0)
-        gchar *replace(const gchar *s)              {  return g_regex_replace (re, s, -1, 0, to.c_str(), G_REGEX_MATCH_NOTEMPTY, NULL);  }
-#else
-        gboolean match(const gchar *s)              {  return regexec(&re, s, 1, &pmatch, 0)==0;  }
-        int start() const                           {  return pmatch.rm_so;                       }
-        int end() const                             {  return pmatch.rm_eo;                       }
-        int length() const                          {  return end() - start();                    }
-#endif
-
-        operator gboolean ()                        {  return !malformed_pattern;                 }
-    };
-
     GnomeCmdAdvrenameDialog(GnomeCmdData::AdvrenameConfig &defaults);
     ~GnomeCmdAdvrenameDialog();
 
@@ -103,48 +65,4 @@
     void update_new_filenames();
 };
 
-inline GnomeCmdAdvrenameDialog::Regex::Regex(const gchar *pattern, const gchar *replacement, gboolean sensitive=FALSE): case_sensitive(sensitive)
-{
-    if (pattern)  from = pattern;
-    if (replacement)  to = replacement;
-#if GLIB_CHECK_VERSION (2, 14, 0)
-    GError *error = NULL;
-    re = g_regex_new (pattern, GRegexCompileFlags(case_sensitive ? G_REGEX_OPTIMIZE : G_REGEX_OPTIMIZE | G_REGEX_CASELESS), G_REGEX_MATCH_NOTEMPTY, &error);
-    malformed_pattern = !pattern || !*pattern || error;
-    if (error)  g_error_free (error);
-#else
-    memset(&pmatch, 0, sizeof(pmatch));
-    malformed_pattern = !pattern || !*pattern || regcomp(&re, pattern, (case_sensitive ? REG_EXTENDED : REG_EXTENDED|REG_ICASE))!=0;
-#endif
-}
-
-inline GnomeCmdAdvrenameDialog::Regex::~Regex()
-{
-#if GLIB_CHECK_VERSION (2, 14, 0)
-    g_regex_unref (re);
-#else
-    if (!malformed_pattern)  regfree(&re);
-#endif
-}
-
-inline void GnomeCmdAdvrenameDialog::Regex::assign(const gchar *pattern, const gchar *replacement, gboolean sensitive=FALSE)
-{
-    from.clear();
-    to.clear();
-    case_sensitive = sensitive;
-    if (pattern)  from = pattern;
-    if (replacement)  to = replacement;
-#if GLIB_CHECK_VERSION (2, 14, 0)
-    g_regex_unref (re);
-    GError *error = NULL;
-    re = g_regex_new (pattern, GRegexCompileFlags(case_sensitive ? G_REGEX_OPTIMIZE : G_REGEX_OPTIMIZE | G_REGEX_CASELESS), G_REGEX_MATCH_NOTEMPTY, &error);
-    malformed_pattern = !pattern || !*pattern || error;
-    if (error)  g_error_free (error);
-#else
-    if (!malformed_pattern)  regfree(&re);
-    memset(&pmatch, 0, sizeof(pmatch));
-    malformed_pattern = !pattern || !*pattern || regcomp(&re, pattern, (case_sensitive ? REG_EXTENDED : REG_EXTENDED|REG_ICASE))!=0;
-#endif
-}
-
 #endif // __GNOME_CMD_ADVRENAME_DIALOG_H__

Modified: trunk/src/gnome-cmd-data.cc
==============================================================================
--- trunk/src/gnome-cmd-data.cc	(original)
+++ trunk/src/gnome-cmd-data.cc	Tue Dec 16 19:01:18 2008
@@ -107,17 +107,17 @@
 
     GtkTreeIter iter;
 
-    for (vector<Profile::Regex>::const_iterator r=profile.regexes.begin(); r!=profile.regexes.end(); ++r)
+    for (vector<GnomeCmd::ReplacePattern>::const_iterator r=profile.regexes.begin(); r!=profile.regexes.end(); ++r)
     {
-        GnomeCmdAdvrenameDialog::Regex *rx = new GnomeCmdAdvrenameDialog::Regex(r->pattern.c_str(), r->replace.c_str(), r->match_case);
+        GnomeCmd::RegexReplace *rx = new GnomeCmd::RegexReplace(r->pattern, r->replacement, r->match_case);
 
         gtk_list_store_append (GTK_LIST_STORE (regexes), &iter);
         gtk_list_store_set (GTK_LIST_STORE (regexes), &iter,
                             GnomeCmdAdvrenameDialog::COL_REGEX, rx,
                             GnomeCmdAdvrenameDialog::COL_MALFORMED_REGEX, !*rx,
-                            GnomeCmdAdvrenameDialog::COL_PATTERN, r->pattern.c_str(),
-                            GnomeCmdAdvrenameDialog::COL_REPLACE, r->replace.c_str(),
-                            GnomeCmdAdvrenameDialog::COL_MATCH_CASE, r->match_case ? _("Yes") : _("No"),
+                            GnomeCmdAdvrenameDialog::COL_PATTERN, rx->pattern.c_str(),
+                            GnomeCmdAdvrenameDialog::COL_REPLACE, rx->replacement.c_str(),
+                            GnomeCmdAdvrenameDialog::COL_MATCH_CASE, rx->match_case ? _("Yes") : _("No"),
                             -1);
     }
 }
@@ -1010,16 +1010,16 @@
 
     for (GtkTreeIter iter; tmp_from && size > 0; --size)
     {
-        GnomeCmdAdvrenameDialog::Regex *rx = new GnomeCmdAdvrenameDialog::Regex((gchar *) tmp_from->data,
-                                                                                  (gchar *) tmp_to->data,
-                                                                                  *((gchar *) tmp_csens->data)=='T');
+        GnomeCmd::RegexReplace *rx = new GnomeCmd::RegexReplace((gchar *) tmp_from->data,
+                                                                (gchar *) tmp_to->data,
+                                                                *((gchar *) tmp_csens->data)=='T');
         gtk_list_store_append (GTK_LIST_STORE (advrename_defaults.regexes), &iter);
         gtk_list_store_set (GTK_LIST_STORE (advrename_defaults.regexes), &iter,
                             GnomeCmdAdvrenameDialog::COL_REGEX, rx,
                             GnomeCmdAdvrenameDialog::COL_MALFORMED_REGEX, !*rx,
-                            GnomeCmdAdvrenameDialog::COL_PATTERN, rx->from.c_str(),
-                            GnomeCmdAdvrenameDialog::COL_REPLACE, rx->to.c_str(),
-                            GnomeCmdAdvrenameDialog::COL_MATCH_CASE, rx->case_sensitive ? _("Yes") : _("No"),
+                            GnomeCmdAdvrenameDialog::COL_PATTERN, rx->pattern.c_str(),
+                            GnomeCmdAdvrenameDialog::COL_REPLACE, rx->replacement.c_str(),
+                            GnomeCmdAdvrenameDialog::COL_MATCH_CASE, rx->match_case ? _("Yes") : _("No"),
                             -1);
 
         tmp_from = tmp_from->next;
@@ -1397,8 +1397,8 @@
 #if GLIB_CHECK_VERSION (2, 14, 0)
         p.name = "CamelCase";
         p.template_string = "$N";
-        p.regexes.push_back(AdvrenameConfig::Profile::Regex("\\s*\\b(\\w)(\\w*)\\b", "\\u\\1\\L\\2\\E", FALSE));
-        p.regexes.push_back(AdvrenameConfig::Profile::Regex("\\.(.+)$", ".\\L\\1", FALSE));
+        p.regexes.push_back(GnomeCmd::ReplacePattern("\\s*\\b(\\w)(\\w*)\\b", "\\u\\1\\L\\2\\E", FALSE));
+        p.regexes.push_back(GnomeCmd::ReplacePattern("\\.(.+)$", ".\\L\\1", FALSE));
 
         advrename_defaults.profiles.push_back(p);
 #endif

Modified: trunk/src/gnome-cmd-data.h
==============================================================================
--- trunk/src/gnome-cmd-data.h	(original)
+++ trunk/src/gnome-cmd-data.h	Tue Dec 16 19:01:18 2008
@@ -27,6 +27,7 @@
 #include "gnome-cmd-app.h"
 #include "gnome-cmd-types.h"
 #include "gnome-cmd-file-list.h"
+#include "gnome-cmd-regex.h"
 #include "filter.h"
 #include "history.h"
 #include "dict.h"
@@ -63,28 +64,13 @@
     {
         struct Profile
         {
-            struct Regex
-            {
-                std::string pattern;
-                std::string replace;
-                gboolean match_case;
-
-                Regex(): match_case(FALSE)   {}
-                Regex(const std::string &from, const std::string &to, gboolean _match_case): pattern(from),
-                                                                                             replace(to),
-                                                                                             match_case(_match_case)  {}
-                Regex(const gchar *from, const gchar *to, gboolean _match_case): pattern(from),
-                                                                                 replace(to),
-                                                                                 match_case(_match_case)  {}
-            };
-
             std::string name;
             std::string template_string;
             guint counter_start;
             guint counter_width;
             gint counter_step;
 
-            std::vector<Regex> regexes;
+            std::vector<GnomeCmd::ReplacePattern> regexes;
 
             guint case_conversion;
             guint trim_blanks;

Added: trunk/src/gnome-cmd-regex.h
==============================================================================
--- (empty file)
+++ trunk/src/gnome-cmd-regex.h	Tue Dec 16 19:01:18 2008
@@ -0,0 +1,236 @@
+/*
+    GNOME Commander - A GNOME based file manager
+    Copyright (C) 2001-2006 Marcus Bjurman
+    Copyright (C) 2007-2008 Piotr Eljasiak
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+*/
+
+#ifndef __GNOME_CMD_REGEX_H__
+#define __GNOME_CMD_REGEX_H__
+
+#include <glib.h>
+#include <string>
+
+#include "utils.h"
+
+#if !GLIB_CHECK_VERSION (2, 14, 0)
+#include <regex.h>
+#endif
+
+namespace GnomeCmd
+{
+    struct FindPattern
+    {
+        std::string pattern;
+        gboolean match_case;
+
+        FindPattern(): match_case(FALSE)    {}
+        FindPattern(const gchar *from, gboolean case_sensitive): match_case(case_sensitive)  {  if (from)  pattern = from;  }
+        FindPattern(const std::string &from, gboolean case_sensitive): pattern(from), match_case(case_sensitive)           {}
+    };
+
+    struct ReplacePattern: virtual FindPattern
+    {
+        std::string replacement;
+
+        ReplacePattern()    {}
+        ReplacePattern(const gchar *from, const gchar *to, gboolean case_sensitive);
+        ReplacePattern(const std::string &from, const std::string &to, gboolean case_sensitive): FindPattern(from,case_sensitive), replacement(to) {}
+    };
+
+    class Regex: virtual public FindPattern
+    {
+      protected:
+#if GLIB_CHECK_VERSION (2, 14, 0)
+        GRegex *re;
+#else
+        regex_t re;
+#endif
+        gboolean malformed_pattern;
+
+        void compile_pattern();
+
+        Regex();
+        Regex(const gchar *from, gboolean case_sensitive): FindPattern(from,case_sensitive)         {  compile_pattern();  }
+        Regex(const std::string &from, gboolean case_sensitive): FindPattern(from,case_sensitive)   {  compile_pattern();  }
+        ~Regex();
+
+        void assign(const gchar *from, gboolean case_sensitive);
+        void assign(const std::string &from, gboolean case_sensitive);
+
+      public:
+
+        operator gboolean ()                    {  return !malformed_pattern;   }
+    };
+
+    class RegexFind: public Regex
+    {
+#if !GLIB_CHECK_VERSION (2, 14, 0)
+        regmatch_t pmatch;
+#endif
+
+      public:
+
+        RegexFind();
+        RegexFind(const gchar *from, gboolean case_sensitive);
+        RegexFind(const std::string &from, gboolean case_sensitive);
+
+        void assign(const gchar *from, gboolean case_sensitive)           {  return Regex::assign(from, case_sensitive);   }
+        void assign(const std::string &from, gboolean case_sensitive)     {  return Regex::assign(from, case_sensitive);   }
+        gboolean match(const gchar *s);
+        gboolean match(const std::string &s) {  return match(s.c_str());  }
+        int start() const;
+        int end() const;
+        int length() const                   {  return end() - start();   }
+    };
+
+    struct RegexReplace: ReplacePattern, Regex
+    {
+        RegexReplace()       {}
+        RegexReplace(const gchar *from, const gchar *to, gboolean case_sensitive): FindPattern(from,case_sensitive), ReplacePattern(from,to,case_sensitive), Regex(from,case_sensitive) {}
+        RegexReplace(const std::string &from, const std::string &to, gboolean case_sensitive): FindPattern(from,case_sensitive), ReplacePattern(from,to,case_sensitive), Regex(from,case_sensitive) {}
+
+        void assign(const gchar *from, const gchar *to, gboolean case_sensitive);
+        void assign(const std::string &from, const std::string &to, gboolean case_sensitive)    {  replacement = to; return Regex::assign(from, case_sensitive);   }
+        gchar *replace(const gchar *s);
+        gchar *replace(const std::string &s)    {  return replace(s.c_str());  }
+    };
+
+
+    inline ReplacePattern::ReplacePattern(const gchar *from, const gchar *to, gboolean case_sensitive): FindPattern(from,case_sensitive)
+    {
+        if (to)  replacement = to;
+    }
+
+    inline Regex::Regex(): malformed_pattern(TRUE)
+    {
+#if GLIB_CHECK_VERSION (2, 14, 0)
+        re = NULL;
+#endif
+    }
+
+    inline Regex::~Regex()
+    {
+#if GLIB_CHECK_VERSION (2, 14, 0)
+        g_regex_unref (re);
+#else
+        if (!malformed_pattern)  regfree(&re);
+#endif
+    }
+
+    inline void Regex::assign(const gchar *from, gboolean case_sensitive)
+    {
+#if GLIB_CHECK_VERSION (2, 14, 0)
+        g_regex_unref (re);
+#else
+        if (!malformed_pattern)  regfree(&re);
+#endif
+        match_case = case_sensitive;
+
+        if (from && *from)
+            pattern = from;
+        else
+            pattern.clear();
+
+        compile_pattern();
+    }
+
+    inline void Regex::assign(const std::string &from, gboolean case_sensitive)
+    {
+#if GLIB_CHECK_VERSION (2, 14, 0)
+        g_regex_unref (re);
+#else
+        if (!malformed_pattern)  regfree(&re);
+#endif
+        match_case = case_sensitive;
+        pattern = from;
+
+        compile_pattern();
+    }
+
+    inline void Regex::compile_pattern()
+    {
+#if GLIB_CHECK_VERSION (2, 14, 0)
+        GError *error = NULL;
+        re = g_regex_new (pattern.c_str(), GRegexCompileFlags(match_case ? G_REGEX_OPTIMIZE : G_REGEX_OPTIMIZE | G_REGEX_CASELESS), G_REGEX_MATCH_NOTEMPTY, &error);
+        malformed_pattern = pattern.empty() || error;
+        if (error)  g_error_free (error);
+#else
+        malformed_pattern = pattern.empty() || regcomp(&re, pattern.c_str(), (match_case ? REG_EXTENDED : REG_EXTENDED|REG_ICASE))!=0;
+#endif
+    }
+
+    inline RegexFind::RegexFind()
+    {
+#if !GLIB_CHECK_VERSION (2, 14, 0)
+        memset(&pmatch, 0, sizeof(pmatch));
+#endif
+    }
+
+    inline RegexFind::RegexFind(const gchar *from, gboolean case_sensitive): Regex(from,case_sensitive)
+    {
+#if !GLIB_CHECK_VERSION (2, 14, 0)
+        memset(&pmatch, 0, sizeof(pmatch));
+#endif
+
+    }
+
+    inline RegexFind::RegexFind(const std::string &from, gboolean case_sensitive): Regex(from,case_sensitive)
+    {
+#if !GLIB_CHECK_VERSION (2, 14, 0)
+        memset(&pmatch, 0, sizeof(pmatch));
+#endif
+    }
+
+    inline int RegexFind::start() const
+    {
+#if GLIB_CHECK_VERSION (2, 14, 0)
+        return 0;
+#else
+        return pmatch.rm_so;
+#endif
+    }
+
+    inline int RegexFind::end() const
+    {
+#if GLIB_CHECK_VERSION (2, 14, 0)
+        return 0;
+#else
+        return pmatch.rm_eo;
+#endif
+    }
+
+    inline void RegexReplace::assign(const gchar *from, const gchar *to, gboolean case_sensitive)
+    {
+        if (to && *to)
+            replacement = to;
+        else
+            replacement.clear();
+
+        return Regex::assign(from, case_sensitive);
+    }
+
+    inline gchar *RegexReplace::replace(const gchar *s)
+    {
+#if GLIB_CHECK_VERSION (2, 14, 0)
+        return g_regex_replace (re, s, -1, 0, replacement.c_str(), G_REGEX_MATCH_NOTEMPTY, NULL);
+#else
+        return NULL;
+#endif
+    }
+}
+
+#endif // __GNOME_CMD_REGEX_H__

Modified: trunk/src/gnome-cmd-xml-config.cc
==============================================================================
--- trunk/src/gnome-cmd-xml-config.cc	(original)
+++ trunk/src/gnome-cmd-xml-config.cc	Tue Dec 16 19:01:18 2008
@@ -48,6 +48,7 @@
 #include "gnome-cmd-includes.h"
 #include "gnome-cmd-xml-config.h"
 #include "gnome-cmd-advrename-dialog.h"
+#include "gnome-cmd-regex.h"
 #include "dict.h"
 #include "utils.h"
 
@@ -422,7 +423,7 @@
                 // FIXME: unescape param1
                 // FIXME: unescape param2
 
-                xml_profile.regexes.push_back(GnomeCmdData::AdvrenameConfig::Profile::Regex(param1, param2, param4));
+                xml_profile.regexes.push_back(GnomeCmd::ReplacePattern(param1, param2, param4));
             }
             break;
 
@@ -607,7 +608,7 @@
 
     for (gboolean valid_iter=gtk_tree_model_get_iter_first (cfg.advrename_defaults.regexes, &i); valid_iter; valid_iter=gtk_tree_model_iter_next (cfg.advrename_defaults.regexes, &i))
     {
-        GnomeCmdAdvrenameDialog::Regex *rx;
+        GnomeCmd::RegexReplace *rx;
 
         gtk_tree_model_get (cfg.advrename_defaults.regexes, &i,
                             GnomeCmdAdvrenameDialog::COL_REGEX, &rx,
@@ -615,7 +616,7 @@
         if (!rx)
             continue;
 
-        fprintf(f, "\t\t\t\t<Regex pattern=\"%s\" replace=\"%s\" match-case=\"%u\" />\n", rx->from.c_str(), rx->to.c_str(), rx->case_sensitive);
+        fprintf(f, "\t\t\t\t<Regex pattern=\"%s\" replace=\"%s\" match-case=\"%u\" />\n", rx->pattern.c_str(), rx->replacement.c_str(), rx->match_case);
     }
 
     fputs("\t\t\t</Regexes>\n", f);
@@ -629,8 +630,8 @@
         fprintf(f, "\t\t\t<Template>%s</Template>\n", p->template_string.empty() ? "$N" : p->template_string.c_str());
         fprintf(f, "\t\t\t<Counter start=\"%u\" step=\"%i\" width=\"%u\" />\n", p->counter_start, p->counter_step, p->counter_width);
         fputs("\t\t\t<Regexes>\n", f);
-        for (std::vector<GnomeCmdData::AdvrenameConfig::Profile::Regex>::const_iterator r=p->regexes.begin(); r!=p->regexes.end(); ++r)
-            fprintf(f, "\t\t\t\t<Regex pattern=\"%s\" replace=\"%s\" match-case=\"%u\" />\n", r->pattern.c_str(), r->replace.c_str(), r->match_case);
+        for (std::vector<GnomeCmd::ReplacePattern>::const_iterator r=p->regexes.begin(); r!=p->regexes.end(); ++r)
+            fprintf(f, "\t\t\t\t<Regex pattern=\"%s\" replace=\"%s\" match-case=\"%u\" />\n", r->pattern.c_str(), r->replacement.c_str(), r->match_case);
         fputs("\t\t\t</Regexes>\n", f);
         fprintf(f, "\t\t\t<CaseConversion use=\"%u\" />\n", p->case_conversion);
         fprintf(f, "\t\t\t<TrimBlanks use=\"%u\" />\n", p->trim_blanks);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]