Change enum legacy_encoding to const char *.
[pspp-builds.git] / src / language / data-io / data-reader.c
index e3ef8ca15be6b841de239d6d578501b5c7d26b47..6f620a6a948341a0dfa5db150763d20f51e92284 100644 (file)
@@ -23,6 +23,7 @@
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <sys/stat.h>
 
 #include <data/casereader.h>
 #include <data/file-handle-def.h>
@@ -63,6 +64,7 @@ struct dfm_reader
     struct string scratch;      /* Extra line buffer. */
     enum dfm_reader_flags flags; /* Zero or more of DFM_*. */
     FILE *file;                 /* Associated file. */
+    off_t file_size;            /* File size, or -1 if unavailable. */
     size_t pos;                 /* Offset in line of current character. */
     unsigned eof_cnt;           /* # of attempts to advance past EOF. */
     struct lexer *lexer;        /* The lexer reading the file */
@@ -137,6 +139,7 @@ dfm_open_reader (struct file_handle *fh, struct lexer *lexer)
   r->block_left = 0;
   if (fh_get_referent (fh) != FH_REF_INLINE)
     {
+      struct stat s;
       r->where.file_name = fh_get_file_name (fh);
       r->where.line_number = 0;
       r->file = fn_open (fh_get_file_name (fh),
@@ -150,7 +153,10 @@ dfm_open_reader (struct file_handle *fh, struct lexer *lexer)
           free (r);
           return NULL;
         }
+      r->file_size = fstat (fileno (r->file), &s) == 0 ? s.st_size : -1;
     }
+  else
+    r->file_size = -1;
   fh_lock_set_aux (lock, r);
 
   return r;
@@ -181,6 +187,7 @@ read_inline_record (struct dfm_reader *r)
 
   if (!lex_get_line_raw (r->lexer))
     {
+      lex_discard_line (r->lexer);
       msg (SE, _("Unexpected end-of-file while reading data in BEGIN "
                  "DATA.  This probably indicates "
                  "a missing or misformatted END DATA command.  "
@@ -334,7 +341,7 @@ read_file_record (struct dfm_reader *r)
   switch (fh_get_mode (r->fh))
     {
     case FH_MODE_TEXT:
-      if (ds_read_line (&r->line, r->file))
+      if (ds_read_line (&r->line, r->file, SIZE_MAX))
         {
           ds_chomp (&r->line, '\n');
           return true;
@@ -590,12 +597,33 @@ dfm_expand_tabs (struct dfm_reader *r)
 }
 
 /* Returns the legacy character encoding of data read from READER. */
-enum legacy_encoding
+const char *
 dfm_reader_get_legacy_encoding (const struct dfm_reader *reader)
 {
   return fh_get_legacy_encoding (reader->fh);
 }
 
+/* Returns a number between 0 and 100 that approximates the
+   percentage of the data in READER that has already been read,
+   or -1 if this value cannot be estimated.
+
+   ftello is slow in glibc (it flushes the read buffer), so don't
+   call this function unless you need to. */
+int
+dfm_get_percent_read (const struct dfm_reader *reader)
+{
+  if (reader->file_size >= 0)
+    {
+      off_t position = ftello (reader->file);
+      if (position >= 0)
+        {
+          double p = 100.0 * position / reader->file_size;
+          return p < 0 ? 0 : p > 100 ? 100 : p;
+        }
+    }
+  return -1;
+}
+
 /* Causes dfm_get_record() or dfm_get_whole_record() to read in
    the next record the next time it is executed on file
    HANDLE. */