Re: [patch] support back-references when bulk renaming
Rogutės <[email protected]> Fri, 5 Dec 2008 04:41:19 +0200
| Newsgroups | gmane.comp.desktop.rox.devel |
|---|---|
| Message-ID | <[email protected]> |
Thomas Leonard (2008-11-30 10:44): > 2008/11/27 Rogutes <[email protected]>: > > Hello, > > > > I thought it would be nice to have back-references support in the bulk > > renaming dialog, so one could, for example, type > > > > Replace: "\(.*\) (\([0-9]\{4\}\))" > > With: "[\2]: \1" > > > If my patch seems awkward (I hardly know C and just tried to mimic the > > surrounding code), then this is a feature request. > > Looks pretty good (and useful). I think your array may be one element > too small (whole pattern plus nine back-refs = 10 offsets). > > You might find the code would be simpler if you used the g_string_* > functions rather than realloc (e.g. g_string_append): > > http://library.gnome.org/devel/glib/unstable/glib-Strings.html > > 1. Append everything before the match > 2. Append the characters in the with string, expanding references as you go > 3. Append everything after the match > > Do you want to update the patch? If not, I'll try to find some time to > do it myself. I have attached an updated patch. Looks much better now, thanks for the advice! I haven't added the REG_EXTENDED cflag to regcomp() in this patch, but I agree that it could be the default. -- Rogutės ------------------------------------------------------------------------------ SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada. The future of the web can't happen without you. Join us at MIX09 to help pave the way to the Next Web now. Learn more and register at http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/ _______________________________________________ rox-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/rox-devel
0001-Support-back-references-when-bulk-renaming.patch
(text/plain, 3.2 KB)
>From 61f6a83a2cdb38ef20a4ea97de1ecb7920d64449 Mon Sep 17 00:00:00 2001 From: =?utf-8?q?Rogut=C4=97s?= <[email protected]> Date: Thu, 27 Nov 2008 21:07:45 +0200 Subject: [PATCH] Support back-references when bulk renaming. Back references from \0 to \9 can be used in the "With:" field of the bulk renaming dialog. Every back-reference is replaced by a captured substring from the "Replace:" field. As a side effect, \0, \1, ..., \9 become special characters and they must be escaped with an additional backslash (e.g. \\3). --- ROX-Filer/src/bulk_rename.c | 49 +++++++++++++++++++++++++++++-------------- 1 files changed, 33 insertions(+), 16 deletions(-) diff --git a/ROX-Filer/src/bulk_rename.c b/ROX-Filer/src/bulk_rename.c index c733886..9b7a19d 100644 --- a/ROX-Filer/src/bulk_rename.c +++ b/ROX-Filer/src/bulk_rename.c @@ -113,7 +113,9 @@ void bulk_rename(const char *dir, GList *items) gtk_tooltips_set_tip(tooltips, with_entry, _("The first match in each filename will be replaced " "by this string. " - "There are no special characters."), NULL); + "The only special characters are back-references " + "from \\0 to \\9. To use them literally, " + "they have to be escaped with a backslash."), NULL); button = gtk_button_new_with_label(_("Apply")); gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, TRUE, 0); @@ -204,6 +206,7 @@ static void update_model(GtkListStore *list, regex_t *replace, const char *with) { GtkTreeIter iter; GtkTreeModel *model = (GtkTreeModel *) list; + int max_subs = 10; int n_matched = 0; int n_changed = 0; int with_len; @@ -218,36 +221,50 @@ static void update_model(GtkListStore *list, regex_t *replace, const char *with) do { - regmatch_t match; + regmatch_t match[max_subs]; char *old = NULL; gtk_tree_model_get(model, &iter, 1, &old, -1); - if (regexec(replace, old, 1, &match, 0) == 0) + if (regexec(replace, old, max_subs, match, 0) == 0) { - char *new; - int new_len; + GString *new; n_matched++; - g_return_if_fail(match.rm_so != -1); + g_return_if_fail(match[0].rm_so != -1); - new_len = match.rm_so + with_len + - (strlen(old) - match.rm_eo) + 1; - new = g_malloc(new_len); + new = g_string_new(NULL); + g_string_append_len(new, old, match[0].rm_so); - strncpy(new, old, match.rm_so); - strcpy(new + match.rm_so, with); - strcpy(new + match.rm_so + with_len, old + match.rm_eo); + int i; + for (i = 0; i < with_len; i++) + { + if (with[i] == '\\' && with[i+1] >= '0' && with[i+1]-'0' < max_subs) + { + if (i != 0 && with[i-1] == '\\') + continue; + + int subpat = with[i+1] - '0'; - g_return_if_fail(new[new_len - 1] == '\0'); + if (match[subpat].rm_so != -1) + g_string_append_len(new, old + match[subpat].rm_so, + match[subpat].rm_eo - match[subpat].rm_so); + + i++; + } + else + g_string_append_c(new, with[i]); + } - if (strcmp(old, new) != 0) + g_string_append(new, old + match[0].rm_eo); + + if (strcmp(old, new->str) != 0) { n_changed++; - gtk_list_store_set(list, &iter, 1, new, -1); + gtk_list_store_set(list, &iter, 1, new->str, -1); } - g_free(new); + g_string_free(new, TRUE); } g_free(old); -- 1.6.0.4