Rewrite expression code.
[pspp-builds.git] / src / tab.c
index 862028a8c5ab56959896e8b9cb8c9deb1f9c1c65..e3c069ac3260b9e2976ab3cb96aa27023ce07611 100644 (file)
--- a/src/tab.c
+++ b/src/tab.c
 #include <config.h>
 #include "tab.h"
 #include <ctype.h>
-#include <assert.h>
 #include <stdarg.h>
 #include <limits.h>
 #include <stdlib.h>
+#include "error.h"
 #include "alloc.h"
 #include "command.h"
 #include "format.h"
 \f
 struct som_table_class tab_table_class;
 
-#if DEBUGGING
-#define DEFFIRST(NAME, LABEL) LABEL,
-#define DEFTAB(NAME, LABEL) LABEL,
-/*
-static const char *tab_names[] =
-  {
-#include "tab.def"
-  };
-*/
-#undef DEFFIRST
-#undef DEFTAB
-#endif
-
 /* Creates a table with NC columns and NR rows.  If REALLOCABLE is
    nonzero then the table's size can be increased later; otherwise,
    its size can only be reduced. */
@@ -169,7 +156,7 @@ tab_realloc (struct tab_table *t, int nc, int nr)
       int mr1 = min (nr, t->nr);
       int mc1 = min (nc, t->nc);
       
-      struct len_string *new_cc;
+      struct fixed_string *new_cc;
       unsigned char *new_ct;
       int r;
 
@@ -223,6 +210,12 @@ void
 tab_headers (struct tab_table *table, int l, int r, int t, int b)
 {
   assert (table != NULL);
+  assert (l < table->nc);
+  assert (r < table->nc);
+  assert (t < table->nr);
+  assert (b < table->nr);
+
+
   table->l = l;
   table->r = r;
   table->t = t;
@@ -252,11 +245,6 @@ tab_vline (struct tab_table *t, int style, int x, int y1, int y2)
   int y;
 
   assert (t != NULL);
-  assert (x  > 0);
-  assert (x  < t->nc);
-  assert (y1 >= 0);
-  assert (y2 >= y1);
-  assert (y2 <=  t->nr);
 
 #if GLOBAL_DEBUGGING
   if (x + t->col_ofs < 0 || x + t->col_ofs > t->nc
@@ -277,6 +265,12 @@ tab_vline (struct tab_table *t, int style, int x, int y1, int y2)
   y1 += t->row_ofs;
   y2 += t->row_ofs;
 
+  assert (x  > 0);
+  assert (x  < t->nc);
+  assert (y1 >= 0);
+  assert (y2 >= y1);
+  assert (y2 <=  t->nr);
+
   if (style != -1)
     {
       if ((style & TAL_SPACING) == 0)
@@ -295,16 +289,16 @@ tab_hline (struct tab_table * t, int style, int x1, int x2, int y)
 
   assert (t != NULL);
 
+  x1 += t->col_ofs;
+  x2 += t->col_ofs;
+  y += t->row_ofs;
+
   assert (y >= 0);
   assert (y < t->nr);
   assert (x2 >= x1 );
   assert (x1 >= 0 );
   assert (x2 < t->nc);
 
-  x1 += t->col_ofs;
-  x2 += t->col_ofs;
-  y += t->row_ofs;
-
   if (style != -1)
     {
       if ((style & TAL_SPACING) == 0)
@@ -326,13 +320,6 @@ tab_box (struct tab_table *t, int f_h, int f_v, int i_h, int i_v,
 {
   assert (t != NULL);
 
-  assert (x2 >= x1);
-  assert (y2 >= y1);
-  assert (x1 >= 0);
-  assert (y1 >= 0);
-  assert (x2 < t->nc);
-  assert (y2 < t->nr);
-
 #if GLOBAL_DEBUGGING
   if (x1 + t->col_ofs < 0 || x1 + t->col_ofs >= t->nc 
       || x2 + t->col_ofs < 0 || x2 + t->col_ofs >= t->nc
@@ -355,6 +342,13 @@ tab_box (struct tab_table *t, int f_h, int f_v, int i_h, int i_v,
   y1 += t->row_ofs;
   y2 += t->row_ofs;
 
+  assert (x2 >= x1);
+  assert (y2 >= y1);
+  assert (x1 >= 0);
+  assert (y1 >= 0);
+  assert (x2 < t->nc);
+  assert (y2 < t->nr);
+
   if (f_h != -1)
     {
       int x;
@@ -416,7 +410,7 @@ tab_box (struct tab_table *t, int f_h, int f_v, int i_h, int i_v,
    the resultant string into S in TABLE's pool. */
 static void
 text_format (struct tab_table *table, int opt, const char *text, va_list args,
-            struct len_string *s)
+            struct fixed_string *s)
 {
   int len;
   
@@ -432,7 +426,8 @@ text_format (struct tab_table *table, int opt, const char *text, va_list args,
   else
     len = strlen (text);
 
-  ls_create_buffer (table->container, s, text, len);
+  ls_create_buffer (s, text, len);
+  pool_register (table->container, free, s->string);
   
   if (opt & TAT_PRINTF)
     local_free (text);
@@ -570,7 +565,6 @@ tab_value (struct tab_table *table, int c, int r, unsigned char opt,
           const union value *v, const struct fmt_spec *f)
 {
   char *contents;
-  union value temp_val;
 
   assert (table != NULL && v != NULL && f != NULL);
 #if GLOBAL_DEBUGGING
@@ -591,11 +585,6 @@ tab_value (struct tab_table *table, int c, int r, unsigned char opt,
   ls_init (&table->cc[c + r * table->cf], contents, f->w);
   table->ct[c + r * table->cf] = opt;
   
-  if (formats[f->type].cat & FCAT_STRING)
-    {
-      temp_val.c = (char *) v->s;
-      v = &temp_val;
-    }
   data_out (contents, f, v);
 }
 
@@ -609,6 +598,7 @@ tab_float (struct tab_table *table, int c, int r, unsigned char opt,
   char buf[40], *cp;
   
   struct fmt_spec f;
+  union value double_value;
 
   assert (table != NULL && w <= 40);
   
@@ -635,7 +625,9 @@ tab_float (struct tab_table *table, int c, int r, unsigned char opt,
     }
 #endif
 
-  data_out (buf, &f, (union value *) &val);
+  double_value.f = val;
+  data_out (buf, &f, &double_value);
+
   cp = buf;
   while (isspace ((unsigned char) *cp) && cp < &buf[w])
     cp++;
@@ -692,12 +684,12 @@ tab_joint_text (struct tab_table *table, int x1, int y1, int x2, int y2,
 
   assert (table != NULL && text != NULL);
 
-  assert (x1 >= 0);
-  assert (y1 >= 0);
+  assert (x1 + table->col_ofs >= 0);
+  assert (y1 + table->row_ofs >= 0);
   assert (y2 >= y1);
   assert (x2 >= x1);
-  assert (y2 < table->nr);
-  assert (x2 < table->nc);
+  assert (y2 + table->row_ofs < table->nr);
+  assert (x2 + table->col_ofs < table->nc);
 
 #if GLOBAL_DEBUGGING
   if (x1 + table->col_ofs < 0 || x1 + table->col_ofs >= table->nc
@@ -734,7 +726,7 @@ tab_joint_text (struct tab_table *table, int x1, int y1, int x2, int y2,
   opt |= TAB_JOIN;
   
   {
-    struct len_string *cc = &table->cc[x1 + y1 * table->cf];
+    struct fixed_string *cc = &table->cc[x1 + y1 * table->cf];
     unsigned char *ct = &table->ct[x1 + y1 * table->cf];
     const int ofs = table->cf - (x2 - x1);
 
@@ -759,7 +751,7 @@ tab_joint_text (struct tab_table *table, int x1, int y1, int x2, int y2,
 /* Sets cell (C,R) in TABLE, with options OPT, to contents STRING. */
 void
 tab_raw (struct tab_table *table, int c, int r, unsigned opt,
-        struct len_string *string)
+        struct fixed_string *string)
 {
   assert (table != NULL && string != NULL);
   
@@ -876,11 +868,12 @@ tab_flags (struct tab_table *t, unsigned flags)
 void
 tab_submit (struct tab_table *t)
 {
-  struct som_table s;
+  struct som_entity s;
 
   assert (t != NULL);
   s.class = &tab_table_class;
   s.ext = t;
+  s.type = SOM_TABLE;
   som_submit (&s);
   tab_destroy (t);
 }
@@ -935,9 +928,11 @@ int tab_hit;
 
 /* Set the current table to TABLE. */
 static void
-tabi_table (struct som_table *table)
+tabi_table (struct som_entity *table)
 {
   assert (table != NULL);
+  assert (table->type == SOM_TABLE);
+
   t = table->ext;
   tab_offset (t, 0, 0);
   
@@ -1164,7 +1159,7 @@ tabi_title (int x, int y)
   cp = stpcpy (cp, ".  ");
   if (!ls_empty_p (&t->title))
     {
-      memcpy (cp, ls_value (&t->title), ls_length (&t->title));
+      memcpy (cp, ls_c_str (&t->title), ls_length (&t->title));
       cp += ls_length (&t->title);
     }
   *cp = 0;
@@ -1339,7 +1334,7 @@ render_strip (int x, int y, int r, int c1, int c2, int r1 UNUSED, int r2)
                    }
                } else {
                  struct tab_joined_cell *j =
-                   (struct tab_joined_cell *) ls_value (&t->cc[index]);
+                   (struct tab_joined_cell *) ls_c_str (&t->cc[index]);
 
                  if (j->hit != tab_hit)
                    {