5759861 [rkeene@sledge /home/rkeene/devel/old/bc-dos/dc]$ cat -n array.c
  1 /* 
  2  * implement arrays for dc
  3  *
  4  * Copyright (C) 1994, 1997, 1998 Free Software Foundation, Inc.
  5  *
  6  * This program is free software; you can redistribute it and/or modify
  7  * it under the terms of the GNU General Public License as published by
  8  * the Free Software Foundation; either version 2, or (at your option)
  9  * any later version.
 10  *
 11  * This program is distributed in the hope that it will be useful,
 12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14  * GNU General Public License for more details.
 15  *
 16  * You should have received a copy of the GNU General Public License
 17  * along with this program; if not, you can either send email to this
 18  * program's author (see below) or write to: The Free Software Foundation,
 19  * Inc.; 675 Mass Ave. Cambridge, MA 02139, USA.
 20  */
 21 
 22 /* This module is the only one that knows what arrays look like. */
 23 
 24 #include "config.h"
 25 
 26 #include <stdio.h>  /* "dc-proto.h" wants this */
 27 #ifdef HAVE_STDLIB_H
 28 /* get size_t definition from "almost ANSI" compiling environments. */
 29 #include <stdlib.h>
 30 #endif
 31 #include "dc.h"
 32 #include "dc-proto.h"
 33 #include "dc-regdef.h"
 34 
 35 /* what's most useful: quick access or sparse arrays? */
 36 /* I'll go with sparse arrays for now */
 37 struct dc_array {
 38     int Index;
 39     dc_data value;
 40     struct dc_array *next;
 41 };
 42 
 43 
 44 /* initialize the arrays */
 45 void
 46 dc_array_init DC_DECLVOID()
 47 {
 48 }
 49 
 50 /* store value into array_id[Index] */
 51 void
 52 dc_array_set DC_DECLARG((array_id, Index, value))
 53     int array_id DC_DECLSEP
 54     int Index DC_DECLSEP
 55     dc_data value DC_DECLEND
 56 {
 57     struct dc_array *cur;
 58     struct dc_array *prev=NULL;
 59     struct dc_array *newentry;
 60 
 61     cur = dc_get_stacked_array(array_id);
 62     while (cur && cur->Index < Index){
 63         prev = cur;
 64         cur = cur->next;
 65     }
 66     if (cur && cur->Index == Index){
 67         if (cur->value.dc_type == DC_NUMBER)
 68             dc_free_num(&cur->value.v.number);
 69         else if (cur->value.dc_type == DC_STRING)
 70             dc_free_str(&cur->value.v.string);
 71         else
 72             dc_garbage(" in array", array_id);
 73         cur->value = value;
 74     }else{
 75         newentry = dc_malloc(sizeof *newentry);
 76         newentry->Index = Index;
 77         newentry->value = value;
 78         newentry->next = cur;
 79         if (prev)
 80             prev->next = newentry;
 81         else
 82             dc_set_stacked_array(array_id, newentry);
 83     }
 84 }
 85 
 86 /* retrieve a dup of a value from array_id[Index] */
 87 /* A zero value is returned if the specified value is unintialized. */
 88 dc_data
 89 dc_array_get DC_DECLARG((array_id, Index))
 90     int array_id DC_DECLSEP
 91     int Index DC_DECLEND
 92 {
 93     struct dc_array *cur;
 94 
 95     for (cur=dc_get_stacked_array(array_id); cur; cur=cur->next)
 96         if (cur->Index == Index)
 97             return dc_dup(cur->value);
 98     return dc_int2data(0);
 99 }
100 
101 /* free an array chain */
102 void
103 dc_array_free DC_DECLARG((a_head))
104     struct dc_array *a_head DC_DECLEND
105 {
106     struct dc_array *cur;
107     struct dc_array *next;
108 
109     for (cur=a_head; cur; cur=next) {
110         next = cur->next;
111         if (cur->value.dc_type == DC_NUMBER)
112             dc_free_num(&cur->value.v.number);
113         else if (cur->value.dc_type == DC_STRING)
114             dc_free_str(&cur->value.v.string);
115         else
116             dc_garbage("in stack", -1);
117         free(cur);
118     }
119 }
5759862 [rkeene@sledge /home/rkeene/devel/old/bc-dos/dc]$

Click here to go back to the directory listing.
Click here to download this file.
last modified: 1998-03-09 05:20:17