From: John Darrington Date: Mon, 23 Mar 2009 07:39:06 +0000 (+0900) Subject: Convert filename encodings when opening files. X-Git-Tag: v0.7.3~207 X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5dd7174d3acde86cfd7e929dabcafe71894a2a3f;p=pspp-builds.git Convert filename encodings when opening files. On w32, filenames have to be converted from utf8 to the libc encoding before passing to the lexer. --- diff --git a/src/ui/gui/helper.c b/src/ui/gui/helper.c index 83319c45..9ac9fde6 100644 --- a/src/ui/gui/helper.c +++ b/src/ui/gui/helper.c @@ -166,6 +166,31 @@ utf8_to_pspp_locale (const gchar *text, gssize len, GError **err) return recode_string (CONV_UTF8_TO_PSPP, text, len); } +/* This function must be used whenever a filename generated by glib, + (eg, from gtk_file_chooser_get_filename) and passed to the C library, + (eg through a pspp syntax string). +*/ +gchar * +convert_glib_filename_to_system_filename (const gchar *fname, GError **err) +{ + gchar *output_name; + +#ifdef G_OS_WIN32 + const gchar *target_encoding; + gchar *utf8_name = NULL; + + g_get_charset (&target_encoding); + + output_name = g_convert (fname, -1, target_encoding, + "UTF-8", NULL, NULL, err); +#else + output_name = strdup (fname); +#endif + + return output_name; +} + + #define _(msgid) gettext (msgid) #define N_(msgid) msgid diff --git a/src/ui/gui/helper.h b/src/ui/gui/helper.h index ca15a35c..30792faf 100644 --- a/src/ui/gui/helper.h +++ b/src/ui/gui/helper.h @@ -47,6 +47,9 @@ GtkWidget * get_widget_assert (GtkBuilder *builder, const gchar *name); gchar * pspp_locale_to_utf8 (const gchar *text, gssize len, GError **err); gchar * utf8_to_pspp_locale (const gchar *text, gssize len, GError **err); +gchar * convert_glib_filename_to_system_filename (const gchar *fname, + GError **err); + void connect_help (GtkBuilder *); diff --git a/src/ui/gui/psppire-data-window.c b/src/ui/gui/psppire-data-window.c index 5efb02de..8eb0a143 100644 --- a/src/ui/gui/psppire-data-window.c +++ b/src/ui/gui/psppire-data-window.c @@ -346,11 +346,18 @@ dump_rm (GtkRecentManager *rm) static gboolean load_file (PsppireWindow *de, const gchar *file_name) { + gchar *native_file_name; struct getl_interface *sss; struct string filename; ds_init_empty (&filename); - syntax_gen_string (&filename, ss_cstr (file_name)); + + native_file_name = + convert_glib_filename_to_system_filename (file_name, NULL); + + syntax_gen_string (&filename, ss_cstr (native_file_name)); + + g_free (native_file_name); sss = create_syntax_string_source ("GET FILE=%s.", ds_cstr (&filename)); @@ -358,10 +365,7 @@ load_file (PsppireWindow *de, const gchar *file_name) ds_destroy (&filename); if (execute_syntax (sss) ) - { - psppire_window_set_filename (de, file_name); - return TRUE; - } + return TRUE; return FALSE; } diff --git a/src/ui/gui/psppire-syntax-window.c b/src/ui/gui/psppire-syntax-window.c index a5cc08aa..03b4093b 100644 --- a/src/ui/gui/psppire-syntax-window.c +++ b/src/ui/gui/psppire-syntax-window.c @@ -563,26 +563,14 @@ syntax_load (PsppireWindow *window, const gchar *filename) GtkTextIter iter; PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (window); - gchar *glibfilename = g_filename_from_utf8 (filename, -1, 0, 0, NULL); - - if ( ! glibfilename ) - return FALSE; - /* FIXME: What if it's a very big file ? */ - if ( ! g_file_get_contents (glibfilename, &text, NULL, NULL) ) - { - g_free (glibfilename); - return FALSE; - } - - g_free (glibfilename); + if ( ! g_file_get_contents (filename, &text, NULL, NULL) ) + return FALSE; gtk_text_buffer_get_iter_at_line (sw->buffer, &iter, 0); gtk_text_buffer_insert (sw->buffer, &iter, text, -1); - psppire_window_set_filename (window, filename); - gtk_text_buffer_set_modified (sw->buffer, FALSE); return TRUE; diff --git a/src/ui/gui/psppire-window.c b/src/ui/gui/psppire-window.c index 54c751d6..b95e6a00 100644 --- a/src/ui/gui/psppire-window.c +++ b/src/ui/gui/psppire-window.c @@ -145,7 +145,7 @@ psppire_window_set_property (GObject *object, candidate_name = uniquify (name, &x); } - window->basename = g_path_get_basename (candidate_name); + window->basename = g_filename_display_basename (candidate_name); g_value_unset (&def); } @@ -315,7 +315,10 @@ menu_activate (GtkMenuItem *mi, gpointer data) static void insert_menuitem_into_menu (PsppireWindow *window, gpointer key) { - GtkWidget *item = gtk_check_menu_item_new_with_label (key); + gchar *filename = g_filename_display_name (key); + GtkWidget *item = gtk_check_menu_item_new_with_label (filename); + + g_free (filename); g_signal_connect (item, "toggled", G_CALLBACK (menu_toggled), NULL); g_signal_connect (item, "activate", G_CALLBACK (menu_activate), key); @@ -625,6 +628,7 @@ psppire_window_load (PsppireWindow *w, const gchar *file) if ( ok ) { + psppire_window_set_filename (w, file); add_most_recent (file, the_recent_mgr); w->dirty = FALSE; }