[easytag] Refactor ET_File_Name_Convert_Character()



commit 97385362051bebd8960137310ac8999648f49b70
Author: David King <amigadave amigadave com>
Date:   Fri Feb 13 18:41:05 2015 +0000

    Refactor ET_File_Name_Convert_Character()
    
    Move ET_File_Name_Convert_Character() to misc.c and rename it
    et_filename_prepare(). Take a boolean argument for whether to replace
    illegal characters, rather than using a setting.

 src/cddb_dialog.c       |    8 ++++-
 src/file.c              |   63 ++++++-----------------------------------------
 src/file.h              |    1 -
 src/load_files_dialog.c |    4 ++-
 src/misc.c              |   54 ++++++++++++++++++++++++++++++++++++++++
 src/misc.h              |    2 +
 src/scan_dialog.c       |    4 ++-
 7 files changed, 76 insertions(+), 60 deletions(-)
---
diff --git a/src/cddb_dialog.c b/src/cddb_dialog.c
index c87e891..6cf8274 100644
--- a/src/cddb_dialog.c
+++ b/src/cddb_dialog.c
@@ -2456,7 +2456,9 @@ Cddb_Set_Track_Infos_To_File_List (EtCDDBDialog *self)
                 filename_generated_utf8 = g_strconcat (track_number, " - ",
                                                        cddbtrackalbum->track_name,
                                                        NULL);
-                ET_File_Name_Convert_Character(filename_generated_utf8); // Replace invalid characters
+                et_filename_prepare (filename_generated_utf8,
+                                     g_settings_get_boolean (MainSettings,
+                                                             "rename-replace-illegal-chars"));
                 filename_new_utf8 = ET_File_Name_Generate (etfile,
                                                            filename_generated_utf8);
 
@@ -2587,7 +2589,9 @@ Cddb_Set_Track_Infos_To_File_List (EtCDDBDialog *self)
                 filename_generated_utf8 = g_strconcat (track_number, " - ",
                                                        cddbtrackalbum->track_name,
                                                        NULL);
-                ET_File_Name_Convert_Character(filename_generated_utf8); // Replace invalid characters
+                et_filename_prepare (filename_generated_utf8,
+                                     g_settings_get_boolean (MainSettings,
+                                                             "rename-replace-illegal-chars"));
                 filename_new_utf8 = ET_File_Name_Generate(etfile,filename_generated_utf8);
 
                 ET_Set_Filename_File_Name_Item(FileName,filename_new_utf8,NULL);
diff --git a/src/file.c b/src/file.c
index eaa722a..2b5b13c 100644
--- a/src/file.c
+++ b/src/file.c
@@ -1424,8 +1424,10 @@ ET_Save_File_Name_From_UI (const ET_File *ETFile, File_Name *FileName)
         return FALSE;
     }
 
-    // Convert the illegal characters
-    ET_File_Name_Convert_Character(filename_new); // FIX ME : should be in UTF8?
+    /* Convert the illegal characters. FIXME should be in UTF8? */
+    et_filename_prepare (filename_new,
+                         g_settings_get_boolean (MainSettings,
+                                                 "rename-replace-illegal-chars"));
 
     /* Set the new filename (in file system encoding). */
     FileName->value = g_strconcat(dirname,G_DIR_SEPARATOR_S,filename_new,NULL);
@@ -1480,8 +1482,10 @@ ET_Save_File_Name_Internal (const ET_File *ETFile,
     // Check if new filename seems to be correct
     if (filename_new)
     {
-        // Convert the illegal characters
-        ET_File_Name_Convert_Character(filename_new);
+        /* Convert the illegal characters. */
+        et_filename_prepare (filename_new,
+                             g_settings_get_boolean (MainSettings,
+                                                     "rename-replace-illegal-chars"));
 
         /* Set the new filename (in file system encoding). */
         FileName->value = g_strconcat(dirname,G_DIR_SEPARATOR_S,filename_new,NULL);
@@ -2265,54 +2269,3 @@ ET_File_Format_File_Extension (const ET_File *ETFile)
             return g_strdup (ETFile->ETFileExtension);
     };
 }
-
-
-/*
- * Used to replace the illegal characters in the filename
- * Paremeter 'filename' musn't contain the path, else directories separators would be replaced!
- */
-gboolean ET_File_Name_Convert_Character (gchar *filename_utf8)
-{
-    gchar *character;
-
-    g_return_val_if_fail (filename_utf8 != NULL, FALSE);
-
-    // Convert automatically the directory separator ('/' on LINUX and '\' on WIN32) to '-'.
-    while ( (character=g_utf8_strchr(filename_utf8, -1, G_DIR_SEPARATOR))!=NULL )
-        *character = '-';
-
-#ifdef G_OS_WIN32
-    /* Convert character '\' on WIN32 to '-'. */
-    while ( (character=g_utf8_strchr(filename_utf8, -1, '\\'))!=NULL )
-        *character = '-';
-    /* Convert character '/' on WIN32 to '-'. May be converted to '\' after. */
-    while ( (character=g_utf8_strchr(filename_utf8, -1, '/'))!=NULL )
-        *character = '-';
-#endif /* G_OS_WIN32 */
-
-    /* Convert other illegal characters on FAT32/16 filesystems and ISO9660 and
-     * Joliet (CD-ROM filesystems). */
-    if (g_settings_get_boolean (MainSettings, "rename-replace-illegal-chars"))
-    {
-        // Commented as we display unicode values as "\351" for "é"
-        //while ( (character=g_utf8_strchr(filename_utf8, -1, '\\'))!=NULL )
-        //    *character = ',';
-        while ( (character=g_utf8_strchr(filename_utf8, -1, ':'))!=NULL )
-            *character = '-';
-        //while ( (character=g_utf8_strchr(filename_utf8, -1, ';'))!=NULL )
-        //    *character = '-';
-        while ( (character=g_utf8_strchr(filename_utf8, -1, '*'))!=NULL )
-            *character = '+';
-        while ( (character=g_utf8_strchr(filename_utf8, -1, '?'))!=NULL )
-            *character = '_';
-        while ( (character=g_utf8_strchr(filename_utf8, -1, '\"'))!=NULL )
-            *character = '\'';
-        while ( (character=g_utf8_strchr(filename_utf8, -1, '<'))!=NULL )
-            *character = '(';
-        while ( (character=g_utf8_strchr(filename_utf8, -1, '>'))!=NULL )
-            *character = ')';
-        while ( (character=g_utf8_strchr(filename_utf8, -1, '|'))!=NULL )
-            *character = '-';
-    }
-    return TRUE;
-}
diff --git a/src/file.h b/src/file.h
index 0aa7d69..496a4a7 100644
--- a/src/file.h
+++ b/src/file.h
@@ -82,7 +82,6 @@ gboolean ET_File_Data_Has_Redo_Data (const ET_File *ETFile);
 
 gboolean ET_Manage_Changes_Of_File_Data (ET_File *ETFile, File_Name *FileName, File_Tag *FileTag);
 void ET_Mark_File_Name_As_Saved (ET_File *ETFile);
-gboolean ET_File_Name_Convert_Character (gchar *filename_utf8);
 gchar *ET_File_Name_Generate (const ET_File *ETFile, const gchar *new_file_name);
 
 gint ET_Comp_Func_Sort_File_By_Ascending_Filename (const ET_File *ETFile1, const ET_File *ETFile2);
diff --git a/src/load_files_dialog.c b/src/load_files_dialog.c
index f3cdb71..b05be00 100644
--- a/src/load_files_dialog.c
+++ b/src/load_files_dialog.c
@@ -120,7 +120,9 @@ Load_Filename_Set_Filenames (EtLoadFilesDialog *self)
             gchar *filename_new_utf8;
 
             list_text_tmp = g_strdup(list_text);
-            ET_File_Name_Convert_Character(list_text_tmp); // Replace invalid characters
+            et_filename_prepare (list_text_tmp,
+                                 g_settings_get_boolean (MainSettings,
+                                                         "rename-replace-illegal-chars"));
 
             /* Build the filename with the path */
             filename_new_utf8 = ET_File_Name_Generate(ETFile,list_text_tmp);
diff --git a/src/misc.c b/src/misc.c
index fd2d2e5..b19b45f 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -373,3 +373,57 @@ et_track_number_to_string (const guint track_number)
         return g_strdup_printf ("%u", track_number);
     }
 }
+
+/*
+ * et_filename_prepare:
+ * @filename_utf8: UTF8-encoded basename
+ * @replace_illegal: whether to replace illegal characters in the file name
+ *
+ * Used to replace (in place) the illegal characters in the filename.
+ */
+void
+et_filename_prepare (gchar *filename_utf8,
+                     gboolean replace_illegal)
+{
+    gchar *character;
+
+    g_return_if_fail (filename_utf8 != NULL);
+
+    // Convert automatically the directory separator ('/' on LINUX and '\' on WIN32) to '-'.
+    while ( (character=g_utf8_strchr(filename_utf8, -1, G_DIR_SEPARATOR))!=NULL )
+        *character = '-';
+
+#ifdef G_OS_WIN32
+    /* Convert character '\' on WIN32 to '-'. */
+    while ( (character=g_utf8_strchr(filename_utf8, -1, '\\'))!=NULL )
+        *character = '-';
+    /* Convert character '/' on WIN32 to '-'. May be converted to '\' after. */
+    while ( (character=g_utf8_strchr(filename_utf8, -1, '/'))!=NULL )
+        *character = '-';
+#endif /* G_OS_WIN32 */
+
+    /* Convert other illegal characters on FAT32/16 filesystems and ISO9660 and
+     * Joliet (CD-ROM filesystems). */
+    if (replace_illegal)
+    {
+        // Commented as we display unicode values as "\351" for "é"
+        //while ( (character=g_utf8_strchr(filename_utf8, -1, '\\'))!=NULL )
+        //    *character = ',';
+        while ( (character=g_utf8_strchr(filename_utf8, -1, ':'))!=NULL )
+            *character = '-';
+        //while ( (character=g_utf8_strchr(filename_utf8, -1, ';'))!=NULL )
+        //    *character = '-';
+        while ( (character=g_utf8_strchr(filename_utf8, -1, '*'))!=NULL )
+            *character = '+';
+        while ( (character=g_utf8_strchr(filename_utf8, -1, '?'))!=NULL )
+            *character = '_';
+        while ( (character=g_utf8_strchr(filename_utf8, -1, '\"'))!=NULL )
+            *character = '\'';
+        while ( (character=g_utf8_strchr(filename_utf8, -1, '<'))!=NULL )
+            *character = '(';
+        while ( (character=g_utf8_strchr(filename_utf8, -1, '>'))!=NULL )
+            *character = ')';
+        while ( (character=g_utf8_strchr(filename_utf8, -1, '|'))!=NULL )
+            *character = '-';
+    }
+}
diff --git a/src/misc.h b/src/misc.h
index ae3a14f..63b2ed8 100644
--- a/src/misc.h
+++ b/src/misc.h
@@ -37,6 +37,8 @@ gboolean et_run_program (const gchar *program_name, GList *args_list, GError **e
 gchar * et_disc_number_to_string (const guint disc_number);
 gchar * et_track_number_to_string (const guint track_number);
 
+void et_filename_prepare (gchar *filename_utf8, gboolean replace_illegal);
+
 G_END_DECLS
 
 #endif /* ET_MISC_H_ */
diff --git a/src/scan_dialog.c b/src/scan_dialog.c
index 6783451..86eef88 100644
--- a/src/scan_dialog.c
+++ b/src/scan_dialog.c
@@ -930,7 +930,9 @@ et_scan_generate_new_filename_from_mask (const ET_File *ETFile,
             {
                 EtConvertSpaces convert_mode;
 
-                ET_File_Name_Convert_Character (mask_item->string);
+                et_filename_prepare (mask_item->string,
+                                     g_settings_get_boolean (MainSettings,
+                                                             "rename-replace-illegal-chars"));
                 convert_mode = g_settings_get_enum (MainSettings,
                                                     "rename-convert-spaces");
 


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