Add scratch file handles.
[pspp-builds.git] / src / matrix-data.c
index a699738ddb04ce8ddbc38d847e371f60fed306bd..994285cd98d06d79c48603c7fed4dc30ebe1e3e8 100644 (file)
@@ -14,8 +14,8 @@
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-   02111-1307, USA. */
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA. */
 
 #include <config.h>
 #include "error.h"
@@ -38,6 +38,9 @@
 #include "var.h"
 #include "vfm.h"
 
+#include "gettext.h"
+#define _(msgid) gettext (msgid)
+
 #include "debug-print.h"
 
 /* FIXME: /N subcommand not implemented.  It should be pretty simple,
@@ -129,7 +132,7 @@ struct matrix_data_pgm
     struct variable *single_split; /* Single SPLIT FILE variable. */
 
     /* Factor variables.  */
-    int n_factors;              /* Number of factor variables. */
+    size_t n_factors;           /* Number of factor variables. */
     struct variable **factors;  /* Factor variables. */
     int is_per_factor[PROX + 1]; /* Is there per-factor data? */
 
@@ -169,7 +172,7 @@ cmd_matrix_data (void)
 {
   struct pool *pool;
   struct matrix_data_pgm *mx;
-  struct file_handle *fh = NULL;
+  struct file_handle *fh = fh_inline_file ();
     
   unsigned seen = 0;
   
@@ -201,7 +204,7 @@ cmd_matrix_data (void)
       if (lex_match_id ("VARIABLES"))
        {
          char **v;
-         int nv;
+         size_t nv;
 
          if (seen & 1)
            {
@@ -215,10 +218,10 @@ cmd_matrix_data (void)
            goto lossage;
          
          {
-           int i;
+           size_t i;
 
            for (i = 0; i < nv; i++)
-             if (!strcmp (v[i], "VARNAME_"))
+             if (!strcasecmp (v[i], "VARNAME_"))
                {
                  msg (SE, _("VARNAME_ cannot be explicitly specified on "
                             "VARIABLES."));
@@ -230,13 +233,13 @@ cmd_matrix_data (void)
          }
          
          {
-           int i;
+           size_t i;
 
            for (i = 0; i < nv; i++)
              {
                struct variable *new_var;
                
-               if (strcmp (v[i], "ROWTYPE_"))
+               if (strcasecmp (v[i], "ROWTYPE_"))
                  {
                    new_var = dict_create_var_assert (default_dict, v[i], 0);
                     attach_mxd_aux (new_var, MXD_CONTINUOUS, i);
@@ -255,7 +258,7 @@ cmd_matrix_data (void)
       else if (lex_match_id ("FILE"))
        {
          lex_match ('=');
-         fh = fh_parse ();
+         fh = fh_parse (FH_REF_FILE | FH_REF_INLINE);
          if (fh == NULL)
            goto lossage;
        }
@@ -306,7 +309,8 @@ cmd_matrix_data (void)
          if (dict_lookup_var (default_dict, tokid) == NULL
              && (lex_look_ahead () == '.' || lex_look_ahead () == '/'))
            {
-             if (!strcmp (tokid, "ROWTYPE_") || !strcmp (tokid, "VARNAME_"))
+             if (!strcasecmp (tokid, "ROWTYPE_")
+                  || !strcasecmp (tokid, "VARNAME_"))
                {
                  msg (SE, _("Split variable may not be named ROWTYPE_ "
                             "or VARNAME_."));
@@ -323,7 +327,7 @@ cmd_matrix_data (void)
          else
            {
              struct variable **split;
-             int n;
+             size_t n;
 
              if (!parse_variables (default_dict, &split, &n, PV_NO_DUPLICATE))
                goto lossage;
@@ -362,11 +366,12 @@ cmd_matrix_data (void)
            }
          seen |= 4;
 
-         if (!parse_variables (default_dict, &mx->factors, &mx->n_factors, PV_NONE))
+         if (!parse_variables (default_dict, &mx->factors, &mx->n_factors,
+                                PV_NONE))
            goto lossage;
          
          {
-           int i;
+           size_t i;
            
            for (i = 0; i < mx->n_factors; i++)
              {
@@ -394,7 +399,7 @@ cmd_matrix_data (void)
              goto lossage;
            }
 
-         if (!lex_integer_p () || lex_integer () < 1)
+         if (!lex_is_integer () || lex_integer () < 1)
            {
              lex_error (_("expecting positive integer"));
              goto lossage;
@@ -413,7 +418,7 @@ cmd_matrix_data (void)
              goto lossage;
            }
 
-         if (!lex_integer_p () || lex_integer () < 1)
+         if (!lex_is_integer () || lex_integer () < 1)
            {
              lex_error (_("expecting positive integer"));
              goto lossage;
@@ -668,7 +673,7 @@ string_to_content_type (char *s, int *collide)
     };
 
   for (tp = tab; tp->value != -1; tp++)
-    if (!strcmp (s, tp->string))
+    if (!strcasecmp (s, tp->string))
       {
        if (collide)
          *collide = tp->collide;
@@ -767,7 +772,7 @@ context (struct dfm_reader *reader)
     strcpy (buf, "at end of file");
   else 
     {
-      struct len_string line;
+      struct fixed_string line;
       const char *sp;
       
       dfm_get_record (reader, &line);
@@ -801,7 +806,7 @@ another_token (struct dfm_reader *reader)
 {
   for (;;)
     {
-      struct len_string line;
+      struct fixed_string line;
       const char *cp;
       
       if (dfm_eof (reader))
@@ -826,7 +831,7 @@ another_token (struct dfm_reader *reader)
 static int
 (mget_token) (struct matrix_token *token, struct dfm_reader *reader)
 {
-  struct len_string line;
+  struct fixed_string line;
   int first_column;
   char *cp;
 
@@ -883,9 +888,7 @@ static int
          di.e = token->string + token->length;
          di.v = (union value *) &token->number;
          di.f1 = first_column;
-         di.format.type = FMT_F;
-         di.format.w = token->length;
-         di.format.d = 0;
+         di.format = make_output_format (FMT_F, token->length, 0);
 
          if (!data_in (&di))
            return 0;
@@ -904,7 +907,7 @@ static int
 static int
 force_eol (struct dfm_reader *reader, const char *content)
 {
-  struct len_string line;
+  struct fixed_string line;
   const char *cp;
 
   if (dfm_eof (reader))
@@ -958,13 +961,13 @@ read_matrices_without_rowtype (struct matrix_data_pgm *mx)
 
   nr.mx = mx;
   nr.data = NULL;
-  nr.factor_values = xmalloc (sizeof *nr.factor_values * mx->n_factors * mx->cells);
+  nr.factor_values = xnmalloc (mx->n_factors * mx->cells,
+                               sizeof *nr.factor_values);
   nr.max_cell_idx = 0;
-  nr.split_values = xmalloc (sizeof *nr.split_values
-                             * dict_get_split_cnt (default_dict));
+  nr.split_values = xnmalloc (dict_get_split_cnt (default_dict),
+                              sizeof *nr.split_values);
 
-  vfm_source = create_case_source (&matrix_data_without_rowtype_source_class,
-                                   default_dict, &nr);
+  vfm_source = create_case_source (&matrix_data_without_rowtype_source_class, &nr);
   
   procedure (NULL, NULL);
 
@@ -1140,7 +1143,7 @@ matrix_data_read_without_rowtype (struct case_source *source,
   {
     int *cp;
 
-    nr->data = pool_alloc (mx->container, (PROX + 1) * sizeof *nr->data);
+    nr->data = pool_nalloc (mx->container, PROX + 1, sizeof *nr->data);
     
     {
       int i;
@@ -1163,12 +1166,12 @@ matrix_data_read_without_rowtype (struct case_source *source,
            int n_vectors = per_factor ? mx->cells : 1;
            int i;
            
-           nr->data[*cp] = pool_alloc (mx->container,
-                                      n_vectors * sizeof **nr->data);
+           nr->data[*cp] = pool_nalloc (mx->container,
+                                         n_vectors, sizeof **nr->data);
            
            for (i = 0; i < n_vectors; i++)
-             nr->data[*cp][i] = pool_alloc (mx->container,
-                                           n_entries * sizeof ***nr->data);
+             nr->data[*cp][i] = pool_nalloc (mx->container,
+                                              n_entries, sizeof ***nr->data);
          }
        }
   }
@@ -1308,7 +1311,7 @@ nr_read_factors (struct nr_aux_data *nr, int cell)
     }
       
   {
-    int i;
+    size_t i;
     
     for (i = 0; i < mx->n_factors; i++)
       {
@@ -1347,8 +1350,8 @@ dump_cell_content (struct matrix_data_pgm *mx, int content, double *cp,
   int type = content_type[content];
 
   {
-    st_bare_pad_copy (case_data_rw (c, mx->rowtype_->fv)->s,
-                     content_names[content], 8);
+    buf_copy_str_rpad (case_data_rw (c, mx->rowtype_->fv)->s, 8,
+                       content_names[content]);
     
     if (type != 1)
       memset (case_data_rw (c, mx->varname_->fv)->s, ' ', 8);
@@ -1369,10 +1372,9 @@ dump_cell_content (struct matrix_data_pgm *mx, int content, double *cp,
            cp++;
          }
        if (type == 1)
-         st_bare_pad_copy (case_data_rw (c, mx->varname_->fv)->s,
-                            dict_get_var (default_dict,
-                                          mx->first_continuous + i)->name,
-                           8);
+         buf_copy_str_rpad (case_data_rw (c, mx->varname_->fv)->s, 8,
+                             dict_get_var (default_dict,
+                                           mx->first_continuous + i)->name);
        write_case (wc_data);
       }
   }
@@ -1403,7 +1405,7 @@ nr_output_data (struct nr_aux_data *nr, struct ccase *c,
       for (cell = 0; cell < mx->cells; cell++)
        {
          {
-           int factor;
+           size_t factor;
 
            for (factor = 0; factor < mx->n_factors; factor++)
              {
@@ -1433,7 +1435,7 @@ nr_output_data (struct nr_aux_data *nr, struct ccase *c,
     int content;
     
     {
-      int factor;
+      size_t factor;
 
       for (factor = 0; factor < mx->n_factors; factor++)
        case_data_rw (c, mx->factors[factor]->fv)->f = SYSMIS;
@@ -1495,7 +1497,7 @@ read_matrices_with_rowtype (struct matrix_data_pgm *mx)
   mx->cells = 0;
 
   vfm_source = create_case_source (&matrix_data_with_rowtype_source_class,
-                                   default_dict, &wr);
+                                   &wr);
   procedure (NULL, NULL);
 
   free (wr.split_values);
@@ -1547,7 +1549,7 @@ wr_read_splits (struct wr_aux_data *wr,
   else
     {
       compare = 0;
-      wr->split_values = xmalloc (split_cnt * sizeof *wr->split_values);
+      wr->split_values = xnmalloc (split_cnt, sizeof *wr->split_values);
     }
   
   {
@@ -1642,7 +1644,7 @@ wr_output_data (struct wr_aux_data *wr,
     struct factor_data *iter;
     int i;
 
-    factors = xmalloc (sizeof *factors * mx->cells);
+    factors = xnmalloc (mx->cells, sizeof *factors);
 
     for (i = 0, iter = wr->data; iter; iter = iter->next, i++)
       factors[i] = iter;
@@ -1664,14 +1666,11 @@ wr_output_data (struct wr_aux_data *wr,
     for (iter = wr->data; iter; iter = iter->next)
       {
        {
-         int factor;
+         size_t factor;
 
          for (factor = 0; factor < mx->n_factors; factor++)
-           {
-             case_data_rw (c, mx->factors[factor]->fv)->f
-               = iter->factors[factor];
-             debug_printf (("f:%s ", factors[factor]->name));
-           }
+            case_data_rw (c, mx->factors[factor]->fv)->f
+              = iter->factors[factor];
        }
        
        {
@@ -1768,7 +1767,7 @@ wr_read_factors (struct wr_aux_data *wr)
 
   wr->content = -1;
   {
-    int i;
+    size_t i;
   
     for (i = 0; i < mx->n_factors; i++)
       {
@@ -1805,7 +1804,7 @@ wr_read_factors (struct wr_aux_data *wr)
      mechanism. */
   if (wr->current)
     {
-      int i;
+      size_t i;
       
       for (i = 0; i < mx->n_factors; i++)
        if (factor_values[i] != wr->current->factors[i])
@@ -1820,7 +1819,7 @@ cache_miss:
 
     for (iter = wr->data; iter; iter = iter->next)
       {
-       int i;
+       size_t i;
 
        for (i = 0; i < mx->n_factors; i++)
          if (factor_values[i] != iter->factors[i])
@@ -1837,10 +1836,11 @@ cache_miss:
   {
     struct factor_data *new = pool_alloc (mx->container, sizeof *new);
 
-    new->factors = pool_alloc (mx->container, sizeof *new->factors * mx->n_factors);
+    new->factors = pool_nalloc (mx->container,
+                                mx->n_factors, sizeof *new->factors);
     
     {
-      int i;
+      size_t i;
 
       for (i = 0; i < mx->n_factors; i++)
        new->factors[i] = factor_values[i];
@@ -1888,8 +1888,8 @@ wr_read_indeps (struct wr_aux_data *wr)
       if (type == 1)
        n_items *= mx->n_continuous;
       
-      c->data[wr->content] = pool_alloc (mx->container,
-                                       sizeof **c->data * n_items);
+      c->data[wr->content] = pool_nalloc (mx->container,
+                                          n_items, sizeof **c->data);
     }
 
   cp = &c->data[wr->content][n_rows * mx->n_continuous];