Get rid of our own int32 type in favor of the standard int32_t type.
[pspp-builds.git] / src / data / sys-file-reader.c
index c8c70342c957f421f6503bbce331665dad4aee1b..5f3e26bdb6134787023fd267fb20f17f71ec5ea9 100644 (file)
    02110-1301, USA. */
 
 #include <config.h>
-#include "sys-file-reader.h"
-#include "sfm-private.h"
-#include "message.h"
+
 #include <stdlib.h>
-#include <ctype.h>
 #include <errno.h>
 #include <float.h>
-#include <setjmp.h>
-#include "alloc.h"
+#include <c-ctype.h>
+
+#include <libpspp/alloc.h>
+#include <libpspp/message.h>
+#include <libpspp/compiler.h>
+#include <libpspp/magic.h>
+#include <libpspp/misc.h>
+#include <libpspp/str.h>
+
+#include "sys-file-reader.h"
+#include "sfm-private.h"
 #include "case.h"
 #include "dictionary.h"
-#include "message.h"
 #include "file-handle-def.h"
 #include "filename.h"
 #include "format.h"
-#include "magic.h"
-#include "misc.h"
 #include "value-labels.h"
-#include "str.h"
 #include "variable.h"
 
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
 
-#include "debug-print.h"
+#include <libpspp/debug-print.h>
 
 /* System file reader. */
 struct sfm_reader
@@ -97,7 +99,7 @@ bswap (char *a, char *b)
 
 /* Reverse the byte order of 32-bit integer *X. */
 static inline void
-bswap_int32 (int32 *x_)
+bswap_int32 (int32_t *x_)
 {
   char *x = (char *) x_;
   bswap (x + 0, x + 3);
@@ -168,7 +170,7 @@ static void *buf_read (struct sfm_reader *, void *buf, size_t byte_cnt,
 
 static int read_header (struct sfm_reader *,
                         struct dictionary *, struct sfm_read_info *);
-static int parse_format_spec (struct sfm_reader *, int32,
+static int parse_format_spec (struct sfm_reader *, int32_t,
                              struct fmt_spec *, const struct variable *);
 static int read_value_labels (struct sfm_reader *, struct dictionary *,
                               struct variable **var_by_idx);
@@ -277,7 +279,7 @@ sfm_open_reader (struct file_handle *fh, struct dictionary **dict,
   /* Read records of types 3, 4, 6, and 7. */
   for (;;)
     {
-      int32 rec_type;
+      int32_t rec_type;
 
       assertive_buf_read (r, &rec_type, sizeof rec_type, 0);
       if (r->reverse_endian)
@@ -305,9 +307,9 @@ sfm_open_reader (struct file_handle *fh, struct dictionary **dict,
          {
            struct
              {
-               int32 subtype P;
-               int32 size P;
-               int32 count P;
+               int32_t subtype P;
+               int32_t size P;
+               int32_t count P;
              }
            data;
             unsigned long bytes;
@@ -359,9 +361,9 @@ sfm_open_reader (struct file_handle *fh, struct dictionary **dict,
                    {
                      struct
                      {
-                       int32 measure P;
-                       int32 width P;
-                       int32 align P;
+                       int32_t measure P;
+                       int32_t width P;
+                       int32_t align P;
                      }
                      params;
 
@@ -474,7 +476,7 @@ sfm_open_reader (struct file_handle *fh, struct dictionary **dict,
 
        case 999:
          {
-           int32 filler;
+           int32_t filler;
 
            assertive_buf_read (r, &filler, sizeof filler, 0);
            goto success;
@@ -507,15 +509,15 @@ error:
 static int
 read_machine_int32_info (struct sfm_reader *r, int size, int count)
 {
-  int32 data[8];
+  int32_t data[8];
   int file_bigendian;
 
   int i;
 
-  if (size != sizeof (int32) || count != 8)
+  if (size != sizeof (int32_t) || count != 8)
     lose ((ME, _("%s: Bad size (%d) or count (%d) field on record type 7, "
                  "subtype 3.   Expected size %d, count 8."),
-          fh_get_filename (r->fh), size, count, sizeof (int32)));
+          fh_get_filename (r->fh), size, count, sizeof (int32_t)));
 
   assertive_buf_read (r, data, sizeof data, 0);
   if (r->reverse_endian)
@@ -618,10 +620,10 @@ read_header (struct sfm_reader *r,
   /* Check eye-category.her string. */
   memcpy (prod_name, hdr.prod_name, sizeof hdr.prod_name);
   for (i = 0; i < 60; i++)
-    if (!isprint ((unsigned char) prod_name[i]))
+    if (!c_isprint ((unsigned char) prod_name[i]))
       prod_name[i] = ' ';
   for (i = 59; i >= 0; i--)
-    if (!isgraph ((unsigned char) prod_name[i]))
+    if (!c_isgraph ((unsigned char) prod_name[i]))
       {
        prod_name[i] = '\0';
        break;
@@ -696,16 +698,18 @@ read_header (struct sfm_reader *r,
     int i;
 
     for (i = sizeof hdr.file_label - 1; i >= 0; i--)
-      if (!isspace ((unsigned char) hdr.file_label[i])
-         && hdr.file_label[i] != 0)
-       {
-          char *label = xmalloc (i + 2);
-         memcpy (label, hdr.file_label, i + 1);
-         label[i + 1] = 0;
-          dict_set_label (dict, label);
-          free (label);
-         break;
-       }
+      {
+       if (!c_isspace ((unsigned char) hdr.file_label[i])
+           && hdr.file_label[i] != 0)
+         {
+           char *label = xmalloc (i + 2);
+           memcpy (label, hdr.file_label, i + 1);
+           label[i + 1] = 0;
+           dict_set_label (dict, label);
+           free (label);
+           break;
+         }
+      }
   }
 
   if (info)
@@ -729,7 +733,7 @@ read_header (struct sfm_reader *r,
       info->case_cnt = hdr.case_cnt;
 
       for (cp = &prod_name[skip_amt]; cp < &prod_name[60]; cp++)
-       if (isgraph ((unsigned char) *cp))
+       if (c_isgraph ((unsigned char) *cp))
          break;
       strcpy (info->product, cp);
     }
@@ -837,52 +841,30 @@ read_variables (struct sfm_reader *r,
                     "-3, -2, 0, 1, 2, or 3."), fh_get_filename (r->fh), i));
 
       /* Copy first character of variable name. */
-      if (!isalpha ((unsigned char) sv.name[0])
-         && sv.name[0] != '@' && sv.name[0] != '#')
+      if (sv.name[0] == '@' || sv.name[0] == '#')
        lose ((ME, _("%s: position %d: Variable name begins with invalid "
                      "character."),
                fh_get_filename (r->fh), i));
-      if (islower ((unsigned char) sv.name[0]))
-       msg (MW, _("%s: position %d: Variable name begins with lowercase letter "
-                   "%c."),
-             fh_get_filename (r->fh), i, sv.name[0]);
-      if (sv.name[0] == '#')
-       msg (MW, _("%s: position %d: Variable name begins with octothorpe "
-                  "(`#').  Scratch variables should not appear in system "
-                  "files."),
-             fh_get_filename (r->fh), i);
-      name[0] = toupper ((unsigned char) (sv.name[0]));
+
+      name[0] = sv.name[0];
 
       /* Copy remaining characters of variable name. */
       for (j = 1; j < SHORT_NAME_LEN; j++)
        {
          int c = (unsigned char) sv.name[j];
 
-         if (isspace (c))
+         if (c == ' ') 
            break;
-         else if (islower (c))
-           {
-             msg (MW, _("%s: position %d: Variable name character %d is "
-                         "lowercase letter %c."),
-                   fh_get_filename (r->fh), i, j + 1, sv.name[j]);
-             name[j] = toupper ((unsigned char) (c));
-           }
-         else if (isalnum (c) || c == '.' || c == '@'
-                  || c == '#' || c == '$' || c == '_')
+         else 
            name[j] = c;
-         else
-           lose ((ME, _("%s: position %d: character `\\%03o' (%c) is not valid in a "
-                         "variable name."),
-                   fh_get_filename (r->fh), i, c, c));
        }
       name[j] = 0;
 
-      if ( ! var_is_valid_name(name, false) ) 
+      if ( ! var_is_plausible_name(name, false) ) 
         lose ((ME, _("%s: Invalid variable name `%s' within system file."),
                fh_get_filename (r->fh), name));
 
       /* Create variable. */
-
       vv = (*var_by_idx)[i] = dict_create_var (dict, name, sv.type);
       if (vv == NULL) 
         lose ((ME, _("%s: Duplicate variable name `%s' within system file."),
@@ -899,7 +881,7 @@ read_variables (struct sfm_reader *r,
       if (sv.has_var_label == 1)
        {
          /* Disk buffer. */
-         int32 len;
+         int32_t len;
 
          /* Read length of label. */
          assertive_buf_read (r, &len, sizeof len, 0);
@@ -915,7 +897,7 @@ read_variables (struct sfm_reader *r,
          if ( len != 0 ) 
            {
              /* Read label into variable structure. */
-             vv->label = buf_read (r, NULL, ROUND_UP (len, sizeof (int32)), len + 1);
+             vv->label = buf_read (r, NULL, ROUND_UP (len, sizeof (int32_t)), len + 1);
              if (vv->label == NULL)
                goto error;
              vv->label[len] = '\0';
@@ -996,7 +978,7 @@ error:
 /* Translates the format spec from sysfile format to internal
    format. */
 static int
-parse_format_spec (struct sfm_reader *r, int32 s,
+parse_format_spec (struct sfm_reader *r, int32_t s,
                    struct fmt_spec *f, const struct variable *v)
 {
   f->type = translate_fmt ((s >> 16) & 0xff);
@@ -1042,10 +1024,10 @@ read_value_labels (struct sfm_reader *r,
     };
 
   struct label *labels = NULL;
-  int32 n_labels;              /* Number of labels. */
+  int32_t n_labels;            /* Number of labels. */
 
   struct variable **var = NULL;        /* Associated variables. */
-  int32 n_vars;                        /* Number of associated variables. */
+  int32_t n_vars;                      /* Number of associated variables. */
 
   int i;
 
@@ -1058,7 +1040,7 @@ read_value_labels (struct sfm_reader *r,
   if (r->reverse_endian)
     bswap_int32 (&n_labels);
 
-  if ( n_labels >= ((int32) ~0) / sizeof *labels)
+  if ( n_labels >= ((int32_t) ~0) / sizeof *labels)
     {    
       corrupt_msg(MW, _("%s: Invalid number of labels: %d.  Ignoring labels."),
                  fh_get_filename (r->fh), n_labels);
@@ -1095,7 +1077,7 @@ read_value_labels (struct sfm_reader *r,
 
   /* Read record type of type 4 record. */
   {
-    int32 rec_type;
+    int32_t rec_type;
     
     assertive_buf_read (r, &rec_type, sizeof rec_type, 0);
     if (r->reverse_endian)
@@ -1121,7 +1103,7 @@ read_value_labels (struct sfm_reader *r,
   var = xnmalloc (n_vars, sizeof *var);
   for (i = 0; i < n_vars; i++)
     {
-      int32 var_idx;
+      int32_t var_idx;
       struct variable *v;
 
       /* Read variable index, check range. */
@@ -1269,7 +1251,7 @@ buf_unread(struct sfm_reader *r, size_t byte_cnt)
 static int
 read_documents (struct sfm_reader *r, struct dictionary *dict)
 {
-  int32 line_cnt;
+  int32_t line_cnt;
   char *documents;
 
   if (dict_get_documents (dict) != NULL)
@@ -1399,9 +1381,13 @@ read_compressed_data (struct sfm_reader *r, flt64 *buf)
       if (r->ptr == NULL || r->ptr >= r->end) 
         {
           if (!buffer_input (r))
-            if (buf_beg != buf)
-              lose ((ME, _("%s: Unexpected end of file."),
-                     fh_get_filename (r->fh))); 
+           {
+             if (buf_beg != buf)
+               lose ((ME, _("%s: Unexpected end of file."),
+                      fh_get_filename (r->fh))); 
+             else
+               return 0;
+           }
         }
       memcpy (r->x, r->ptr++, sizeof *buf);
       p = r->x;
@@ -1553,3 +1539,4 @@ sfm_detect (FILE *file)
     return false;
   return true; 
 }
+