Fix memory leaks.
[pspp-builds.git] / src / matrix-data.c
index 5835e8229286d7072039ec9935cc2a8b0bcbcf32..e90aef0955ed5872509a27a4a7726912320346df 100644 (file)
    02111-1307, USA. */
 
 #include <config.h>
-#include <assert.h>
+#include "error.h"
 #include <stdlib.h>
 #include <ctype.h>
 #include <float.h>
 #include "algorithm.h"
 #include "alloc.h"
+#include "case.h"
 #include "command.h"
 #include "data-in.h"
 #include "dfm.h"
@@ -141,10 +142,6 @@ static void read_matrices_without_rowtype (struct matrix_data_pgm *);
 static void read_matrices_with_rowtype (struct matrix_data_pgm *);
 static int string_to_content_type (char *, int *);
 
-#if DEBUGGING
-static void debug_print (void);
-#endif
-
 int
 cmd_matrix_data (void)
 {
@@ -593,10 +590,6 @@ cmd_matrix_data (void)
       goto lossage;
     }
 
-#if DEBUGGING
-  debug_print ();
-#endif
-
   if (!dfm_open_for_reading (mx->data_file))
     goto lossage;
 
@@ -676,106 +669,6 @@ compare_variables_by_mxd_vartype (const void *a_, const void *b_)
   else
     return a->subtype < b->subtype ? -1 : a->subtype > b->subtype;
 }
-
-#if DEBUGGING
-/* Print out the command as input. */
-static void
-debug_print (void)
-{
-  printf ("MATRIX DATA\n\t/VARIABLES=");
-  
-  {
-    int i;
-    
-    for (i = 0; i < default_dict.nvar; i++)
-      printf ("%s ", default_dict.var[i]->name);
-  }
-  printf ("\n");
-
-  printf ("\t/FORMAT=");
-  if (fmt == LIST)
-    printf ("LIST");
-  else if (fmt == FREE)
-    printf ("FREE");
-  else
-    assert (0);
-  if (section == LOWER)
-    printf (" LOWER");
-  else if (section == UPPER)
-    printf (" UPPER");
-  else if (section == FULL)
-    printf (" FULL");
-  else
-    assert (0);
-  if (diag == DIAGONAL)
-    printf (" DIAGONAL\n");
-  else if (diag == NODIAGONAL)
-    printf (" NODIAGONAL\n");
-  else
-    assert (0);
-
-  if (dict_get_split_cnt (default_dict) != 0)
-    {
-      int i;
-
-      printf ("\t/SPLIT=");
-      for (i = 0; i < dict_get_split_cnt (default_dict); i++)
-       printf ("%s ", dict_get_split_vars (default_dict)[i]->name);
-      if (single_split)
-       printf ("\t/* single split");
-      printf ("\n");
-    }
-  
-  if (n_factors)
-    {
-      int i;
-
-      printf ("\t/FACTORS=");
-      for (i = 0; i < n_factors; i++)
-       printf ("%s ", factors[i]->name);
-      printf ("\n");
-    }
-
-  if (cells != -1)
-    printf ("\t/CELLS=%d\n", cells);
-
-  if (mx->pop_n != -1)
-    printf ("\t/N=%d\n", mx->pop_n);
-
-  if (mx->n_contents)
-    {
-      int i;
-      int space = 0;
-      
-      printf ("\t/CONTENTS=");
-      for (i = 0; i < mx->n_contents; i++)
-       {
-         if (mx->contents[i] == LPAREN)
-           {
-             if (space)
-               printf (" ");
-             printf ("(");
-             space = 0;
-           }
-         else if (mx->contents[i] == RPAREN)
-           {
-             printf (")");
-             space = 1;
-           }
-         else 
-           {
-
-             assert (mx->contents[i] >= 0 && mx->contents[i] <= PROX);
-             if (space)
-               printf (" ");
-             printf ("%s", content_names[mx->contents[i]]);
-             space = 1;
-           }
-       }
-      printf ("\n");
-    }
-}
-#endif /* DEBUGGING */
 \f
 /* Matrix tokenizer. */
 
@@ -831,22 +724,34 @@ static const char *
 context (struct file_handle *data_file)
 {
   static char buf[32];
-  int len;
-  char *p = dfm_get_record (data_file, &len);
-  
-  if (!p || !len)
-    strcpy (buf, "at end of line");
-  else
+
+  if (dfm_eof (data_file))
+    strcpy (buf, "at end of file");
+  else 
     {
-      char *cp = buf;
-      int n_copy = min (10, len);
-      cp = stpcpy (buf, "before `");
-      while (n_copy && isspace ((unsigned char) *p))
-       p++, n_copy++;
-      while (n_copy && !isspace ((unsigned char) *p))
-       *cp++ = *p++, n_copy--;
-      *cp++ = '\'';
-      *cp = 0;
+      struct len_string line;
+      const char *sp;
+      
+      dfm_get_record (data_file, &line);
+      sp = ls_c_str (&line);
+      while (sp < ls_end (&line) && isspace ((unsigned char) *sp))
+        sp++;
+      if (sp >= ls_end (&line))
+        strcpy (buf, "at end of line");
+      else
+        {
+          char *dp;
+          size_t copy_cnt = 0;
+
+          dp = stpcpy (buf, "before `");
+          while (sp < ls_end (&line) && !isspace ((unsigned char) *sp)
+                 && copy_cnt < 10) 
+            {
+              *dp++ = *sp++;
+              copy_cnt++; 
+            }
+          strcpy (dp, "'");
+        }
     }
   
   return buf;
@@ -856,68 +761,55 @@ context (struct file_handle *data_file)
 static int
 another_token (struct file_handle *data_file)
 {
-  char *cp, *ep;
-  int len;
-
   for (;;)
     {
-      cp = dfm_get_record (data_file, &len);
-      if (!cp)
-       return 0;
+      struct len_string line;
+      const char *cp;
+      
+      if (dfm_eof (data_file))
+        return 0;
+      dfm_get_record (data_file, &line);
 
-      ep = cp + len;
-      while (isspace ((unsigned char) *cp) && cp < ep)
+      cp = ls_c_str (&line);
+      while (isspace ((unsigned char) *cp) && cp < ls_end (&line))
        cp++;
 
-      if (cp < ep)
-       break;
+      if (cp < ls_end (&line)) 
+        {
+          dfm_forward_columns (data_file, cp - ls_c_str (&line));
+          return 1;
+        }
 
-      dfm_fwd_record (data_file);
+      dfm_forward_record (data_file);
     }
-  
-  dfm_set_record (data_file, cp);
-
-  return 1;
 }
 
 /* Parse a MATRIX DATA token from mx->data_file into TOKEN. */
 static int
 (mget_token) (struct matrix_token *token, struct file_handle *data_file)
 {
-  char *cp, *ep;
-  int len;
+  struct len_string line;
   int first_column;
-    
-  for (;;)
-    {
-      cp = dfm_get_record (data_file, &len);
-      if (!cp)
-        return 0;
-
-      ep = cp + len;
-      while (isspace ((unsigned char) *cp) && cp < ep)
-       cp++;
+  char *cp;
 
-      if (cp < ep)
-       break;
+  if (!another_token (data_file))
+    return 0;
 
-      dfm_fwd_record (data_file);
-    }
-  
-  dfm_set_record (data_file, cp);
-  first_column = dfm_get_cur_col (data_file) + 1;
+  dfm_get_record (data_file, &line);
+  first_column = dfm_column_start (data_file);
 
   /* Three types of fields: quoted with ', quoted with ", unquoted. */
+  cp = ls_c_str (&line);
   if (*cp == '\'' || *cp == '"')
     {
       int quote = *cp;
 
       token->type = MSTR;
       token->string = ++cp;
-      while (cp < ep && *cp != quote)
+      while (cp < ls_end (&line) && *cp != quote)
        cp++;
       token->length = cp - token->string;
-      if (cp < ep)
+      if (cp < ls_end (&line))
        cp++;
       else
        msg (SW, _("Scope of string exceeds line."));
@@ -927,7 +819,8 @@ static int
       int is_num = isdigit ((unsigned char) *cp) || *cp == '.';
 
       token->string = cp++;
-      while (cp < ep && !isspace ((unsigned char) *cp) && *cp != ','
+      while (cp < ls_end (&line)
+             && !isspace ((unsigned char) *cp) && *cp != ','
             && *cp != '-' && *cp != '+')
        {
          if (isdigit ((unsigned char) *cp))
@@ -963,7 +856,7 @@ static int
        token->type = MSTR;
     }
 
-  dfm_set_record (data_file, cp);
+  dfm_forward_columns (data_file, cp - ls_c_str (&line));
     
   return 1;
 }
@@ -973,24 +866,25 @@ static int
 static int
 force_eol (struct file_handle *data_file, const char *content)
 {
-  char *cp;
-  int len;
-  
-  cp = dfm_get_record (data_file, &len);
-  if (!cp)
+  struct len_string line;
+  const char *cp;
+
+  if (dfm_eof (data_file))
     return 0;
-  while (len && isspace (*cp))
-    cp++, len--;
+  dfm_get_record (data_file, &line);
+
+  cp = ls_c_str (&line);
+  while (isspace ((unsigned char) *cp) && cp < ls_end (&line))
+    cp++;
   
-  if (len)
+  if (cp < ls_end (&line))
     {
       msg (SE, _("End of line expected %s while reading %s."),
           context (data_file), content);
       return 0;
     }
   
-  dfm_fwd_record (data_file);
-  
+  dfm_forward_record (data_file);
   return 1;
 }
 \f
@@ -1034,7 +928,7 @@ read_matrices_without_rowtype (struct matrix_data_pgm *mx)
   vfm_source = create_case_source (&matrix_data_without_rowtype_source_class,
                                    default_dict, &nr);
   
-  procedure (NULL, &nr);
+  procedure (NULL, NULL);
 
   free (nr.split_values);
   free (nr.factor_values);
@@ -1152,6 +1046,7 @@ nr_read_data_lines (struct nr_aux_data *nr,
              break;
            default:
              assert (0);
+              abort ();
            }
          break;
        case 2:
@@ -1159,6 +1054,7 @@ nr_read_data_lines (struct nr_aux_data *nr,
          break;
        default:
          assert (0);
+          abort ();
        }
 
       {
@@ -1413,11 +1309,11 @@ dump_cell_content (struct matrix_data_pgm *mx, int content, double *cp,
   int type = content_type[content];
 
   {
-    st_bare_pad_copy (c->data[mx->rowtype_->fv].s,
+    st_bare_pad_copy (case_data_rw (c, mx->rowtype_->fv)->s,
                      content_names[content], 8);
     
     if (type != 1)
-      memset (&c->data[mx->varname_->fv].s, ' ', 8);
+      memset (case_data_rw (c, mx->varname_->fv)->s, ' ', 8);
   }
 
   {
@@ -1431,11 +1327,11 @@ dump_cell_content (struct matrix_data_pgm *mx, int content, double *cp,
        for (j = 0; j < mx->n_continuous; j++)
          {
             int fv = dict_get_var (default_dict, mx->first_continuous + j)->fv;
-           c->data[fv].f = *cp;
+            case_data_rw (c, fv)->f = *cp;
            cp++;
          }
        if (type == 1)
-         st_bare_pad_copy (c->data[mx->varname_->fv].s,
+         st_bare_pad_copy (case_data_rw (c, mx->varname_->fv)->s,
                             dict_get_var (default_dict,
                                           mx->first_continuous + i)->name,
                            8);
@@ -1459,7 +1355,7 @@ nr_output_data (struct nr_aux_data *nr, struct ccase *c,
     split_cnt = dict_get_split_cnt (default_dict);
     split = dict_get_split_vars (default_dict);
     for (i = 0; i < split_cnt; i++)
-      c->data[split[i]->fv].f = nr->split_values[i];
+      case_data_rw (c, split[i]->fv)->f = nr->split_values[i];
   }
 
   if (mx->n_factors)
@@ -1473,7 +1369,7 @@ nr_output_data (struct nr_aux_data *nr, struct ccase *c,
 
            for (factor = 0; factor < mx->n_factors; factor++)
              {
-               c->data[mx->factors[factor]->fv].f
+               case_data_rw (c, mx->factors[factor]->fv)->f
                  = nr->factor_values[factor + cell * mx->n_factors];
                debug_printf (("f:%s ", mx->factors[factor]->name));
              }
@@ -1502,7 +1398,7 @@ nr_output_data (struct nr_aux_data *nr, struct ccase *c,
       int factor;
 
       for (factor = 0; factor < mx->n_factors; factor++)
-       c->data[mx->factors[factor]->fv].f = SYSMIS;
+       case_data_rw (c, mx->factors[factor]->fv)->f = SYSMIS;
     }
     
     for (content = 0; content <= PROX; content++)
@@ -1562,7 +1458,7 @@ read_matrices_with_rowtype (struct matrix_data_pgm *mx)
 
   vfm_source = create_case_source (&matrix_data_with_rowtype_source_class,
                                    default_dict, &wr);
-  procedure (NULL, &wr);
+  procedure (NULL, NULL);
 
   free (wr.split_values);
   fh_close_handle (mx->data_file);
@@ -1700,7 +1596,7 @@ wr_output_data (struct wr_aux_data *wr,
     split_cnt = dict_get_split_cnt (default_dict);
     split = dict_get_split_vars (default_dict);
     for (i = 0; i < split_cnt; i++)
-      c->data[split[i]->fv].f = wr->split_values[i];
+      case_data_rw (c, split[i]->fv)->f = wr->split_values[i];
   }
 
   /* Sort the wr->data list. */
@@ -1735,7 +1631,7 @@ wr_output_data (struct wr_aux_data *wr,
 
          for (factor = 0; factor < mx->n_factors; factor++)
            {
-             c->data[mx->factors[factor]->fv].f
+             case_data_rw (c, mx->factors[factor]->fv)->f
                = iter->factors[factor];
              debug_printf (("f:%s ", factors[factor]->name));
            }
@@ -2006,10 +1902,12 @@ wr_read_indeps (struct wr_aux_data *wr)
          break;
        default:
          assert (0);
+          abort ();
        }
       break;
     default:
       assert (0);
+      abort ();
     }
   c->n_rows[wr->content]++;