Browse Source

(vfs_path_free): add 2nd parameter...

...to free or not to free the string representation
of vfs_path_t object.

It allows to get rid of string duplication in following cases:

vfs_path_t *vpath;
char *path;
...
vpath = vfs_path_from_str (...);
path = g_strdup (vfs_path_as_str (vpath));
vfs_path_free (vpath);

Now we can write:

vfs_path_t *vpath;
char *path;
...
vpath = vfs_path_from_str (...);
path = vfs_path_free (vpath, FALSE);

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
Andrew Borodin 4 years ago
parent
commit
536fb676d8
10 changed files with 44 additions and 33 deletions
  1. 2 2
      lib/mcconfig/common.c
  2. 2 2
      lib/util.c
  3. 4 6
      lib/vfs/direntry.c
  4. 3 3
      lib/vfs/interface.c
  5. 19 6
      lib/vfs/path.c
  6. 1 1
      lib/vfs/path.h
  7. 3 3
      lib/vfs/vfs.c
  8. 3 3
      lib/widget/input_complete.c
  9. 5 5
      src/args.c
  10. 2 2
      src/clipboard.c

+ 2 - 2
lib/mcconfig/common.c

@@ -69,7 +69,7 @@ mc_config_new_or_override_file (mc_config_t * mc_config, const gchar * ini_path,
 
     ini_vpath = vfs_path_from_str (ini_path);
     fd = mc_open (ini_vpath, O_WRONLY | O_TRUNC, 0);
-    vfs_path_free (ini_vpath);
+    vfs_path_free (ini_vpath, TRUE);
 
     if (fd == -1)
     {
@@ -136,7 +136,7 @@ mc_config_init (const gchar * ini_path, gboolean read_only)
             /* file exists and not empty */
             g_key_file_load_from_file (mc_config->handle, ini_path, flags, NULL);
         }
-        vfs_path_free (vpath);
+        vfs_path_free (vpath, TRUE);
     }
 
     mc_config->ini_path = g_strdup (ini_path);

+ 2 - 2
lib/util.c

@@ -331,7 +331,7 @@ path_trunc (const char *path, size_t trunc_len)
 
     vpath = vfs_path_from_str (path);
     secure_path = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_PASSWORD);
-    vfs_path_free (vpath);
+    vfs_path_free (vpath, TRUE);
 
     ret = str_trunc (secure_path, trunc_len);
     g_free (secure_path);
@@ -1384,7 +1384,7 @@ mc_util_unlink_backup_if_possible (const char *file_name, const char *backup_suf
 
         vpath = vfs_path_from_str (backup_path);
         mc_unlink (vpath);
-        vfs_path_free (vpath);
+        vfs_path_free (vpath, TRUE);
     }
 
     g_free (backup_path);

+ 4 - 6
lib/vfs/direntry.c

@@ -1154,7 +1154,7 @@ vfs_get_super_by_vpath (const vfs_path_t * vpath)
     }
 
   ret:
-    vfs_path_free (vpath_archive);
+    vfs_path_free (vpath_archive, TRUE);
     return super;
 }
 
@@ -1205,7 +1205,7 @@ vfs_s_get_path (const vfs_path_t * vpath, struct vfs_s_super **archive, int flag
         vfs_path_remove_element_by_index (vpath_archive, -1);
 
         result = subclass->open_archive (super, vpath_archive, path_element);
-        vfs_path_free (vpath_archive);
+        vfs_path_free (vpath_archive, TRUE);
     }
     if (result == -1)
     {
@@ -1344,8 +1344,7 @@ vfs_s_open (const vfs_path_t * vpath, int flags, mode_t mode)
             vfs_path_t *tmp_vpath;
 
             tmp_handle = vfs_mkstemps (&tmp_vpath, path_element->class->name, name);
-            ino->localname = g_strdup (vfs_path_as_str (tmp_vpath));
-            vfs_path_free (tmp_vpath);
+            ino->localname = vfs_path_free (tmp_vpath, FALSE);
             if (tmp_handle == -1)
             {
                 g_free (dirname);
@@ -1447,8 +1446,7 @@ vfs_s_retrieve_file (struct vfs_class *me, struct vfs_s_inode *ino)
         return (-1);
 
     handle = vfs_mkstemps (&tmp_vpath, me->name, ino->ent->name);
-    ino->localname = g_strdup (vfs_path_as_str (tmp_vpath));
-    vfs_path_free (tmp_vpath);
+    ino->localname = vfs_path_free (tmp_vpath, FALSE);
     if (handle == -1)
     {
         me->verrno = errno;

+ 3 - 3
lib/vfs/interface.c

@@ -115,7 +115,7 @@ mc_def_getlocalcopy (const vfs_path_t * filename_vpath)
     return tmp_vpath;
 
   fail:
-    vfs_path_free (tmp_vpath);
+    vfs_path_free (tmp_vpath, TRUE);
     if (fdout != -1)
         close (fdout);
     if (fdin != -1)
@@ -723,7 +723,7 @@ mc_chdir (const vfs_path_t * vpath)
     return 0;
 
   error_end:
-    vfs_path_free (cd_vpath);
+    vfs_path_free (cd_vpath, TRUE);
     return (-1);
 }
 
@@ -894,7 +894,7 @@ mc_tmpdir (void)
             g_snprintf (buffer, sizeof (buffer), "%s", "/dev/null/");
         }
 
-        vfs_path_free (test_vpath);
+        vfs_path_free (test_vpath, TRUE);
         fprintf (stderr, "%s\n", _("Press any key to continue..."));
         getc (stdin);
     }

+ 19 - 6
lib/vfs/path.c

@@ -939,16 +939,19 @@ vfs_path_clone (const vfs_path_t * vpath)
  * Free vfs_path_t object.
  *
  * @param vpath pointer to vfs_path_t object
+ * @param free_str if TRUE the string representation of vpath is freed as well
  *
+ * @return the string representation of vpath (i.e. NULL if free_str is TRUE)
  */
 
-void
-vfs_path_free (vfs_path_t * vpath)
+char *
+vfs_path_free (vfs_path_t * vpath, gboolean free_str)
 {
     int vpath_element_index;
+    char *ret;
 
     if (vpath == NULL)
-        return;
+        return NULL;
 
     for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
          vpath_element_index++)
@@ -960,8 +963,18 @@ vfs_path_free (vfs_path_t * vpath)
     }
 
     g_array_free (vpath->path, TRUE);
-    g_free (vpath->str);
+
+    if (!free_str)
+        ret = vpath->str;
+    else
+    {
+        g_free (vpath->str);
+        ret = NULL;
+    }
+
     g_free (vpath);
+
+    return ret;
 }
 
 /* --------------------------------------------------------------------------------------------- */
@@ -1167,7 +1180,7 @@ vfs_path_deserialize (const char *data, GError ** mcerror)
         eclass = vfs_get_class_by_name (cfg_value);
         if (eclass == NULL)
         {
-            vfs_path_free (vpath);
+            vfs_path_free (vpath, TRUE);
             g_set_error (mcerror, MC_ERROR, 0, "Unable to find VFS class by name '%s'", cfg_value);
             g_free (cfg_value);
             mc_config_deinit (cpath);
@@ -1198,7 +1211,7 @@ vfs_path_deserialize (const char *data, GError ** mcerror)
     mc_config_deinit (cpath);
     if (vfs_path_elements_count (vpath) == 0)
     {
-        vfs_path_free (vpath);
+        vfs_path_free (vpath, TRUE);
         g_set_error (mcerror, MC_ERROR, 0, "No any path elements found");
         return NULL;
     }

+ 1 - 1
lib/vfs/path.h

@@ -60,7 +60,7 @@ typedef struct
 vfs_path_t *vfs_path_new (void);
 vfs_path_t *vfs_path_clone (const vfs_path_t * vpath);
 void vfs_path_remove_element_by_index (vfs_path_t * vpath, int element_index);
-void vfs_path_free (vfs_path_t * path);
+char *vfs_path_free (vfs_path_t * path, gboolean free_str);
 int vfs_path_elements_count (const vfs_path_t * path);
 
 char *vfs_path_to_str_elements_count (const vfs_path_t * path, int elements_count);

+ 3 - 3
lib/vfs/vfs.c

@@ -429,7 +429,7 @@ vfs_get_raw_current_dir (void)
 void
 vfs_set_raw_current_dir (const vfs_path_t * vpath)
 {
-    vfs_path_free (current_path);
+    vfs_path_free (current_path, TRUE);
     current_path = (vfs_path_t *) vpath;
 }
 
@@ -653,7 +653,7 @@ vfs_setup_cwd (void)
             if (vfs_test_current_dir (tmp_vpath))
                 vfs_set_raw_current_dir (tmp_vpath);
             else
-                vfs_path_free (tmp_vpath);
+                vfs_path_free (tmp_vpath, TRUE);
         }
     }
 
@@ -673,7 +673,7 @@ vfs_setup_cwd (void)
             if (!vfs_test_current_dir (tmp_vpath))
                 vfs_set_raw_current_dir (tmp_vpath);
             else
-                vfs_path_free (tmp_vpath);
+                vfs_path_free (tmp_vpath, TRUE);
         }
     }
 }

+ 3 - 3
lib/widget/input_complete.c

@@ -168,7 +168,7 @@ filename_completion_function (const char *text, int state, input_complete_t flag
         g_free (dirname);
         g_free (filename);
         g_free (users_dirname);
-        vfs_path_free (dirname_vpath);
+        vfs_path_free (dirname_vpath, TRUE);
 
         if ((*text != '\0') && (temp = strrchr (text, PATH_SEP)) != NULL)
         {
@@ -255,7 +255,7 @@ filename_completion_function (const char *text, int state, input_complete_t flag
                 /* stat failed, strange. not a dir in any case */
                 isdir = FALSE;
             }
-            vfs_path_free (tmp_vpath);
+            vfs_path_free (tmp_vpath, TRUE);
         }
 
         if ((flags & INPUT_COMPLETE_COMMANDS) != 0 && (isexec || isdir))
@@ -274,7 +274,7 @@ filename_completion_function (const char *text, int state, input_complete_t flag
             directory = NULL;
         }
         MC_PTR_FREE (dirname);
-        vfs_path_free (dirname_vpath);
+        vfs_path_free (dirname_vpath, TRUE);
         dirname_vpath = NULL;
         MC_PTR_FREE (filename);
         MC_PTR_FREE (users_dirname);

+ 5 - 5
src/args.c

@@ -601,12 +601,12 @@ parse_mcedit_arguments (int argc, char **argv)
             if (mc_stat (tmp_vpath, &st) == -1 && mc_stat (fname_vpath, &st) != -1)
             {
                 arg = mcedit_arg_vpath_new (fname_vpath, atoi (p));
-                vfs_path_free (tmp_vpath);
+                vfs_path_free (tmp_vpath, TRUE);
             }
             else
             {
                 arg = mcedit_arg_vpath_new (tmp_vpath, 0);
-                vfs_path_free (fname_vpath);
+                vfs_path_free (fname_vpath, TRUE);
             }
 
             g_free (fname);
@@ -803,12 +803,12 @@ mc_setup_by_args (int argc, char **argv, GError ** mcerror)
 #ifdef ENABLE_VFS_FTP
         vpath = vfs_path_from_str ("ftp://");
         mc_setctl (vpath, VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
-        vfs_path_free (vpath);
+        vfs_path_free (vpath, TRUE);
 #endif /* ENABLE_VFS_FTP */
 #ifdef ENABLE_VFS_SMB
         vpath = vfs_path_from_str ("smb://");
         mc_setctl (vpath, VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
-        vfs_path_free (vpath);
+        vfs_path_free (vpath, TRUE);
 #endif /* ENABLE_VFS_SMB */
         (void) vpath;
     }
@@ -869,7 +869,7 @@ mc_setup_by_args (int argc, char **argv, GError ** mcerror)
 void
 mcedit_arg_free (mcedit_arg_t * arg)
 {
-    vfs_path_free (arg->file_vpath);
+    vfs_path_free (arg->file_vpath, TRUE);
     g_free (arg);
 }
 

+ 2 - 2
src/clipboard.c

@@ -146,7 +146,7 @@ clipboard_file_from_ext_clip (const gchar * event_group_name, const gchar * even
 
                 fname_vpath = mc_config_get_full_vpath (EDIT_HOME_CLIP_FILE);
                 file = mc_open (fname_vpath, clip_open_flags, clip_open_mode);
-                vfs_path_free (fname_vpath);
+                vfs_path_free (fname_vpath, TRUE);
 
                 if (file < 0)
                     break;
@@ -186,7 +186,7 @@ clipboard_text_to_file (const gchar * event_group_name, const gchar * event_name
 
     fname_vpath = mc_config_get_full_vpath (EDIT_HOME_CLIP_FILE);
     file = mc_open (fname_vpath, clip_open_flags, clip_open_mode);
-    vfs_path_free (fname_vpath);
+    vfs_path_free (fname_vpath, TRUE);
 
     if (file == -1)
         return TRUE;

Some files were not shown because too many files changed in this diff