📄 iofun.c
字号:
{ theToken->type = STRING; theToken->value = (VOID *) AddSymbol("*** READ ERROR ***"); } /*====================================================*/ /* Return the EOF symbol if the end of file for stdin */ /* has been encountered. This typically won't occur, */ /* but is possible (for example by pressing control-d */ /* in the UNIX operating system). */ /*====================================================*/ if ((theToken->type == STOP) && (inchar == EOF)) { theToken->type = SYMBOL; theToken->value = (VOID *) AddSymbol("EOF"); } } }/*************************************************************//* OpenFunction: CLIPS access routine for the open function. *//*************************************************************/globle int OpenFunction() { int numberOfArguments; char *fileName, *logicalName, *accessMode = NULL; DATA_OBJECT theArgument; /*========================================*/ /* Check for a valid number of arguments. */ /*========================================*/ if ((numberOfArguments = ArgRangeCheck("open",2,3)) == -1) return(0); /*====================*/ /* Get the file name. */ /*====================*/ if ((fileName = GetFileName("open",1)) == NULL) return(0); /*=======================================*/ /* Get the logical name to be associated */ /* with the opened file. */ /*=======================================*/ logicalName = GetLogicalName(2,NULL); if (logicalName == NULL) { SetHaltExecution(CLIPS_TRUE); SetEvaluationError(CLIPS_TRUE); IllegalLogicalNameMessage("open"); return(0); } /*==================================*/ /* Check to see if the logical name */ /* is already in use. */ /*==================================*/ if (FindFile(logicalName)) { SetHaltExecution(CLIPS_TRUE); SetEvaluationError(CLIPS_TRUE); PrintErrorID("IOFUN",2,CLIPS_FALSE); PrintCLIPS(WERROR,"Logical name "); PrintCLIPS(WERROR,logicalName); PrintCLIPS(WERROR," already in use.\n"); return(0); } /*===========================*/ /* Get the file access mode. */ /*===========================*/ if (numberOfArguments == 2) { accessMode = "r"; } else if (numberOfArguments == 3) { if (ArgTypeCheck("open",3,STRING,&theArgument) == CLIPS_FALSE) return(0); accessMode = DOToString(theArgument); } /*=====================================*/ /* Check for a valid file access mode. */ /*=====================================*/ if ((strcmp(accessMode,"r") != 0) && (strcmp(accessMode,"r+") != 0) && (strcmp(accessMode,"w") != 0) && (strcmp(accessMode,"a") != 0)) { SetHaltExecution(CLIPS_TRUE); SetEvaluationError(CLIPS_TRUE); ExpectedTypeError1("open",3,"string with value \"r\", \"r+\", \"w\", or \"a\""); return(0); } /*================================================*/ /* Open the named file and associate it with the */ /* specified logical name. Return TRUE if the */ /* file was opened successfully, otherwise FALSE. */ /*================================================*/ return(OpenFile(fileName,accessMode,logicalName)); }/***************************************************************//* CloseFunction: CLIPS access routine for the close function. *//***************************************************************/globle int CloseFunction() { int numberOfArguments; char *logicalName; /*======================================*/ /* Check for valid number of arguments. */ /*======================================*/ if ((numberOfArguments = ArgCountCheck("close",NO_MORE_THAN,1)) == -1) return(0); /*=====================================================*/ /* If no arguments are specified, then close all files */ /* opened with the open command. Return TRUE if all */ /* files were closed successfully, otherwise FALSE. */ /*=====================================================*/ if (numberOfArguments == 0) return(CloseAllFiles()); /*================================*/ /* Get the logical name argument. */ /*================================*/ logicalName = GetLogicalName(1,NULL); if (logicalName == NULL) { IllegalLogicalNameMessage("close"); SetHaltExecution(CLIPS_TRUE); SetEvaluationError(CLIPS_TRUE); return(0); } /*========================================================*/ /* Close the file associated with the specified logical */ /* name. Return TRUE if the file was closed successfully, */ /* otherwise false. */ /*========================================================*/ return(CloseFile(logicalName)); }#endif#if EXT_IO/****************************************//* RemoveFunction: CLIPS access routine *//* for the remove function. *//****************************************/globle int RemoveFunction() { char *theFileName; /*======================================*/ /* Check for valid number of arguments. */ /*======================================*/ if (ArgCountCheck("remove",EXACTLY,1) == -1) return(CLIPS_FALSE); /*====================*/ /* Get the file name. */ /*====================*/ if ((theFileName = GetFileName("remove",1)) == NULL) return(CLIPS_FALSE); /*==============================================*/ /* Remove the file. Return TRUE if the file was */ /* sucessfully removed, otherwise FALSE. */ /*==============================================*/ return(genremove(theFileName)); } /****************************************//* RenameFunction: CLIPS access routine *//* for the rename function. *//****************************************/globle int RenameFunction() { char *oldFileName, *newFileName; /*========================================*/ /* Check for a valid number of arguments. */ /*========================================*/ if (ArgCountCheck("rename",EXACTLY,2) == -1) return(CLIPS_FALSE); /*===========================*/ /* Check for the file names. */ /*===========================*/ if ((oldFileName = GetFileName("rename",1)) == NULL) return(CLIPS_FALSE); if ((newFileName = GetFileName("rename",2)) == NULL) return(CLIPS_FALSE); /*==============================================*/ /* Rename the file. Return TRUE if the file was */ /* sucessfully renamed, otherwise FALSE. */ /*==============================================*/ return(genrename(oldFileName,newFileName)); } /****************************************//* FormatFunction: CLIPS access routine *//* for the format function. *//****************************************/globle VOID *FormatFunction() { int argCount, start_pos; char *formatString, *logicalName; char formatFlagType; int f_cur_arg = 3; int form_pos = 0; char buffer[FORMAT_MAX]; char percentBuffer[FLAG_MAX]; char *fstr = NULL; int fmax = 0, fpos = 0; VOID *hptr; int longFound; char *theString; /*======================================*/ /* Set default return value for errors. */ /*======================================*/ hptr = AddSymbol(""); /*=========================================*/ /* Format requires at least two arguments: */ /* a logical name and a format string. */ /*=========================================*/ if ((argCount = ArgCountCheck("format",AT_LEAST,2)) == -1) { return(hptr); } /*========================================*/ /* First argument must be a logical name. */ /*========================================*/ if ((logicalName = GetLogicalName(1,"stdout")) == NULL) { IllegalLogicalNameMessage("format"); SetHaltExecution(CLIPS_TRUE); SetEvaluationError(CLIPS_TRUE); return(hptr); } if (strcmp(logicalName,"nil") == 0) { /* do nothing */ } else if (QueryRouters(logicalName) == CLIPS_FALSE) { UnrecognizedRouterMessage(logicalName); return(hptr); } /*=====================================================*/ /* Second argument must be a string. The appropriate */ /* number of arguments specified by the string must be */ /* present in the argument list. */ /*=====================================================*/ if ((formatString = ControlStringCheck (argCount)) == NULL) { return (hptr); } /*==============================================*/ /* Locate a string of 80 character for scanning */ /* sub_string from control_string */ /*==============================================*/ /* Scanning and print the format */ while (formatString[form_pos] != '\0') { if (formatString[form_pos] != '%') { start_pos = form_pos; while ((formatString[form_pos] != '%') && (formatString[form_pos] != '\0') && ((form_pos - start_pos) < FLAG_MAX)) { form_pos++; } fstr = AppendNToString(&formatString[start_pos],fstr,form_pos-start_pos,&fpos,&fmax); } else { start_pos = form_pos; form_pos++; formatFlagType = FindFormatFlag(formatString,&form_pos,buffer,&longFound); if (formatFlagType != ' ') { strncpy(percentBuffer,&formatString[start_pos], (CLIPS_STD_SIZE) form_pos-start_pos); percentBuffer[form_pos-start_pos] = EOS; if ((! longFound) && ((formatFlagType == 'd') || (formatFlagType == 'o') || (formatFlagType == 'u') || (formatFlagType == 'x'))) { longFound = CLIPS_TRUE; percentBuffer[(form_pos-start_pos) - 1] = 'l'; percentBuffer[form_pos-start_pos] = formatFlagType; percentBuffer[(form_pos-start_pos) + 1] = EOS; } if ((theString = PrintFormatFlag(percentBuffer,f_cur_arg,formatFlagType,longFound)) == NULL) { if (fstr != NULL) rm(fstr,fmax); return (hptr); } fstr = AppendToString(theString,fstr,&fpos,&fmax); if (fstr == NULL) return(hptr); f_cur_arg++; } else { fstr = AppendToString(buffer,fstr,&fpos,&fmax); if (fstr == NULL) return(hptr); } } } if (fstr != NULL) { hptr = AddSymbol(fstr); if (strcmp(logicalName,"nil") != 0) PrintCLIPS(logicalName,fstr); rm(fstr,fmax); } else { hptr = AddSymbol(""); } return(hptr); }/*********************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -