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

📄 int_streams.c

📁 LastWave
💻 C
📖 第 1 页 / 共 4 页
字号:
}    /* Same as above but generate an error (no default value) */void ParseStream(char *arg, STREAM *stream){  if (ParseStream_(arg,NULL,stream) == NO) Errorf1("");}/* * Function for testing whether a stream is eof */char FEof(STREAM stream){  if (stream->mode == StreamWrite)     Errorf("FEof() : the stream is a write only stream");    if (stream->buffer != NULL) return(stream->buffer->flagEof);  return(feof(stream->stream));}/* * Function for testing whether the stdin is eof */char Eof(void){  return(FEof(toplevelCur->in));}/* * The file command */ void C_File(char **argv){  static char name[500];  char *action;  char *filename,*filename1,*mode;  STREAM stream;  FILE *file;  char *str,*expr,*dir,*path;  int res,i,flag,size,type;  LISTV lv;    argv = ParseArgv(argv,tWORD,&action,-1);    /* Open action */  if (!strcmp(action,"open")) {    argv = ParseArgv(argv,tSTR,&filename,tSTR,&mode,0);    if (strcmp(mode,"w") && strcmp(mode,"r") && strcmp(mode,"a")) Errorf("Bad mode '%s'",mode);     stream = OpenFileStream(filename,mode);    if (stream == NULL) Errorf("Cannot open file '%s'",filename);    SetResultInt(stream->id);  }  /* remove action */  else if (!strcmp(action,"remove")) {    argv = ParseArgv(argv,tSTR,&filename,0);    if (XXRemoveFile(filename) != 0) Errorf("Could not find file %s",filename);  }  /* move action */  else if (!strcmp(action,"move")) {    argv = ParseArgv(argv,tSTR,&filename,tSTR,&filename1,0);    if (XXRenameFile(filename,filename1) != 0) Errorf("Could not find file %s",filename);  }  /* tmp action */  else if (!strcmp(action,"tmp")) {    NoMoreArgs(argv);    SetResultStr(XXGetTmpFilename());  }     /* Exist action */  else if (!strcmp(action,"exist")) {    argv = ParseArgv(argv,tSTR,&filename,tSTR_,"r",&mode,0);    file = _FOpen(filename,mode);    if (file == NULL) SetResultInt(0);    else {      SetResultInt(1);      fclose(file);    }  }      /* Openstr action */  else if (!strcmp(action,"openstr")) {    argv = ParseArgv(argv,tSTR,&str,0);    stream = OpenStringStream(str);    SetResultInt(stream->id);  }    /* Close action */  else if (!strcmp(action,"close")) {    argv = ParseArgv(argv,tSTREAM,&stream,0);    if (stream == _StdnullStream || stream == _StdoutStream || stream == _StdinStream || stream == _StderrStream) return;    CloseStream(stream);  }  /* CreateDir action */  else if (!strcmp(action,"createdir")) {    argv = ParseArgv(argv,tSTR,&filename,tSTR,&filename1,0);    if (XXCreateDirectory(filename,filename1) == NO)       Errorf("Unable to create directory named '%s' in directory '%s",filename1,filename);  }   /* Eof action */  else if (!strcmp(action,"eof")) {    argv = ParseArgv(argv,tSTREAM_,NULL,&stream,0);    if (stream != NULL) res = FEof(stream);    else res = Eof();    SetResultInt(res);  }  /* set action */  else if (!strcmp(action,"set")) {     argv = ParseArgv(argv,tWORD,&str,tSTREAM_,NULL,&stream,-1);     if (stream == NULL && *argv != NULL) Errorf("Bad stream '%s'",*argv);        NoMoreArgs(argv);        /* Case we have to get ... */    if (stream == NULL) {      if (!strcmp(str,"stdin")) SetResultInt(stdinStream->id);      else if (!strcmp(str,"stdout")) SetResultInt(stdoutStream->id);      else if (!strcmp(str,"stderr")) SetResultInt(stderrStream->id);      else Errorf("Bad argument '%s'",str);    }    /* Case we have to set ... */    else if (levelFirst == levelCur) Errorf("Sorry cannot redirect '%s' out of a command definition",str);    else {      if (!strcmp(str,"stdin")) stdinStream = CopyStream(&stream,&levelCur->in);      else if (!strcmp(str,"stdout")) stdoutStream = CopyStream(&stream,&levelCur->out);      else if (!strcmp(str,"stderr")) stderrStream = CopyStream(&stream,&levelCur->err);      else Errorf("Bad argument '%s'",str);    }  }  /* list action */  else if (!strcmp(action,"list") || !strcmp(action,"listp")) {    argv = ParseArgv(argv,tSTR,&path,0);    if (action[4] == '\0') flag = YES;    else flag = NO;    /* Getting the directory and the expr to match */    for (i=strlen(path)-1;i>=0;i--) if (path[i] == '/') break;    if (i == -1) {      expr = TCopyStr(path);      dir = TCopyStr(".");      flag = YES;    }    else {      path[i] = '\0';      dir = TCopyStr(path);      path[i] = '/';      expr = TCopyStr(path+i+1);    }    /* Getting the corresponding filename */    XXGetFilenames(dir);    lv = TNewListv();    while (str = XXGetFilenames(NULL)) {      if (MatchStr(str,expr)) {	    if (flag == YES) AppendStr2Listv(lv,str);	    else {	      strcpy(name,dir);	      strcat(name,"/");	      strcat(name,str);          AppendStr2Listv(lv,name);        }      }    }    SetResultValue(lv);    return;  }  /* info action */  else if (!strcmp(action,"info")) {    argv = ParseArgv(argv,tSTR,&path,0);    size = 0;    XXGetFilenameInfo(path,&type,&size);    lv = TNewListv();    switch(type) {    case DirectoryFile: AppendStr2Listv(lv,"directory"); break;    case RegularFile: AppendStr2Listv(lv,"file"); break;    case UnknownTypeFile: AppendStr2Listv(lv,"unknown"); break;    default: return;    }    AppendInt2Listv(lv,size);    SetResultValue(lv);  }  /* cd action */  else if (!strcmp(action,"cd")) {    argv = ParseArgv(argv,tSTR_,NULL,&path,0);    path = XXChangeDirectory(path);    if (path != NULL) SetResultStr(path);  }  else Errorf("Unknown action '%s'",action); }/*************************************************************** * *   Functions for output  * ***************************************************************/static char linePrinted[10000];/* * Same as fprintf in C. * Instead of a FILE * the first argument is a streamId. */void FPrintf(STREAM stream, char *format,...){  va_list ap;    /* Case the stream is null */  if (stream == stdnullStream) return;  va_start(ap,format);  /* Case we are not in the main loop of the program yet */  if (toplevelCur == NULL) {    fprintf(stderr,format,ap);    va_end(ap);    return;  }    /* Get the current stream and check we can write on it */  if (stream->mode != StreamWrite) Errorf("FPrintf() : Stream '%d' is not writable",stream->id);    /* Case the stream corresponds to the terminal stdout */  if (stream == _StdoutStream) {    vsprintf(linePrinted,format,ap);    XXTerminalPrintStr(linePrinted);  }  /* Case the stream corresponds to the terminal stderr */  else if (stream == _StderrStream) {    vsprintf(linePrinted,format,ap);    XXTerminalPrintErrStr(linePrinted);  }    /* Case the stream corresponds to a file */  else vfprintf(stream->stream,format,ap);  va_end(ap);}/* * Perform an FPrintf of the current stdout */void Printf(char *format,...){  va_list ap;  STREAM stream;  int streamId;  /* Case the stream is null */    if (stdoutStream == stdnullStream) return;  va_start(ap,format);  /* Case we are not in the main loop of the program yet */  if (toplevelCur == NULL) {    fprintf(stderr,format,ap);    va_end(ap);    return;  }  /* Get the current stream and check we can write on it */  stream = toplevelCur->out;   streamId = stream->id;  if (stream->mode != StreamWrite) Errorf("Printf() : Stream '%d' is not writable",streamId);  /* Case the stream corresponds to the terminal stdout */    if (stream == _StdoutStream) {    vsprintf(linePrinted,format,ap);    InitTerminalInput();    XXTerminalPrintStr(linePrinted);    Flush();  }  /* Case the stream corresponds to the terminal stderr */  else if (stream == _StderrStream) {    vsprintf(linePrinted,format,ap);    InitTerminalInput();    XXTerminalPrintErrStr(linePrinted);    Flush();  }  /* Case the stream corresponds to a file */  else vfprintf(stream->stream,format,ap);  va_end(ap);}/* * Perform an FPrintf of the current stdin */void PrintfErr(char *format,...){  va_list ap;  STREAM stream;  int streamId;  /* Case the stream is null */    if (stderrStream == stdnullStream) return;    va_start(ap,format);  /* Case we are not in the main loop of the program yet */  if (toplevelCur == NULL) {    fprintf(stderr,format,ap);    va_end(ap);    return;  }  /* Get the current stream and check we can write on it */  stream = toplevelCur->err;   streamId = stream->id;    if (stream->mode != StreamWrite) Errorf("Printf() : Stream '%d' is not writable",streamId);    /* Case the stream corresponds to the terminal stdout */    if (stream == _StdoutStream) {    vsprintf(linePrinted,format,ap);    InitTerminalInput();    XXTerminalPrintStr(linePrinted);  }  /* Case the stream corresponds to the terminal stderr */  else if (stream == _StderrStream) {    vsprintf(linePrinted,format,ap);    InitTerminalInput();    XXTerminalPrintErrStr(linePrinted);  }  /* Case the stream corresponds to a file */  else vfprintf(stream->stream,format,ap);  va_end(ap);}/**************************************************** *  * Commands for output * ****************************************************//* * Just print on stdin each of it's argument */ void C_Echo(char **argv){  while(*argv != NULL) {    if (*(argv+1) != NULL) Printf("%s ",*argv);    else Printf("%s",*argv);    argv++;  }  Printf("\n");  Flush();}/* * The main function for the printf and sprintf commands *   (The line to be printed is in result)  ????? OPTIMISER  */#define PrintfStrLength 2000#define PrintfFormatLength 1000  void _CPrintf(char **argv, char *result){  char *format,*str1;  char f[PrintfFormatLength+1],*f1;  char *result1;  char argType;  int ival;  LWFLOAT fval;  char *sval,c;  VALUE val;    argv = ParseArgv(argv,tSTR,&format,-1);  /* Some inits */  result1 = result;  result[0] = '\0';    /* First we print whatever is before the first % sign */  while (*format != '\0' && *format != '%') *(result1++) = *(format++);  *result1 = '\0';            /* Loop on the different arguments */  while(1) {    /* If format is over then go out of the loop */    if (*format == '\0') break;    /* If %% --> character is % */    if (format[1] == '%') {      *result1 = '%';      result1++;      *result1 = '\0';      format += 2;      while (*format != '\0' && *format != '%') *(result1++) = *(format++);      *result1 = '\0';      continue;    }        /* If % then get everything up to the next % */    f[0] = '\0';    f1 = f;    *(f1++) = *(format++); /* Copy the % sign */    while (*format != '\0' && *format != '%' && !isalpha(*format)) *(f1++) = *(format++);        if (isalpha(*format)) {      argType = *format;      *(f1++) = *(format++);      while (*format != '\0' && *format != '%') *(f1++) = *(format++);      *f1 = '\0';    }    else Errorf("Bad format in 'printf'");                /* We have to get the next argument variable */    /* Case of an int */    if (argType == 'd' || argType == 'i' || argType == 'o' ||        argType == 'x' || argType == 'X' || argType == 'u') {      argv = ParseArgv(argv,tINT,&ival,-1);      sprintf(result1,f,ival);    }    /* Case of an double */    else if (argType == 'f' || argType == 'e' || argType == 'E' ||        argType == 'g' || argType == 'G') {      argv = ParseArgv(argv,tFLOAT,&fval,-1);      sprintf(result1,f,fval);    }    /* Case of a string */    else if (argType == 's') {      argv = ParseArgv(argv,tSTR,&sval,-1);      sprintf(result1,f,sval);    }    /* Case of a char */    else if (argType == 'c') {      argv = ParseArgv(argv,tCHAR,&c,-1);      sprintf(result1,f,c);    }    /* Case of a VAL (LONG) */    else if (argType == 'V') {      argv = ParseArgv(argv,tVAL,&val,-1);      str1 = ToStrValue(val,NO);      sprintf(result1,"%s",str1);      sprintf(result1+strlen(str1),f+2);    }    /* Case of a VAL (SHORT) */    else if (argType == 'v') {      argv = ParseArgv(argv,tVAL,&val,-1);      str1 = ToStrValue(val,YES);      sprintf(result1,"%s",str1);      sprintf(result1+strlen(str1),f+2);    }             /* Case unknown */    else Errorf("_CPrintf() : Format '%c' unknown",argType);        result1 = result + strlen(result);  }        NoMoreArgs(argv);    return;}/* * The printf command */void C_Printf(char **argv){  char result[PrintfStrLength+1];    _CPrintf(argv,result);  Printf("%s",result);  Flush();  InitResult();}

⌨️ 快捷键说明

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