📄 bload.c
字号:
/*******************************************************/ /* "C" Language Integrated Production System */ /* */ /* CLIPS Version 6.05 04/09/97 */ /* */ /* BLOAD MODULE */ /*******************************************************//*************************************************************//* Purpose: Provides core routines for loading constructs *//* from a binary file. *//* *//* Principal Programmer(s): *//* Gary D. Riley *//* Brian L. Donnell *//* *//* Contributing Programmer(s): *//* *//* Revision History: *//* *//*************************************************************/#define _BLOAD_SOURCE_#include "setup.h"#include "clipsmem.h"#include "exprnpsr.h"#include "argacces.h"#include "router.h"#include "constrct.h"#include "bsave.h"#include "cstrnbin.h"#include "utility.h"#include "bload.h"#if (BLOAD || BLOAD_ONLY || BLOAD_AND_BSAVE)/***************************************//* LOCAL INTERNAL FUNCTION DEFINITIONS *//***************************************/#if ANSI_COMPILER static struct FunctionDefinition * HUGE_ADDR *ReadNeededFunctions(long *,int *); static struct FunctionDefinition *FastFindFunction(char *,struct FunctionDefinition *); static int ClearBload(void); static VOID AbortBload(void); static int BloadOutOfMemoryFunction(unsigned long);#else static struct FunctionDefinition * HUGE_ADDR *ReadNeededFunctions(); static struct FunctionDefinition *FastFindFunction(); static int ClearBload(); static VOID AbortBload(); static int BloadOutOfMemoryFunction();#endif/***************************************//* LOCAL INTERNAL VARIABLE DEFINITIONS *//***************************************/ static int BloadActive = CLIPS_FALSE; static struct callFunctionItem *BeforeBloadFunctions = NULL; static struct callFunctionItem *AfterBloadFunctions = NULL; static struct callFunctionItem *ClearBloadReadyFunctions = NULL; static struct callFunctionItem *AbortBloadFunctions = NULL;/****************************************//* GLOBAL INTERNAL VARIABLE DEFINITIONS *//****************************************/ globle char *BinaryPrefixID = "\1\2\3\4CLIPS"; globle char *BinaryVersionID = "V6.05"; globle struct FunctionDefinition * HUGE_ADDR *FunctionArray;/****************************//* Bload: C access routine *//* for the bload command. *//****************************/globle int Bload(fileName) char *fileName; { long numberOfFunctions; unsigned long space; int error; char IDbuffer[20]; char constructBuffer[CONSTRUCT_HEADER_SIZE]; struct BinaryItem *biPtr; struct callFunctionItem *bfPtr; /*================*/ /* Open the file. */ /*================*/ if (GenOpen("bload",fileName) == 0) return(CLIPS_FALSE); /*=====================================*/ /* Determine if this is a binary file. */ /*=====================================*/ GenRead(IDbuffer,(unsigned long) strlen(BinaryPrefixID) + 1); if (strcmp(IDbuffer,BinaryPrefixID) != 0) { PrintErrorID("BLOAD",2,CLIPS_FALSE); PrintCLIPS(WERROR,"File "); PrintCLIPS(WERROR,fileName); PrintCLIPS(WERROR," is not a binary construct file.\n"); GenClose(); return(CLIPS_FALSE); } /*===========================================*/ /* Determine if it's a binary file using a */ /* format from a different version of CLIPS. */ /*===========================================*/ GenRead(IDbuffer,(unsigned long) strlen(BinaryVersionID) + 1); if (strcmp(IDbuffer,BinaryVersionID) != 0) { PrintErrorID("BLOAD",3,CLIPS_FALSE); PrintCLIPS(WERROR,"File "); PrintCLIPS(WERROR,fileName); PrintCLIPS(WERROR," is an incompatible binary construct file.\n"); GenClose(); return(CLIPS_FALSE); } /*====================*/ /* Clear environment. */ /*====================*/ if (BloadActive) { if (ClearBload() == CLIPS_FALSE) { GenClose(); return(CLIPS_FALSE); } } /*====================================*/ /* Determine if the CLIPS environment */ /* was successfully cleared. */ /*====================================*/ if (ClearReady() == CLIPS_FALSE) { GenClose(); PrintCLIPS(WERROR,"The CLIPS environment could not be cleared.\n"); PrintCLIPS(WERROR,"Binary load cannot continue.\n"); return(CLIPS_FALSE); } /*==================================*/ /* Call the list of functions to be */ /* executed before a bload occurs. */ /*==================================*/ for (bfPtr = BeforeBloadFunctions; bfPtr != NULL; bfPtr = bfPtr->next) { (*bfPtr->func)(); } /*====================================================*/ /* Read in the functions needed by this binary image. */ /*====================================================*/ FunctionArray = ReadNeededFunctions(&numberOfFunctions,&error); if (error) { GenClose(); AbortBload(); return(CLIPS_FALSE); } /*================================================*/ /* Read in the atoms needed by this binary image. */ /*================================================*/ ReadNeededAtomicValues(); /*===========================================*/ /* Determine the number of expressions to be */ /* read and allocate the appropriate space */ /*===========================================*/ AllocateExpressions(); /*==========================================================*/ /* Read in the memory requirements of the constructs stored */ /* in this binary image and allocate the necessary space */ /*==========================================================*/ for (GenRead(constructBuffer,(unsigned long) CONSTRUCT_HEADER_SIZE); strncmp(constructBuffer,BinaryPrefixID,CONSTRUCT_HEADER_SIZE) != 0; GenRead(constructBuffer,(unsigned long) CONSTRUCT_HEADER_SIZE)) { BOOLEAN found; /*================================================*/ /* Search for the construct type in the list of */ /* binary items. If found, allocate the storage */ /* needed by the construct for this binary image. */ /*================================================*/ found = CLIPS_FALSE; for (biPtr = ListOfBinaryItems; biPtr != NULL; biPtr = biPtr->next) { if (strncmp(biPtr->name,constructBuffer,CONSTRUCT_HEADER_SIZE) == 0) { if (biPtr->bloadStorageFunction != NULL) { (*biPtr->bloadStorageFunction)(); found = CLIPS_TRUE; } break; } } /*==========================================*/ /* If the construct type wasn't found, skip */ /* the storage binary load information for */ /* this construct. */ /*==========================================*/ if (! found) { GenRead(&space,(unsigned long) sizeof(unsigned long)); GenSeek((long) space); if (space != 0) { PrintCLIPS(WDIALOG,"\nSkipping "); PrintCLIPS(WDIALOG,constructBuffer); PrintCLIPS(WDIALOG," constructs because of unavailibility\n"); } } } /*======================================*/ /* Refresh the pointers in expressions. */ /*======================================*/ RefreshExpressions(); /*==========================*/ /* Read in the constraints. */ /*==========================*/ ReadNeededConstraints(); /*======================================================*/ /* Read in the constructs stored in this binary image. */ /*======================================================*/ for (GenRead(constructBuffer,(unsigned long) CONSTRUCT_HEADER_SIZE); strncmp(constructBuffer,BinaryPrefixID,CONSTRUCT_HEADER_SIZE) != 0; GenRead(constructBuffer,(unsigned long) CONSTRUCT_HEADER_SIZE)) { BOOLEAN found; /*==================================================*/ /* Search for the function to load the construct */ /* into the previously allocated storage. If found, */ /* call the function to load the construct. */ /*==================================================*/ found = CLIPS_FALSE; for (biPtr = ListOfBinaryItems; biPtr != NULL; biPtr = biPtr->next) { if (strncmp(biPtr->name,constructBuffer,CONSTRUCT_HEADER_SIZE) == 0) { if (biPtr->bloadFunction != NULL) { (*biPtr->bloadFunction)(); found = CLIPS_TRUE; } break; } } /*==========================================*/ /* If the construct type wasn't found, skip */ /* the binary data for this construct. */ /*==========================================*/ if (! found) { GenRead(&space,(unsigned long) sizeof(unsigned long)); GenSeek((long) space); } } /*=================*/ /* Close the file. */ /*=================*/ GenClose(); /*========================================*/ /* Free up temporary storage used for the */ /* function and atomic value information. */ /*========================================*/ if (FunctionArray != NULL) { genlongfree((VOID *) FunctionArray, (unsigned long) sizeof(struct FunctionDefinition *) * numberOfFunctions); } FreeAtomicValueStorage(); /*==================================*/ /* Call the list of functions to be */ /* executed after a bload occurs. */ /*==================================*/ for (bfPtr = AfterBloadFunctions; bfPtr != NULL; bfPtr = bfPtr->next) { (*bfPtr->func)(); } /*=======================================*/ /* Add a clear function to remove binary */ /* load when a clear command is issued. */ /*=======================================*/ BloadActive = CLIPS_TRUE; AddClearFunction("bload",(VOID (*)(VOID_ARG)) ClearBload,10000); /*=============================*/ /* Return TRUE to indicate the */ /* binary load was successful. */ /*=============================*/ return(CLIPS_TRUE); }/************************************************************ NAME : BloadandRefresh DESCRIPTION : Loads and refreshes objects - will bload all objects at once, if possible, but will aslo work in increments if memory is restricted INPUTS : 1) the number of objects to bload and update 2) the size of one object 3) An update function which takes a bloaded object buffer and the index of the object to refresh as arguments RETURNS : Nothing useful SIDE EFFECTS : Objects bloaded and updated NOTES : Assumes binary file pointer is positioned for bloads of the objects ************************************************************/globle VOID BloadandRefresh(objcnt,objsz,objupdate) long objcnt; unsigned objsz;#if ANSI_COMPILER VOID (*objupdate)(VOID *,long);#else VOID (*objupdate)();#endif { register long i,bi; char HUGE_ADDR *buf; long objsmaxread,objsread; unsigned long space;#if ANSI_COMPILER int (*oldOutOfMemoryFunction)(unsigned long);#else int (*oldOutOfMemoryFunction)();#endif if (objcnt == 0L) return; oldOutOfMemoryFunction = SetOutOfMemoryFunction(BloadOutOfMemoryFunction); objsmaxread = objcnt; do { space = objsmaxread * objsz; buf = (char HUGE_ADDR *) genlongalloc(space); if (buf == NULL) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -