From f23643cfa3b4c6b9a5fe0cd890a8d1b7240a8552 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Mon, 3 Feb 2014 22:53:10 -0800 Subject: [PATCH] SYSFILE INFO: Add ENCODING subcommand. --- NEWS | 5 ++- doc/files.texi | 11 +++++- src/language/dictionary/sys-file-info.c | 52 ++++++++++++++++++++----- src/ui/gui/psppire-data-window.c | 13 +++++-- src/ui/gui/psppire-encoding-selector.c | 5 ++- 5 files changed, 69 insertions(+), 17 deletions(-) diff --git a/NEWS b/NEWS index 50e1332bb5..e650552850 100644 --- a/NEWS +++ b/NEWS @@ -4,11 +4,14 @@ See the end for copying conditions. Please send PSPP bug reports to bug-gnu-pspp@gnu.org. -Changes from 0.8.2 to 0.8.3: +Changes since 0.8.2: * REGRESSION now recognises /STATISTICS=CI(x) which causes confidence intervals for the coefficients to be printed. + * The SYSFILE INFO command now accepts an ENCODING subcommand to + specify the character encoding of string data in the system file. + Changes from 0.8.1 to 0.8.2: * Charts are now rendered with colours from the Tango palette instead diff --git a/doc/files.texi b/doc/files.texi index 369a2e46f4..318c887265 100644 --- a/doc/files.texi +++ b/doc/files.texi @@ -176,7 +176,7 @@ file on disk. Only the active dataset read from the file is affected by these subcommands. @pspp{} tries to automatically detect the encoding of string data in the -file. Sometimes, however, this does not work well encoding, +file. Sometimes, however, this does not work well, especially for files written by old versions of SPSS or @pspp{}. Specify the @subcmd{ENCODING} subcommand with an @acronym{IANA} character set name as its string argument to override the default. The @subcmd{ENCODING} subcommand is a @pspp{} @@ -910,7 +910,7 @@ qualifier character that appears within a value is doubled. @vindex SYSFILE INFO @display -SYSFILE INFO FILE='@var{file_name}'. +SYSFILE INFO FILE='@var{file_name}' [ENCODING='@var{encoding}']. @end display @cmd{SYSFILE INFO} reads the dictionary in a system file and @@ -919,6 +919,13 @@ displays the information in its dictionary. Specify a file name or file handle. @cmd{SYSFILE INFO} reads that file as a system file and displays information on its dictionary. +@pspp{} tries to automatically detect the encoding of string data in +the file. Sometimes, however, this does not work well, especially for +files written by old versions of SPSS or @pspp{}. Specify the +@subcmd{ENCODING} subcommand with an @acronym{IANA} character set name +as its string argument to override the default. The @subcmd{ENCODING} +subcommand is a @pspp{} extension. + @cmd{SYSFILE INFO} does not affect the current active dataset. @node XEXPORT diff --git a/src/language/dictionary/sys-file-info.c b/src/language/dictionary/sys-file-info.c index 2fdbdaf3c2..f719ba2fb1 100644 --- a/src/language/dictionary/sys-file-info.c +++ b/src/language/dictionary/sys-file-info.c @@ -73,21 +73,50 @@ cmd_sysfile_info (struct lexer *lexer, struct dataset *ds UNUSED) struct tab_table *t; struct casereader *reader; struct sfm_read_info info; + char *encoding; int r, i; - lex_match_id (lexer, "FILE"); - lex_match (lexer, T_EQUALS); + h = NULL; + encoding = NULL; + for (;;) + { + lex_match (lexer, T_SLASH); - h = fh_parse (lexer, FH_REF_FILE, NULL); - if (!h) - return CMD_FAILURE; + if (lex_match_id (lexer, "FILE") || lex_is_string (lexer)) + { + lex_match (lexer, T_EQUALS); - reader = sfm_open_reader (h, NULL, &d, &info); - if (!reader) + fh_unref (h); + h = fh_parse (lexer, FH_REF_FILE, NULL); + if (h == NULL) + goto error; + } + else if (lex_match_id (lexer, "ENCODING")) + { + lex_match (lexer, T_EQUALS); + + if (!lex_force_string (lexer)) + goto error; + + free (encoding); + encoding = ss_xstrdup (lex_tokss (lexer)); + + lex_get (lexer); + } + else + break; + } + + if (h == NULL) { - fh_unref (h); - return CMD_FAILURE; + lex_sbc_missing ("FILE"); + goto error; } + + reader = sfm_open_reader (h, encoding, &d, &info); + if (!reader) + goto error; + casereader_destroy (reader); t = tab_create (2, 11 + (info.product_ext != NULL)); @@ -184,6 +213,11 @@ cmd_sysfile_info (struct lexer *lexer, struct dataset *ds UNUSED) fh_unref (h); sfm_read_info_destroy (&info); return CMD_SUCCESS; + +error: + fh_unref (h); + free (encoding); + return CMD_FAILURE; } /* DISPLAY utility. */ diff --git a/src/ui/gui/psppire-data-window.c b/src/ui/gui/psppire-data-window.c index a0c4b9645a..35ffdb840f 100644 --- a/src/ui/gui/psppire-data-window.c +++ b/src/ui/gui/psppire-data-window.c @@ -1,5 +1,5 @@ /* PSPPIRE - a graphical user interface for PSPP. - Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation + Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -35,6 +35,7 @@ #include "ui/gui/helper.h" #include "ui/gui/psppire-data-window.h" #include "ui/gui/psppire-dialog-action.h" +#include "ui/gui/psppire-encoding-selector.h" #include "ui/gui/psppire-syntax-window.h" #include "ui/gui/psppire-window.h" #include "ui/gui/psppire.h" @@ -445,10 +446,12 @@ sysfile_info (PsppireDataWindow *de) struct string filename; gchar *file_name = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog)); - gchar *utf8_file_name = g_filename_to_utf8 (file_name, -1, NULL, NULL, NULL); + const gchar *encoding = psppire_encoding_selector_get_encoding ( + gtk_file_chooser_get_extra_widget (GTK_FILE_CHOOSER (dialog))); + gchar *syntax; ds_init_empty (&filename); @@ -457,7 +460,11 @@ sysfile_info (PsppireDataWindow *de) g_free (utf8_file_name); - syntax = g_strdup_printf ("SYSFILE INFO %s.", ds_cstr (&filename)); + if (encoding) + syntax = g_strdup_printf ("SYSFILE INFO %s ENCODING='%s'.", + ds_cstr (&filename), encoding); + else + syntax = g_strdup_printf ("SYSFILE INFO %s.", ds_cstr (&filename)); g_free (execute_syntax_string (de, syntax)); } diff --git a/src/ui/gui/psppire-encoding-selector.c b/src/ui/gui/psppire-encoding-selector.c index 0352051c3a..42fbea44e6 100644 --- a/src/ui/gui/psppire-encoding-selector.c +++ b/src/ui/gui/psppire-encoding-selector.c @@ -1,5 +1,5 @@ /* PSPPIRE - a graphical user interface for PSPP. - Copyright (C) 2011, 2012 Free Software Foundation, Inc. + Copyright (C) 2011, 2012, 2014 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -207,5 +207,6 @@ psppire_encoding_selector_get_encoding (GtkWidget *selector) } } g_list_free (list); - return encoding; + + return encoding && !strcmp (encoding, "Auto") ? NULL : encoding; } -- 2.30.2