📄 xlfio.c
字号:
fptr = (moreargs() ? xlgetfile() : getvalue(s_stdout)); xllastarg(); /* put character to the file */ xlputc(fptr,getchcode(chr)); /* return the character */ return (chr);}/* xwrbyte - write a byte to a file */LVAL xwrbyte(void){ LVAL fptr,chr; /* get the byte and file pointer */ chr = xlgafixnum(); fptr = (moreargs() ? xlgetfile() : getvalue(s_stdout)); xllastarg(); /* put byte to the file */ xlputc(fptr,(int)getfixnum(chr)); /* return the character */ return (chr);}/* xwrint - write an integer to a file *//* positive count means write big-endian */LVAL xwrint(void){ LVAL val, fptr; unsigned char b[4]; long i; int n = 4; int index = 3; /* where to start in array */ int incr = -1; /* how to step through array */ int v; /* get the int and file pointer and optional byte count */ val = xlgafixnum(); v = getfixnum(val); fptr = (moreargs() ? xlgetfile() : getvalue(s_stdout)); if (moreargs()) { LVAL count = typearg(fixp); n = getfixnum(count); index = n - 1; if (n < 0) { n = -n; index = 0; incr = 1; } if (n > 4) { xlerror("4-byte limit", count); } } xllastarg(); /* build output b as little-endian */ for (i = 0; i < n; i++) { b[i] = (unsigned char) v; v = v >> 8; } /* put bytes to the file */ while (n) { n--; xlputc(fptr, b[index]); index += incr; } /* return the integer */ return val;}/* xwrfloat - write a float to a file */LVAL xwrfloat(void){ LVAL val, fptr; union { char b[8]; float f; double d; } v; int n = 4; int i; int index = 3; /* where to start in array */ int incr = -1; /* how to step through array */ /* get the float and file pointer and optional byte count */ val = xlgaflonum(); fptr = (moreargs() ? xlgetfile() : getvalue(s_stdout)); if (moreargs()) { LVAL count = typearg(fixp); n = getfixnum(count); if (n < 0) { n = -n; index = 0; incr = 1; } if (n != 4 && n != 8) { xlerror("must be 4 or 8 bytes", count); } } xllastarg();#ifdef XL_BIG_ENDIAN /* flip the bytes */ index = n - 1 - index; incr = -incr;#endif /* build output v.b */ if (n == 4) v.f = (float) getflonum(val); else v.d = getflonum(val); /* put bytes to the file */ for (i = 0; i < n; i++) { xlputc(fptr, v.b[index]); index += incr; } /* return the flonum */ return val;}/* xreadline - read a line from a file */LVAL xreadline(void){ unsigned char buf[STRMAX+1],*p,*sptr; LVAL fptr,str,newstr; int len,blen,ch; /* protect some pointers */ xlsave1(str); /* get file pointer */ fptr = (moreargs() ? xlgetfile() : getvalue(s_stdin)); xllastarg(); /* get character and check for eof */ len = blen = 0; p = buf; while ((ch = xlgetc(fptr)) != EOF && ch != '\n') { /* check for buffer overflow */ if (blen >= STRMAX) { newstr = new_string(len + STRMAX + 1); sptr = getstring(newstr); *sptr = '\0'; if (str) strcat((char *) sptr, (char *) getstring(str)); *p = '\0'; strcat((char *) sptr, (char *) buf); p = buf; blen = 0; len += STRMAX; str = newstr; } /* store the character */ *p++ = ch; ++blen; } /* check for end of file */ if (len == 0 && p == buf && ch == EOF) { xlpop(); return (NIL); } /* append the last substring */ if (str == NIL || blen) { newstr = new_string(len + blen + 1); sptr = getstring(newstr); *sptr = '\0'; if (str) strcat((char *) sptr, (char *) getstring(str)); *p = '\0'; strcat((char *) sptr, (char *) buf); str = newstr; } /* restore the stack */ xlpop(); /* return the string */ return (str);}/* xmkstrinput - make a string input stream */LVAL xmkstrinput(void){ int start,end,len,i; unsigned char *str; LVAL string,val; /* protect the return value */ xlsave1(val); /* get the string and length */ string = xlgastring(); str = getstring(string); len = getslength(string) - 1; /* get the starting offset */ if (moreargs()) { val = xlgafixnum(); start = (int)getfixnum(val); } else start = 0; /* get the ending offset */ if (moreargs()) { val = xlgafixnum(); end = (int)getfixnum(val); } else end = len; xllastarg(); /* check the bounds */ if (start < 0 || start > len) xlerror("string index out of bounds",cvfixnum((FIXTYPE)start)); if (end < 0 || end > len) xlerror("string index out of bounds",cvfixnum((FIXTYPE)end)); /* make the stream */ val = newustream(); /* copy the substring into the stream */ for (i = start; i < end; ++i) xlputc(val,str[i]); /* restore the stack */ xlpop(); /* return the new stream */ return (val);}/* xmkstroutput - make a string output stream */LVAL xmkstroutput(void){ return (newustream());}/* xgetstroutput - get output stream string */LVAL xgetstroutput(void){ LVAL stream; stream = xlgaustream(); xllastarg(); return (getstroutput(stream));}/* xgetlstoutput - get output stream list */LVAL xgetlstoutput(void){ LVAL stream,val; /* get the stream */ stream = xlgaustream(); xllastarg(); /* get the output character list */ val = gethead(stream); /* empty the character list */ sethead(stream,NIL); settail(stream,NIL); /* return the list */ return (val);}/* xformat - formatted output function */LVAL xformat(void){ unsigned char *fmt; LVAL stream,val; int ch; /* protect stream in case it is a new ustream */ xlsave1(stream); /* get the stream and format string */ stream = xlgetarg(); if (stream == NIL) val = stream = newustream(); else { if (stream == s_true) stream = getvalue(s_stdout); else if (!streamp(stream) && !ustreamp(stream)) xlbadtype(stream); val = NIL; } fmt = getstring(xlgastring()); /* process the format string */ while ((ch = *fmt++)) if (ch == '~') { switch (*fmt++) { case '\0': xlerror("expecting a format directive",cvstring((char *) (fmt-1))); case 'a': case 'A': xlprint(stream,xlgetarg(),FALSE); break; case 's': case 'S': xlprint(stream,xlgetarg(),TRUE); break; case '%': xlterpri(stream); break; case '~': xlputc(stream,'~'); break; case '\n': while (*fmt && *fmt != '\n' && isspace(*fmt)) ++fmt; break; default: xlerror("unknown format directive",cvstring((char *) (fmt-1))); } } else xlputc(stream,ch); /* return the value */ if (val) val = getstroutput(val); xlpop(); return val;}/* getstroutput - get the output stream string (internal) */LOCAL LVAL getstroutput(LVAL stream){ unsigned char *str; LVAL next,val; int len,ch; /* compute the length of the stream */ for (len = 0, next = gethead(stream); next != NIL; next = cdr(next)) ++len; /* create a new string */ val = new_string(len + 1); /* copy the characters into the new string */ str = getstring(val); while ((ch = xlgetc(stream)) != EOF) *str++ = ch; *str = '\0'; /* return the string */ return (val);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -