📄 utility.c
字号:
/* AppendStrings: Appends two strings together. The string *//* created is added to the SymbolTable, so it is not *//* necessary to deallocate the string returned. *//***********************************************************/globle char *AppendStrings( void *theEnv, char *str1, char *str2) { size_t pos = 0; size_t max = 0; char *theString = NULL; void *thePtr; theString = AppendToString(theEnv,str1,theString,&pos,&max); theString = AppendToString(theEnv,str2,theString,&pos,&max); thePtr = EnvAddSymbol(theEnv,theString); rm(theEnv,theString,max); return(ValueToString(thePtr)); }/******************************************************//* AppendToString: Appends a string to another string *//* (expanding the other string if necessary). *//******************************************************/globle char *AppendToString( void *theEnv, char *appendStr, char *oldStr, size_t *oldPos, size_t *oldMax) { size_t length; /*=========================================*/ /* Expand the old string so it can contain */ /* the new string (if necessary). */ /*=========================================*/ length = strlen(appendStr); if (length + *oldPos + 1 > *oldMax) { oldStr = (char *) genrealloc(theEnv,oldStr,*oldMax,length + *oldPos + 1); *oldMax = length + *oldPos + 1; } /*==============================================================*/ /* Return NULL if the old string was not successfully expanded. */ /*==============================================================*/ if (oldStr == NULL) { return(NULL); } /*===============================================*/ /* Append the new string to the expanded string. */ /*===============================================*/ genstrcpy(&oldStr[*oldPos],appendStr); *oldPos += (int) length; /*============================================================*/ /* Return the expanded string containing the appended string. */ /*============================================================*/ return(oldStr); }/*******************************************************//* AppendNToString: Appends a string to another string *//* (expanding the other string if necessary). Only a *//* specified number of characters are appended from *//* the string. *//*******************************************************/globle char *AppendNToString( void *theEnv, char *appendStr, char *oldStr, size_t length, size_t *oldPos, size_t *oldMax) { size_t lengthWithEOS; /*====================================*/ /* Determine the number of characters */ /* to be appended from the string. */ /*====================================*/ if (appendStr[length-1] != '\0') lengthWithEOS = length + 1; else lengthWithEOS = length; /*=========================================*/ /* Expand the old string so it can contain */ /* the new string (if necessary). */ /*=========================================*/ if (lengthWithEOS + *oldPos > *oldMax) { oldStr = (char *) genrealloc(theEnv,oldStr,*oldMax,*oldPos + lengthWithEOS); *oldMax = *oldPos + lengthWithEOS; } /*==============================================================*/ /* Return NULL if the old string was not successfully expanded. */ /*==============================================================*/ if (oldStr == NULL) { return(NULL); } /*==================================*/ /* Append N characters from the new */ /* string to the expanded string. */ /*==================================*/ genstrncpy(&oldStr[*oldPos],appendStr,length); *oldPos += (lengthWithEOS - 1); oldStr[*oldPos] = '\0'; /*============================================================*/ /* Return the expanded string containing the appended string. */ /*============================================================*/ return(oldStr); }/*******************************************************//* ExpandStringWithChar: Adds a character to a string, *//* reallocating space for the string if it needs to *//* be enlarged. The backspace character causes the *//* size of the string to reduced if it is "added" to *//* the string. *//*******************************************************/globle char *ExpandStringWithChar( void *theEnv, int inchar, char *str, size_t *pos, size_t *max, size_t newSize) { if ((*pos + 1) >= *max) { str = (char *) genrealloc(theEnv,str,*max,newSize); *max = newSize; } if (inchar != '\b') { str[*pos] = (char) inchar; (*pos)++; str[*pos] = '\0'; } else { if (*pos > 0) (*pos)--; str[*pos] = '\0'; } return(str); }/*****************************************************************//* AddFunctionToCallList: Adds a function to a list of functions *//* which are called to perform certain operations (e.g. clear, *//* reset, and bload functions). *//*****************************************************************/globle struct callFunctionItem *AddFunctionToCallList( void *theEnv, char *name, int priority, void (*func)(void *), struct callFunctionItem *head, intBool environmentAware) { struct callFunctionItem *newPtr, *currentPtr, *lastPtr = NULL; newPtr = get_struct(theEnv,callFunctionItem); newPtr->name = name; newPtr->func = func; newPtr->priority = priority; newPtr->environmentAware = (short) environmentAware; if (head == NULL) { newPtr->next = NULL; return(newPtr); } currentPtr = head; while ((currentPtr != NULL) ? (priority < currentPtr->priority) : FALSE) { lastPtr = currentPtr; currentPtr = currentPtr->next; } if (lastPtr == NULL) { newPtr->next = head; head = newPtr; } else { newPtr->next = currentPtr; lastPtr->next = newPtr; } return(head); }/*****************************************************************//* RemoveFunctionFromCallList: Removes a function from a list of *//* functions which are called to perform certain operations *//* (e.g. clear, reset, and bload functions). *//*****************************************************************/globle struct callFunctionItem *RemoveFunctionFromCallList( void *theEnv, char *name, struct callFunctionItem *head, int *found) { struct callFunctionItem *currentPtr, *lastPtr; *found = FALSE; lastPtr = NULL; currentPtr = head; while (currentPtr != NULL) { if (strcmp(name,currentPtr->name) == 0) { *found = TRUE; if (lastPtr == NULL) { head = currentPtr->next; } else { lastPtr->next = currentPtr->next; } rtn_struct(theEnv,callFunctionItem,currentPtr); return(head); } lastPtr = currentPtr; currentPtr = currentPtr->next; } return(head); }/**************************************************************//* DeallocateCallList: Removes all functions from a list of *//* functions which are called to perform certain operations *//* (e.g. clear, reset, and bload functions). *//**************************************************************/globle void DeallocateCallList( void *theEnv, struct callFunctionItem *theList) { struct callFunctionItem *tmpPtr, *nextPtr; tmpPtr = theList; while (tmpPtr != NULL) { nextPtr = tmpPtr->next; rtn_struct(theEnv,callFunctionItem,tmpPtr); tmpPtr = nextPtr; } }/*****************************************//* ItemHashValue: Returns the hash value *//* for the specified value. *//*****************************************/globle unsigned ItemHashValue( void *theEnv, unsigned short theType, void *theValue, unsigned theRange) { union { void *vv; unsigned uv; } fis; switch(theType) { case FLOAT: return(HashFloat(ValueToDouble(theValue),theRange)); case INTEGER: return(HashInteger(ValueToLong(theValue),theRange)); case SYMBOL: case STRING:#if OBJECT_SYSTEM case INSTANCE_NAME:#endif return(HashSymbol(ValueToString(theValue),theRange)); case MULTIFIELD: return(HashMultifield((struct multifield *) theValue,theRange));#if DEFTEMPLATE_CONSTRUCT case FACT_ADDRESS: return(((struct fact *) theValue)->hashValue % theRange);#endif case EXTERNAL_ADDRESS:#if OBJECT_SYSTEM case INSTANCE_ADDRESS:#endif fis.uv = 0; fis.vv = theValue; return(fis.uv % theRange); } SystemError(theEnv,"UTILITY",1); return(0); }/********************************************//* YieldTime: Yields time to a user-defined *//* function. Intended to allow foreground *//* application responsiveness when CLIPS *//* is running in the background. *//********************************************/void YieldTime( void *theEnv) { if ((UtilityData(theEnv)->YieldTimeFunction != NULL) && UtilityData(theEnv)->YieldFunctionEnabled) { (*UtilityData(theEnv)->YieldTimeFunction)(); } } /********************************************//* SetGarbageCollectionHeuristics: *//********************************************/short SetGarbageCollectionHeuristics( void *theEnv, short newValue) { short oldValue; oldValue = UtilityData(theEnv)->GarbageCollectionHeuristicsEnabled; UtilityData(theEnv)->GarbageCollectionHeuristicsEnabled = newValue; return(oldValue); } /**********************************************//* EnvIncrementGCLocks: Increments the number *//* of garbage collection locks. *//**********************************************/globle void EnvIncrementGCLocks( void *theEnv) { UtilityData(theEnv)->GarbageCollectionLocks++; }/**********************************************//* EnvDecrementGCLocks: Decrements the number *//* of garbage collection locks. *//**********************************************/globle void EnvDecrementGCLocks( void *theEnv) { if (UtilityData(theEnv)->GarbageCollectionLocks > 0) { UtilityData(theEnv)->GarbageCollectionLocks--; } } /********************************************//* EnablePeriodicFunctions: *//********************************************/short EnablePeriodicFunctions( void *theEnv, short value) { short oldValue; oldValue = UtilityData(theEnv)->PeriodicFunctionsEnabled; UtilityData(theEnv)->PeriodicFunctionsEnabled = value; return(oldValue); } /********************************************//* EnableYieldFunction: *//********************************************/short EnableYieldFunction( void *theEnv, short value) { short oldValue; oldValue = UtilityData(theEnv)->YieldFunctionEnabled; UtilityData(theEnv)->YieldFunctionEnabled = value; return(oldValue); }/********************************************//* AddTrackedMemory: *//********************************************/struct trackedMemory *AddTrackedMemory( void *theEnv, void *theMemory, size_t theSize) { struct trackedMemory *newPtr; newPtr = get_struct(theEnv,trackedMemory); newPtr->prev = NULL; newPtr->theMemory = theMemory; newPtr->memSize = theSize; newPtr->next = UtilityData(theEnv)->trackList; UtilityData(theEnv)->trackList = newPtr; return newPtr; }/********************************************//* RemoveTrackedMemory: *//********************************************/void RemoveTrackedMemory( void *theEnv, struct trackedMemory *theTracker) { if (theTracker->prev == NULL) { UtilityData(theEnv)->trackList = theTracker->next; } else { theTracker->prev->next = theTracker->next; } if (theTracker->next != NULL) { theTracker->next->prev = theTracker->prev; } rtn_struct(theEnv,trackedMemory,theTracker); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -