Added coefficient-handling routines
[pspp-builds.git] / src / pfm-read.c
index 4d95eba22ae3a05b7ff716c3d7dfb96c9fcb2025..a1ee05509566225fea31693fb8127896bcc72b7d 100644 (file)
 #include <math.h>
 #include <setjmp.h>
 #include "alloc.h"
-#include "bool.h"
+#include <stdbool.h>
 #include "case.h"
 #include "dictionary.h"
 #include "file-handle.h"
 #include "format.h"
-#include "getline.h"
+#include "getl.h"
 #include "hash.h"
 #include "magic.h"
 #include "misc.h"
@@ -44,6 +44,9 @@
 #include "value-labels.h"
 #include "var.h"
 
+#include "gettext.h"
+#define _(msgid) gettext (msgid)
+
 #include "debug-print.h"
 
 /* Portable file reader. */
@@ -56,8 +59,7 @@ struct pfm_reader
     struct file_handle *fh;     /* File handle. */
     FILE *file;                        /* File stream. */
     char cc;                   /* Current character. */
-    unsigned char *trans;      /* 256-byte character set translation table. */
-
+    char *trans;                /* 256-byte character set translation table. */
     int var_cnt;                /* Number of variables. */
     int weight_index;          /* 0-based index of weight variable, or -1. */
     int *widths;                /* Variable widths, 0 for numeric. */
@@ -339,7 +341,7 @@ read_string (struct pfm_reader *r, char *buf)
 
 /* Reads a string and returns a copy of it allocated from R's
    pool. */
-static unsigned char *
+static char *
 read_pool_string (struct pfm_reader *r) 
 {
   char string[256];
@@ -353,7 +355,7 @@ read_header (struct pfm_reader *r)
 {
   /* portable_to_local[PORTABLE] translates the given portable
      character into the local character set. */
-  static const unsigned char portable_to_local[256] =
+  static const char portable_to_local[256] =
     {
       "                                                                "
       "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ."
@@ -361,7 +363,7 @@ read_header (struct pfm_reader *r)
       "                                                                "
     };
 
-  unsigned char *trans;
+  char *trans;
   int i;
 
   /* Read and ignore vanity splash strings. */
@@ -408,6 +410,7 @@ read_header (struct pfm_reader *r)
 static void
 read_version_data (struct pfm_reader *r, struct pfm_read_info *info)
 {
+  static char empty_string[] = "";
   char *date, *time, *product, *author, *subproduct;
   int i;
 
@@ -416,10 +419,9 @@ read_version_data (struct pfm_reader *r, struct pfm_read_info *info)
     error (r, "Unrecognized version code `%c'.", r->cc);
   date = read_pool_string (r);
   time = read_pool_string (r);
-  product = match (r, '1') ? read_pool_string (r) : (unsigned char *) "";
-  author = match (r, '2') ? read_pool_string (r) : (unsigned char *) "";
-  subproduct
-    = match (r, '3') ? read_pool_string (r) : (unsigned char *) "";
+  product = match (r, '1') ? read_pool_string (r) : empty_string;
+  author = match (r, '2') ? read_pool_string (r) : empty_string;
+  subproduct = match (r, '3') ? read_pool_string (r) : empty_string;
 
   /* Validate file. */
   if (strlen (date) != 8)
@@ -490,7 +492,7 @@ read_variables (struct pfm_reader *r, struct dictionary *dict)
   r->var_cnt = read_int (r);
   if (r->var_cnt <= 0 || r->var_cnt == NOT_INT)
     error (r, _("Invalid number of variables %d."), r->var_cnt);
-  r->widths = pool_alloc (r->pool, sizeof *r->widths * r->var_cnt);
+  r->widths = pool_nalloc (r->pool, r->var_cnt, sizeof *r->widths);
 
   /* Purpose of this value is unknown.  It is typically 161. */
   read_int (r);
@@ -537,45 +539,23 @@ read_variables (struct pfm_reader *r, struct dictionary *dict)
       convert_format (r, &fmt[3], &v->write, v);
 
       /* Range missing values. */
-      if (match (r, 'B'))
-       {
-         v->miss_type = MISSING_RANGE;
-          v->missing[0] = parse_value (r, v);
-          v->missing[1] = parse_value (r, v);
-       }
+      if (match (r, 'B')) 
+        {
+          double x = read_float (r);
+          double y = read_float (r);
+          mv_add_num_range (&v->miss, x, y);
+        }
       else if (match (r, 'A'))
-       {
-         v->miss_type = MISSING_HIGH;
-          v->missing[0] = parse_value (r, v);
-       }
+        mv_add_num_range (&v->miss, read_float (r), HIGHEST);
       else if (match (r, '9'))
-       {
-         v->miss_type = MISSING_LOW;
-          v->missing[0] = parse_value (r, v);
-       }
+        mv_add_num_range (&v->miss, LOWEST, read_float (r));
 
       /* Single missing values. */
-      while (match (r, '8'))
-       {
-         static const int map_next[MISSING_COUNT] =
-           {
-             MISSING_1, MISSING_2, MISSING_3, -1,
-             MISSING_RANGE_1, MISSING_LOW_1, MISSING_HIGH_1,
-             -1, -1, -1,
-           };
-
-         static const int map_ofs[MISSING_COUNT] = 
-           {
-             -1, 0, 1, 2, -1, -1, -1, 2, 1, 1,
-           };
-
-         v->miss_type = map_next[v->miss_type];
-         if (v->miss_type == -1)
-           error (r, _("Bad missing values for %s."), v->name);
-         
-         assert (map_ofs[v->miss_type] != -1);
-          v->missing[map_ofs[v->miss_type]] = parse_value (r, v);
-       }
+      while (match (r, '8')) 
+        {
+          union value value = parse_value (r, v);
+          mv_add_value (&v->miss, &value); 
+        }
 
       if (match (r, 'C')) 
         {
@@ -628,7 +608,7 @@ read_value_label (struct pfm_reader *r, struct dictionary *dict)
   int i;
 
   nv = read_int (r);
-  v = pool_alloc (r->pool, sizeof *v * nv);
+  v = pool_nalloc (r->pool, nv, sizeof *v);
   for (i = 0; i < nv; i++)
     {
       char name[256];