📄 bsave.c
字号:
/*******************************************************/ /* "C" Language Integrated Production System */ /* */ /* CLIPS Version 6.05 04/09/97 */ /* */ /* BSAVE MODULE */ /*******************************************************//*************************************************************//* Purpose: Provides core routines for saving constructs to *//* a binary file. *//* *//* Principal Programmer(s): *//* Gary D. Riley *//* Brian L. Donnell *//* *//* Contributing Programmer(s): *//* *//* Revision History: *//* *//*************************************************************/#define _BSAVE_SOURCE_#include "setup.h"#include "clipsmem.h"#include "exprnpsr.h"#include "argacces.h"#include "router.h"#include "cstrnbin.h"#include "moduldef.h"#include "symblbin.h"#include "bload.h"#include "bsave.h"/*******************//* DATA STRUCTURES *//*******************/#if BLOAD_AND_BSAVEtypedef struct bloadcntsv { long val; struct bloadcntsv *nxt; } BLOADCNTSV; #endif /***************************************//* LOCAL INTERNAL FUNCTION DEFINITIONS *//***************************************/#if ANSI_COMPILER#if BLOAD_AND_BSAVE static VOID FindNeededItems(void); static VOID InitializeFunctionNeededFlags(void); static VOID WriteNeededFunctions(FILE *); static unsigned long int FunctionBinarySize(void); static VOID WriteBinaryHeader(FILE *); static VOID WriteBinaryFooter(FILE *);#endif#else#if BLOAD_AND_BSAVE static VOID FindNeededItems(); static VOID InitializeFunctionNeededFlags(); static VOID WriteNeededFunctions(); static unsigned long int FunctionBinarySize(); static VOID WriteBinaryHeader(); static VOID WriteBinaryFooter();#endif#endif/****************************************//* GLOBAL INTERNAL VARIABLE DEFINITIONS *//****************************************/ globle struct BinaryItem *ListOfBinaryItems = NULL;/****************************************//* LOCAL INTERNAL VARIABLE DEFINITIONS *//****************************************/#if BLOAD_AND_BSAVE static BLOADCNTSV *BloadCountSaveTop = NULL;#endif/**************************************//* BsaveCommand: CLIPS access routine *//* for the bsave command. *//**************************************/globle int BsaveCommand() {#if (! RUN_TIME) && BLOAD_AND_BSAVE char *fileName; if (ArgCountCheck("bsave",EXACTLY,1) == -1) return(CLIPS_FALSE); fileName = GetFileName("bsave",1); if (fileName != NULL) { if (Bsave(fileName)) return(CLIPS_TRUE); }#endif return(CLIPS_FALSE); }#if BLOAD_AND_BSAVE/****************************//* Bsave: C access routine *//* for the bsave command. *//****************************/globle BOOLEAN Bsave(fileName) char *fileName; { FILE *fp; struct BinaryItem *biPtr; char constructBuffer[CONSTRUCT_HEADER_SIZE]; long saveExpressionCount; /*===================================*/ /* A bsave can't occur when a binary */ /* image is already loaded. */ /*===================================*/ if (Bloaded()) { PrintErrorID("BSAVE",1,CLIPS_FALSE); PrintCLIPS(WERROR, "Cannot perform a binary save while a binary load is in effect.\n"); return(0); } /*================*/ /* Open the file. */ /*================*/ if ((fp = fopen(fileName,"wb")) == NULL) { OpenErrorMessage("bsave",fileName); return(0); } /*==============================*/ /* Remember the current module. */ /*==============================*/ SaveCurrentModule(); /*==================================*/ /* Write binary header to the file. */ /*==================================*/ WriteBinaryHeader(fp); /*===========================================*/ /* Initialize count variables, index values, */ /* and determine some of the data structures */ /* which need to be saved. */ /*===========================================*/ ExpressionCount = 0; InitializeFunctionNeededFlags(); InitAtomicValueNeededFlags(); FindHashedExpressions(); FindNeededItems(); SetAtomicValueIndices(CLIPS_FALSE); /*===============================*/ /* Save the functions and atoms. */ /*===============================*/ WriteNeededFunctions(fp); WriteNeededAtomicValues(fp); /*=========================================*/ /* Write out the number of expression data */ /* structures in the binary image. */ /*=========================================*/ GenWrite((VOID *) &ExpressionCount,(unsigned long) sizeof(unsigned long),fp); /*===========================================*/ /* Save the numbers indicating the amount of */ /* memory needed to bload the constructs. */ /*===========================================*/ for (biPtr = ListOfBinaryItems; biPtr != NULL; biPtr = biPtr->next) { if (biPtr->bsaveStorageFunction != NULL) { strncpy(constructBuffer,biPtr->name,CONSTRUCT_HEADER_SIZE); GenWrite(constructBuffer,(unsigned long) CONSTRUCT_HEADER_SIZE,fp); (*biPtr->bsaveStorageFunction)(fp); } } /*====================================*/ /* Write a binary footer to the file. */ /*====================================*/ WriteBinaryFooter(fp); /*===================*/ /* Save expressions. */ /*===================*/ ExpressionCount = 0; BsaveHashedExpressions(fp); saveExpressionCount = ExpressionCount; BsaveConstructExpressions(fp); ExpressionCount = saveExpressionCount; /*===================*/ /* Save constraints. */ /*===================*/ WriteNeededConstraints(fp); /*==================*/ /* Save constructs. */ /*==================*/ for (biPtr = ListOfBinaryItems; biPtr != NULL; biPtr = biPtr->next) { if (biPtr->bsaveFunction != NULL) { strncpy(constructBuffer,biPtr->name,CONSTRUCT_HEADER_SIZE); GenWrite(constructBuffer,(unsigned long) CONSTRUCT_HEADER_SIZE,fp); (*biPtr->bsaveFunction)(fp); } } /*===================================*/ /* Save a binary footer to the file. */ /*===================================*/ WriteBinaryFooter(fp); /*===========*/ /* Clean up. */ /*===========*/ RestoreAtomicValueBuckets(); /*=================*/ /* Close the file. */ /*=================*/ fclose(fp); /*=============================*/ /* Restore the current module. */ /*=============================*/ RestoreCurrentModule(); /*========================================*/ /* Return CLIPS_TRUE to indicate success. */ /*========================================*/ return(CLIPS_TRUE); }/*********************************************//* InitializeFunctionNeededFlags: Marks each *//* function in the list of functions as *//* being unneeded by this binary image. *//*********************************************/static VOID InitializeFunctionNeededFlags() { struct FunctionDefinition *functionList; for (functionList = GetFunctionList(); functionList != NULL; functionList = functionList->next) { functionList->bsaveIndex = 0; } } /**********************************************************//* 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() { struct BinaryItem *biPtr; for (biPtr = ListOfBinaryItems; biPtr != NULL; biPtr = biPtr->next) { if (biPtr->findFunction != NULL) (*biPtr->findFunction)(); } }/****************************************************//* WriteNeededFunctions: Writes the names of needed *//* functions to the binary save file. *//****************************************************/static VOID WriteNeededFunctions(fp) FILE *fp; {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -