[gnome-commander/gcmd-1-2-8] advrename: eliminate redundant g_strdup()



commit b9b8ac0d3faa6c5186f47654fa8e83821282e226
Author: Piotr Eljasiak <epiotr src gnome org>
Date:   Tue Oct 20 23:01:42 2009 +0200

    advrename: eliminate redundant g_strdup()
    
    GnomeCmdAdvrenameDialog::update_new_filenames() used local buff[] for generating new fname from the given template,
    and then it copied the buff[] content into newly allocated string with g_strdup(). The fix makes it generate
    new fname directly in the allocated memory avoiding unnecessary copy.

 src/gnome-cmd-advrename-dialog.cc |    6 +-----
 src/gnome-cmd-advrename-lexer.h   |    3 +--
 src/gnome-cmd-advrename-lexer.ll  |    9 ++++++---
 3 files changed, 8 insertions(+), 10 deletions(-)
---
diff --git a/src/gnome-cmd-advrename-dialog.cc b/src/gnome-cmd-advrename-dialog.cc
index 5313c4e..d628ec8 100644
--- a/src/gnome-cmd-advrename-dialog.cc
+++ b/src/gnome-cmd-advrename-dialog.cc
@@ -596,8 +596,6 @@ void GnomeCmdAdvrenameDialog::update_new_filenames()
     gnome_cmd_advrename_reset_counter (defaults.default_profile.counter_start,
                                        defaults.default_profile.counter_width,
                                        defaults.default_profile.counter_step);
-
-    char buff[256];
     GtkTreeIter i;
 
     vector<GnomeCmd::RegexReplace *> rx;
@@ -629,9 +627,7 @@ void GnomeCmdAdvrenameDialog::update_new_filenames()
         if (!f)
             continue;
 
-        gnome_cmd_advrename_gen_fname (buff, sizeof(buff), f);
-
-        gchar *fname = g_strdup (buff);
+        gchar *fname = gnome_cmd_advrename_gen_fname (f);
 
         for (vector<GnomeCmd::RegexReplace *>::iterator j=rx.begin(); j!=rx.end(); ++j)
         {
diff --git a/src/gnome-cmd-advrename-lexer.h b/src/gnome-cmd-advrename-lexer.h
index 04138f7..381745d 100644
--- a/src/gnome-cmd-advrename-lexer.h
+++ b/src/gnome-cmd-advrename-lexer.h
@@ -25,7 +25,6 @@
 
 void gnome_cmd_advrename_reset_counter(long start=1, int precision=-1, int step=1);
 void gnome_cmd_advrename_parse_template(const char *template_string, gboolean &has_counters);
-char *gnome_cmd_advrename_gen_fname(char *new_fname, size_t new_fname_size, GnomeCmdFile *f);
-
+char *gnome_cmd_advrename_gen_fname(GnomeCmdFile *f, size_t new_fname_size=NAME_MAX);
 
 #endif // __GNOME_CMD_ADVRENAME_LEXER_H__
diff --git a/src/gnome-cmd-advrename-lexer.ll b/src/gnome-cmd-advrename-lexer.ll
index 55cc17d..bf44ba0 100644
--- a/src/gnome-cmd-advrename-lexer.ll
+++ b/src/gnome-cmd-advrename-lexer.ll
@@ -411,7 +411,7 @@ inline void find_dirs (const gchar *path, const gchar *&parent_dir, const gchar
 }
 
 
-char *gnome_cmd_advrename_gen_fname (char *new_fname, size_t new_fname_size, GnomeCmdFile *f)
+char *gnome_cmd_advrename_gen_fname (GnomeCmdFile *f, size_t new_fname_size)
 {
   string fmt;
   fmt.reserve(256);
@@ -499,9 +499,12 @@ char *gnome_cmd_advrename_gen_fname (char *new_fname, size_t new_fname_size, Gno
       default :     break;
     }
 
-  strftime(new_fname, new_fname_size, fmt.c_str(), localtime(&f->info->mtime));
+  g_free (fname);
 
-  g_free(fname);
+  char *new_fname = g_new (char, new_fname_size+1);
+
+  if (!strftime(new_fname, new_fname_size+1, fmt.c_str(), localtime(&f->info->mtime)))      // if new_fname is not big enough...
+    new_fname[new_fname_size] = '\0';                                                       //      ... truncate
 
   return new_fname;
 }



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