-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbsave.c
634 lines (534 loc) · 20.1 KB
/
bsave.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
/*******************************************************/
/* "C" Language Integrated Production System */
/* */
/* CLIPS Version 6.40 10/04/17 */
/* */
/* BSAVE MODULE */
/*******************************************************/
/*************************************************************/
/* Purpose: Provides core routines for saving constructs to */
/* a binary file. */
/* */
/* Principal Programmer(s): */
/* Gary D. Riley */
/* Brian L. Dantes */
/* */
/* Contributing Programmer(s): */
/* */
/* Revision History: */
/* */
/* 6.24: Renamed BOOLEAN macro type to intBool. */
/* */
/* Added environment parameter to GenClose. */
/* Added environment parameter to GenOpen. */
/* */
/* 6.30: Changed integer type/precision. */
/* */
/* Used genstrncpy instead of strncpy. */
/* */
/* Borland C (IBM_TBC) and Metrowerks CodeWarrior */
/* (MAC_MCW, IBM_MCW) are no longer supported. */
/* */
/* Added const qualifiers to remove C++ */
/* deprecation warnings. */
/* */
/* Converted API macros to function calls. */
/* */
/* 6.31: Data sizes written to binary files for */
/* validation when loaded. */
/* */
/* 6.40: Pragma once and other inclusion changes. */
/* */
/* Added support for booleans with <stdbool.h>. */
/* */
/* Removed use of void pointers for specific */
/* data structures. */
/* */
/* ALLOW_ENVIRONMENT_GLOBALS no longer supported. */
/* */
/* UDF redesign. */
/* */
/*************************************************************/
#include "setup.h"
#include "argacces.h"
#include "bload.h"
#include "cstrnbin.h"
#include "envrnmnt.h"
#include "exprnpsr.h"
#include "memalloc.h"
#include "moduldef.h"
#include "prntutil.h"
#include "router.h"
#include "symblbin.h"
#include "bsave.h"
/***************************************/
/* LOCAL INTERNAL FUNCTION DEFINITIONS */
/***************************************/
#if BLOAD_AND_BSAVE
static void FindNeededItems(Environment *);
static void InitializeFunctionNeededFlags(Environment *);
static void WriteNeededFunctions(Environment *,FILE *);
static size_t FunctionBinarySize(Environment *);
static void WriteBinaryHeader(Environment *,FILE *);
static void WriteBinaryFooter(Environment *,FILE *);
#endif
static void DeallocateBsaveData(Environment *);
/**********************************************/
/* InitializeBsaveData: Allocates environment */
/* data for the bsave command. */
/**********************************************/
void InitializeBsaveData(
Environment *theEnv)
{
AllocateEnvironmentData(theEnv,BSAVE_DATA,sizeof(struct bsaveData),DeallocateBsaveData);
}
/************************************************/
/* DeallocateBsaveData: Deallocates environment */
/* data for the bsave command. */
/************************************************/
static void DeallocateBsaveData(
Environment *theEnv)
{
struct BinaryItem *tmpPtr, *nextPtr;
tmpPtr = BsaveData(theEnv)->ListOfBinaryItems;
while (tmpPtr != NULL)
{
nextPtr = tmpPtr->next;
rtn_struct(theEnv,BinaryItem,tmpPtr);
tmpPtr = nextPtr;
}
}
/**************************************/
/* BsaveCommand: H/L access routine */
/* for the bsave command. */
/**************************************/
void BsaveCommand(
Environment *theEnv,
UDFContext *context,
UDFValue *returnValue)
{
#if (! RUN_TIME) && BLOAD_AND_BSAVE
const char *fileName;
fileName = GetFileName(context);
if (fileName != NULL)
{
if (Bsave(theEnv,fileName))
{
returnValue->lexemeValue = TrueSymbol(theEnv);
return;
}
}
#else
#if MAC_XCD
#pragma unused(theEnv,context)
#endif
#endif
returnValue->lexemeValue = FalseSymbol(theEnv);
}
#if BLOAD_AND_BSAVE
/****************************/
/* Bsave: C access routine */
/* for the bsave command. */
/****************************/
bool Bsave(
Environment *theEnv,
const char *fileName)
{
FILE *fp;
struct BinaryItem *biPtr;
char constructBuffer[CONSTRUCT_HEADER_SIZE];
unsigned long saveExpressionCount;
/*=====================================*/
/* If embedded, clear the error flags. */
/*=====================================*/
if (EvaluationData(theEnv)->CurrentExpression == NULL)
{ ResetErrorFlags(theEnv); }
/*===================================*/
/* A bsave can't occur when a binary */
/* image is already loaded. */
/*===================================*/
if (Bloaded(theEnv))
{
PrintErrorID(theEnv,"BSAVE",1,false);
WriteString(theEnv,STDERR,
"Cannot perform a binary save while a binary load is in effect.\n");
return false;
}
/*================*/
/* Open the file. */
/*================*/
if ((fp = GenOpen(theEnv,fileName,"wb")) == NULL)
{
OpenErrorMessage(theEnv,"bsave",fileName);
return false;
}
/*==============================*/
/* Remember the current module. */
/*==============================*/
SaveCurrentModule(theEnv);
/*==================================*/
/* Write binary header to the file. */
/*==================================*/
WriteBinaryHeader(theEnv,fp);
/*===========================================*/
/* Initialize count variables, index values, */
/* and determine some of the data structures */
/* which need to be saved. */
/*===========================================*/
ExpressionData(theEnv)->ExpressionCount = 0;
InitializeFunctionNeededFlags(theEnv);
InitAtomicValueNeededFlags(theEnv);
FindHashedExpressions(theEnv);
FindNeededItems(theEnv);
SetAtomicValueIndices(theEnv,false);
/*===============================*/
/* Save the functions and atoms. */
/*===============================*/
WriteNeededFunctions(theEnv,fp);
WriteNeededAtomicValues(theEnv,fp);
/*=========================================*/
/* Write out the number of expression data */
/* structures in the binary image. */
/*=========================================*/
GenWrite(&ExpressionData(theEnv)->ExpressionCount,sizeof(unsigned long),fp);
/*===========================================*/
/* Save the numbers indicating the amount of */
/* memory needed to bload the constructs. */
/*===========================================*/
for (biPtr = BsaveData(theEnv)->ListOfBinaryItems;
biPtr != NULL;
biPtr = biPtr->next)
{
if (biPtr->bsaveStorageFunction != NULL)
{
genstrncpy(constructBuffer,biPtr->name,CONSTRUCT_HEADER_SIZE);
GenWrite(constructBuffer,CONSTRUCT_HEADER_SIZE,fp);
(*biPtr->bsaveStorageFunction)(theEnv,fp);
}
}
/*====================================*/
/* Write a binary footer to the file. */
/*====================================*/
WriteBinaryFooter(theEnv,fp);
/*===================*/
/* Save expressions. */
/*===================*/
ExpressionData(theEnv)->ExpressionCount = 0;
BsaveHashedExpressions(theEnv,fp);
saveExpressionCount = ExpressionData(theEnv)->ExpressionCount;
BsaveConstructExpressions(theEnv,fp);
ExpressionData(theEnv)->ExpressionCount = saveExpressionCount;
/*===================*/
/* Save constraints. */
/*===================*/
WriteNeededConstraints(theEnv,fp);
/*==================*/
/* Save constructs. */
/*==================*/
for (biPtr = BsaveData(theEnv)->ListOfBinaryItems;
biPtr != NULL;
biPtr = biPtr->next)
{
if (biPtr->bsaveFunction != NULL)
{
genstrncpy(constructBuffer,biPtr->name,CONSTRUCT_HEADER_SIZE);
GenWrite(constructBuffer,CONSTRUCT_HEADER_SIZE,fp);
(*biPtr->bsaveFunction)(theEnv,fp);
}
}
/*===================================*/
/* Save a binary footer to the file. */
/*===================================*/
WriteBinaryFooter(theEnv,fp);
/*===========*/
/* Clean up. */
/*===========*/
RestoreAtomicValueBuckets(theEnv);
/*=================*/
/* Close the file. */
/*=================*/
GenClose(theEnv,fp);
/*=============================*/
/* Restore the current module. */
/*=============================*/
RestoreCurrentModule(theEnv);
/*==================================*/
/* Return true to indicate success. */
/*==================================*/
return true;
}
/*********************************************/
/* InitializeFunctionNeededFlags: Marks each */
/* function in the list of functions as */
/* being unneeded by this binary image. */
/*********************************************/
static void InitializeFunctionNeededFlags(
Environment *theEnv)
{
struct functionDefinition *functionList;
for (functionList = GetFunctionList(theEnv);
functionList != NULL;
functionList = functionList->next)
{ functionList->neededFunction = false; }
}
/**********************************************************/
/* FindNeededItems: Searches through the constructs for */
/* the functions, constraints, or atoms that are needed */
/* by that construct. This routine also counts the */
/* number of expressions in use (through a global). */
/**********************************************************/
static void FindNeededItems(
Environment *theEnv)
{
struct BinaryItem *biPtr;
for (biPtr = BsaveData(theEnv)->ListOfBinaryItems;
biPtr != NULL;
biPtr = biPtr->next)
{ if (biPtr->findFunction != NULL) (*biPtr->findFunction)(theEnv); }
}
/****************************************************/
/* WriteNeededFunctions: Writes the names of needed */
/* functions to the binary save file. */
/****************************************************/
static void WriteNeededFunctions(
Environment *theEnv,
FILE *fp)
{
unsigned long count = 0;
size_t space, length;
struct functionDefinition *functionList;
/*================================================*/
/* Assign each function an index if it is needed. */
/*================================================*/
for (functionList = GetFunctionList(theEnv);
functionList != NULL;
functionList = functionList->next)
{
if (functionList->neededFunction)
{ functionList->bsaveIndex = count++; }
else
{ functionList->bsaveIndex = ULONG_MAX; }
}
/*===================================================*/
/* Write the number of function names to be written. */
/*===================================================*/
GenWrite(&count,sizeof(unsigned long),fp);
if (count == 0)
{
GenWrite(&count,sizeof(unsigned long),fp);
return;
}
/*================================*/
/* Determine the amount of space */
/* needed for the function names. */
/*================================*/
space = FunctionBinarySize(theEnv);
GenWrite(&space,sizeof(unsigned long),fp);
/*===============================*/
/* Write out the function names. */
/*===============================*/
for (functionList = GetFunctionList(theEnv);
functionList != NULL;
functionList = functionList->next)
{
if (functionList->neededFunction)
{
length = strlen(functionList->callFunctionName->contents) + 1;
GenWrite((void *) functionList->callFunctionName->contents,length,fp);
}
}
}
/*********************************************/
/* FunctionBinarySize: Determines the number */
/* of bytes needed to save all of the */
/* function names in the binary save file. */
/*********************************************/
static size_t FunctionBinarySize(
Environment *theEnv)
{
size_t size = 0;
struct functionDefinition *functionList;
for (functionList = GetFunctionList(theEnv);
functionList != NULL;
functionList = functionList->next)
{
if (functionList->neededFunction)
{ size += strlen(functionList->callFunctionName->contents) + 1; }
}
return(size);
}
/***************************************************/
/* SaveBloadCount: Used to save the data structure */
/* count values when a binary save command is */
/* issued when a binary image is loaded. */
/***************************************************/
void SaveBloadCount(
Environment *theEnv,
unsigned long cnt)
{
BLOADCNTSV *tmp, *prv;
tmp = get_struct(theEnv,bloadcntsv);
tmp->val = cnt;
tmp->nxt = NULL;
if (BsaveData(theEnv)->BloadCountSaveTop == NULL)
{ BsaveData(theEnv)->BloadCountSaveTop = tmp; }
else
{
prv = BsaveData(theEnv)->BloadCountSaveTop;
while (prv->nxt != NULL)
{ prv = prv->nxt; }
prv->nxt = tmp;
}
}
/**************************************************/
/* RestoreBloadCount: Restores the data structure */
/* count values after a binary save command is */
/* completed when a binary image is loaded. */
/**************************************************/
void RestoreBloadCount(
Environment *theEnv,
unsigned long *cnt)
{
BLOADCNTSV *tmp;
*cnt = BsaveData(theEnv)->BloadCountSaveTop->val;
tmp = BsaveData(theEnv)->BloadCountSaveTop;
BsaveData(theEnv)->BloadCountSaveTop = BsaveData(theEnv)->BloadCountSaveTop->nxt;
rtn_struct(theEnv,bloadcntsv,tmp);
}
/**********************************************/
/* MarkNeededItems: Examines an expression to */
/* determine which items are needed to save */
/* an expression as part of a binary image. */
/**********************************************/
void MarkNeededItems(
Environment *theEnv,
struct expr *testPtr)
{
while (testPtr != NULL)
{
switch (testPtr->type)
{
case SYMBOL_TYPE:
case STRING_TYPE:
case GBL_VARIABLE:
case INSTANCE_NAME_TYPE:
testPtr->lexemeValue->neededSymbol = true;
break;
case FLOAT_TYPE:
testPtr->floatValue->neededFloat = true;
break;
case INTEGER_TYPE:
testPtr->integerValue->neededInteger = true;
break;
case FCALL:
testPtr->functionValue->neededFunction = true;
break;
case VOID_TYPE:
break;
default:
if (EvaluationData(theEnv)->PrimitivesArray[testPtr->type] == NULL) break;
if (EvaluationData(theEnv)->PrimitivesArray[testPtr->type]->bitMap)
{ ((CLIPSBitMap *) testPtr->value)->neededBitMap = true; }
break;
}
if (testPtr->argList != NULL)
{ MarkNeededItems(theEnv,testPtr->argList); }
testPtr = testPtr->nextArg;
}
}
/******************************************************/
/* WriteBinaryHeader: Writes a binary header used for */
/* verification when a binary image is loaded. */
/******************************************************/
static void WriteBinaryHeader(
Environment *theEnv,
FILE *fp)
{
GenWrite((void *) BloadData(theEnv)->BinaryPrefixID,strlen(BloadData(theEnv)->BinaryPrefixID) + 1,fp);
GenWrite((void *) BloadData(theEnv)->BinaryVersionID,strlen(BloadData(theEnv)->BinaryVersionID) + 1,fp);
GenWrite((void *) BloadData(theEnv)->BinarySizes,strlen(BloadData(theEnv)->BinarySizes) + 1,fp);
}
/******************************************************/
/* WriteBinaryFooter: Writes a binary footer used for */
/* verification when a binary image is loaded. */
/******************************************************/
static void WriteBinaryFooter(
Environment *theEnv,
FILE *fp)
{
char footerBuffer[CONSTRUCT_HEADER_SIZE];
genstrncpy(footerBuffer,BloadData(theEnv)->BinaryPrefixID,CONSTRUCT_HEADER_SIZE);
GenWrite(footerBuffer,CONSTRUCT_HEADER_SIZE,fp);
}
#endif /* BLOAD_AND_BSAVE */
#if BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE
/**********************************************************/
/* AddBinaryItem: Informs the bload/bsave commands of the */
/* appropriate access functions needed to save/load the */
/* data structures of a construct or other "item" to a */
/* binary file. */
/**********************************************************/
bool AddBinaryItem(
Environment *theEnv,
const char *name,
int priority,
void (*findFunction)(Environment *),
void (*expressionFunction)(Environment *,FILE *),
void (*bsaveStorageFunction)(Environment *,FILE *),
void (*bsaveFunction)(Environment *,FILE *),
void (*bloadStorageFunction)(Environment *),
void (*bloadFunction)(Environment *),
void (*clearFunction)(Environment *))
{
struct BinaryItem *newPtr, *currentPtr, *lastPtr = NULL;
/*========================================*/
/* Create the binary item data structure. */
/*========================================*/
newPtr = get_struct(theEnv,BinaryItem);
newPtr->name = name;
newPtr->findFunction = findFunction;
newPtr->expressionFunction = expressionFunction;
newPtr->bsaveStorageFunction = bsaveStorageFunction;
newPtr->bsaveFunction = bsaveFunction;
newPtr->bloadStorageFunction = bloadStorageFunction;
newPtr->bloadFunction = bloadFunction;
newPtr->clearFunction = clearFunction;
newPtr->priority = priority;
/*=================================*/
/* If no binary items are defined, */
/* just put the item on the list. */
/*=================================*/
if (BsaveData(theEnv)->ListOfBinaryItems == NULL)
{
newPtr->next = NULL;
BsaveData(theEnv)->ListOfBinaryItems = newPtr;
return true;
}
/*=========================================*/
/* Otherwise, place the binary item at the */
/* appropriate place in the list of binary */
/* items based on its priority. */
/*=========================================*/
currentPtr = BsaveData(theEnv)->ListOfBinaryItems;
while ((currentPtr != NULL) ? (priority < currentPtr->priority) : false)
{
lastPtr = currentPtr;
currentPtr = currentPtr->next;
}
if (lastPtr == NULL)
{
newPtr->next = BsaveData(theEnv)->ListOfBinaryItems;
BsaveData(theEnv)->ListOfBinaryItems = newPtr;
}
else
{
newPtr->next = currentPtr;
lastPtr->next = newPtr;
}
/*==================================*/
/* Return true to indicate the item */
/* was successfully added. */
/*==================================*/
return true;
}
#endif /* BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE */