Fixed compiler warning placement-parser.c
[pspp] / src / language / data-io / get-data.c
index 9b878c553a92bf1a16e37f5985506e025db924c6..ac2944caee4974197333c1467c92f9c2966ebc20 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
+   Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013 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
@@ -35,6 +35,7 @@
 #include "language/data-io/placement-parser.h"
 #include "language/lexer/format-parser.h"
 #include "language/lexer/lexer.h"
+#include "libpspp/cast.h"
 #include "libpspp/i18n.h"
 #include "libpspp/message.h"
 
@@ -64,10 +65,12 @@ cmd_get_data (struct lexer *lexer, struct dataset *ds)
   tok = strdup (lex_tokcstr (lexer));
   if (lex_match_id (lexer, "TXT"))
     {
+      free (tok);
       return parse_get_txt (lexer, ds);
     }
   else if (lex_match_id (lexer, "PSQL"))
     {
+      free (tok);
       return parse_get_psql (lexer, ds);
     }
   else if (lex_match_id (lexer, "GNM") || 
@@ -224,10 +227,19 @@ parse_spreadsheet (struct lexer *lexer)
          else if (lex_match_id (lexer, "INDEX"))
            {
              sri->sheet_index = lex_integer (lexer);
+             if (sri->sheet_index <= 0)
+               {
+                 msg (SE, _("The sheet index must be greater than or equal to 1"));
+                 goto error;
+               }
              lex_get (lexer);
            }
          else
-           goto error;
+           {
+             msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
+                  "/SHEET", "NAME", "INDEX");
+             goto error;
+           }
        }
       else if (lex_match_id (lexer, "CELLRANGE"))
        {
@@ -246,7 +258,11 @@ parse_spreadsheet (struct lexer *lexer)
              lex_get (lexer);
            }
          else
-           goto error;
+           {
+             msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
+                  "/CELLRANGE", "FULL", "RANGE");
+             goto error;
+           }
        }
       else if (lex_match_id (lexer, "READNAMES"))
        {
@@ -261,7 +277,11 @@ parse_spreadsheet (struct lexer *lexer)
              sri->read_names = false;
            }
          else
-           goto error;
+           {
+             msg (SE, _("%s must be followed by either \"%s\" or \"%s\"."),
+                  "/READNAMES", "ON", "OFF");
+             goto error;
+           }
        }
       else
        {
@@ -306,6 +326,7 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
   struct dictionary *dict = dict_create (get_default_encoding ());
   struct file_handle *fh = NULL;
   struct dfm_reader *reader = NULL;
+  char *encoding = NULL;
   char *name = NULL;
 
   int record;
@@ -333,7 +354,18 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
       if (!lex_force_match (lexer, T_SLASH))
         goto error;
 
-      if (lex_match_id (lexer, "ARRANGEMENT"))
+      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 if (lex_match_id (lexer, "ARRANGEMENT"))
         {
           bool ok;
 
@@ -345,7 +377,7 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
                            DP_DELIMITED, &has_type);
           else
             {
-              lex_error (lexer, _("expecting %s or %s"), "FIXED", "DELIMITED");
+              lex_error_expecting (lexer, "FIXED", "DELIMITED", NULL_SENTINEL);
               goto error;
             }
           if (!ok)
@@ -383,7 +415,7 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
             }
           else
             {
-              lex_error (lexer, _("expecting %s or %s"), "LINE", "VARIABLES");
+              lex_error_expecting (lexer, "LINE", "VARIABLES", NULL_SENTINEL);
               goto error;
             }
         }
@@ -494,7 +526,7 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
         break;
       else
         {
-          lex_error (lexer, _("expecting %s"), "VARIABLES");
+          lex_error_expecting (lexer, "VARIABLES", NULL_SENTINEL);
           goto error;
         }
     }
@@ -605,12 +637,13 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
     }
   while (lex_token (lexer) != T_ENDCMD);
 
-  reader = dfm_open_reader (fh, lexer);
+  reader = dfm_open_reader (fh, lexer, encoding);
   if (reader == NULL)
     goto error;
 
   data_parser_make_active_file (parser, ds, reader, dict);
   fh_unref (fh);
+  free (encoding);
   return CMD_SUCCESS;
 
  error:
@@ -618,6 +651,7 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
   dict_destroy (dict);
   fh_unref (fh);
   free (name);
+  free (encoding);
   return CMD_CASCADING_FAILURE;
 }