-
Notifications
You must be signed in to change notification settings - Fork 3
/
storage.c
309 lines (256 loc) · 8.31 KB
/
storage.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include <string.h>
#include "storage.h"
#include "utcommon.h"
#define DEBUG 0
#define INBUF_SIZE 8192
#define ITEM_CODE_LEN 16
#define forever 1
/*====================================================================*/
/* FUNCTION: itemComp */
/*--------------------------------------------------------------------*/
static int
itemComp (const void *item1, const void *item2)
{
return strcmp (((struct _itemLookup *) item1)->itemName,
((struct _itemLookup *) item2)->itemName);
}
/*====================================================================*/
/* FUNCTION: initItemLookup */
/*--------------------------------------------------------------------*/
int
initItemLookup ()
{
static int initialized = 0;
int tags;
int items;
if (!initialized)
{
initialized = 1;
qsort ((char *) itemLookup, NUM_ITEMS, sizeof (struct _itemLookup),
itemComp);
memset ((void *) &bibDoc, '\0', sizeof (bibDoc));
for (tags = 0; tags < NUM_ITEMS; tags++)
{
for (items = 0; items < MAX_DOC_ITEMS; items++)
{
bibDoc.tag[tags].data[items] = (char *) malloc (1);
}
}
}
else
{
for (tags = 0; tags < NUM_ITEMS; tags++)
{
bibDoc.tag[tags].count = bibDoc.tag[tags].retrieved = 0;
}
}
return NUM_ITEMS;
}
/*====================================================================*/
/* FUNCTION: lookupItem */
/*--------------------------------------------------------------------*/
enum ITEM_VALS
lookupItem (const char *itemName)
{
struct _itemLookup srchItem;
struct _itemLookup *dataItem;
srchItem.itemName = strdup (itemName);
/* fprintf(stderr, "Looking up TAG--->%s\n", itemName); */
dataItem = (struct item *) bsearch ((void *) &srchItem, (void *)
itemLookup,
NUM_ITEMS,
sizeof (srchItem),
itemComp);
free (srchItem.itemName);
if (dataItem)
return dataItem->itemVal;
else
return UNKNOWN_ITEM;
}
/*====================================================================*/
/* FUNCTION: storeItemData */
/*--------------------------------------------------------------------*/
enum ERR_CODES
storeItemData (enum ITEM_VALS type, char *itemText)
{
int itemTextLen; /* how much do we have to store? */
int tagIdx; /* shortcut to the member element */
int requiredTextSize; /* how big do we make it? */
#if DEBUG == 1
fprintf (stderr, "Came into STOREITEMDATA with ITEMTEXT-->%s\n",
itemText);
#endif
if (itemText == NULL)
return NO_DATA;
if ((itemTextLen = strlen (itemText)) == 0)
return NO_DATA;
if ((tagIdx = bibDoc.tag[type].count) >= MAX_DOC_ITEMS)
return FAILURE;
/* check to see if we have room to store this? */
#if DEBUG == 1
fprintf (stderr, "itemTextLen:%d space available:%d tagIdx:%d\n",
itemTextLen, bibDoc.tag[type].size[tagIdx], tagIdx);
#endif
if (itemTextLen >= bibDoc.tag[type].size[tagIdx])
{
/* no, allocate the space */
requiredTextSize = (itemTextLen + 1 +(itemTextLen >> 1));
#if DEBUG == 1
fprintf (stderr, "requiredTextSize:%d\n", requiredTextSize);
#endif
bibDoc.tag[type].data[tagIdx] =
(char *) realloc (bibDoc.tag[type].data[tagIdx], requiredTextSize);
bibDoc.tag[type].size[tagIdx] = requiredTextSize;
}
#if DEBUG == 1
fprintf (stderr, "ITEMTEXT Contains--->%s\n", itemText);
#endif
strcpy (bibDoc.tag[type].data[tagIdx], itemText);
bibDoc.tag[type].count++;
return SUCCESS;
}
/*====================================================================*/
/* FUNCTION: getItemCount */
/*--------------------------------------------------------------------*/
int
getItemCount (enum ITEM_VALS type)
{
return bibDoc.tag[type].count;
}
/*====================================================================*/
/* FUNCTION: getItemData */
/*--------------------------------------------------------------------*/
char *
getItemData (enum ITEM_VALS type)
{
int i;
if (bibDoc.tag[type].count == 0)
{
bibDoc.tag[type].retrieved = 0;
return NULL;
}
bibDoc.tag[type].count--;
i = bibDoc.tag[type].retrieved;
bibDoc.tag[type].retrieved++;
return bibDoc.tag[type].data[i];
}
/*====================================================================*/
/* FUNCTION: lookupItemName */
/*--------------------------------------------------------------------*/
char *
lookupItemName (enum ITEM_VALS type)
{
int loopVar;
for (loopVar = 0; loopVar < NUM_ITEMS; loopVar++)
if (itemLookup[loopVar].itemVal == type)
return itemLookup[loopVar].itemName;
return "No Name Found";
}
/*====================================================================*/
/* FUNCTION: readIPSDoc */
/*--------------------------------------------------------------------*/
enum ERR_CODES
readIPSDoc (FILE * inFP, long *bytes, int error_on)
{
static char inBuf[INBUF_SIZE];
static long lineCount = 0;
long begptr, endptr;
char *itemText;
char *ptr;
char endtag[30];
int endlen;
enum ITEM_VALS itemVal;
begptr = ftell (inFP);
do
{
if (fgets (inBuf, INBUF_SIZE, inFP) == NULL)
{
*bytes = 0;
return NO_DATA;
}
lineCount++;
if (inBuf[0] == '\x1e')
{
endptr = ftell (inFP);
*bytes = (endptr - begptr) + 1;
return SUCCESS;
}
if ((ptr = strchr (inBuf, '>')) == NULL)
{
fprintf (stderr, "(storage.c)Missing '>' on tag (readIPSDoc)\n");
exit (EXIT_FAILURE);
}
else
*ptr = '\0';
if ((itemVal = lookupItem (&inBuf[1])) == UNKNOWN_ITEM)
{
if (error_on)
fprintf (stderr,
"(storage) Add \"%s\" to item_defs.h, no data saved\n",
&inBuf[1]);
}
else
{
memset (endtag, '\0', sizeof (endtag));
sprintf (endtag, "</%s>", &inBuf[1]); /* create the end
tag */
endlen = strlen (endtag);
itemText = ++ptr; /* point past the null */
if ((ptr = strstr (itemText, endtag)) != NULL)
*ptr = '\0';
else if ((ptr = strchr (itemText, '\n')) != NULL)
*ptr = '\0';
if (error_on)
fprintf (stderr, "Storing \"%s\" for THE item \"%s\"\n",
itemText, &inBuf[1]);
/* here we do special things to individual tag values */
switch (storeItemData (itemVal, itemText))
{
case SUCCESS:
break;
case FAILURE:
fprintf (stderr,
"Too many Data items for \"%s\""
", change MAX_DOC_ITEMS in storage.h\n", &inBuf[1]);
exit (EXIT_FAILURE);
break;
case NO_DATA:
if (error_on)
fprintf(stderr, "No data for \"%s\" (%ld)\n",
&inBuf[1], lineCount);
break;
}
}
}
while (1);
*bytes = 0;
return NO_DATA;
}
/*====================================================================*/
/* FUNCTION: dumpStorage */
/*--------------------------------------------------------------------*/
void
dumpStorage (enum ITEM_VALS type)
{
int i;
fprintf (stderr, "bibDoc.tag[%s].count == %d\n",
lookupItemName (type), bibDoc.tag[type].count);
for (i = 0; i < bibDoc.tag[type].count; i++)
fprintf (stderr, "%d %s\n", i, bibDoc.tag[type].data[i]);
}
/*====================================================================*/
/* FUNCTION: getItemName */
/*--------------------------------------------------------------------*/
char *
getItemName (enum ITEM_VALS type)
{
int i;
for (i = 0; i < MAX_DOC_ITEMS; i++)
{
if (itemLookup[i].itemVal == type)
{
return itemLookup[i].itemName;
}
}
return "UnknownItem";
}