Patch #6262. New developers guide and resulting fixes and cleanups.
[pspp-builds.git] / src / language / data-io / data-list.c
index 07a130c3e9d9dbe0003045bc49f2f98a8fb2089c..9e950e9ce69bd24799660908606a308962282e84 100644 (file)
@@ -1,25 +1,24 @@
-/* PSPP - computes sample statistics.
+/* PSPP - a program for statistical analysis.
    Copyright (C) 1997-9, 2000, 2006 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 the Free Software Foundation; either version 2 of the
-   License, or (at your option) any later version.
+   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
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
 
-   This program is distributed in the hope that it will be useful, but
-   WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   General Public License for more details.
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-   02110-1301, USA. */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <config.h>
 
 #include <ctype.h>
 #include <float.h>
+#include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 
@@ -41,7 +40,6 @@
 #include <language/lexer/format-parser.h>
 #include <language/lexer/lexer.h>
 #include <language/lexer/variable-parser.h>
-#include <libpspp/alloc.h>
 #include <libpspp/assertion.h>
 #include <libpspp/compiler.h>
 #include <libpspp/ll.h>
@@ -51,8 +49,8 @@
 #include <libpspp/str.h>
 #include <output/table.h>
 
-#include "size_max.h"
 #include "xsize.h"
+#include "xalloc.h"
 
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
@@ -67,7 +65,7 @@ struct dls_var_spec
     /* All parsers. */
     struct fmt_spec input;     /* Input format of this field. */
     int fv;                    /* First value in case. */
-    char name[LONG_NAME_LEN + 1]; /* Var name for error messages and tables. */
+    char name[VAR_NAME_LEN + 1]; /* Var name for error messages and tables. */
 
     /* Fixed format only. */
     int record;                        /* Record number (1-based). */
@@ -75,7 +73,7 @@ struct dls_var_spec
   };
 
 static struct dls_var_spec *
-ll_to_dls_var_spec (struct ll *ll) 
+ll_to_dls_var_spec (struct ll *ll)
 {
   return ll_data (ll, struct dls_var_spec, ll);
 }
@@ -104,9 +102,9 @@ struct data_list_pgm
 
 static const struct casereader_class data_list_casereader_class;
 
-static bool parse_fixed (struct lexer *, struct dictionary *dict, 
+static bool parse_fixed (struct lexer *, struct dictionary *dict,
                         struct pool *tmp_pool, struct data_list_pgm *);
-static bool parse_free (struct lexer *, struct dictionary *dict, 
+static bool parse_free (struct lexer *, struct dictionary *dict,
                        struct pool *tmp_pool, struct data_list_pgm *);
 static void dump_fixed_table (const struct ll_list *,
                               const struct file_handle *, int record_cnt);
@@ -122,7 +120,7 @@ cmd_data_list (struct lexer *lexer, struct dataset *ds)
   struct dictionary *dict;
   struct data_list_pgm *dls;
   int table = -1;                /* Print table if nonzero, -1=undecided. */
-  struct file_handle *fh = fh_inline_file ();
+  struct file_handle *fh = NULL;
   struct pool *tmp_pool;
   bool ok;
 
@@ -145,6 +143,7 @@ cmd_data_list (struct lexer *lexer, struct dataset *ds)
       if (lex_match_id (lexer, "FILE"))
        {
          lex_match (lexer, '=');
+          fh_unref (fh);
          fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE);
          if (fh == NULL)
            goto error;
@@ -174,12 +173,12 @@ cmd_data_list (struct lexer *lexer, struct dataset *ds)
              msg (SE, _("The END subcommand may only be specified once."));
              goto error;
            }
-         
+
          lex_match (lexer, '=');
          if (!lex_force_id (lexer))
            goto error;
          dls->end = dict_lookup_var (dict, lex_tokid (lexer));
-         if (!dls->end) 
+         if (!dls->end)
             dls->end = dict_create_var_assert (dict, lex_tokid (lexer), 0);
          lex_get (lexer);
        }
@@ -189,7 +188,7 @@ cmd_data_list (struct lexer *lexer, struct dataset *ds)
             table = 0;
           else if (lex_match_id (lexer, "TABLE"))
             table = 1;
-          else 
+          else
             {
               int type;
               if (lex_match_id (lexer, "FIXED"))
@@ -198,7 +197,7 @@ cmd_data_list (struct lexer *lexer, struct dataset *ds)
                 type = DLS_FREE;
               else if (lex_match_id (lexer, "LIST"))
                 type = DLS_LIST;
-              else 
+              else
                 {
                   lex_error (lexer, NULL);
                   goto error;
@@ -213,7 +212,7 @@ cmd_data_list (struct lexer *lexer, struct dataset *ds)
              dls->type = type;
 
               if ((dls->type == DLS_FREE || dls->type == DLS_LIST)
-                  && lex_match (lexer, '(')) 
+                  && lex_match (lexer, '('))
                 {
                   while (!lex_match (lexer, ')'))
                     {
@@ -226,7 +225,7 @@ cmd_data_list (struct lexer *lexer, struct dataset *ds)
                          delim = ds_first (lex_tokstr (lexer));
                          lex_get (lexer);
                        }
-                      else 
+                      else
                         {
                           lex_error (lexer, NULL);
                           goto error;
@@ -246,11 +245,19 @@ cmd_data_list (struct lexer *lexer, struct dataset *ds)
        }
     }
 
+  if (fh == NULL)
+    fh = fh_inline_file ();
   fh_set_default_handle (fh);
 
   if (dls->type == -1)
     dls->type = DLS_FIXED;
 
+  if (dls->type != DLS_FIXED && dls->end != NULL)
+    {
+      msg (SE, _("The END keyword may be used only with DATA LIST FIXED."));
+      goto error;
+    }
+
   if (table == -1)
     table = dls->type != DLS_FREE;
 
@@ -277,21 +284,23 @@ cmd_data_list (struct lexer *lexer, struct dataset *ds)
 
   if (in_input_program ())
     add_transformation (ds, data_list_trns_proc, data_list_trns_free, dls);
-  else 
+  else
     {
       struct casereader *reader;
       reader = casereader_create_sequential (NULL,
                                              dict_get_next_value_idx (dict),
                                              -1, &data_list_casereader_class,
                                              dls);
-      proc_set_active_file (ds, reader, dict); 
+      proc_set_active_file (ds, reader, dict);
     }
 
   pool_destroy (tmp_pool);
+  fh_unref (fh);
 
   return CMD_SUCCESS;
 
  error:
+  fh_unref (fh);
   data_list_trns_free (dls);
   return CMD_CASCADING_FAILURE;
 }
@@ -303,7 +312,7 @@ cmd_data_list (struct lexer *lexer, struct dataset *ds)
    needed once parsing is complete.  Returns true only if
    successful. */
 static bool
-parse_fixed (struct lexer *lexer, struct dictionary *dict, 
+parse_fixed (struct lexer *lexer, struct dictionary *dict,
             struct pool *tmp_pool, struct data_list_pgm *dls)
 {
   int last_nonempty_record;
@@ -319,7 +328,7 @@ parse_fixed (struct lexer *lexer, struct dictionary *dict,
 
       /* Parse everything. */
       if (!parse_record_placement (lexer, &record, &column)
-          || !parse_DATA_LIST_vars_pool (lexer, tmp_pool, 
+          || !parse_DATA_LIST_vars_pool (lexer, tmp_pool,
                                         &names, &name_cnt, PV_NONE)
           || !parse_var_placements (lexer, tmp_pool, name_cnt, true,
                                     &formats, &format_cnt))
@@ -334,7 +343,7 @@ parse_fixed (struct lexer *lexer, struct dictionary *dict,
             int width;
             struct variable *v;
             struct dls_var_spec *spec;
-              
+
             name = names[name_idx++];
 
             /* Create variable. */
@@ -352,7 +361,7 @@ parse_fixed (struct lexer *lexer, struct dictionary *dict,
                    This can be acceptable if we're in INPUT
                    PROGRAM, but only if the existing variable has
                    the same width as the one we would have
-                   created. */ 
+                   created. */
                 if (!in_input_program ())
                   {
                     msg (SE, _("%s is a duplicate variable name."), name);
@@ -388,7 +397,7 @@ parse_fixed (struct lexer *lexer, struct dictionary *dict,
           }
       assert (name_idx == name_cnt);
     }
-  if (ll_is_empty (&dls->specs)) 
+  if (ll_is_empty (&dls->specs))
     {
       msg (SE, _("At least one variable must be specified."));
       return false;
@@ -401,7 +410,7 @@ parse_fixed (struct lexer *lexer, struct dictionary *dict,
                 "should not exist according to RECORDS subcommand."));
       return false;
     }
-  else if (!dls->record_cnt) 
+  else if (!dls->record_cnt)
     dls->record_cnt = last_nonempty_record;
 
   return true;
@@ -455,7 +464,7 @@ dump_fixed_table (const struct ll_list *specs,
    them to DLS.  Uses TMP_POOL for data that is not needed once
    parsing is complete.  Returns true only if successful. */
 static bool
-parse_free (struct lexer *lexer, struct dictionary *dict, struct pool *tmp_pool, 
+parse_free (struct lexer *lexer, struct dictionary *dict, struct pool *tmp_pool,
                struct data_list_pgm *dls)
 {
   lex_get (lexer);
@@ -466,7 +475,7 @@ parse_free (struct lexer *lexer, struct dictionary *dict, struct pool *tmp_pool,
       size_t name_cnt;
       size_t i;
 
-      if (!parse_DATA_LIST_vars_pool (lexer, tmp_pool, 
+      if (!parse_DATA_LIST_vars_pool (lexer, tmp_pool,
                                      &name, &name_cnt, PV_NONE))
        return 0;
 
@@ -474,14 +483,14 @@ parse_free (struct lexer *lexer, struct dictionary *dict, struct pool *tmp_pool,
        {
          if (!parse_format_specifier (lexer, &input)
               || !fmt_check_input (&input)
-              || !lex_force_match (lexer, ')')) 
+              || !lex_force_match (lexer, ')'))
             return NULL;
 
           /* As a special case, N format is treated as F format
              for free-field input. */
           if (input.type == FMT_N)
             input.type = FMT_F;
-          
+
          output = fmt_for_output_from_input (&input);
        }
       else
@@ -527,7 +536,7 @@ dump_free_table (const struct data_list_pgm *dls,
   int row;
 
   spec_cnt = ll_count (&dls->specs);
-  
+
   t = tab_create (2, spec_cnt + 1, 0);
   tab_columns (t, TAB_COL_DOWN, 1);
   tab_headers (t, 0, 0, 1, 0);
@@ -547,11 +556,11 @@ dump_free_table (const struct data_list_pgm *dls,
     }
 
   tab_title (t, _("Reading free-form data from %s."), fh_get_name (fh));
-  
+
   tab_submit (t);
 }
 \f
-/* Input procedure. */ 
+/* Input procedure. */
 
 /* Extracts a field from the current position in the current
    record.  Fields can be unquoted or quoted with single- or
@@ -559,7 +568,7 @@ dump_free_table (const struct data_list_pgm *dls,
 
    *FIELD is set to the field content.  The caller must not
    or destroy this constant string.
-   
+
    After parsing the field, sets the current position in the
    record to just past the field and any trailing delimiter.
    Returns 0 on failure or a 1-based column number indicating the
@@ -575,15 +584,15 @@ cut_field (const struct data_list_pgm *dls, struct substring *field)
     dfm_expand_tabs (dls->reader);
   line = p = dfm_get_record (dls->reader);
 
-  if (ds_is_empty (&dls->delims)) 
+  if (ds_is_empty (&dls->delims))
     {
       bool missing_quote = false;
-      
+
       /* Skip leading whitespace. */
       ss_ltrim (&p, ss_cstr (CC_SPACES));
       if (ss_is_empty (p))
         return false;
-      
+
       /* Handle actual data, whether quoted or unquoted. */
       if (ss_match_char (&p, '\''))
         missing_quote = !ss_get_until (&p, '\'', field);
@@ -600,7 +609,7 @@ cut_field (const struct data_list_pgm *dls, struct substring *field)
 
       dfm_forward_columns (dls->reader, ss_length (line) - ss_length (p));
     }
-  else 
+  else
     {
       if (!ss_is_empty (p))
         ss_get_chars (&p, ss_cspan (p, ds_ss (&dls->delims)), field);
@@ -610,11 +619,11 @@ cut_field (const struct data_list_pgm *dls, struct substring *field)
              trailing blank field. */
           *field = p;
         }
-      else 
+      else
         return false;
 
       /* Advance past the field.
-         
+
          Also advance past a trailing delimiter, regardless of
          whether one actually existed.  If we "skip" a delimiter
          that was not actually there, then we will return
@@ -634,7 +643,7 @@ static bool read_from_data_list_list (const struct data_list_pgm *,
 /* Reads a case from DLS into C.
    Returns true if successful, false at end of file or on I/O error. */
 static bool
-read_from_data_list (const struct data_list_pgm *dls, struct ccase *c) 
+read_from_data_list (const struct data_list_pgm *dls, struct ccase *c)
 {
   bool retval;
 
@@ -659,16 +668,17 @@ read_from_data_list (const struct data_list_pgm *dls, struct ccase *c)
 }
 
 /* Reads a case from the data file into C, parsing it according
-   to fixed-format syntax rules in DLS.  
+   to fixed-format syntax rules in DLS.
    Returns true if successful, false at end of file or on I/O error. */
 static bool
 read_from_data_list_fixed (const struct data_list_pgm *dls, struct ccase *c)
 {
+  enum legacy_encoding encoding = dfm_reader_get_legacy_encoding (dls->reader);
   struct dls_var_spec *spec;
   int row;
 
-  if (dfm_eof (dls->reader)) 
-    return false; 
+  if (dfm_eof (dls->reader))
+    return false;
 
   spec = ll_to_dls_var_spec (ll_head (&dls->specs));
   for (row = 1; row <= dls->record_cnt; row++)
@@ -680,14 +690,21 @@ read_from_data_list_fixed (const struct data_list_pgm *dls, struct ccase *c)
           msg (SW, _("Partial case of %d of %d records discarded."),
                row - 1, dls->record_cnt);
           return false;
-        } 
+        }
       dfm_expand_tabs (dls->reader);
       line = dfm_get_record (dls->reader);
 
-      ll_for_each_continue (spec, struct dls_var_spec, ll, &dls->specs) 
-        data_in (ss_substr (line, spec->first_column - 1, spec->input.w),
-                 spec->input.type, spec->input.d, spec->first_column,
-                 case_data_rw_idx (c, spec->fv), fmt_var_width (&spec->input));
+      ll_for_each_continue (spec, struct dls_var_spec, ll, &dls->specs)
+        {
+          if (row < spec->record)
+            break;
+
+          data_in (ss_substr (line, spec->first_column - 1,
+                              spec->input.w),
+                   encoding, spec->input.type, spec->input.d,
+                   spec->first_column, case_data_rw_idx (c, spec->fv),
+                   fmt_var_width (&spec->input));
+        }
 
       dfm_forward_record (dls->reader);
     }
@@ -696,21 +713,22 @@ read_from_data_list_fixed (const struct data_list_pgm *dls, struct ccase *c)
 }
 
 /* Reads a case from the data file into C, parsing it according
-   to free-format syntax rules in DLS.  
+   to free-format syntax rules in DLS.
    Returns true if successful, false at end of file or on I/O error. */
 static bool
 read_from_data_list_free (const struct data_list_pgm *dls, struct ccase *c)
 {
+  enum legacy_encoding encoding = dfm_reader_get_legacy_encoding (dls->reader);
   struct dls_var_spec *spec;
 
   ll_for_each (spec, struct dls_var_spec, ll, &dls->specs)
     {
       struct substring field;
-      
+
       /* Cut out a field and read in a new record if necessary. */
       while (!cut_field (dls, &field))
        {
-         if (!dfm_eof (dls->reader)) 
+         if (!dfm_eof (dls->reader))
             dfm_forward_record (dls->reader);
          if (dfm_eof (dls->reader))
            {
@@ -720,8 +738,8 @@ read_from_data_list_free (const struct data_list_pgm *dls, struct ccase *c)
              return false;
            }
        }
-      
-      data_in (field, spec->input.type, 0,
+
+      data_in (field, encoding, spec->input.type, 0,
                dfm_get_column (dls->reader, ss_data (field)),
                case_data_rw_idx (c, spec->fv), fmt_var_width (&spec->input));
     }
@@ -729,11 +747,12 @@ read_from_data_list_free (const struct data_list_pgm *dls, struct ccase *c)
 }
 
 /* Reads a case from the data file and parses it according to
-   list-format syntax rules.  
+   list-format syntax rules.
    Returns true if successful, false at end of file or on I/O error. */
 static bool
 read_from_data_list_list (const struct data_list_pgm *dls, struct ccase *c)
 {
+  enum legacy_encoding encoding = dfm_reader_get_legacy_encoding (dls->reader);
   struct dls_var_spec *spec;
 
   if (dfm_eof (dls->reader))
@@ -756,12 +775,12 @@ read_from_data_list_list (const struct data_list_pgm *dls, struct ccase *c)
               if (width == 0)
                 case_data_rw_idx (c, spec->fv)->f = SYSMIS;
               else
-                memset (case_data_rw_idx (c, spec->fv)->s, ' ', width); 
+                memset (case_data_rw_idx (c, spec->fv)->s, ' ', width);
             }
          break;
        }
-      
-      data_in (field, spec->input.type, 0,
+
+      data_in (field, encoding, spec->input.type, 0,
                dfm_get_column (dls->reader, ss_data (field)),
                case_data_rw_idx (c, spec->fv), fmt_var_width (&spec->input));
     }
@@ -790,7 +809,7 @@ data_list_trns_proc (void *dls_, struct ccase *c, casenumber case_num UNUSED)
 
   if (read_from_data_list (dls, c))
     retval = TRNS_CONTINUE;
-  else if (dfm_reader_error (dls->reader) || dfm_eof (dls->reader) > 1) 
+  else if (dfm_reader_error (dls->reader) || dfm_eof (dls->reader) > 1)
     {
       /* An I/O error, or encountering end of file for a second
          time, should be escalated into a more serious error. */
@@ -798,15 +817,15 @@ data_list_trns_proc (void *dls_, struct ccase *c, casenumber case_num UNUSED)
     }
   else
     retval = TRNS_END_FILE;
-  
+
   /* If there was an END subcommand handle it. */
-  if (dls->end != NULL) 
+  if (dls->end != NULL)
     {
       double *end = &case_data_rw (c, dls->end)->f;
-      if (retval == TRNS_DROP_CASE)
+      if (retval == TRNS_END_FILE)
         {
           *end = 1.0;
-          retval = TRNS_END_FILE;
+          retval = TRNS_CONTINUE;
         }
       else
         *end = 0.0;
@@ -824,10 +843,10 @@ data_list_casereader_read (struct casereader *reader UNUSED, void *dls_,
 {
   struct data_list_pgm *dls = dls_;
   bool ok;
-  
+
   /* Skip the requested number of records before reading the
      first case. */
-  while (dls->skip_records > 0) 
+  while (dls->skip_records > 0)
     {
       if (dfm_eof (dls->reader))
         return false;