Improved robustness of axis implementation
[pspp-builds.git] / lib / gtksheet / psppire-axis-impl.c
index 0505d1a51e5247f474d34c434abeb5cc198196fb..2e9228f3e62868fa556ef397b9ec38107890300e 100644 (file)
@@ -41,19 +41,24 @@ struct axis_node
 };
 
 static gint
-get_unit_at_pixel (const PsppireAxis *axis, glong pixel)
+unit_at_pixel (const PsppireAxis *axis, glong pixel)
 {
   PsppireAxisImpl *a = PSPPIRE_AXIS_IMPL (axis);
 
   unsigned long int start;
+  struct tower_node *n;
+  struct axis_node *an;
+  gfloat fraction;
 
-  struct tower_node *n = tower_lookup (&a->pixel_tower, pixel, &start);
+  g_return_val_if_fail (pixel >= 0, -1);
 
-  struct axis_node *an = tower_data (n, struct axis_node, pixel_node);
+  n = tower_lookup (&a->pixel_tower, pixel, &start);
+  an = tower_data (n, struct axis_node, pixel_node);
 
-  gfloat fraction = (pixel - start) / (gfloat) tower_node_get_size (&an->pixel_node);
+  fraction = (pixel - start) / (gfloat) tower_node_get_size (&an->pixel_node);
 
-  return  tower_node_get_level (&an->unit_node) + fraction * tower_node_get_size (&an->unit_node);
+  return  tower_node_get_level (&an->unit_node)
+    + fraction * tower_node_get_size (&an->unit_node);
 }
 
 
@@ -68,7 +73,7 @@ unit_count (const PsppireAxis *axis)
 
 /* Returns the pixel at the start of UNIT */
 static glong
-pixel_start (const PsppireAxis *axis, gint unit)
+start_pixel (const PsppireAxis *axis, gint unit)
 {
   gfloat fraction;
   PsppireAxisImpl *a = PSPPIRE_AXIS_IMPL (axis);
@@ -134,8 +139,8 @@ psppire_impl_iface_init (PsppireAxisIface *iface)
 {
   iface->unit_size = unit_size;
   iface->unit_count = unit_count;
-  iface->pixel_start = pixel_start;
-  iface->get_unit_at_pixel = get_unit_at_pixel;
+  iface->start_pixel = start_pixel;
+  iface->unit_at_pixel = unit_at_pixel;
   iface->total_size = total_size;
 }
 
@@ -238,8 +243,11 @@ psppire_axis_impl_append (PsppireAxisImpl *a, gint size)
 void
 psppire_axis_impl_append_n (PsppireAxisImpl *a, gint n_units, gint size)
 {
-  struct axis_node *node = pool_alloc (a->pool, sizeof *node);
+  struct axis_node *node;
 
+  g_return_if_fail (n_units > 0);
+
+  node = pool_alloc (a->pool, sizeof *node);
 
   tower_insert (&a->unit_tower, n_units, &node->unit_node, NULL);
   tower_insert (&a->pixel_tower, size * n_units, &node->pixel_node, NULL);
@@ -255,10 +263,18 @@ split (PsppireAxisImpl *a, gint posn)
   unsigned long int start;
   gfloat fraction;
   struct axis_node *new_node ;
-  struct tower_node *n = tower_lookup (&a->unit_tower, posn, &start);
+  struct tower_node *n;
+  struct axis_node *existing_node;
+
+  g_return_if_fail (posn <= tower_height (&a->unit_tower));
 
-  struct axis_node *existing_node =
-    tower_data (n, struct axis_node, unit_node);
+  /* Nothing needs to be done */
+  if ( posn == 0 || posn  == tower_height (&a->unit_tower))
+    return;
+
+  n = tower_lookup (&a->unit_tower, posn, &start);
+
+  existing_node = tower_data (n, struct axis_node, unit_node);
 
   /* Nothing needs to be done, if the range element is already split here */
   if ( posn - start == 0)
@@ -293,28 +309,32 @@ split (PsppireAxisImpl *a, gint posn)
 void
 psppire_axis_impl_insert (PsppireAxisImpl *a, gint posn, gint size)
 {
-  struct tower_node *n;
-  unsigned long int start;
-  struct axis_node *before;
+  struct axis_node *before = NULL;
   struct axis_node *new_node = pool_alloc (a->pool, sizeof (*new_node));
 
-  split (a, posn);
+  if ( posn > 0)
+    {
+      unsigned long int start = 0;
+      struct tower_node *n;
 
-  n = tower_lookup (&a->unit_tower, posn, &start);
-  g_assert (posn == start);
+      split (a, posn);
+
+      n = tower_lookup (&a->unit_tower, posn, &start);
+      g_assert (posn == start);
 
-  before = tower_data (n, struct axis_node, unit_node);
+      before = tower_data (n, struct axis_node, unit_node);
+    }
 
   tower_insert (&a->unit_tower,
                1,
                &new_node->unit_node,
-               &before->unit_node);
+               before ? &before->unit_node : NULL);
 
 
   tower_insert (&a->pixel_tower,
                size,
                &new_node->pixel_node,
-               &before->pixel_node);
+               before ? &before->pixel_node : NULL);
 }
 
 
@@ -325,6 +345,9 @@ make_single (PsppireAxisImpl *a, gint posn)
 {
   unsigned long int start;
   struct tower_node *n;
+
+  g_return_val_if_fail (posn < tower_height (&a->unit_tower), NULL);
+
   n = tower_lookup (&a->unit_tower, posn, &start);
 
   if ( 1 != tower_node_get_size (n))
@@ -348,7 +371,15 @@ make_single (PsppireAxisImpl *a, gint posn)
 void
 psppire_axis_impl_resize (PsppireAxisImpl *a, gint posn, gint size)
 {
-  struct axis_node *an =  make_single (a, posn);
+  struct axis_node *an;
+  g_return_if_fail (posn >= 0);
+
+  /* Silently ignore this request if the position is greater than the number of
+     units in the axis */
+  if (posn >= tower_height (&a->unit_tower))
+    return ;
+
+  an = make_single (a, posn);
 
   tower_resize (&a->pixel_tower, &an->pixel_node, size);
 }
@@ -368,14 +399,16 @@ psppire_axis_impl_clear (PsppireAxisImpl *a)
 
 
 void
-psppire_axis_impl_delete (PsppireAxisImpl *a, gint first, gint n_cases)
+psppire_axis_impl_delete (PsppireAxisImpl *a, gint first, gint n_units)
 {
   gint i;
   g_warning ("%s FIXME: This is an inefficient implementation", __FUNCTION__);
 
-  for (i = first; i < first + n_cases; ++i)
+  g_return_if_fail (first + n_units < tower_height (&a->unit_tower));
+
+  for (i = first; i < first + n_units; ++i)
     {
-      struct axis_node *an = make_single (a, i);
+      struct axis_node *an = make_single (a, first);
 
       tower_delete (&a->unit_tower, &an->unit_node);
       tower_delete (&a->pixel_tower, &an->pixel_node);