any-reader: Add some comments on interface and implementation.
authorBen Pfaff <blp@cs.stanford.edu>
Sat, 12 Sep 2015 18:43:14 +0000 (11:43 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Sat, 12 Sep 2015 18:43:14 +0000 (11:43 -0700)
Also, adjust pfm_detect() and sfm_detect() code to be consistent with
pcp_detect().  This should not cause a change in behavior.

src/data/any-reader.h
src/data/por-file-reader.c
src/data/sys-file-reader.c

index 5614a6007507c3f71539a183197dc01661b46829..a12c256ae91023dd9bc90a76513f1a12e42c041b 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2006, 2010, 2012, 2014 Free Software Foundation, Inc.
+   Copyright (C) 2006, 2010, 2012, 2014, 2015 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
@@ -34,9 +34,12 @@ struct any_reader
 
 struct any_reader_class
   {
+    /* The name of this kind of data file, e.g. "SPSS System File". */
     const char *name;
 
-    int (*detect) (FILE *);
+    /* Detects whether FILE contains this type of file.  Returns 1 if so, 0 if
+       not, and a negative errno value if there is an error reading FILE. */
+    int (*detect) (FILE *file);
 
     struct any_reader *(*open) (struct file_handle *);
     bool (*close) (struct any_reader *);
index 4fb6c5fb452de7fbf64a00cf3001124594d85d0e..bba62d168f8f72fa2c8842c16bee0ad5ff85b455 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
+   Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011, 2012, 2013, 2014, 2015 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
@@ -922,8 +922,8 @@ por_file_casereader_read (struct casereader *reader, void *r_)
   return c;
 }
 
-/* Returns true if FILE is an SPSS portable file,
-   false otherwise. */
+/* Detects whether FILE is an SPSS portable file.  Returns 1 if so, 0 if not,
+   and a negative errno value if there is an error reading FILE. */
 int
 pfm_detect (FILE *file)
 {
@@ -938,7 +938,7 @@ pfm_detect (FILE *file)
     {
       int c = getc (file);
       if (c == EOF || raw_cnt++ > 512)
-        return 0;
+        return ferror (file) ? -errno : 0;
       else if (c == '\n')
         {
           while (line_len < 80 && cooked_cnt < sizeof header)
index 8718a5f940ba9d96acca418c4b2fbbeb787a9048..b855b45566a179951cfde9315245bf90ee44dd4a 100644 (file)
@@ -921,9 +921,8 @@ sys_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
   sfm_close (&r->any_reader);
 }
 
-/* Returns 1 if FILE is an SPSS system file,
-   0 if it is not,
-   otherwise a negative errno value. */
+/* Detects whether FILE is an SPSS system file.  Returns 1 if so, 0 if not, and
+   a negative errno value if there is an error reading FILE. */
 static int
 sfm_detect (FILE *file)
 {
@@ -932,7 +931,7 @@ sfm_detect (FILE *file)
   if (fseek (file, 0, SEEK_SET) != 0)
     return -errno;
   if (fread (magic, 4, 1, file) != 1)
-    return feof (file) ? 0 : -errno;
+    return ferror (file) ? -errno : 0;
   magic[4] = '\0';
 
   return (!strcmp (ASCII_MAGIC, magic)