📄 iofun.c
字号:
/*==================================================*/
/* 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;
unsigned start_pos;
char *formatString, *logicalName;
char formatFlagType;
int f_cur_arg = 3;
unsigned form_pos = 0;
char buffer[FORMAT_MAX];
char percentBuffer[FLAG_MAX];
char *fstr = NULL;
unsigned fmaxm = 0;
int fpos = 0;
void *hptr;
int longFound;
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); }
/*==============================================*/
/* 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(theEnv,&formatString[start_pos],fstr,form_pos-start_pos,&fpos,&fmaxm);
}
else
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -