"SfR Fresh" - the SfR Freeware/Shareware Archive 
Member "bc-1.06.95/dc/array.c" of archive bc-1.06.95.tar.gz:
As a special service "SfR Fresh" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting with prefixed line numbers.
Alternatively you can here view or download the uninterpreted source code file.
That can be also achieved for any archive member file by clicking within an archive contents listing on the first character of the file(path) respectively on the according byte size field.
1 /*
2 * implement arrays for dc
3 *
4 * Copyright (C) 1994, 1997, 1998, 2000, 2006 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 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02110-1301 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
63 cur = dc_get_stacked_array(array_id);
64 while (cur != NULL && cur->Index < Index){
65 prev = cur;
66 cur = cur->next;
67 }
68 if (cur != NULL && cur->Index == Index){
69 if (cur->value.dc_type == DC_NUMBER)
70 dc_free_num(&cur->value.v.number);
71 else if (cur->value.dc_type == DC_STRING)
72 dc_free_str(&cur->value.v.string);
73 else
74 dc_garbage(" in array", array_id);
75 cur->value = value;
76 }else{
77 struct dc_array *newentry = dc_malloc(sizeof *newentry);
78 newentry->Index = Index;
79 newentry->value = value;
80 newentry->next = cur;
81 if (prev != NULL)
82 prev->next = newentry;
83 else
84 dc_set_stacked_array(array_id, newentry);
85 }
86 }
87
88 /* retrieve a dup of a value from array_id[Index] */
89 /* A zero value is returned if the specified value is unintialized. */
90 dc_data
91 dc_array_get DC_DECLARG((array_id, Index))
92 int array_id DC_DECLSEP
93 int Index DC_DECLEND
94 {
95 struct dc_array *cur = dc_get_stacked_array(array_id);
96
97 while (cur != NULL && cur->Index < Index)
98 cur = cur->next;
99 if (cur !=NULL && 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!=NULL; 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 }
123
124
125 /*
126 * Local Variables:
127 * mode: C
128 * tab-width: 4
129 * End:
130 * vi: set ts=4 :
131 */