SF.net SVN: geany-plugins:[1794] trunk/geany-plugins/geanygendoc

colombanw at users.sourceforge.net colombanw at xxxxx
Fri Dec 31 14:27:06 UTC 2010


Revision: 1794
          http://geany-plugins.svn.sourceforge.net/geany-plugins/?rev=1794&view=rev
Author:   colombanw
Date:     2010-12-31 14:27:06 +0000 (Fri, 31 Dec 2010)

Log Message:
-----------
GeanyGenDoc: Fix file names encoding usage

Now the file names should be correctly converted from an to the correct
encoding. This may improve support of systems where the encoding is not
UTF-8 (Windows, a small portion of Unices, ...).

Note that this code was not tested under non-UTF-8 systems, especially
not with paths that may cause problems. Further testing would be
needed, let's wait for bug reports :)

Modified Paths:
--------------
    trunk/geany-plugins/geanygendoc/TODO
    trunk/geany-plugins/geanygendoc/src/ggd-file-type-loader.c
    trunk/geany-plugins/geanygendoc/src/ggd-file-type-manager.c
    trunk/geany-plugins/geanygendoc/src/ggd-options.c
    trunk/geany-plugins/geanygendoc/src/ggd-plugin.c
    trunk/geany-plugins/geanygendoc/src/ggd-utils.c

Modified: trunk/geany-plugins/geanygendoc/TODO
===================================================================
--- trunk/geany-plugins/geanygendoc/TODO	2010-12-31 14:26:41 UTC (rev 1793)
+++ trunk/geany-plugins/geanygendoc/TODO	2010-12-31 14:27:06 UTC (rev 1794)
@@ -14,5 +14,3 @@
 
 * Store doctype in a per-document basis? It would be cool, but not sure it is
   possible without re-implementing a lot of document history.
-
-* Fix file names encoding, it's a complete mess.

Modified: trunk/geany-plugins/geanygendoc/src/ggd-file-type-loader.c
===================================================================
--- trunk/geany-plugins/geanygendoc/src/ggd-file-type-loader.c	2010-12-31 14:26:41 UTC (rev 1793)
+++ trunk/geany-plugins/geanygendoc/src/ggd-file-type-loader.c	2010-12-31 14:27:06 UTC (rev 1794)
@@ -26,13 +26,14 @@
 
 #include "ggd-file-type-loader.h"
 
-#include <glib.h>
 #include <string.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <errno.h>
+#include <glib.h>
+#include <glib/gstdio.h>
 #include <ctpl/ctpl.h>
 #include <geanyplugin.h>
 
@@ -918,7 +919,7 @@
   GList  *list = NULL;
   gint    fd;
   
-  fd = open (file, O_RDONLY, 0);
+  fd = g_open (file, O_RDONLY, 0);
   if (fd < 0) {
     gint errnum = errno;
     
@@ -926,16 +927,19 @@
                  "%s", g_strerror (errnum));
   } else {
     GScanner *scanner;
+    gchar    *filename;
     
+    filename = g_filename_display_name (file);
     scanner = g_scanner_new (NULL);
     scanner->config->scan_float = FALSE; /* disable float scanning since it
                                           * prevent dot to be usable alone */
-    scanner->input_name   = file;
+    scanner->input_name   = filename;
     scanner->user_data    = error;
     scanner->msg_handler  = scanner_msg_handler;
     g_scanner_input_file (scanner, fd);
     list = ggd_conf_read (scanner);
     g_scanner_destroy (scanner);
+    g_free (filename);
     close (fd);
   }
   
@@ -946,7 +950,7 @@
 /**
  * ggd_file_type_load:
  * @filetype: A #GgdFileType to fill with read settings
- * @file: a file from which load the settings
+ * @file: a file from which load the settings, in the GLib file names encoding
  * @error: Return location for errors, or %NULL to ignore them
  * 
  * Tries to load a file type configuration from a file.
@@ -967,7 +971,7 @@
   gboolean  success = FALSE;
   gint      fd;
   
-  fd = open (file, O_RDONLY, 0);
+  fd = g_open (file, O_RDONLY, 0);
   if (fd < 0) {
     gint errnum = errno;
     
@@ -975,16 +979,19 @@
                  "%s", g_strerror (errnum));
   } else {
     GScanner *scanner;
+    gchar    *filename;
     
+    filename = g_filename_display_name (file);
     scanner = g_scanner_new (NULL);
     scanner->config->scan_float = FALSE; /* disable float scanning since it
                                           * prevent dot to be usable alone */
-    scanner->input_name   = file;
+    scanner->input_name   = filename;
     scanner->user_data    = error;
     scanner->msg_handler  = scanner_msg_handler;
     g_scanner_input_file (scanner, fd);
     success = ggd_file_type_read (scanner, filetype);
     g_scanner_destroy (scanner);
+    g_free (filename);
     close (fd);
   }
   

Modified: trunk/geany-plugins/geanygendoc/src/ggd-file-type-manager.c
===================================================================
--- trunk/geany-plugins/geanygendoc/src/ggd-file-type-manager.c	2010-12-31 14:26:41 UTC (rev 1793)
+++ trunk/geany-plugins/geanygendoc/src/ggd-file-type-manager.c	2010-12-31 14:27:06 UTC (rev 1794)
@@ -106,7 +106,8 @@
 }
 
 /* Same as ggd_file_type_manager_get_conf_path() but uses a #GeanyFiletype and
- * doesn't do come safety checks. */
+ * don't do some safety checks.
+ * Returns filename encoded in GLib file names encoding */
 static gchar *
 ggd_file_type_manager_get_conf_path_intern (GeanyFiletype  *geany_ft,
                                             GgdPerms        prems_req,
@@ -134,8 +135,9 @@
  * Gets the path to the configuration file of a file type from a given
  * #GeanyFiletype ID.
  * 
- * Returns: A newly allocated path to the requested configuration file that
- *          should be freed with g_free(), or %NULL on error.
+ * Returns: A newly allocated path to the requested configuration file in the
+ *          GLib file names encoding that should be freed with g_free(), or
+ *          %NULL on error.
  */
 gchar *
 ggd_file_type_manager_get_conf_path (filetype_id  id,
@@ -179,9 +181,13 @@
   } else {
     ft = ggd_file_type_new (id);
     if (! ggd_file_type_load (ft, filename, &err)) {
+      gchar *display_filename;
+      
+      display_filename = g_filename_display_name (filename);
       msgwin_status_add (_("Failed to load file type \"%s\" from file \"%s\": "
                            "%s"),
-                         geany_ft->name, filename, err->message);
+                         geany_ft->name, display_filename, err->message);
+      g_free (display_filename);
       g_error_free (err);
       ggd_file_type_unref (ft), ft = NULL;
     } else {

Modified: trunk/geany-plugins/geanygendoc/src/ggd-options.c
===================================================================
--- trunk/geany-plugins/geanygendoc/src/ggd-options.c	2010-12-31 14:26:41 UTC (rev 1793)
+++ trunk/geany-plugins/geanygendoc/src/ggd-options.c	2010-12-31 14:27:06 UTC (rev 1794)
@@ -541,7 +541,8 @@
 /**
  * ggd_opt_group_load_from_file:
  * @group: A #GgdOptGroup
- * @filename: Name of the file from which load values
+ * @filename: Name of the file from which load values in the GLib file names
+ *            encoding
  * @error: return location for or %NULL to ignore them
  * 
  * Loads values of a #GgdOptGroup from a file.
@@ -583,7 +584,8 @@
 /**
  * ggd_opt_group_write_to_file:
  * @group: A #GgdOptGroup
- * @filename: Name of the file in which save the values
+ * @filename: Name of the file in which save the values, in the GLib file names
+ *            encoding
  * @error: Return location for errors or %NULL to ignore them
  * 
  * Writes a #GgdOptGroup to a file.
@@ -616,9 +618,7 @@
   ggd_opt_group_write_to_key_file (group, key_file);
   data = g_key_file_to_data (key_file, &data_length, error);
   if (data) {
-    if (g_file_set_contents (filename, data, data_length, error)) {
-      success = TRUE;
-    }
+    success = g_file_set_contents (filename, data, data_length, error);
   }
   g_key_file_free (key_file);
   

Modified: trunk/geany-plugins/geanygendoc/src/ggd-plugin.c
===================================================================
--- trunk/geany-plugins/geanygendoc/src/ggd-plugin.c	2010-12-31 14:26:41 UTC (rev 1793)
+++ trunk/geany-plugins/geanygendoc/src/ggd-plugin.c	2010-12-31 14:27:06 UTC (rev 1794)
@@ -231,8 +231,8 @@
 static void
 unload_configuration (void)
 {
-  gchar    *conffile;
-  GError   *err = NULL;
+  gchar  *conffile;
+  GError *err = NULL;
   
   conffile = ggd_get_config_file ("ggd.conf", NULL, GGD_PERM_RW, &err);
   if (conffile) {
@@ -318,6 +318,7 @@
       g_error_free (err);
     } else {
       gchar *text = NULL;
+      gchar *path_write_u8;
       
       path_read = ggd_file_type_manager_get_conf_path (doc->file_type->id,
                                                        GGD_PERM_R, &err);
@@ -332,8 +333,12 @@
         gsize   length;
         
         if (! g_file_get_contents (path_read, &content, &length, &err)) {
+          gchar *display_path_read;
+          
+          display_path_read = g_filename_display_name (path_read);
           g_warning (_("Failed to load file \"%s\": %s"),
-                     path_read, err->message);
+                     display_path_read, err->message);
+          g_free (display_path_read);
           g_error_free (err);
         } else {
           text = encodings_convert_to_utf8 (content, length, NULL);
@@ -341,11 +346,13 @@
         }
         g_free (path_read);
       }
+      path_write_u8 = utils_get_utf8_from_locale (path_write);
       /* It's no Ruby, but it is the closest one I've found. It has:
        *  - # comments
        *  - multi-line double-quoted strings
        */
-      document_new_file (path_write, filetypes[GEANY_FILETYPES_RUBY], text);
+      document_new_file (path_write_u8, filetypes[GEANY_FILETYPES_RUBY], text);
+      g_free (path_write_u8);
       g_free (text);
       g_free (path_write);
     }

Modified: trunk/geany-plugins/geanygendoc/src/ggd-utils.c
===================================================================
--- trunk/geany-plugins/geanygendoc/src/ggd-utils.c	2010-12-31 14:26:41 UTC (rev 1793)
+++ trunk/geany-plugins/geanygendoc/src/ggd-utils.c	2010-12-31 14:27:06 UTC (rev 1794)
@@ -28,6 +28,7 @@
 #include <errno.h>
 #include <stdio.h> /* for BUFSIZ */
 #include <glib.h>
+#include <glib/gstdio.h>
 #include <gio/gio.h> /* for G_FILE_ERROR and friends */
 #include <geanyplugin.h>
 
@@ -38,22 +39,26 @@
  * set_file_error_from_errno:
  * @error: A #GError
  * @errnum: An errno value
- * @filename: The file name for which the error applies
+ * @filename: The file name for which the error applies in the GLib file names
+ *            encoding
  * 
  * Sets a #GError from an errno value, prefixed with a file's name.
  */
 #define set_file_error_from_errno(error, errnum, filename)                     \
   G_STMT_START {                                                               \
-    gint s_e_f_e_errum = errnum; /* need if @errnum is errno */                \
+    gchar  *s_e_f_e_filename;                                                  \
+    gint    s_e_f_e_errum = errnum; /* need if @errnum is errno */             \
                                                                                \
+    s_e_f_e_filename = g_filename_display_name (filename);                     \
     g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (s_e_f_e_errum), \
-                 "%s: %s", filename, g_strerror (s_e_f_e_errum));              \
+                 "%s: %s", s_e_f_e_filename, g_strerror (s_e_f_e_errum));      \
+    g_free (s_e_f_e_filename);                                                 \
   } G_STMT_END
 
 /*
  * ggd_copy_file:
- * @input: Path of the file to copy
- * @output: Path of the destination
+ * @input: Path of the file to copy, in the GLib file names encoding
+ * @output: Path of the destination, in the GLib file names encoding
  * @exclusive: %FALSE to override the destination if it already exist, %TRUE
  *             otherwise
  * @mode: Mode to use for creating the file
@@ -76,7 +81,7 @@
   gboolean  success = FALSE;
   gint      fd_in;
   
-  fd_in = open (input, O_RDONLY);
+  fd_in = g_open (input, O_RDONLY);
   if (fd_in < 0) {
     set_file_error_from_errno (error, errno, input);
   } else {
@@ -85,7 +90,7 @@
     
     flags_out = O_WRONLY | O_CREAT | O_TRUNC;
     if (exclusive) flags_out |= O_EXCL;
-    fd_out = open (output, flags_out, mode);
+    fd_out = g_open (output, flags_out, mode);
     if (fd_out < 0) {
       set_file_error_from_errno (error, errno, output);
     } else {
@@ -106,10 +111,14 @@
             set_file_error_from_errno (error, errno, output);
             success = FALSE;
           } else if (size_out < size_in) {
+            gchar *display_input;
+            
+            display_input = g_filename_display_name (input);
             g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
                          "%s: failed to write %"G_GSIZE_FORMAT" bytes "
                          "(read %"G_GSIZE_FORMAT", wrote %"G_GSIZE_FORMAT")",
-                         input, size_in - size_out, size_in, size_out);
+                         display_input, size_in - size_out, size_in, size_out);
+            g_free (display_input);
             success = FALSE;
           }
         }
@@ -124,9 +133,9 @@
 
 /**
  * ggd_get_config_file:
- * @name: The name of the configuration file to get
+ * @name: The name of the configuration file to get (ASCII string)
  * @section: The name of the configuration section of the file, or %NULL for the
- *           default one
+ *           default one (ASCII string)
  * @perms_req: Requested permissions on the configuration file
  * @error: Return location for errors, or %NULL to ignore them
  * 
@@ -138,7 +147,8 @@
  * at the returned path will be copied from the system configuration directory,
  * or created empty if the system file doesn't exist.
  * 
- * Returns: The path for the requested configuration file or %NULL if not found.
+ * Returns: The path for the requested configuration file in the GLib file names
+ *          encoding, or %NULL if path cannot be found.
  */
 gchar *
 ggd_get_config_file (const gchar *name,
@@ -155,6 +165,9 @@
   g_return_val_if_fail (name != NULL, NULL);
   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
   
+  /* here we guess the locale encoding is ASCII-compatible, anyway it's the case
+   * on Windows since we use UTF-8 and on UNIX it would cause too much troubles
+   * everywhere if it is not anyway */
   user_dir = g_build_filename (geany->app->configdir, "plugins",
                                GGD_PLUGIN_CNAME, section, NULL);
   system_dir = g_build_filename (PKGDATADIR, GGD_PLUGIN_CNAME, section, NULL);
@@ -216,7 +229,7 @@
           gint fd;
           
           g_clear_error (&gerr);
-          fd = open (user_path, O_CREAT | O_WRONLY, 0640);
+          fd = g_open (user_path, O_CREAT | O_WRONLY, 0640);
           if (fd < 0) {
             set_file_error_from_errno (&gerr, errno, user_path);
           } else {


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.



More information about the Plugins-Commits mailing list