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

📄 function.c

📁 YBasic编程语言的移植和开发 YBasic编程语言的移植和开发 YBasic编程语言的移植和开发
💻 C
📖 第 1 页 / 共 3 页
字号:
    do {      i++;      pointer[i]=toupper((int)str[i]);    } while(pointer[i]);    result=stSTRING;    break;  case fLOWER:    str=a1->pointer;    pointer=my_malloc(strlen(str)+1);    i=-1;    do {      i++;      pointer[i]=tolower((int)str[i]);    } while(pointer[i]);    result=stSTRING;    break;  case fLTRIM:    str=a1->pointer;    while(isspace(*str)) str++;    pointer=my_strdup(str);    result=stSTRING;    break;  case fRTRIM:    str=a1->pointer;    i=strlen(str)-1;    while(isspace(str[i]) && i>=0) i--;    str[i+1]='\0';    pointer=my_strdup(str);    result=stSTRING;    break;  case fTRIM:    str=a1->pointer;    i=strlen(str)-1;    while(isspace(str[i]) && i>=0) i--;    str[i+1]='\0';    while(isspace(*str)) str++;    pointer=my_strdup(str);    result=stSTRING;    break;  case fINSTR:    str=a1->pointer;    str2=a2->pointer;    if (*str2)      pointer=strstr(str,str2);    else      pointer=NULL;    if (pointer==NULL)       value=0;    else      value=pointer-str+1;    result=stNUMBER;    break;     case fINSTR2:    str=a1->pointer;    str2=a2->pointer;    start=(int)a3->value;    if (start>strlen(str)) {      value=0;    } else {      if (start<1) start=1;      pointer=strstr(str+start-1,str2);      if (pointer==NULL) 	value=0;      else	value=pointer-str+1;    }    result=stNUMBER;    break;     case fRINSTR:    str=a1->pointer;    str2=a2->pointer;    len=strlen(str2);    for(i=strlen(str)-1;i>=0;i--) if (!strncmp(str+i,str2,len)) break;    value=i+1;    result=stNUMBER;    break;     case fRINSTR2:    str=a1->pointer;    str2=a2->pointer;    len=strlen(str2);    start=(int)a3->value;    if (start<1) {      value=0;    } else {      if (start>strlen(str)) start=strlen(str);      for(i=start-1;i;i--) if (!strncmp(str+i,str2,len)) break;      value=i+1;    }    result=stNUMBER;    break;     case fDATE:    pointer=my_malloc(100);    time(&datetime);    strftime(pointer,100,"%w-%m-%d-%Y-%a-%b",localtime(&datetime));    result=stSTRING;    break;  case fTIME:    pointer=my_malloc(100);    time(&datetime);    strftime(pointer,100,"%H-%M-%S",localtime(&datetime));    sprintf(pointer+strlen(pointer),"-%d",      (int)(time(NULL)-compilation_start));    result=stSTRING;    break;  case fSYSTEM:    str=a1->pointer;    pointer=do_system(str);    result=stSTRING;    break;  case fSYSTEM2:    str=a1->pointer;    value=do_system2(str);    result=stNUMBER;    break;  case fPEEK:    str=a1->pointer;    value=peek(str);    result=stNUMBER;    break;  case fPEEK2:    str=a1->pointer;    pointer=peek2(str,current);    result=stSTRING;    break;  case fPEEK3:    str=a1->pointer;    str2=a2->pointer;    pointer=peek3(str,str2);    result=stSTRING;    break;  case fPEEK4:    value=peekfile((int)a1->value);    result=stNUMBER;    break;  case fGETBIT:    pointer=getbit((int)a1->value,(int)a2->value,(int)a3->value,(int)a4->value);    result=stSTRING;    break;  case fGETCHAR:    pointer=getchars((int)a1->value,(int)a2->value,(int)a3->value,(int)a4->value);    result=stSTRING;    break;  case fTELL:    i=(int)(a1->value);    if (badstream(i,0)) return;    if (!(stream_modes[i] & (smREAD | smWRITE))) {      sprintf(string,"stream %d not opened",i);      error(ERROR,string);      value=0;    } else {      value=ftell(streams[i]);    }    result=stNUMBER;    break;  default:    error(ERROR,"function called but not implemented");    return;  }    stack=push();  /* copy result */  stack->type=result;  if (result==stSTRING)    stack->pointer=pointer;  else    stack->value=value;}static int do_system2(char *cmd) /* execute command as system */{#ifdef UNIX  int ret;  if (curinized) reset_shell_mode();  ret=system(cmd);  if (curinized) reset_prog_mode();  return ret;#else  STARTUPINFO start;  PROCESS_INFORMATION proc;  DWORD ec; /* exit code */  SECURITY_ATTRIBUTES prosec;  SECURITY_ATTRIBUTES thrsec;
  char *comspec;		  ZeroMemory(&prosec,sizeof(prosec));  prosec.nLength=sizeof(prosec);  prosec.bInheritHandle=TRUE;  ZeroMemory(&thrsec,sizeof(thrsec));  thrsec.nLength=sizeof(thrsec);  thrsec.bInheritHandle=TRUE;  ZeroMemory(&start,sizeof(start));  start.cb=sizeof(STARTUPINFO);   start.dwFlags=STARTF_USESTDHANDLES;  start.hStdOutput=GetStdHandle(STD_OUTPUT_HANDLE);  start.hStdError=GetStdHandle(STD_ERROR_HANDLE);  start.hStdInput=GetStdHandle(STD_INPUT_HANDLE);
  comspec=getenv("COMSPEC");
  if (!comspec) comspec="command.com";
  sprintf(string,"%s /C %s",comspec,cmd);  if (!CreateProcess(NULL,string,&prosec,&thrsec,TRUE,0,    NULL,NULL,&start,&proc)) {    sprintf(string,"couldn't execute '%s'",cmd);    error(ERROR,string);    return -1;  }  WaitForSingleObject(proc.hProcess,INFINITE);  if (!GetExitCodeProcess(proc.hProcess,&ec)) ec=-1;  CloseHandle(proc.hProcess);  CloseHandle(proc.hThread);  return ec;#endif  }static void clear_buff() /* clear system-input buffers */{  buffcurr=&buffroot;  buffcount=0;}static void store_buff(char *buff,int len) /* store system-input buffer */{  *buffcurr=my_malloc(sizeof(struct buff_chain));  memcpy((*buffcurr)->buff,buff,SYSBUFFLEN+1);  (*buffcurr)->len=len;  buffcurr=&((*buffcurr)->next);  buffcount++;}char *recall_buff() /* recall store buffer */{  struct buff_chain *curr,*old;  char *result;  int done,len;		  result=(char *)my_malloc(buffcount*(SYSBUFFLEN+1));  curr=buffroot;  len=0;  for(done=0;done<buffcount;done++) {    memcpy(result+len,curr->buff,SYSBUFFLEN);    len+=curr->len;    old=curr;    curr=curr->next;    my_free(old);  }  return result;}static char *do_system(char *cmd) /* executes command via command.com */{  static char buff[SYSBUFFLEN+1]; /* buffer to store command */  int len; /* number of bytes read */#ifdef UNIX  FILE *p; /* points to pipe */  int c; /* char read from pipe */#else  int ret;  STARTUPINFO start;  PROCESS_INFORMATION proc;  HANDLE piperead,pipewrite; /* both ends of pipes */  SECURITY_ATTRIBUTES prosec;  SECURITY_ATTRIBUTES thrsec;  char *comspec;#endif    clear_buff();  #ifdef UNIX  p=popen(cmd,"r");  if (p==NULL) {    sprintf(string,"couldn't execute '%s'",cmd);    error(ERROR,string);    return my_strdup("");  }  do {    len=0;    while(len<SYSBUFFLEN) {      c=fgetc(p);      if (c==EOF) {        buff[len]='\0';        break;      }      buff[len]=c;      len++;    }    store_buff(buff,len);  } while(c!=EOF);  pclose(p);		#else	       ZeroMemory(&prosec,sizeof(prosec));  prosec.nLength=sizeof(prosec);  prosec.bInheritHandle=TRUE;  ZeroMemory(&thrsec,sizeof(thrsec));  thrsec.nLength=sizeof(thrsec);  thrsec.bInheritHandle=TRUE;		  /* create pipe for writing */  CreatePipe(&piperead,&pipewrite,&prosec,0);		  ZeroMemory(&start,sizeof(start));  start.cb=sizeof(STARTUPINFO);   start.dwFlags=STARTF_USESTDHANDLES;  start.hStdOutput=pipewrite;  start.hStdError=pipewrite;  start.hStdInput=GetStdHandle(STD_INPUT_HANDLE);		  comspec=getenv("COMSPEC");
  if (!comspec) comspec="command.com";
  sprintf(string,"%s /C %s",comspec,cmd);  if (!CreateProcess(NULL,string,&prosec,&thrsec,TRUE,0,    NULL,NULL,&start,&proc)) {    sprintf(string,"couldn't execute '%s'",cmd);    error(ERROR,string);    return my_strdup("");  }  CloseHandle(pipewrite);		  do {    /* wait for output to arrive */    if (!ReadFile(piperead,buff,SYSBUFFLEN,(LPDWORD)&len,NULL))      ret=GetLastError();    else      ret=0;    buff[len]='\0';    if (len>0) store_buff(buff,len);  } while(ret!=ERROR_BROKEN_PIPE && ret!=ERROR_HANDLE_EOF);  CloseHandle(piperead);  CloseHandle(proc.hProcess);  CloseHandle(proc.hThread);#endif  return recall_buff();}void getmousexybm(char *s,int *px,int *py,int *pb,int *pm) /* get mouse coordinates */{  int x=0,y=0,b=0,m=0;  char c;    if (*s) {    sscanf(s,"MB%d%c+%d:%04d,%04d",&b,&c,&m,&x,&y);    if (px) *px=x;    if (py) *py=y;    if (pb) {      if (c=='d')         *pb=b;      else        *pb=-b;    }    if (pm) *pm=m;    return;  }  if (px) *px=mousex;  if (py) *py=mousey;  if (pb) *pb=mouseb;  if (pm) *pm=mousemod;}static char *dec2other(double d,int base) /* convert double to hex or binary number */{  int len;  double dec,dec2;  char *other;  int negative=FALSE;    if (d<0) {    dec2=floor(-d);    negative=TRUE;  } else {    dec2=floor(d);  }  len=negative?2:1;  for(dec=dec2;dec>=base;dec/=base) len++;  other=my_malloc(len+1);  other[len]='\0';  dec=dec2;  for(len--;len>=0;len--) {    other[len]="0123456789abcdef"[(int)(floor(dec-base*floor(dec/base)+0.5))];    dec=floor(dec/base);  }  if (negative) other[0]='-';  return other;}static double other2dec(char *hex,int base) /* convert hex or binary to double number */{  double dec;  static char *digits="0123456789abcdef";  char *found;  int i,len;    if (base!=2 && base !=16) {    sprintf(string,"Cannot convert base-%d numbers",base);    error(ERROR,string);    return 0.;  }  dec=0;  len=strlen(hex);  for(i=0;i<len;i++) {    dec*=base;    found=strchr(digits,tolower(hex[i]));    if (!found || found-digits>=base) {      sprintf(string,"Not a base-%d number: '%s'",base,hex);      error(ERROR,string);      return 0.;    }    dec+=found-digits;  }  return dec;}int myformat(char *dest,double num,char *format,char *sep) /* format number according to string */{  int i1,i2; /* dummy */  char c1; /* dummy */  static char ctrl[6];  char *found,*form;  int pre,post,dot,len,i,j,digit,colons,dots;  int neg=FALSE;  double ip,fp,round;  static char *digits="0123456789";    form=format;  if (*form=='%') { /* c-style format */    strcpy(ctrl,"+- #0"); /* allowed control chars for c-format */    form++;    while((found=strchr(ctrl,*form))!=NULL) {      *found='?';      form++;    }     if (sscanf(form,"%u.%u%c%n",&i1,&i2,&c1,&i)!=3 &&	sscanf(form,"%u.%c%n",&i2,&c1,&i)!=2 &&	sscanf(form,".%u%c%n",&i2,&c1,&i)!=2 &&	sscanf(form,"%u%c%n",&i2,&c1,&i)!=2) return FALSE;    if (!strchr("feEgG",c1) || form[i]) return FALSE;    /* seems okay, let's print */    sprintf(dest,format,num);  } else { /* basic-style format */    if (num<0) {      neg=TRUE;      num=-num;    }    colons=0;    dots=0;    pre=0;    post=0;    for(form=format;*form;form++) {      if (*form==',') {	if (dots) return FALSE;	colons++;      } else if (*form=='.') {	dots++;      } else if (*form=='#') {	if (dots) 	  post++;	else	  pre++;      } else {	return FALSE;      }    }    if (dots>1) return FALSE;    len=strlen(format);    dest[len]='\0';    round=0.5;    for(i=0;i<post;i++) round/=10.;    if (fabs(num)<round) neg=FALSE;    num+=round;    ip=floor(num);    fp=num-ip;    if (fp>1 || fp<0) fp=0;    dest[pre+colons]=format[pre+colons];    if ((int)ip) {      for(i=pre+colons-1;i>=0;i--) {	if (neg && !(int)ip) {	  neg=0;	  dest[i]='-';	} else {	  if (format[i]=='#') {	    digit=((int)ip)%10;	    ip/=10;	    if (((int)ip) || digit>0) 	      dest[i]=digits[digit];	    else	      dest[i]=' ';	  } else {	    if ((int)ip)	      dest[i]=format[i];	    else	      dest[i]=' ';	  }	}      }    } else {      i=pre+colons-1;      dest[i--]='0';    }    if ((neg && i<0) || ((int)ip)) {      strcpy(dest,format);      return TRUE;    }    if (neg) dest[i--]='-';    for(;i>=0;i--) dest[i]=' ';    for(i=pre+colons+1;i<len;i++) {      fp*=10;      digit=(int)fp;      fp-=digit;      dest[i]=digits[digit];    }    if (sep && sep[0] && sep[1]) {      for(i=0;i<len;i++) {	if (dest[i]==',') dest[i++]=sep[0];	if (dest[i]=='.') dest[i++]=sep[1];      }    }  }  return TRUE;}static char *fromto(char *str,int from,int to) /* gives back portion of string *//* from and to can be in the range 1...strlen(str) */{  int len,i;  char *part;    len=strlen(str);  if (from>to || to<0 || from>len-1) {    /* give back empty string */    part=my_malloc(1);    part[0]='\0';  }  else {    if (from<=0) from=0;    if (to>=len) to=len-1;    part=my_malloc(sizeof(char)*(to-from+2)); /* characters and '/0' */    for(i=from;i<=to;i++) part[i-from]=str[i]; /* copy */    part[i-from]='\0';  }  return part;}void mywait() /* wait given number of seconds */{  double delay;  #ifdef UNIX  struct timeval tv;#else	  MSG msg;  int timerid;

⌨️ 快捷键说明

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