1 |
/* $Id: dd.c,v 1.2 2003/07/23 17:54:35 edhill Exp $ */ |
2 |
#include <stdio.h> |
3 |
#include <stdlib.h> |
4 |
#include <string.h> |
5 |
#include "GLOBALS.h" |
6 |
#include "DD.h" |
7 |
|
8 |
/*==================================================================== |
9 |
Package of routines for managing a table of names and associated |
10 |
parameters. Used as tool for manipulating the data dictionary |
11 |
associated with a list of program variables. |
12 |
Data dictionary contains symbol names, certain symbol attributes |
13 |
and pointers to a definition for that name. |
14 |
======================================================================*/ |
15 |
|
16 |
#define ddBLOCK 100 /* Dictionary. Initial size ddBLOCK. Grown in */ |
17 |
ddRecord *dd=NULL; /* Initial dictionary pointer. */ |
18 |
ddRecord *ddTmp=NULL; |
19 |
int ddSize = 0; /* Size of table. */ |
20 |
int ddNused = 0; /* No. slots used in table. */ |
21 |
int ddCurrent = 0; /* Current record. */ |
22 |
int ddKey = 0; /* Identifier key. */ |
23 |
|
24 |
char *ddkey(); |
25 |
|
26 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ |
27 |
ddRecord *ddAdd( rec ) |
28 |
/* Add record at end of dictionary */ |
29 |
ddRecord *rec; |
30 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ |
31 |
{ |
32 |
/* On first call create one block dd */ |
33 |
/* use malloc so we can realloc later */ |
34 |
if ( dd == NULL ) { |
35 |
dd = (ddRecord *)malloc(sizeof(ddRecord)*ddBLOCK); |
36 |
if ( dd == NULL ) return((ddRecord *)NULL); |
37 |
ddSize = ddBLOCK; |
38 |
} |
39 |
|
40 |
/* No name so we can't insert */ |
41 |
if ( rec->name == NULL ) return ((ddRecord *)NULL); |
42 |
|
43 |
/* Allocate more storage - if it is needed. */ |
44 |
if ( ddCurrent == ddSize ) { |
45 |
/* Allocate another block of dd space. */ |
46 |
ddTmp=dd; |
47 |
dd = (ddRecord *)realloc(dd,sizeof(*dd)*ddBLOCK+sizeof(*dd)*ddSize); |
48 |
if ( dd == NULL ) { |
49 |
dd=ddTmp; |
50 |
return((ddRecord *)NULL); |
51 |
} |
52 |
ddSize = ddSize+ddBLOCK; |
53 |
} |
54 |
|
55 |
/* Now set values that are not NULL */ |
56 |
dd[ddCurrent].name = strdup(rec->name); |
57 |
|
58 |
/* CNH Debug starts */ |
59 |
printf("ddAdd name=\"%s\", rec. no. = %d\n",rec->name,ddKey+1); |
60 |
|
61 |
/* CNH Debug ends */ |
62 |
|
63 |
|
64 |
if ( dd[ddCurrent].name == NULL ) return((ddRecord *)NULL); |
65 |
dd[ddCurrent].id = ddKey+1; |
66 |
dd[ddCurrent].key = strdup(ddkey(ddKey+1)); |
67 |
if ( rec->hrefEntry != NULL ){ |
68 |
dd[ddCurrent].hrefEntry = strdup(rec->hrefEntry); |
69 |
if ( dd[ddCurrent].hrefEntry == NULL ) return((ddRecord *)NULL); |
70 |
} else { |
71 |
dd[ddCurrent].hrefEntry = NULL; |
72 |
} |
73 |
|
74 |
if ( rec->textEntry != NULL ){ |
75 |
dd[ddCurrent].textEntry = strdup(rec->textEntry); |
76 |
if ( dd[ddCurrent].textEntry == NULL ) return((ddRecord *)NULL); |
77 |
} else { |
78 |
dd[ddCurrent].textEntry = NULL; |
79 |
} |
80 |
|
81 |
if ( rec->footNotesEntry != NULL ){ |
82 |
dd[ddCurrent].footNotesEntry = strdup(rec->footNotesEntry); |
83 |
if ( dd[ddCurrent].footNotesEntry == NULL ) return((ddRecord *)NULL); |
84 |
} else { |
85 |
dd[ddCurrent].footNotesEntry = NULL; |
86 |
} |
87 |
|
88 |
if ( rec->unitsEntry != NULL ){ |
89 |
dd[ddCurrent].unitsEntry = strdup(rec->unitsEntry); |
90 |
if ( dd[ddCurrent].unitsEntry == NULL ) return((ddRecord *)NULL); |
91 |
} else { |
92 |
dd[ddCurrent].unitsEntry = NULL; |
93 |
} |
94 |
|
95 |
dd[ddCurrent].active = rec->active; |
96 |
|
97 |
/* Set to not a namelist member by default */ |
98 |
dd[ddCurrent].isInNameList = 0; |
99 |
/* Set to not an ifdef entry by default */ |
100 |
dd[ddCurrent].isInIfdef = 0; |
101 |
/* Set to not a procedure name by default */ |
102 |
dd[ddCurrent].isProcName = 0; |
103 |
|
104 |
/* Move current record pointer forward */ |
105 |
if ( ddCurrent == ddNused ){ |
106 |
++ddNused; |
107 |
} |
108 |
++ddCurrent; ++ddKey; |
109 |
return(dd+(ddCurrent-1)); |
110 |
} |
111 |
|
112 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ |
113 |
static int ddCompar( a, b ) |
114 |
/* DD entry comparison routine. Used by ddSort */ |
115 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ |
116 |
ddRecord *a; |
117 |
ddRecord *b; |
118 |
{ |
119 |
int rc; |
120 |
rc = strcasecmp(a->name,b->name); |
121 |
return(rc); |
122 |
} |
123 |
|
124 |
int ddSort() |
125 |
/* Sort the DD table */ |
126 |
{ |
127 |
int ddElSize; |
128 |
/* Sort the table */ |
129 |
ddElSize = sizeof(*dd); |
130 |
qsort(dd,ddNused,ddElSize,ddCompar); |
131 |
ddCurrent=0; |
132 |
} |
133 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ |
134 |
int ddSetCurrent(n) |
135 |
/* Set current DD record */ |
136 |
int n; |
137 |
{ |
138 |
ddCurrent = n; |
139 |
} |
140 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ |
141 |
|
142 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ |
143 |
int ddGetCurrent(rec) |
144 |
/* Return current DD record */ |
145 |
ddRecord **rec; |
146 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ |
147 |
{ |
148 |
/* Return if at or past end of table */ |
149 |
if ( ddCurrent == ddNused ) { |
150 |
return(0); |
151 |
} |
152 |
|
153 |
*rec = &dd[ddCurrent]; |
154 |
++ddCurrent; |
155 |
return(dd[ddCurrent-1].id); |
156 |
} |
157 |
|
158 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ |
159 |
ddRecord *ddFind(rec) |
160 |
/* Return current DD record */ |
161 |
/* Usage: |
162 |
Routine can be called with ddRecord or with NULL. If record "rec" |
163 |
is not NULL search starts from top. If record is NULL search |
164 |
starts from where previous search finished. |
165 |
*/ |
166 |
ddRecord *rec; |
167 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ |
168 |
{ |
169 |
static ddRecord *curRec = NULL; |
170 |
static int curInd = 0; |
171 |
|
172 |
/* Conditions under which we know nothing can be found. */ |
173 |
if ( curRec == NULL && rec == NULL ) return((ddRecord *)NULL); |
174 |
if ( rec != NULL && rec->name == NULL ) return((ddRecord *)NULL); |
175 |
if ( rec == NULL && curInd >= ddNused-1 ) return((ddRecord *)NULL); |
176 |
|
177 |
/* It is worth looking */ |
178 |
/* Start new search */ |
179 |
if ( rec != NULL ) { curInd = 0; curRec = rec; } |
180 |
/* Do search */ |
181 |
while ( curInd < ddNused ) { |
182 |
if ( strcmp(curRec->name,(dd[curInd]).name) == 0 ) { |
183 |
return(&(dd[curInd])); |
184 |
} |
185 |
++curInd; |
186 |
} |
187 |
/* Nothing found */ |
188 |
return((ddRecord *)NULL); |
189 |
} |
190 |
char *ddkey(n) |
191 |
int n; |
192 |
{ |
193 |
return(base36(n)); |
194 |
} |
195 |
|
196 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ |
197 |
void ddPrint() |
198 |
/* Prints DD table to standard out */ |
199 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ |
200 |
{ |
201 |
int curInd = 0; |
202 |
|
203 |
curInd = 0; |
204 |
/* Do print */ |
205 |
while ( curInd < ddNused ) { |
206 |
printf("DD Record No. %d == \"%s\"", curInd, (dd[curInd]).name); |
207 |
printf(", NL = %d", (dd[curInd]).isInNameList); |
208 |
printf(", IFDEF = %d", (dd[curInd]).isInIfdef); |
209 |
printf(", PROC = %d", (dd[curInd]).isProcName); |
210 |
printf("\n"); |
211 |
++curInd; |
212 |
} |
213 |
/* Nothing found */ |
214 |
return; |
215 |
} |
216 |
|
217 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ |
218 |
ddRecord *ddSetIsInNamelist( rec ) |
219 |
/* Tags dd entry used in NAMELIST */ |
220 |
ddRecord *rec; |
221 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ |
222 |
{ |
223 |
rec->isInNameList=1; |
224 |
} |
225 |
|
226 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ |
227 |
ddRecord *ddSetIsInIfdef( rec ) |
228 |
/* Tags dd entry used in ifdef */ |
229 |
ddRecord *rec; |
230 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ |
231 |
{ |
232 |
rec->isInIfdef=1; |
233 |
} |
234 |
|
235 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ |
236 |
ddRecord *ddSetIsProcName( rec ) |
237 |
/* Tags dd entry used in ifdef */ |
238 |
ddRecord *rec; |
239 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ |
240 |
{ |
241 |
rec->isProcName=1; |
242 |
} |