Patch #6262. New developers guide and resulting fixes and cleanups.
[pspp-builds.git] / src / language / data-io / data-list.c
index d2ca99185833c745cbb3daff0763fd9244a7a470..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). */
@@ -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;
@@ -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;
 
@@ -288,10 +295,12 @@ cmd_data_list (struct lexer *lexer, struct dataset *ds)
     }
 
   pool_destroy (tmp_pool);
+  fh_unref (fh);
 
   return CMD_SUCCESS;
 
  error:
+  fh_unref (fh);
   data_list_trns_free (dls);
   return CMD_CASCADING_FAILURE;
 }
@@ -664,6 +673,7 @@ read_from_data_list (const struct data_list_pgm *dls, struct ccase *c)
 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;
 
@@ -685,9 +695,16 @@ read_from_data_list_fixed (const struct data_list_pgm *dls, struct ccase *c)
       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));
+        {
+          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);
     }
@@ -701,6 +718,7 @@ read_from_data_list_fixed (const struct data_list_pgm *dls, struct ccase *c)
 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)
@@ -721,7 +739,7 @@ read_from_data_list_free (const struct data_list_pgm *dls, struct ccase *c)
            }
        }
 
-      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));
     }
@@ -734,6 +752,7 @@ read_from_data_list_free (const struct data_list_pgm *dls, struct ccase *c)
 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))
@@ -761,7 +780,7 @@ read_from_data_list_list (const struct data_list_pgm *dls, struct ccase *c)
          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));
     }
@@ -803,10 +822,10 @@ data_list_trns_proc (void *dls_, struct ccase *c, casenumber case_num UNUSED)
   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;