Had to get last call to multipass_split_output() inside
[pspp-builds.git] / src / recode.c
index 146e8a36a5b28e102558638a16b85bd18cd9cf8b..e415dcf84bbcd89d7a18cb986267322043aadf50 100644 (file)
    02111-1307, USA. */
 
 #include <config.h>
-#include <assert.h>
+#include "error.h"
 #include <ctype.h>
+#include <math.h>
 #include <stdlib.h>
 #include "alloc.h"
-#include "approx.h"
-#include "cases.h"
 #include "command.h"
 #include "error.h"
 #include "lexer.h"
 #include "magic.h"
 #include "str.h"
 #include "var.h"
-
-#include "debug-print.h"
 \f
 /* Definitions. */
 
+/* Type of source value for RECODE. */
 enum
   {
     RCD_END,                   /* sentinel value */
@@ -105,23 +103,12 @@ struct recode_trns
 static int parse_dest_spec (struct rcd_var * rcd, union value *v,
                            size_t *max_dst_width);
 static int parse_src_spec (struct rcd_var * rcd, int type, size_t max_src_width);
-static int recode_trns_proc (struct trns_header *, struct ccase *);
-static void recode_trns_free (struct trns_header *);
+static trns_proc_func recode_trns_proc;
+static trns_free_func recode_trns_free;
 static double convert_to_double (char *, int);
-
-#if DEBUGGING
-static void debug_print (struct rcd_var * head);
-#endif
 \f
 /* Parser. */
 
-/* First transformation in the list.  rcd is in this list. */
-static struct rcd_var *head;
-
-/* Variables in the current part of the recoding. */
-struct variable **v;
-int nv;
-
 /* Parses the RECODE transformation. */
 int
 cmd_recode (void)
@@ -148,10 +135,16 @@ cmd_recode (void)
      rcd_var's. */
   struct recode_trns *trns;
 
-  lex_match_id ("RECODE");
+  /* First transformation in the list.  rcd is in this list. */
+  struct rcd_var *head;
+
+  /* Variables in the current part of the recoding. */
+  struct variable **v;
+  int nv;
 
   /* Parses each specification between slashes. */
   head = rcd = xmalloc (sizeof *rcd);
+  v = NULL;
   for (;;)
     {
       /* Whether we've already encountered a specification for SYSMIS. */
@@ -165,7 +158,7 @@ cmd_recode (void)
       rcd->sysmis.f = 0;
 
       /* Parse variable names. */
-      if (!parse_variables (NULL, &v, &nv, PV_SAME_TYPE))
+      if (!parse_variables (default_dict, &v, &nv, PV_SAME_TYPE))
        goto lossage;
 
       /* Ensure all variables are same type; find length of longest
@@ -295,7 +288,7 @@ cmd_recode (void)
          if ((rcd->flags & RCD_DEST_MASK) == RCD_DEST_STRING)
            for (i = 0, iter = rcd; i < nv; i++, iter = iter->next)
              {
-               struct variable *v = find_variable (names[i]);
+               struct variable *v = dict_lookup_var (default_dict, names[i]);
 
                if (!v)
                  {
@@ -321,7 +314,7 @@ cmd_recode (void)
          else
            for (i = 0, iter = rcd; i < nv; i++, iter = iter->next)
              {
-               struct variable *v = find_variable (names[i]);
+               struct variable *v = dict_lookup_var (default_dict, names[i]);
 
                if (v)
                  {
@@ -402,6 +395,7 @@ cmd_recode (void)
       rcd = rcd->next = xmalloc (sizeof *rcd);
 
       free (v);
+      v = NULL;
     }
 
   if (token != '.')
@@ -413,17 +407,14 @@ cmd_recode (void)
   for (rcd = head; rcd; rcd = rcd->next)
     if (rcd->dest_name[0])
       {
-       rcd->dest = create_variable (&default_dict, rcd->dest_name,
-                                    NUMERIC, 0);
+       rcd->dest = dict_create_var (default_dict, rcd->dest_name, 0);
        if (!rcd->dest)
          {
-           /* This can occur if a destname is duplicated.  We could
-              give an error at parse time but I don't care enough. */
-           rcd->dest = find_variable (rcd->dest_name);
-           assert (rcd->dest != NULL);
+           /* FIXME: This can occur if a destname is duplicated.
+              We could give an error at parse time but I don't
+              care enough. */
+           rcd->dest = dict_lookup_var_assert (default_dict, rcd->dest_name);
          }
-       else
-         envector (rcd->dest);
       }
 
   trns = xmalloc (sizeof *trns);
@@ -432,13 +423,10 @@ cmd_recode (void)
   trns->codings = head;
   add_transformation ((struct trns_header *) trns);
 
-#if DEBUGGING
-  debug_print (head);
-#endif
-
   return CMD_SUCCESS;
 
  lossage:
+  free (v);
   {
     struct recode_trns t;
 
@@ -491,6 +479,11 @@ parse_dest_spec (struct rcd_var * rcd, union value * v, size_t *max_dst_width)
          v->c = NULL;
        }
     }
+  else 
+    {
+      lex_error (_("expecting output value"));
+      return 0;
+    }
 
   if ((rcd->flags & RCD_DEST_MASK) == RCD_DEST_ERROR)
     rcd->flags |= flags;
@@ -720,19 +713,19 @@ find_src_numeric (struct rcd_var * v, struct ccase * c)
          return cp;
        break;
       case RCD_SINGLE:
-       if (approx_eq (cmp, cp->f1.f))
+       if (cmp == cp->f1.f)
          return cp;
        break;
       case RCD_HIGH:
-       if (approx_ge (cmp, cp->f1.f))
+       if (cmp >= cp->f1.f)
          return cp;
        break;
       case RCD_LOW:
-       if (approx_le (cmp, cp->f1.f))
+       if (cmp <= cp->f1.f)
          return cp;
        break;
       case RCD_RANGE:
-       if (approx_in_range (cmp, cp->f1.f, cp->f2.f))
+       if (cmp >= cp->f1.f && cmp <= cp->f2.f)
          return cp;
        break;
       case RCD_ELSE:
@@ -776,13 +769,15 @@ find_src_string (struct rcd_var * v, struct ccase * c)
 }
 
 static int
-recode_trns_proc (struct trns_header * t, struct ccase * c)
+recode_trns_proc (struct trns_header * t, struct ccase * c,
+                  int case_num UNUSED)
 {
   struct rcd_var *v;
-  struct coding *cp;
 
   for (v = ((struct recode_trns *) t)->codings; v; v = v->next)
     {
+      struct coding *cp;
+
       switch (v->flags & RCD_SRC_MASK)
        {
        case RCD_SRC_NUMERIC:
@@ -791,6 +786,9 @@ recode_trns_proc (struct trns_header * t, struct ccase * c)
        case RCD_SRC_STRING:
          cp = find_src_string (v, c);
          break;
+        default:
+          assert (0);
+          abort ();
        }
       if (!cp)
        continue;
@@ -818,118 +816,12 @@ recode_trns_proc (struct trns_header * t, struct ccase * c)
 
   return -1;
 }
-\f
-/* Debug output. */
-
-#if DEBUGGING
-static void
-dump_dest (struct rcd_var * v, union value * c)
-{
-  if ((v->flags & RCD_DEST_MASK) == RCD_DEST_NUMERIC)
-    if (c->f == SYSMIS)
-      printf ("=SYSMIS");
-    else if (c->f == -SYSMIS)
-      printf ("=COPY");
-    else
-      printf ("=%g", c->f);
-  else if (c->c)
-    printf ("=\"%s\"", c->c);
-  else
-    printf ("=COPY");
-}
-
-static void
-debug_print (struct rcd_var * head)
-{
-  struct rcd_var *iter, *start;
-  struct coding *c;
-
-  printf ("RECODE\n");
-  for (iter = head; iter; iter = iter->next)
-    {
-      start = iter;
-      printf ("  %s%s", iter == head ? "" : "/", iter->src->name);
-      while (iter->next && (iter->next->flags & RCD_MISC_DUPLICATE))
-       {
-         iter = iter->next;
-         printf (" %s", iter->src->name);
-       }
-      if (iter->has_sysmis)
-       {
-         printf ("(SYSMIS");
-         dump_dest (iter, &iter->sysmis);
-         printf (")");
-       }
-      for (c = iter->map; c->type != RCD_END; c++)
-       {
-         printf ("(");
-         if ((iter->flags & RCD_SRC_MASK) == RCD_SRC_NUMERIC)
-           switch (c->type)
-             {
-             case RCD_END:
-               printf (_("!!END!!"));
-               break;
-             case RCD_USER:
-               printf ("MISSING");
-               break;
-             case RCD_SINGLE:
-               printf ("%g", c->f1.f);
-               break;
-             case RCD_HIGH:
-               printf ("%g THRU HIGH", c->f1.f);
-               break;
-             case RCD_LOW:
-               printf ("LOW THRU %g", c->f1.f);
-               break;
-             case RCD_RANGE:
-               printf ("%g THRU %g", c->f1.f, c->f2.f);
-               break;
-             case RCD_ELSE:
-               printf ("ELSE");
-               break;
-             default:
-               printf (_("!!ERROR!!"));
-               break;
-             }
-         else
-           switch (c->type)
-             {
-             case RCD_SINGLE:
-               printf ("\"%s\"", c->f1.c);
-               break;
-             case RCD_ELSE:
-               printf ("ELSE");
-               break;
-             case RCD_CONVERT:
-               printf ("CONVERT");
-               break;
-             default:
-               printf (_("!!ERROR!!"));
-               break;
-             }
-         if (c->type != RCD_CONVERT)
-           dump_dest (iter, &c->t);
-         printf (")");
-       }
-      printf ("\n    INTO");
-      for (;;)
-       {
-         printf (" %s",
-               start->dest_name[0] ? start->dest_name : start->dest->name);
-         if (start == iter)
-           break;
-         start = start->next;
-       }
-      printf ("\n");
-    }
-}
-#endif
 
 /* Convert NPTR to a `long int' in base 10.  Returns the long int on
    success, NOT_LONG on failure.  On success stores a pointer to the
    first character after the number into *ENDPTR.  From the GNU C
    library. */
-long int
+static long int
 string_to_long (char *nptr, int width, char **endptr)
 {
   int negative;