SYSFILE INFO: Add ENCODING subcommand.
authorBen Pfaff <blp@cs.stanford.edu>
Tue, 4 Feb 2014 06:53:10 +0000 (22:53 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Tue, 4 Feb 2014 06:56:39 +0000 (22:56 -0800)
NEWS
doc/files.texi
src/language/dictionary/sys-file-info.c
src/ui/gui/psppire-data-window.c
src/ui/gui/psppire-encoding-selector.c

diff --git a/NEWS b/NEWS
index 50e1332bb5f2b3a8701fa63983a64c65a9119638..e6505528500428dcfffbd08fbd7321239e44efb9 100644 (file)
--- 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
index 369a2e46f48737b483243748a4e521026a61801c..318c887265eb83df35ed85437ff4db5564b3603b 100644 (file)
@@ -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
index 2fdbdaf3c2a1609107bbb1ff9e81325407978555..f719ba2fb1e8c0b9e508c49017fc0f3811120faa 100644 (file)
@@ -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;
 }
 \f
 /* DISPLAY utility. */
index a0c4b9645aa62ef27dedadaa4d5b91da67ae7283..35ffdb840f403836b2376a168100939331ef5541 100644 (file)
@@ -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));
     }
 
index 0352051c3a984f776e8483ea694921e2fda81aa3..42fbea44e6c4cfe78a878346397ea79a08efd25d 100644 (file)
@@ -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;
 }