⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iofun.c

📁 clips源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
      inputString = NULL;      RouterData(theEnv)->CommandBufferInputCount = 0;      inputStringSize = 0;      inchar = EnvGetcRouter(theEnv,"stdin");      /*========================================================*/      /* Continue reading characters until a carriage return is */      /* entered or the user halts execution (usually with      */      /* control-c). Waiting for the carriage return prevents   */      /* the input from being prematurely parsed (such as when  */      /* a space is entered after a symbol has been typed).     */      /*========================================================*/      while ((inchar != '\n') && (inchar != '\r') && (inchar != EOF) &&             (! GetHaltExecution(theEnv)))        {         inputString = ExpandStringWithChar(theEnv,inchar,inputString,&RouterData(theEnv)->CommandBufferInputCount,                                            &inputStringSize,inputStringSize + 80);         inchar = EnvGetcRouter(theEnv,"stdin");        }      /*==================================================*/      /* Open a string input source using the characters  */      /* retrieved from stdin and extract the first token */      /* contained in the string.                         */      /*==================================================*/      OpenStringSource(theEnv,"read",inputString,0);      GetToken(theEnv,"read",theToken);      CloseStringSource(theEnv,"read");      if (inputStringSize > 0) rm(theEnv,inputString,inputStringSize);      /*===========================================*/      /* Pressing control-c (or comparable action) */      /* aborts the read function.                 */      /*===========================================*/      if (GetHaltExecution(theEnv))        {         theToken->type = STRING;         theToken->value = (void *) EnvAddSymbol(theEnv,"*** 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 *) EnvAddSymbol(theEnv,"EOF");        }     }  }/*************************************************************//* OpenFunction: H/L access routine for the open function.   *//*************************************************************/globle int OpenFunction(  void *theEnv)  {   int numberOfArguments;   char *fileName, *logicalName, *accessMode = NULL;   DATA_OBJECT theArgument;   /*========================================*/   /* Check for a valid number of arguments. */   /*========================================*/   if ((numberOfArguments = EnvArgRangeCheck(theEnv,"open",2,3)) == -1) return(0);   /*====================*/   /* Get the file name. */   /*====================*/   if ((fileName = GetFileName(theEnv,"open",1)) == NULL) return(0);   /*=======================================*/   /* Get the logical name to be associated */   /* with the opened file.                 */   /*=======================================*/   logicalName = GetLogicalName(theEnv,2,NULL);   if (logicalName == NULL)     {      SetHaltExecution(theEnv,TRUE);      SetEvaluationError(theEnv,TRUE);      IllegalLogicalNameMessage(theEnv,"open");      return(0);     }   /*==================================*/   /* Check to see if the logical name */   /* is already in use.               */   /*==================================*/   if (FindFile(theEnv,logicalName))     {      SetHaltExecution(theEnv,TRUE);      SetEvaluationError(theEnv,TRUE);      PrintErrorID(theEnv,"IOFUN",2,FALSE);      EnvPrintRouter(theEnv,WERROR,"Logical name ");      EnvPrintRouter(theEnv,WERROR,logicalName);      EnvPrintRouter(theEnv,WERROR," already in use.\n");      return(0);     }   /*===========================*/   /* Get the file access mode. */   /*===========================*/   if (numberOfArguments == 2)     { accessMode = "r"; }   else if (numberOfArguments == 3)     {      if (EnvArgTypeCheck(theEnv,"open",3,STRING,&theArgument) == 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) &&       (strcmp(accessMode,"wb") != 0))     {      SetHaltExecution(theEnv,TRUE);      SetEvaluationError(theEnv,TRUE);      ExpectedTypeError1(theEnv,"open",3,"string with value \"r\", \"r+\", \"w\", \"wb\", 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(OpenAFile(theEnv,fileName,accessMode,logicalName));  }/***************************************************************//* CloseFunction: H/L access routine for the close function.   *//***************************************************************/globle int CloseFunction(  void *theEnv)  {   int numberOfArguments;   char *logicalName;   /*======================================*/   /* Check for valid number of arguments. */   /*======================================*/   if ((numberOfArguments = EnvArgCountCheck(theEnv,"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(theEnv));   /*================================*/   /* Get the logical name argument. */   /*================================*/   logicalName = GetLogicalName(theEnv,1,NULL);   if (logicalName == NULL)     {      IllegalLogicalNameMessage(theEnv,"close");      SetHaltExecution(theEnv,TRUE);      SetEvaluationError(theEnv,TRUE);      return(0);     }   /*========================================================*/   /* Close the file associated with the specified logical   */   /* name. Return TRUE if the file was closed successfully, */   /* otherwise false.                                       */   /*========================================================*/   return(CloseFile(theEnv,logicalName));  }/***************************************//* GetCharFunction: H/L access routine *//*   for the get-char function.        *//***************************************/globle int GetCharFunction(  void *theEnv)  {   int numberOfArguments;   char *logicalName;   if ((numberOfArguments = EnvArgCountCheck(theEnv,"get-char",NO_MORE_THAN,1)) == -1)     { return(-1); }   if (numberOfArguments == 0 )     { logicalName = "stdin"; }   else     {      logicalName = GetLogicalName(theEnv,1,"stdin");      if (logicalName == NULL)        {         IllegalLogicalNameMessage(theEnv,"get-char");         SetHaltExecution(theEnv,TRUE);         SetEvaluationError(theEnv,TRUE);         return(-1);        }     }   if (QueryRouters(theEnv,logicalName) == FALSE)     {      UnrecognizedRouterMessage(theEnv,logicalName);      SetHaltExecution(theEnv,TRUE);      SetEvaluationError(theEnv,TRUE);      return(-1);     }   return(EnvGetcRouter(theEnv,logicalName));  }#endif#if EXT_IO/****************************************//* RemoveFunction: H/L access routine   *//*   for the remove function.           *//****************************************/globle int RemoveFunction(  void *theEnv)  {   char *theFileName;   /*======================================*/   /* Check for valid number of arguments. */   /*======================================*/   if (EnvArgCountCheck(theEnv,"remove",EXACTLY,1) == -1) return(FALSE);   /*====================*/   /* Get the file name. */   /*====================*/   if ((theFileName = GetFileName(theEnv,"remove",1)) == NULL) return(FALSE);   /*==============================================*/   /* Remove the file. Return TRUE if the file was */   /* sucessfully removed, otherwise FALSE.        */   /*==============================================*/   return(genremove(theFileName));  }/****************************************//* RenameFunction: H/L access routine   *//*   for the rename function.           *//****************************************/globle int RenameFunction(  void *theEnv)  {   char *oldFileName, *newFileName;   /*========================================*/   /* Check for a valid number of arguments. */   /*========================================*/   if (EnvArgCountCheck(theEnv,"rename",EXACTLY,2) == -1) return(FALSE);   /*===========================*/   /* Check for the file names. */   /*===========================*/   if ((oldFileName = GetFileName(theEnv,"rename",1)) == NULL) return(FALSE);   if ((newFileName = GetFileName(theEnv,"rename",2)) == NULL) return(FALSE);   /*==============================================*/   /* Rename the file. Return TRUE if the file was */   /* sucessfully renamed, otherwise FALSE.        */   /*==============================================*/   return(genrename(oldFileName,newFileName));  }/****************************************//* FormatFunction: H/L access routine   *//*   for the format function.           *//****************************************/globle void *FormatFunction(  void *theEnv)  {   int argCount;   size_t start_pos;   char *formatString, *logicalName;   char formatFlagType;   int  f_cur_arg = 3;   size_t form_pos = 0;   char percentBuffer[FLAG_MAX];   char *fstr = NULL;   size_t fmaxm = 0;   size_t fpos = 0;   void *hptr;   char *theString;   /*======================================*/   /* Set default return value for errors. */   /*======================================*/   hptr = EnvAddSymbol(theEnv,"");   /*=========================================*/   /* Format requires at least two arguments: */   /* a logical name and a format string.     */   /*=========================================*/   if ((argCount = EnvArgCountCheck(theEnv,"format",AT_LEAST,2)) == -1)     { return(hptr); }   /*========================================*/   /* First argument must be a logical name. */   /*========================================*/   if ((logicalName = GetLogicalName(theEnv,1,"stdout")) == NULL)     {      IllegalLogicalNameMessage(theEnv,"format");      SetHaltExecution(theEnv,TRUE);      SetEvaluationError(theEnv,TRUE);      return(hptr);     }   if (strcmp(logicalName,"nil") == 0)     { /* do nothing */ }   else if (QueryRouters(theEnv,logicalName) == FALSE)     {      UnrecognizedRouterMessage(theEnv,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(theEnv,argCount)) == NULL)     { return (hptr); }   /*========================================*/   /* Search the format string, printing the */   /* format flags as they are encountered.  */   /*========================================*/   while (formatString[form_pos] != '\0')     {      if (formatString[form_pos] != '%')        {         start_pos = form_pos;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -