Continue reforming procedure execution. In this phase, add `const' to
[pspp-builds.git] / src / language / data-io / data-reader.c
index 24b27b4048732b870d3ae15045537a5359bfcecf..a8b484d469010808af15ac90e9098225086c9123 100644 (file)
    02110-1301, USA. */
 
 #include <config.h>
-#include "data-reader.h"
+
+#include <language/data-io/data-reader.h>
+
 #include <ctype.h>
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include "alloc.h"
-#include "command.h"
-#include "message.h"
-#include "file-handle.h"
-#include "file-handle-def.h"
-#include "filename.h"
-#include "line-buffer.h"
-#include "lexer.h"
-#include "str.h"
-#include "procedure.h"
+
+#include <data/file-handle-def.h>
+#include <data/file-name.h>
+#include <data/procedure.h>
+#include <language/command.h>
+#include <language/data-io/file-handle.h>
+#include <language/lexer/lexer.h>
+#include <language/line-buffer.h>
+#include <libpspp/alloc.h>
+#include <libpspp/message.h>
+#include <libpspp/str.h>
 
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
 
-#include "debug-print.h"
-
 /* Flags for DFM readers. */
 enum dfm_reader_flags
   {
@@ -52,11 +53,11 @@ enum dfm_reader_flags
 struct dfm_reader
   {
     struct file_handle *fh;     /* File handle. */
-    struct file_locator where;  /* Current location in data file. */
+    struct msg_locator where;   /* Current location in data file. */
     struct string line;         /* Current line. */
     struct string scratch;      /* Extra line buffer. */
     enum dfm_reader_flags flags; /* Zero or more of DFM_*. */
-    struct file_ext file;      /* Associated file. */
+    FILE *file;                 /* Associated file. */
     size_t pos;                 /* Offset in line of current character. */
     unsigned eof_cnt;           /* # of attempts to advance past EOF. */
   };
@@ -67,21 +68,22 @@ dfm_close_reader (struct dfm_reader *r)
 {
   int still_open;
   bool is_inline;
+  char *file_name;
 
   if (r == NULL)
     return;
 
   is_inline = r->fh == fh_inline_file ();
+  file_name = is_inline ? NULL : xstrdup (fh_get_file_name (r->fh));
   still_open = fh_close (r->fh, "data file", "rs");
-  if (still_open)
-    return;
-
-  if (!is_inline)
+  if (still_open) 
     {
-      fn_close_ext (&r->file);
-      free (r->file.filename);
-      r->file.filename = NULL;
+      free (file_name);
+      return; 
     }
+
+  if (!is_inline)
+    fn_close (file_name, r->file);
   else
     {
       /* Skip any remaining data on the inline file. */
@@ -96,6 +98,7 @@ dfm_close_reader (struct dfm_reader *r)
   ds_destroy (&r->line);
   ds_destroy (&r->scratch);
   free (r);
+  free (file_name);
 }
 
 /* Opens the file designated by file handle FH for reading as a
@@ -123,20 +126,13 @@ dfm_open_reader (struct file_handle *fh)
   r->eof_cnt = 0;
   if (fh != fh_inline_file ()) 
     {
-      r->where.filename = fh_get_filename (fh);
+      r->where.file_name = fh_get_file_name (fh);
       r->where.line_number = 0; 
-      r->file.file = NULL;
-      r->file.filename = xstrdup (fh_get_filename (r->fh));
-      r->file.mode = "rb";
-      r->file.file = NULL;
-      r->file.sequence_no = NULL;
-      r->file.param = NULL;
-      r->file.postopen = NULL;
-      r->file.preclose = NULL;
-      if (!fn_open_ext (&r->file))
+      r->file = fn_open (fh_get_file_name (fh), "rb");
+      if (r->file == NULL)
         {
           msg (ME, _("Could not open \"%s\" for reading as a data file: %s."),
-               fh_get_filename (r->fh), strerror (errno));
+               fh_get_file_name (r->fh), strerror (errno));
           fh_close (fh,"data file", "rs");
           free (r);
           return NULL;
@@ -151,7 +147,7 @@ dfm_open_reader (struct file_handle *fh)
 bool
 dfm_reader_error (const struct dfm_reader *r) 
 {
-  return fh_get_referent (r->fh) == FH_REF_FILE && ferror (r->file.file);
+  return fh_get_referent (r->fh) == FH_REF_FILE && ferror (r->file);
 }
 
 /* Reads a record from the inline file into R.
@@ -187,7 +183,7 @@ read_inline_record (struct dfm_reader *r)
       return false;
     }
 
-  ds_replace (&r->line, ds_c_str (&getl_buf));
+  ds_assign_string (&r->line, &getl_buf);
   return true;
 }
 
@@ -200,9 +196,9 @@ read_file_record (struct dfm_reader *r)
   if (fh_get_mode (r->fh) == FH_MODE_TEXT)
     {
       ds_clear (&r->line);
-      if (!ds_gets (&r->line, r->file.file)) 
+      if (!ds_gets (&r->line, r->file)) 
         {
-          if (ferror (r->file.file))
+          if (ferror (r->file))
             msg (ME, _("Error reading file %s: %s."),
                  fh_get_name (r->fh), strerror (errno));
           return false;
@@ -216,11 +212,10 @@ read_file_record (struct dfm_reader *r)
       if (ds_length (&r->line) < record_width) 
         ds_rpad (&r->line, record_width, 0);
           
-      amt = fread (ds_c_str (&r->line), 1, record_width,
-                   r->file.file);
+      amt = fread (ds_c_str (&r->line), 1, record_width, r->file);
       if (record_width != amt)
         {
-          if (ferror (r->file.file))
+          if (ferror (r->file))
             msg (ME, _("Error reading file %s: %s."),
                  fh_get_name (r->fh), strerror (errno));
           else if (amt != 0)
@@ -395,20 +390,20 @@ dfm_column_start (struct dfm_reader *r)
   return r->pos + 1;
 }
 
-/* Pushes the filename and line number on the fn/ln stack. */
+/* Pushes the file name and line number on the fn/ln stack. */
 void
 dfm_push (struct dfm_reader *r)
 {
   if (r->fh != fh_inline_file ())
-    err_push_file_locator (&r->where);
+    msg_push_msg_locator (&r->where);
 }
 
-/* Pops the filename and line number from the fn/ln stack. */
+/* Pops the file name and line number from the fn/ln stack. */
 void
 dfm_pop (struct dfm_reader *r)
 {
   if (r->fh != fh_inline_file ())
-    err_pop_file_locator (&r->where);
+    msg_pop_msg_locator (&r->where);
 }
 \f
 /* BEGIN DATA...END DATA procedure. */