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

📄 io.c

📁 YBasic编程语言的移植和开发 YBasic编程语言的移植和开发 YBasic编程语言的移植和开发
💻 C
📖 第 1 页 / 共 3 页
字号:
          sprintf(keybuff,"key%x",key);        }        if (skey>0) strcpy(keybuff,ykey[skey]);      }      if (!keybuff[0]) printf("Loop !\n");    } while(!keybuff[0]);    ResetEvent(wantkey);    SetEvent(gotkey);  }while(TRUE);    return 0;}static int is_valid_key(INPUT_RECORD *rec) /* check if input rec contains valid key */{  if (rec->EventType!=KEY_EVENT ||     !rec->Event.KeyEvent.bKeyDown ||    rec->Event.KeyEvent.wVirtualKeyCode==VK_SHIFT ||    rec->Event.KeyEvent.wVirtualKeyCode==VK_CONTROL) {    return FALSE;  }  return TRUE;}#endifchar *replace(char *string) /* replace \n,\a, etc. */{  char *from,*to;  char *p;  int val;  static char *hexdigits="0123456789abcdef";  from=to=string;  while(*from) {    if (*from=='\\') {      from++;      switch(*from) {      case 'n':	*to='\n';break;      case 't':	*to='\t';break;      case 'v':	*to='\v';break;      case 'b':	*to='\b';break;      case 'r':	*to='\r';break;      case 'f':	*to='\f';break;      case 'a':	*to='\a';break;      case '\\': *to='\\';break;      case '\?': *to='\?';break;      case '\'': *to='\'';break;      case '\"': *to='\"';break;      case 'x':	val=0;	if ((p=strchr(hexdigits,tolower(*(from+1)))) && p-hexdigits<16) {	  from++;	  val=p-hexdigits;	  if ((p=strchr(hexdigits,tolower(*(from+1)))) && p-hexdigits<16) {	    from++;	    val*=16;	    val+=p-hexdigits;	  }	}	*to=(char)val;	break;      default:        *to='\\';        to++;        *to=*from;      }    }    else      *to=*from;    from++;    to++;  }  *to='\0';  return string;}void create_myopen(int num) /* create command 'myopen' */{  struct command *cmd;    cmd=add_command(cOPEN,NULL);  cmd->tag=num;}void myopen(struct command *cmd) /* open specified file for given name */{#ifdef WINDOWS  char PrinterName[200]; /* Name of default Printer */  char *n; /* points into PrinterName */    DOC_INFO_1 di;#endif  FILE *handle=NULL;  int stream,i;  char *name=NULL;  char *mode=NULL;  char **pmode;  static char *valid_modes[]={"r","w","a","rb","wb","ab",""};  static int smodes[]={smREAD,smWRITE,smWRITE,smREAD,smWRITE,smWRITE};  int smode;  struct stackentry *p;  int has_mode,has_stream,printer;  /* decode cmd->tag */  has_stream=cmd->tag&OPEN_HAS_STREAM;  has_mode=cmd->tag&OPEN_HAS_MODE;  printer=cmd->tag&OPEN_PRINTER;  if (has_mode)    mode=my_strdup(pop(stSTRING)->pointer);  else     mode=printer ? my_strdup("w") : my_strdup("r");  if (printer)     name=my_strdup("/usr/bin/lpr");  else     name=my_strdup(pop(stSTRING)->pointer);  if (has_stream) {    stream=(int)pop(stNUMBER)->value;  } else {     stream=0;    for(i=1;i<FOPEN_MAX-4;i++) {      if (stream_modes[i]==smCLOSED) {	stream=i;	break;      }    }    if (!stream) {      sprintf(errorstring,"reached maximum number of open files");      errorcode=5;      goto open_done;    }  }  p=push();  p->value=0.;  p->type=stNUMBER;  if (printer && print_to_file) {    sprintf(errorstring,"cannot open printer: already printing grafics");    errorcode=6;    goto open_done;  }  if (badstream(stream,1)) {    sprintf(errorstring,"invalid stream number %d",stream);    errorcode=9;    goto open_done;  }  if (stream_modes[stream]!=smCLOSED) {    sprintf(errorstring,"stream already in use");    errorcode=2;    goto open_done;  }  smode=0;  for(pmode=valid_modes;**pmode;pmode++) {    if (!strcmp(*pmode,mode)) break;    smode++;  }  if (!**pmode) {    sprintf(errorstring,"\'%s\' is not a valid filemode",mode);    errorcode=3;    goto open_done;  }  if (printer) {#ifdef UNIX    lineprinter=popen(name,"w");    if (!lineprinter) {      sprintf(errorstring,"could not open line printer");      errorcode=7;      goto open_done;    }#else    /* query win.ini for default printer */    GetProfileString("windows","device",",,,",PrinterName,200);        /* truncate printer name */    n=PrinterName;    while(*n && *n!=',') n++;    *n='\0';        OpenPrinter(PrinterName,&lineprinter,NULL);      di.pDocName="yabasic text";    di.pOutputFile=(LPTSTR)NULL;    di.pDatatype="RAW";    if (!StartDocPrinter(lineprinter,1,(LPBYTE)&di)) {      sprintf(errorstring,"could not open line printer");      errorcode=7;      goto open_done;    }    StartPagePrinter(lineprinter);#endif    lprstream=stream;  } else {    handle=fopen(name,mode);    if (handle==NULL) {      sprintf(errorstring,"could not open '%s': %s",name,my_strerror(errno));      errorcode=4;      goto open_done;    }    streams[stream]=handle;  }  stream_modes[stream]=smodes[smode];  errorcode=0;  p->value=stream; open_done:  if (name) my_free(name);  if (mode) my_free(mode);}void checkopen(void) /* check, if open has been sucessfull */{  double result;    result=pop(stNUMBER)->value;  if (result<=0) {    error(ERROR,errorstring);  } }void myclose(void) /* close the specified stream */{  int s;#ifdef WINDOWS  DWORD written;#endif    s=(int)pop(stNUMBER)->value;  if (abs(s)==STDIO_STREAM || badstream(s,0)) return;  if (stream_modes[s]==smCLOSED) {    sprintf(string,"stream %d already closed",s);    error(WARNING,string);    return;  }  if (s==lprstream) {#ifdef UNIX    pclose(lineprinter);#else    WritePrinter(lineprinter,"\f",2,&written);    EndPagePrinter(lineprinter);    EndDocPrinter(lineprinter);    ClosePrinter(lineprinter);    lineprinter=INVALID_HANDLE_VALUE;#endif    lprstream=-1;  } else {    fclose(streams[s]);  }  streams[s]=NULL;  stream_modes[s]=smCLOSED;}void myseek(struct command *cmd) /* reposition file pointer */{  int s,p,m,i;  struct stackentry *pp;  char *mode;  if (cmd->type==cSEEK2)     mode=(char *)my_strdup(pop(stSTRING)->pointer);  else    mode=my_strdup("begin");  p=(int)pop(stNUMBER)->value;  s=(int)pop(stNUMBER)->value;  pp=push();  pp->value=0.;  pp->type=stNUMBER;  for(i=0;mode[i];i++) mode[i]=tolower(mode[i]);  if (!strcmp(mode,"begin")) {    m=SEEK_SET;  } else if (!strcmp(mode,"end")) {    m=SEEK_END;  } else if (!strcmp(mode,"here")) {    m=SEEK_CUR;  } else {    sprintf(errorstring,"seek mode '%s' is none of begin,end,here",mode);    errorcode=12;    my_free(mode);    return;  }  my_free(mode);  if (abs(s)==STDIO_STREAM || badstream(s,0)) return;  if (!(stream_modes[s] & (smREAD | smWRITE))) {    sprintf(errorstring,"stream %d not open",s);    errorcode=11;    return;  }  if (fseek(streams[s],(long)p,m)) {    sprintf(errorstring,"could not position stream %d to byte %d",s,p);    errorcode=10;    return;  }  pp->value=1.0;}void create_pps(int type,int input) /* create command pushswitch or popswitch */{  struct command *cmd;    cmd=add_command(type,NULL);  cmd->args=input;}void push_switch(struct command *cmd) /* push current stream on stack and switch to new one */{  static int oldstream=STDIO_STREAM;  struct stackentry *s;  int stream;  stream=(int)pop(stNUMBER)->value;  if (badstream(stream,0)) return;  if (!cmd->args) stream=-stream;  s=push();  s->type=stNUMBER;  s->value=oldstream;  if (infolevel>=DEBUG) {    sprintf(string,"pushing %d on stack, switching to %d",oldstream,stream);    error(DEBUG,string);  }  oldstream=stream;  myswitch(stream);}void pop_switch(void) /* pop current stream from stack and switch to it */{  int stream;  stream=(int)pop(stNUMBER)->value;  if (infolevel>=DEBUG) {    sprintf(string,"popping %d from stack, switching to it",stream);    error(DEBUG,string);  }  myswitch(stream);}void myswitch(int stream) /* switch to specified stream */{  int stdio,input;  stdio=(abs(stream)==STDIO_STREAM);  input=(stream>0);  currstr=stream;  if (stream<0) stream=-stream;  if (badstream(stream,0)) return;  if (stdio) {    cinstr=stdin;    coutstr=stdout;  } else {    cinstr=coutstr=NULL;    if (input)       cinstr=streams[stream];    else      coutstr=streams[stream];  }}int checkstream(void)  /* test if currst is still valid */{  int stdio,input;  stdio=(abs(currstr)==STDIO_STREAM);  input=(currstr>0);  if (!stdio) {    if (input && !(stream_modes[abs(currstr)] & smREAD)) {      sprintf(string,"stream %d not open for reading",abs(currstr));      error(ERROR,string);      return FALSE;    }    if (!input && !(stream_modes[abs(currstr)] & (smWRITE | smPRINT))) {      sprintf(string,"stream %d not open for writing or printing",abs(currstr));      error(ERROR,string);      return FALSE;    }  }  return TRUE;}void testeof(struct command *cmd) /* close the specified stream */{  int s,c;  struct stackentry *result;    s=(int)pop(stNUMBER)->value;  if (s!=STDIO_STREAM && badstream(s,0)) return;  result=push();  result->type=stNUMBER;  if (s && !(stream_modes[s] & smREAD)) {    result->value=1.;    return;  }  if (!s) {    result->value=0.;    return;  }  c=getc(streams[s]);  if (c==EOF) {    result->value=1.;    return;  }    result->value=0.;  ungetc(c,streams[s]);  return;}int badstream(int stream,int errcode) /* test for valid stream id */{  if (stream!=STDIO_STREAM && (stream>FOPEN_MAX-4 || stream<=0)) {    sprintf(errcode?errorstring:string,"invalid stream: %d (can handle only streams from 1 to %d)",stream,FOPEN_MAX-4);    if (errcode)       errorcode=errcode;    else      error(ERROR,string);    return TRUE;  }  return FALSE;}void create_myread(char type,int tileol) /* create command 'read' */{  struct command *cmd;    cmd=add_command(cREAD,NULL);  cmd->args=tileol; /* true, if read should go til eol */  cmd->tag=type; /* can be 'd' or 's' */}void myread(struct command *cmd) /* read string or double */{  double d;  static char buffer[INBUFFLEN]; /* buffer with current input */  int numread; /* number of bytes read */  int tileol; /* true, if read should go til end of line */  struct stackentry *s;  int currch; /* current character */    numread=0; /* no chars read'til now */  buffer[0]='\0';  tileol=cmd->args;    /* skip leading whitespace */  if (!tileol) {    do {      currch=onechar();    } while(currch==' ' || currch=='\t');    /* put back last char */    if (currch!=EOF && currch!='\0') backchar(currch);    if (currch=='\0' || currch==EOF) goto done;  }          /* read chars */  do {    currch=onechar();    buffer[numread]=currch;    numread++;  } while(((tileol && currch!='\0') ||	   (!tileol && currch!=' ' &&	    currch!='\t' && currch!='\0')) &&	  currch!=EOF && numread<INBUFFLEN);    /* put back last char */  if (currch!=EOF && currch!='\0') backchar(currch);  /* and remove it from buff */  if (currch!=EOF) numread--;  buffer[numread]='\0';  if (currch=='\0' || currch==EOF) goto done;    /* skip trailing whitespace */  if (!tileol) {    do {      currch=onechar();    } while(currch==' ' || currch=='\t');    if (currch!=EOF && currch!='\0') backchar(currch);  }  done:  if (cmd->tag=='s') { /* read string */    s=push();    s->type=stSTRING;    s->pointer=my_strdup(buffer);}  else { /* read double */    s=push();    s->type=stNUMBER;    s->value=0.0;    if (buffer[0] && (sscanf(buffer,"%lf",&d)==1)) s->value=d;  }}static void readline(void) /* read one line from current stream */{  

⌨️ 快捷键说明

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