Merge from vendor branch OPENSSH:
[dragonfly.git] / contrib / bc / dc / 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:
19  *
20  *    The Free Software Foundation, Inc.
21  *    59 Temple Place, Suite 330
22  *    Boston, MA 02111 USA
23  */
24
25 /* This module is the only one that knows what arrays look like. */
26
27 #include "config.h"
28
29 #include <stdio.h>      /* "dc-proto.h" wants this */
30 #ifdef HAVE_STDLIB_H
31 /* get size_t definition from "almost ANSI" compiling environments. */
32 #include <stdlib.h>
33 #endif
34 #include "dc.h"
35 #include "dc-proto.h"
36 #include "dc-regdef.h"
37
38 /* what's most useful: quick access or sparse arrays? */
39 /* I'll go with sparse arrays for now */
40 struct dc_array {
41         int Index;
42         dc_data value;
43         struct dc_array *next;
44 };
45
46
47 /* initialize the arrays */
48 void
49 dc_array_init DC_DECLVOID()
50 {
51 }
52
53 /* store value into array_id[Index] */
54 void
55 dc_array_set DC_DECLARG((array_id, Index, value))
56         int array_id DC_DECLSEP
57         int Index DC_DECLSEP
58         dc_data value DC_DECLEND
59 {
60         struct dc_array *cur;
61         struct dc_array *prev=NULL;
62         struct dc_array *newentry;
63
64         cur = dc_get_stacked_array(array_id);
65         while (cur && cur->Index < Index){
66                 prev = cur;
67                 cur = cur->next;
68         }
69         if (cur && cur->Index == Index){
70                 if (cur->value.dc_type == DC_NUMBER)
71                         dc_free_num(&cur->value.v.number);
72                 else if (cur->value.dc_type == DC_STRING)
73                         dc_free_str(&cur->value.v.string);
74                 else
75                         dc_garbage(" in array", array_id);
76                 cur->value = value;
77         }else{
78                 newentry = dc_malloc(sizeof *newentry);
79                 newentry->Index = Index;
80                 newentry->value = value;
81                 newentry->next = cur;
82                 if (prev)
83                         prev->next = newentry;
84                 else
85                         dc_set_stacked_array(array_id, newentry);
86         }
87 }
88
89 /* retrieve a dup of a value from array_id[Index] */
90 /* A zero value is returned if the specified value is unintialized. */
91 dc_data
92 dc_array_get DC_DECLARG((array_id, Index))
93         int array_id DC_DECLSEP
94         int Index DC_DECLEND
95 {
96         struct dc_array *cur;
97
98         for (cur=dc_get_stacked_array(array_id); cur; cur=cur->next)
99                 if (cur->Index == Index)
100                         return dc_dup(cur->value);
101         return dc_int2data(0);
102 }
103
104 /* free an array chain */
105 void
106 dc_array_free DC_DECLARG((a_head))
107         struct dc_array *a_head DC_DECLEND
108 {
109         struct dc_array *cur;
110         struct dc_array *next;
111
112         for (cur=a_head; cur; cur=next) {
113                 next = cur->next;
114                 if (cur->value.dc_type == DC_NUMBER)
115                         dc_free_num(&cur->value.v.number);
116                 else if (cur->value.dc_type == DC_STRING)
117                         dc_free_str(&cur->value.v.string);
118                 else
119                         dc_garbage("in stack", -1);
120                 free(cur);
121         }
122 }