data-parser: Make data parser not require a dictionary.
[pspp] / src / language / data-io / get-data.c
index 2879e34a4bb3f329a3ab244cd8bb4c6b75f6e228..0c60bd3d63bb2c84104a28442e331499a088c30d 100644 (file)
@@ -1,6 +1,6 @@
 /* PSPP - a program for statistical analysis.
    Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012,
-                 2013 Free Software Foundation, Inc.
+                 2013, 2015, 2016 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
@@ -58,14 +58,28 @@ int
 cmd_get_data (struct lexer *lexer, struct dataset *ds)
 {
   char *tok = NULL;
-  lex_force_match (lexer, T_SLASH);
+  struct spreadsheet_read_options opts;
+
+  opts.sheet_name = NULL;
+  opts.sheet_index = -1;
+  opts.cell_range = NULL;
+  opts.read_names = false;
+  opts.asw = -1;
+
+  if (! lex_force_match (lexer, T_SLASH))
+    goto error;
 
   if (!lex_force_match_id (lexer, "TYPE"))
-    return CMD_FAILURE;
+    goto error;
 
-  lex_force_match (lexer, T_EQUALS);
+  if (!lex_force_match (lexer, T_EQUALS))
+    goto error;
+
+  const char *s = lex_tokcstr (lexer);
+
+  if (s)
+    tok = strdup (s);
 
-  tok = strdup (lex_tokcstr (lexer));
   if (lex_match_id (lexer, "TXT"))
     {
       free (tok);
@@ -76,52 +90,45 @@ cmd_get_data (struct lexer *lexer, struct dataset *ds)
       free (tok);
       return parse_get_psql (lexer, ds);
     }
-  else if (lex_match_id (lexer, "GNM") || 
+  else if (lex_match_id (lexer, "GNM") ||
       lex_match_id (lexer, "ODS"))
     {
       char *filename = NULL;
-      struct casereader *reader = NULL;
-      struct dictionary *dict = NULL;
-      struct spreadsheet_read_options opts;
       if (!parse_spreadsheet (lexer, &filename, &opts))
        goto error;
 
-      if ( 0 == strncasecmp (tok, "GNM", 3))
-       {
-         struct spreadsheet *spreadsheet = gnumeric_probe (filename, true);
-         if (spreadsheet == NULL)
-           goto error;
-         reader = gnumeric_make_reader (spreadsheet, &opts);
-         dict = spreadsheet->dict;
-       }
+      struct spreadsheet *spreadsheet = NULL;
+      if (0 == strncasecmp (tok, "GNM", 3))
+        spreadsheet = gnumeric_probe (filename, true);
       else if (0 == strncasecmp (tok, "ODS", 3))
-       {
-         struct spreadsheet *spreadsheet = ods_probe (filename, true);
-         if (spreadsheet == NULL)
-           goto error;
-         reader = ods_make_reader (spreadsheet, &opts);
-         dict = spreadsheet->dict;
-       }
+        spreadsheet = ods_probe (filename, true);
 
+      if (spreadsheet == NULL)
+        {
+          msg (SE, _("error reading file `%s'"), filename);
+          free (filename);
+          goto error;
+        }
       free (filename);
 
+      struct casereader *reader = spreadsheet_make_reader (spreadsheet, &opts);
       if (reader)
        {
-         dataset_set_dict (ds, dict);
+         dataset_set_dict (ds, dict_clone (spreadsheet->dict));
          dataset_set_source (ds, reader);
          free (tok);
          destroy_spreadsheet_read_info (&opts);
+         spreadsheet_unref (spreadsheet);
          return CMD_SUCCESS;
        }
-      destroy_spreadsheet_read_info (&opts);
+      spreadsheet_unref (spreadsheet);
     }
   else
     msg (SE, _("Unsupported TYPE %s."), tok);
 
 
-
-
  error:
+  destroy_spreadsheet_read_info (&opts);
   free (tok);
   return CMD_FAILURE;
 }
@@ -136,12 +143,14 @@ parse_get_psql (struct lexer *lexer, struct dataset *ds)
   psql.bsize = -1;
   ds_init_empty (&psql.sql);
 
-  lex_force_match (lexer, T_SLASH);
+  if (! lex_force_match (lexer, T_SLASH))
+    goto error;
 
   if (!lex_force_match_id (lexer, "CONNECT"))
     goto error;
 
-  lex_force_match (lexer, T_EQUALS);
+  if (! lex_force_match (lexer, T_EQUALS))
+    goto error;
 
   if (!lex_force_string (lexer))
     goto error;
@@ -150,28 +159,34 @@ parse_get_psql (struct lexer *lexer, struct dataset *ds)
 
   lex_get (lexer);
 
-  while (lex_match (lexer, T_SLASH) )
+  while (lex_match (lexer, T_SLASH))
     {
-      if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
+      if (lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
        {
          lex_match (lexer, T_EQUALS);
-         psql.str_width = lex_integer (lexer);
-         lex_get (lexer);
+          if (lex_force_int_range (lexer, "ASSUMEDSTRWIDTH", 1, 32767))
+            {
+              psql.str_width = lex_integer (lexer);
+              lex_get (lexer);
+            }
        }
-      else if ( lex_match_id (lexer, "BSIZE"))
+      else if (lex_match_id (lexer, "BSIZE"))
        {
          lex_match (lexer, T_EQUALS);
-         psql.bsize = lex_integer (lexer);
-         lex_get (lexer);
+          if (lex_force_int_range (lexer, "BSIZE", 1, INT_MAX))
+            {
+              psql.bsize = lex_integer (lexer);
+              lex_get (lexer);
+            }
        }
-      else if ( lex_match_id (lexer, "UNENCRYPTED"))
+      else if (lex_match_id (lexer, "UNENCRYPTED"))
        {
          psql.allow_clear = true;
        }
       else if (lex_match_id (lexer, "SQL"))
        {
          lex_match (lexer, T_EQUALS);
-         if ( ! lex_force_string (lexer) )
+         if (! lex_force_string (lexer))
            goto error;
 
          ds_put_substring (&psql.sql, lex_tokss (lexer));
@@ -182,7 +197,7 @@ parse_get_psql (struct lexer *lexer, struct dataset *ds)
     struct dictionary *dict = NULL;
     struct casereader *reader = psql_open_reader (&psql, &dict);
 
-    if ( reader )
+    if (reader)
       {
         dataset_set_dict (ds, dict);
         dataset_set_source (ds, reader);
@@ -203,7 +218,7 @@ parse_get_psql (struct lexer *lexer, struct dataset *ds)
 }
 
 static bool
-parse_spreadsheet (struct lexer *lexer, char **filename, 
+parse_spreadsheet (struct lexer *lexer, char **filename,
                   struct spreadsheet_read_options *opts)
 {
   opts->sheet_index = 1;
@@ -212,12 +227,14 @@ parse_spreadsheet (struct lexer *lexer, char **filename,
   opts->read_names = true;
   opts->asw = -1;
 
-  lex_force_match (lexer, T_SLASH);
+  if (! lex_force_match (lexer, T_SLASH))
+    goto error;
 
   if (!lex_force_match_id (lexer, "FILE"))
     goto error;
 
-  lex_force_match (lexer, T_EQUALS);
+  if (! lex_force_match (lexer, T_EQUALS))
+    goto error;
 
   if (!lex_force_string (lexer))
     goto error;
@@ -226,20 +243,23 @@ parse_spreadsheet (struct lexer *lexer, char **filename,
 
   lex_get (lexer);
 
-  while (lex_match (lexer, T_SLASH) )
+  while (lex_match (lexer, T_SLASH))
     {
-      if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
+      if (lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
        {
          lex_match (lexer, T_EQUALS);
-         opts->asw = lex_integer (lexer);
-         lex_get (lexer);
+          if (lex_force_int_range (lexer, "ASSUMEDSTRWIDTH", 1, 32767))
+            {
+              opts->asw = lex_integer (lexer);
+              lex_get (lexer);
+            }
        }
       else if (lex_match_id (lexer, "SHEET"))
        {
          lex_match (lexer, T_EQUALS);
          if (lex_match_id (lexer, "NAME"))
            {
-             if ( ! lex_force_string (lexer) )
+             if (! lex_force_string (lexer))
                goto error;
 
              opts->sheet_name = ss_xstrdup (lex_tokss (lexer));
@@ -249,12 +269,9 @@ parse_spreadsheet (struct lexer *lexer, char **filename,
            }
          else if (lex_match_id (lexer, "INDEX"))
            {
+              if (!lex_force_int_range (lexer, "INDEX", 1, INT_MAX))
+                goto error;
              opts->sheet_index = lex_integer (lexer);
-             if (opts->sheet_index <= 0)
-               {
-                 msg (SE, _("The sheet index must be greater than or equal to 1"));
-                 goto error;
-               }
              lex_get (lexer);
            }
          else
@@ -274,7 +291,7 @@ parse_spreadsheet (struct lexer *lexer, char **filename,
            }
          else if (lex_match_id (lexer, "RANGE"))
            {
-             if ( ! lex_force_string (lexer) )
+             if (! lex_force_string (lexer))
                goto error;
 
              opts->cell_range = ss_xstrdup (lex_tokss (lexer));
@@ -291,7 +308,7 @@ parse_spreadsheet (struct lexer *lexer, char **filename,
        {
          lex_match (lexer, T_EQUALS);
 
-         if ( lex_match_id (lexer, "ON"))
+         if (lex_match_id (lexer, "ON"))
            {
              opts->read_names = true;
            }
@@ -355,20 +372,23 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
   enum data_parser_type type;
   bool has_type;
 
-  lex_force_match (lexer, T_SLASH);
+  if (! lex_force_match (lexer, T_SLASH))
+    goto error;
 
   if (!lex_force_match_id (lexer, "FILE"))
     goto error;
-  lex_force_match (lexer, T_EQUALS);
+  if (! lex_force_match (lexer, T_EQUALS))
+    goto error;
   fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE, NULL);
   if (fh == NULL)
     goto error;
 
-  parser = data_parser_create (dict);
+  parser = data_parser_create ();
   has_type = false;
   data_parser_set_type (parser, DP_DELIMITED);
   data_parser_set_span (parser, false);
   data_parser_set_quotes (parser, ss_empty ());
+  data_parser_set_quote_escape (parser, true);
   data_parser_set_empty_line_has_field (parser, true);
 
   for (;;)
@@ -399,7 +419,7 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
                            DP_DELIMITED, &has_type);
           else
             {
-              lex_error_expecting (lexer, "FIXED", "DELIMITED", NULL_SENTINEL);
+              lex_error_expecting (lexer, "FIXED", "DELIMITED");
               goto error;
             }
           if (!ok)
@@ -408,13 +428,8 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
       else if (lex_match_id (lexer, "FIRSTCASE"))
         {
          lex_match (lexer, T_EQUALS);
-          if (!lex_force_int (lexer))
+          if (!lex_force_int_range (lexer, "FIRSTCASE", 1, INT_MAX))
             goto error;
-          if (lex_integer (lexer) < 1)
-            {
-              msg (SE, _("Value of FIRSTCASE must be 1 or greater."));
-              goto error;
-            }
           data_parser_set_skip (parser, lex_integer (lexer) - 1);
           lex_get (lexer);
         }
@@ -437,7 +452,7 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
             }
           else
             {
-              lex_error_expecting (lexer, "LINE", "VARIABLES", NULL_SENTINEL);
+              lex_error_expecting (lexer, "LINE", "VARIABLES");
               goto error;
             }
         }
@@ -446,13 +461,8 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
           if (!set_type (parser, "FIXCASE", DP_FIXED, &has_type))
             goto error;
           lex_match (lexer, T_EQUALS);
-          if (!lex_force_int (lexer))
+          if (!lex_force_int_range (lexer, "FIXCASE", 1, INT_MAX))
             goto error;
-          if (lex_integer (lexer) < 1)
-            {
-              msg (SE, _("Value of FIXCASE must be at least 1."));
-              goto error;
-            }
           data_parser_set_records (parser, lex_integer (lexer));
           lex_get (lexer);
         }
@@ -461,33 +471,22 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
           lex_match (lexer, T_EQUALS);
           if (lex_match (lexer, T_ALL))
             {
-              data_parser_set_case_limit (parser, -1);
-              data_parser_set_case_percent (parser, 100);
+              /* Nothing to do. */
             }
           else if (lex_match_id (lexer, "FIRST"))
             {
               if (!lex_force_int (lexer))
                 goto error;
-              if (lex_integer (lexer) < 1)
-                {
-                  msg (SE, _("Value of FIRST must be at least 1."));
-                  goto error;
-                }
-              data_parser_set_case_limit (parser, lex_integer (lexer));
               lex_get (lexer);
             }
           else if (lex_match_id (lexer, "PERCENT"))
             {
               if (!lex_force_int (lexer))
                 goto error;
-              if (lex_integer (lexer) < 1 || lex_integer (lexer) > 100)
-                {
-                  msg (SE, _("Value of PERCENT must be between 1 and 100."));
-                  goto error;
-                }
-              data_parser_set_case_percent (parser, lex_integer (lexer));
               lex_get (lexer);
             }
+          msg (SW, _("Ignoring obsolete IMPORTCASES subcommand.  (N OF CASES "
+                     "or SAMPLE may be used to substitute.)"));
         }
       else if (lex_match_id_n (lexer, "DELIMITERS", 4))
         {
@@ -541,14 +540,11 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
           data_parser_set_quotes (parser, lex_tokss (lexer));
           lex_get (lexer);
         }
-      else if (settings_get_syntax () == ENHANCED
-               && lex_match_id (lexer, "ESCAPE"))
-        data_parser_set_quote_escape (parser, true);
       else if (lex_match_id (lexer, "VARIABLES"))
         break;
       else
         {
-          lex_error_expecting (lexer, "VARIABLES", NULL_SENTINEL);
+          lex_error_expecting (lexer, "VARIABLES");
           goto error;
         }
     }
@@ -564,48 +560,42 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
 
       while (type == DP_FIXED && lex_match (lexer, T_SLASH))
         {
-          if (!lex_force_int (lexer))
+          if (!lex_force_int_range (lexer, NULL, record,
+                                    data_parser_get_records (parser)))
             goto error;
-          if (lex_integer (lexer) < record)
-            {
-              msg (SE, _("The record number specified, %ld, is at or "
-                         "before the previous record, %d.  Data "
-                         "fields must be listed in order of "
-                         "increasing record number."),
-                   lex_integer (lexer), record);
-              goto error;
-            }
-          if (lex_integer (lexer) > data_parser_get_records (parser))
-            {
-              msg (SE, _("The record number specified, %ld, exceeds "
-                         "the number of records per case specified "
-                         "on FIXCASE, %d."),
-                   lex_integer (lexer), data_parser_get_records (parser));
-              goto error;
-            }
           record = lex_integer (lexer);
           lex_get (lexer);
         }
 
+      const char * tstr = lex_tokcstr (lexer);
+      if (tstr == NULL)
+       {
+         lex_error (lexer, NULL);
+         goto error;
+       }
+      name = xstrdup (tstr);
       if (!lex_force_id (lexer)
-          || !dict_id_is_valid (dict, lex_tokcstr (lexer), true))
-        goto error;
-      name = xstrdup (lex_tokcstr (lexer));
+          || !dict_id_is_valid (dict, name, true))
+       {
+         goto error;
+       }
       lex_get (lexer);
-
       if (type == DP_DELIMITED)
         {
           if (!parse_format_specifier (lexer, &input)
-              || !fmt_check_input (&input))
-            goto error;
-
-          output = fmt_for_output_from_input (&input);
+             || !fmt_check_input (&input))
+           {
+             goto error;
+           }
+          output = fmt_for_output_from_input (&input,
+                                              settings_get_fmt_settings ());
         }
       else
         {
           char fmt_type_name[FMT_TYPE_LEN_MAX + 1];
           enum fmt_type fmt_type;
-          int w, d;
+          uint16_t w;
+          uint8_t d;
 
           if (!parse_column_range (lexer, 0, &fc, &lc, NULL))
             goto error;
@@ -618,14 +608,12 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
               msg (SE, _("Unknown format type `%s'."), fmt_type_name);
               goto error;
             }
-
           /* Compose input format. */
           input.type = fmt_type;
           input.w = lc - fc + 1;
           input.d = 0;
           if (!fmt_check_input (&input))
             goto error;
-
           /* Compose output format. */
           if (w != 0)
             {
@@ -636,9 +624,9 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
                 goto error;
             }
           else
-            output = fmt_for_output_from_input (&input);
+            output = fmt_for_output_from_input (&input,
+                                                settings_get_fmt_settings ());
         }
-
       v = dict_create_var (dict, name, fmt_var_width (&input));
       if (v == NULL)
         {
@@ -646,7 +634,6 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
           goto error;
         }
       var_set_both_formats (v, &output);
-
       if (type == DP_DELIMITED)
         data_parser_add_delimited_field (parser, &input,
                                          var_get_case_index (v),
@@ -663,14 +650,14 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
   if (reader == NULL)
     goto error;
 
-  data_parser_make_active_file (parser, ds, reader, dict);
+  data_parser_make_active_file (parser, ds, reader, dict, NULL, NULL);
   fh_unref (fh);
   free (encoding);
   return CMD_SUCCESS;
 
  error:
   data_parser_destroy (parser);
-  dict_destroy (dict);
+  dict_unref (dict);
   fh_unref (fh);
   free (name);
   free (encoding);
@@ -678,8 +665,9 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
 }
 
 
-static void 
+static void
 destroy_spreadsheet_read_info (struct spreadsheet_read_options *opts)
 {
   free (opts->cell_range);
+  free (opts->sheet_name);
 }