Finish converting struct variable to an opaque type. In this
[pspp-builds.git] / src / data / sys-file-reader.c
index 24054393468f1e4d3a84a01209f0ec95dbefdd46..c1a8292eaae19fb01e2c09384a85fc83c536ed6f 100644 (file)
 
 #include <config.h>
 
+#include "sys-file-reader.h"
+#include "sfm-private.h"
+#include "sys-file-private.h"
+
 #include <stdlib.h>
 #include <errno.h>
 #include <float.h>
 #include <libpspp/hash.h>
 #include <libpspp/array.h>
 
-#include "sys-file-reader.h"
-#include "sfm-private.h"
 #include "case.h"
 #include "dictionary.h"
 #include "file-handle-def.h"
 #include "file-name.h"
 #include "format.h"
+#include "missing-values.h"
 #include "value-labels.h"
-#include "variable.h"
 #include "value.h"
+#include "variable.h"
 
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
@@ -346,7 +349,7 @@ sfm_open_reader (struct file_handle *fh, struct dictionary **dict,
        lose ((ME,
                _("%s: Weighting variable may not be a continuation of "
                 "a long string variable."), fh_get_file_name (fh)));
-      else if (weight_var->type == ALPHA)
+      else if (var_is_alpha (weight_var))
        lose ((ME, _("%s: Weighting variable may not be a string variable."),
               fh_get_file_name (fh)));
 
@@ -439,7 +442,7 @@ sfm_open_reader (struct file_handle *fh, struct dictionary **dict,
                      break;
                    }
 
-                 for ( i = 0 ; i < min(n_vars, dict_get_var_cnt(*dict)) ; ++i ) 
+                 for ( i = 0 ; i < MIN(n_vars, dict_get_var_cnt(*dict)) ; ++i ) 
                    {
                      struct
                      {
@@ -465,9 +468,9 @@ sfm_open_reader (struct file_handle *fh, struct dictionary **dict,
 
                      v = dict_get_var(*dict, i);
 
-                     v->measure = params.measure;
-                     v->display_width = params.width;
-                     v->alignment = params.align;
+                     var_set_measure (v, params.measure);
+                     var_set_display_width (v, params.width);
+                     var_set_alignment (v, params.align);
                    }
                }
                break;
@@ -528,7 +531,7 @@ sfm_open_reader (struct file_handle *fh, struct dictionary **dict,
                         }
 
                       /* Identify any duplicates. */
-                     if ( compare_var_names(short_name, long_name, 0) &&
+                     if ( strcasecmp (short_name, long_name) &&
                           NULL != dict_lookup_var (*dict, long_name))
                         lose ((ME, _("%s: Duplicate long variable name `%s' "
                                      "within system file."),
@@ -630,31 +633,28 @@ sfm_open_reader (struct file_handle *fh, struct dictionary **dict,
                                }
 
                              l = length;
-                             if ( v->width > EFFECTIVE_LONG_STRING_LENGTH ) 
+                             if ( var_get_width (v) > EFFECTIVE_LONG_STRING_LENGTH ) 
                                l -= EFFECTIVE_LONG_STRING_LENGTH;
                              else
-                               l -= v->width;
+                               l -= var_get_width (v);
 
-                             idx = v->index;
+                             idx = var_get_dict_index (v);
                              while ( l > 0 ) 
                                {
                                  struct variable *v_next;
                                  v_next = dict_get_var(*dict, idx + 1);
 
-                                 if ( v_next->width > EFFECTIVE_LONG_STRING_LENGTH ) 
+                                 if ( var_get_width (v_next) > EFFECTIVE_LONG_STRING_LENGTH ) 
                                    l -= EFFECTIVE_LONG_STRING_LENGTH;
                                  else
-                                   l -= v_next->width;
+                                   l -= var_get_width (v_next);
 
                                  dict_delete_var(*dict, v_next);
                                }
 
-                             assert ( length > MAX_LONG_STRING );
+                             assert ( length >= MIN_VERY_LONG_STRING );
 
-                             v->width = length;
-                             v->print.w = v->width;
-                             v->write.w = v->width;
-                             v->nv = DIV_RND_UP (length, MAX_SHORT_STRING);
+                              var_set_width (v, length);
                            }
                          eq_seen = false;
                          memset(name, 0, SHORT_NAME_LEN+1); 
@@ -725,8 +725,8 @@ sfm_open_reader (struct file_handle *fh, struct dictionary **dict,
       {
         struct variable *v = dict_get_var (*dict, i);
         struct sfm_var *sv = &r->vars[i];
-        sv->width = v->width;
-        sv->fv = v->fv
+        sv->width = var_get_width (v);
+        sv->fv = var_get_case_index (v)
       }
   }
 
@@ -1015,6 +1015,8 @@ read_variables (struct sfm_reader *r,
       char name[SHORT_NAME_LEN + 1];
       int nv;
       int j;
+      struct fmt_spec print, write;
+
 
       assertive_buf_read (r, &sv, sizeof sv, 0);
 
@@ -1101,7 +1103,7 @@ read_variables (struct sfm_reader *r,
                fh_get_file_name (r->fh), name));
 
       /* Set the short name the same as the long name */
-      var_set_short_name (vv, vv->name);
+      var_set_short_name (vv, var_get_name (vv));
 
       /* Case reading data. */
       nv = sv.type == 0 ? 1 : DIV_RND_UP (sv.type, sizeof (flt64));
@@ -1123,15 +1125,16 @@ read_variables (struct sfm_reader *r,
          if (len < 0 || len > 255)
            lose ((ME, _("%s: Variable %s indicates variable label of invalid "
                          "length %d."),
-                   fh_get_file_name (r->fh), vv->name, len));
+                   fh_get_file_name (r->fh), var_get_name (vv), len));
 
          if ( len != 0 ) 
            {
              /* Read label into variable structure. */
-             vv->label = buf_read (r, NULL, ROUND_UP (len, sizeof (int32_t)), len + 1);
-             if (vv->label == NULL)
-               goto error;
-             vv->label[len] = '\0';
+             char label[256];
+              assertive_buf_read (r, label, ROUND_UP (len, sizeof (int32_t)),
+                                  0);
+             label[len] = '\0';
+              var_set_label (vv, label);
            }
        }
 
@@ -1140,48 +1143,54 @@ read_variables (struct sfm_reader *r,
        {
          flt64 mv[3];
           int mv_cnt = abs (sv.n_missing_values);
+          struct missing_values miss;
 
-         if (vv->width > MAX_SHORT_STRING)
+         if (var_get_width (vv) > MAX_SHORT_STRING)
            lose ((ME, _("%s: Long string variable %s may not have missing "
                          "values."),
-                   fh_get_file_name (r->fh), vv->name));
+                   fh_get_file_name (r->fh), var_get_name (vv)));
+          mv_init (&miss, var_get_width (vv));
 
          assertive_buf_read (r, mv, sizeof *mv * mv_cnt, 0);
 
-         if (r->reverse_endian && vv->type == NUMERIC)
+         if (r->reverse_endian && var_is_numeric (vv))
            for (j = 0; j < mv_cnt; j++)
              bswap_flt64 (&mv[j]);
 
          if (sv.n_missing_values > 0)
            {
               for (j = 0; j < sv.n_missing_values; j++)
-                if (vv->type == NUMERIC)
-                  mv_add_num (&vv->miss, mv[j]);
+                if (var_is_numeric (vv))
+                  mv_add_num (&miss, mv[j]);
                 else
-                  mv_add_str (&vv->miss, (char *) &mv[j]);
+                  mv_add_str (&miss, (char *) &mv[j]);
            }
          else
            {
-             if (vv->type == ALPHA)
+             if (var_is_alpha (vv))
                lose ((ME, _("%s: String variable %s may not have missing "
                              "values specified as a range."),
-                       fh_get_file_name (r->fh), vv->name));
+                       fh_get_file_name (r->fh), var_get_name (vv)));
 
              if (mv[0] == r->lowest)
-                mv_add_num_range (&vv->miss, LOWEST, mv[1]);
+                mv_add_num_range (&miss, LOWEST, mv[1]);
              else if (mv[1] == r->highest)
-                mv_add_num_range (&vv->miss, mv[0], HIGHEST);
+                mv_add_num_range (&miss, mv[0], HIGHEST);
              else
-                mv_add_num_range (&vv->miss, mv[0], mv[1]);
+                mv_add_num_range (&miss, mv[0], mv[1]);
 
              if (sv.n_missing_values == -3)
-                mv_add_num (&vv->miss, mv[2]);
+                mv_add_num (&miss, mv[2]);
            }
+          var_set_missing_values (vv, &miss);
        }
 
-      if (!parse_format_spec (r, sv.print, &vv->print, vv)
-         || !parse_format_spec (r, sv.write, &vv->write, vv))
+      if (!parse_format_spec (r, sv.print, &print, vv)
+         || !parse_format_spec (r, sv.write, &write, vv))
        goto error;
+
+      var_set_print_format (vv, &print);
+      var_set_write_format (vv, &write);
     }
 
   /* Some consistency checks. */
@@ -1216,27 +1225,27 @@ parse_format_spec (struct sfm_reader *r, int32_t s,
   f->w = (s >> 8) & 0xff;
   f->d = s & 0xff;
 
-  if ((v->type == ALPHA) ^ (fmt_is_string (f->type) != 0))
+  if (var_is_alpha (v) != fmt_is_string (f->type))
     lose ((ME, _("%s: %s variable %s has %s format specifier %s."),
           fh_get_file_name (r->fh),
-           v->type == ALPHA ? _("String") : _("Numeric"),
-          v->name,
+           var_is_alpha (v) ? _("String") : _("Numeric"),
+          var_get_name (v),
           fmt_is_string (f->type) ? _("string") : _("numeric"),
           fmt_name (f->type)));
 
   msg_disable ();
-  ok = fmt_check_output (f) && fmt_check_width_compat (f, v->width);
+  ok = fmt_check_output (f) && fmt_check_width_compat (f, var_get_width (v)); 
   msg_enable ();
   
   if (!ok) 
     {
       char fmt_string[FMT_STRING_LEN_MAX + 1];
       msg (ME, _("%s variable %s has invalid format specifier %s."),
-           v->type == NUMERIC ? _("Numeric") : _("String"),
-           v->name, fmt_to_string (f, fmt_string));
-      *f = (v->type == NUMERIC
+           var_is_numeric (v) ? _("Numeric") : _("String"),
+           var_get_name (v), fmt_to_string (f, fmt_string));
+      *f = (var_is_numeric (v)
             ? fmt_for_output (FMT_F, 8, 2) 
-            : fmt_for_output (FMT_A, v->width, 0));
+            : fmt_for_output (FMT_A, var_get_width (v), 0));
     }
   return 1;
 
@@ -1356,10 +1365,10 @@ read_value_labels (struct sfm_reader *r,
                      "refers to a continuation of a string variable, not to "
                      "an actual variable."),
                fh_get_file_name (r->fh), var_idx));
-      if (v->type == ALPHA && v->width > MAX_SHORT_STRING)
+      if (var_is_long_string (v))
        lose ((ME, _("%s: Value labels are not allowed on long string "
                      "variables (%s)."),
-               fh_get_file_name (r->fh), v->name));
+               fh_get_file_name (r->fh), var_get_name (v)));
 
       /* Add it to the list of variables. */
       var[i] = v;
@@ -1367,22 +1376,24 @@ read_value_labels (struct sfm_reader *r,
 
   /* Type check the variables. */
   for (i = 1; i < n_vars; i++)
-    if (var[i]->type != var[0]->type)
+    if (var_get_type (var[i]) != var_get_type (var[0]))
       lose ((ME, _("%s: Variables associated with value label are not all of "
                    "identical type.  Variable %s has %s type, but variable "
                    "%s has %s type."),
              fh_get_file_name (r->fh),
-            var[0]->name, var[0]->type == ALPHA ? _("string") : _("numeric"),
-            var[i]->name, var[i]->type == ALPHA ? _("string") : _("numeric")));
+            var_get_name (var[0]),
+             var_is_alpha (var[0]) ? _("string") : _("numeric"),
+            var_get_name (var[i]),
+             var_is_alpha (var[i]) ? _("string") : _("numeric")));
 
   /* Fill in labels[].value, now that we know the desired type. */
   for (i = 0; i < n_labels; i++) 
     {
       struct label *label = labels + i;
       
-      if (var[0]->type == ALPHA)
+      if (var_is_alpha (var[0]))
         {
-          const int copy_len = min (sizeof label->raw_value,
+          const int copy_len = MIN (sizeof label->raw_value,
                                     sizeof label->label);
           memcpy (label->value.s, label->raw_value, copy_len);
         } else {
@@ -1405,17 +1416,18 @@ read_value_labels (struct sfm_reader *r,
       for (j = 0; j < n_labels; j++)
        {
           struct label *label = labels + j;
-         if (!val_labs_replace (v->val_labs, label->value, label->label))
+         if (var_add_value_label (v, &label->value, label->label))
            continue;
 
-         if (var[0]->type == NUMERIC)
+         if (var_is_numeric (var[0]))
            msg (MW, _("%s: File contains duplicate label for value %g for "
                        "variable %s."),
-                 fh_get_file_name (r->fh), label->value.f, v->name);
+                 fh_get_file_name (r->fh), label->value.f, var_get_name (v));
          else
            msg (MW, _("%s: File contains duplicate label for value `%.*s' "
                        "for variable %s."),
-                 fh_get_file_name (r->fh), v->width, label->value.s, v->name);
+                 fh_get_file_name (r->fh), var_get_width (v),
+                 label->value.s, var_get_name (v));
        }
     }
 
@@ -1446,7 +1458,7 @@ buf_read (struct sfm_reader *r, void *buf, size_t byte_cnt, size_t min_alloc)
   assert (r);
 
   if (buf == NULL && byte_cnt > 0 )
-    buf = xmalloc (max (byte_cnt, min_alloc));
+    buf = xmalloc (MAX (byte_cnt, min_alloc));
 
   if ( byte_cnt == 0 )
     return buf;
@@ -1667,7 +1679,7 @@ sfm_read_case (struct sfm_reader *r, struct ccase *c)
           
           for (i = 0; i < r->var_cnt; i++) 
             if (r->vars[i].width == 0)
-              bswap_flt64 (&case_data_rw (c, r->vars[i].fv)->f);
+              bswap_flt64 (&case_data_rw_idx (c, r->vars[i].fv)->f);
         }
 
       /* Fix up SYSMIS values if needed.
@@ -1678,8 +1690,8 @@ sfm_read_case (struct sfm_reader *r, struct ccase *c)
           int i;
           
           for (i = 0; i < r->var_cnt; i++) 
-            if (r->vars[i].width == 0 && case_num (c, i) == r->sysmis)
-              case_data_rw (c, r->vars[i].fv)->f = SYSMIS;
+            if (r->vars[i].width == 0 && case_num_idx (c, i) == r->sysmis)
+              case_data_rw_idx (c, r->vars[i].fv)->f = SYSMIS;
         }
     }
   else 
@@ -1716,7 +1728,7 @@ sfm_read_case (struct sfm_reader *r, struct ccase *c)
               flt64 f = *bounce_cur++;
               if (r->reverse_endian)
                 bswap_flt64 (&f);
-              case_data_rw (c, sv->fv)->f = f == r->sysmis ? SYSMIS : f;
+              case_data_rw_idx (c, sv->fv)->f = f == r->sysmis ? SYSMIS : f;
             }
           else
             {
@@ -1724,14 +1736,16 @@ sfm_read_case (struct sfm_reader *r, struct ccase *c)
              int ofs = 0;
               while (ofs < sv->width )
                 {
-                  const int chunk = MIN (MAX_LONG_STRING, sv->width - ofs);
-                  memcpy (case_data_rw (c, sv->fv)->s + ofs, bounce_cur, chunk);
+                  const int chunk = MIN (MIN_VERY_LONG_STRING - 1,
+                                         sv->width - ofs);
+                  memcpy (case_data_rw_idx (c, sv->fv)->s + ofs,
+                          bounce_cur, chunk);
 
                   bounce_cur += DIV_RND_UP (chunk, sizeof (flt64));
 
                   ofs += chunk;
                 }
-             bounce_cur = bc_start + width_to_bytes(sv->width) / sizeof(flt64);
+             bounce_cur = bc_start + sfm_width_to_bytes (sv->width) / sizeof(flt64);
             }
         }