treewide: Replace <name>_cnt by n_<name>s and <name>_cap by allocated_<name>.
[pspp] / src / language / expressions / evaluate.c
index 6ac0572185619dc83977cb81655957cf437f6581..1ad2be802e437eadd50a481f0ae0499c5d5b6a83 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 1997-9, 2000, 2006, 2007, 2009, 2010, 2011 Free Software Foundation, Inc.
+   Copyright (C) 1997-9, 2000, 2006, 2007, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <config.h>
+
 #include "evaluate.h"
 
 #include <ctype.h>
-#include <libpspp/assertion.h>
-#include <libpspp/message.h>
-#include <language/expressions/helpers.h>
-#include <language/expressions/private.h>
-#include <language/lexer/value-parser.h>
-#include <libpspp/pool.h>
+
+#include "libpspp/assertion.h"
+#include "libpspp/message.h"
+#include "language/expressions/helpers.h"
+#include "language/expressions/private.h"
+#include "language/lexer/value-parser.h"
+#include "libpspp/pool.h"
+#include "output/driver.h"
 
 #include "xalloc.h"
 
@@ -48,7 +51,7 @@ expr_evaluate (struct expression *e, const struct ccase *c, int case_idx,
 
   for (;;)
     {
-      assert (op < e->ops + e->op_cnt);
+      assert (op < e->ops + e->n_ops);
       switch (op++->operation)
        {
         case OP_number:
@@ -102,8 +105,8 @@ expr_evaluate_str (struct expression *e, const struct ccase *c, int case_idx,
   buf_copy_rpad (dst, dst_size, s.string, s.length, ' ');
 }
 \f
-#include <language/lexer/lexer.h>
-#include <language/command.h>
+#include "language/lexer/lexer.h"
+#include "language/command.h"
 
 int
 cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
@@ -130,7 +133,6 @@ cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
       else if (lex_match (lexer, T_LPAREN))
         {
           struct variable *v;
-          size_t old_value_cnt;
           int width;
 
           if (!lex_force_id (lexer))
@@ -151,13 +153,12 @@ cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
               goto done;
             }
 
-         if  ( ds == NULL )
+         if  (ds == NULL)
            {
-             ds = create_dataset ();
+             ds = dataset_create (NULL, "");
              d = dataset_dict (ds);
            }
 
-          old_value_cnt = dict_get_next_value_idx (d);
           v = dict_create_var (d, name, width);
           if (v == NULL)
             {
@@ -172,7 +173,7 @@ cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
           else
             c = case_unshare_and_resize (c, dict_get_proto (d));
 
-          if (!parse_value (lexer, case_data_rw (c, v), var_get_width (v)))
+          if (!parse_value (lexer, case_data_rw (c, v), v))
             NOT_REACHED ();
 
           if (!lex_force_match (lexer, T_RPAREN))
@@ -181,20 +182,16 @@ cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
       else
         break;
     }
-  if (lex_token (lexer) != T_SLASH)
-    {
-      lex_force_match (lexer, T_SLASH);
-      goto done;
-    }
 
-  lex_get (lexer);
+  if (!lex_force_match (lexer, T_SLASH))
+      goto done;
 
   expr = expr_parse_any (lexer, ds, optimize);
   if (!expr || lex_end_of_command (lexer) != CMD_SUCCESS)
     {
       if (expr != NULL)
         expr_free (expr);
-      printf ("error\n");
+      output_log ("error");
       goto done;
     }
 
@@ -207,28 +204,25 @@ cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
         {
           double d = expr_evaluate_num (expr, c, 0);
           if (d == SYSMIS)
-            printf ("sysmis\n");
+            output_log ("sysmis");
           else
-            printf ("%.2f\n", d);
+            output_log ("%.2f", d);
         }
         break;
 
       case OP_boolean:
         {
           double b = expr_evaluate_num (expr, c, 0);
-          printf ("%s\n",
-                   b == SYSMIS ? "sysmis" : b == 0.0 ? "false" : "true");
+          output_log ("%s",
+                      b == SYSMIS ? "sysmis" : b == 0.0 ? "false" : "true");
         }
         break;
 
       case OP_string:
         {
-          struct substring s;
-          expr_evaluate (expr, c, 0, &s);
-
-          putchar ('"');
-          fwrite (s.string, s.length, 1, stdout);
-          puts ("\"");
+          struct substring out;
+          expr_evaluate (expr, c, 0, &out);
+          output_log ("\"%.*s\"", (int) out.length, out.string);
           break;
         }
 
@@ -240,8 +234,7 @@ cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
   retval = CMD_SUCCESS;
 
  done:
-  if (ds)
-    destroy_dataset (ds);
+  dataset_destroy (ds);
 
   case_unref (c);
 
@@ -253,57 +246,57 @@ cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
 void
 expr_debug_print_postfix (const struct expression *e)
 {
-  size_t i;
+  struct string s = DS_EMPTY_INITIALIZER;
 
-  for (i = 0; i < e->op_cnt; i++)
+  for (size_t i = 0; i < e->n_ops; i++)
     {
       union operation_data *op = &e->ops[i];
       if (i > 0)
-        putc (' ', stderr);
+        ds_put_byte (&s, ' ');
       switch (e->op_types[i])
         {
         case OP_operation:
           if (op->operation == OP_return_number)
-            printf ("return_number");
+            ds_put_cstr (&s, "return_number");
           else if (op->operation == OP_return_string)
-            printf ("return_string");
+            ds_put_cstr (&s, "return_string");
           else if (is_function (op->operation))
-            printf ("%s", operations[op->operation].prototype);
+            ds_put_format (&s, "%s", operations[op->operation].prototype);
           else if (is_composite (op->operation))
-            printf ("%s", operations[op->operation].name);
+            ds_put_format (&s, "%s", operations[op->operation].name);
           else
-            printf ("%s:", operations[op->operation].name);
+            ds_put_format (&s, "%s:", operations[op->operation].name);
           break;
         case OP_number:
           if (op->number != SYSMIS)
-            printf ("n<%g>", op->number);
+            ds_put_format (&s, "n<%g>", op->number);
           else
-            printf ("n<SYSMIS>");
+            ds_put_cstr (&s, "n<SYSMIS>");
           break;
         case OP_string:
-          printf ("s<%.*s>",
-                   (int) op->string.length,
-                   op->string.string != NULL ? op->string.string : "");
+          ds_put_cstr (&s, "s<");
+          ds_put_substring (&s, op->string);
+          ds_put_byte (&s, '>');
           break;
         case OP_format:
           {
             char str[FMT_STRING_LEN_MAX + 1];
             fmt_to_string (op->format, str);
-            printf ("f<%s>", str);
+            ds_put_format (&s, "f<%s>", str);
           }
           break;
         case OP_variable:
-          printf ("v<%s>", var_get_name (op->variable));
+          ds_put_format (&s, "v<%s>", var_get_name (op->variable));
           break;
         case OP_vector:
-          printf ("vec<%s>", vector_get_name (op->vector));
+          ds_put_format (&s, "vec<%s>", vector_get_name (op->vector));
           break;
         case OP_integer:
-          printf ("i<%d>", op->integer);
+          ds_put_format (&s, "i<%d>", op->integer);
           break;
         default:
           NOT_REACHED ();
         }
     }
-  printf ("\n");
+  output_log_nocopy (ds_steal_cstr (&s));
 }