picrin/extlib/benz/file.c

415 lines
8.4 KiB
C
Raw Normal View History

2015-05-28 10:28:55 -04:00
#include "picrin.h"
2015-06-18 14:06:57 -04:00
xFILE *xfunopen(pic_state *pic, void *cookie, int (*read)(pic_state *, void *, char *, int), int (*write)(pic_state *, void *, const char *, int), long (*seek)(pic_state *, void *, long, int), int (*close)(pic_state *, void *)) {
2015-05-28 10:28:55 -04:00
xFILE *fp;
2015-06-18 14:06:57 -04:00
for (fp = pic->files; fp < pic->files + XOPEN_MAX; fp++)
2015-05-28 10:28:55 -04:00
if ((fp->flag & (X_READ | X_WRITE)) == 0)
break; /* found free slot */
2015-06-18 14:06:57 -04:00
if (fp >= pic->files + XOPEN_MAX) /* no free slots */
2015-05-28 10:28:55 -04:00
return NULL;
fp->cnt = 0;
fp->base = NULL;
fp->flag = read? X_READ : X_WRITE;
fp->vtable.cookie = cookie;
fp->vtable.read = read;
fp->vtable.write = write;
fp->vtable.seek = seek;
fp->vtable.close = close;
return fp;
}
2015-06-18 13:05:56 -04:00
int xfclose(pic_state *pic, xFILE *fp) {
xfflush(pic, fp);
2015-05-28 10:28:55 -04:00
fp->flag = 0;
if (fp->base != fp->buf)
2015-06-18 13:05:56 -04:00
pic_free(pic, fp->base);
return fp->vtable.close(pic, fp->vtable.cookie);
2015-05-28 10:28:55 -04:00
}
2015-06-18 13:05:56 -04:00
int x_fillbuf(pic_state *pic, xFILE *fp) {
2015-05-28 10:28:55 -04:00
int bufsize;
if ((fp->flag & (X_READ|X_EOF|X_ERR)) != X_READ)
return EOF;
if (fp->base == NULL) {
if ((fp->flag & X_UNBUF) == 0) {
/* no buffer yet */
2015-06-18 13:05:56 -04:00
if ((fp->base = pic_malloc(pic, XBUFSIZ)) == NULL) {
2015-05-28 10:28:55 -04:00
/* can't get buffer, try unbuffered */
fp->flag |= X_UNBUF;
}
}
if (fp->flag & X_UNBUF) {
fp->base = fp->buf;
}
}
bufsize = (fp->flag & X_UNBUF) ? sizeof(fp->buf) : XBUFSIZ;
fp->ptr = fp->base;
fp->cnt = fp->vtable.read(pic, fp->vtable.cookie, fp->ptr, bufsize);
2015-05-28 10:28:55 -04:00
if (--fp->cnt < 0) {
if (fp->cnt == -1)
fp->flag |= X_EOF;
else
fp->flag |= X_ERR;
fp->cnt = 0;
return EOF;
}
return (unsigned char) *fp->ptr++;
}
2015-06-18 13:05:56 -04:00
int x_flushbuf(pic_state *pic, int x, xFILE *fp) {
2015-05-28 10:28:55 -04:00
int num_written=0, bufsize=0;
char c = x;
if ((fp->flag & (X_WRITE|X_EOF|X_ERR)) != X_WRITE)
return EOF;
if (fp->base == NULL && ((fp->flag & X_UNBUF) == 0)) {
/* no buffer yet */
2015-06-18 13:05:56 -04:00
if ((fp->base = pic_malloc(pic, XBUFSIZ)) == NULL) {
2015-05-28 10:28:55 -04:00
/* couldn't allocate a buffer, so try unbuffered */
fp->flag |= X_UNBUF;
} else {
fp->ptr = fp->base;
fp->cnt = XBUFSIZ - 1;
}
}
if (fp->flag & X_UNBUF) {
/* unbuffered write */
fp->ptr = fp->base = NULL;
fp->cnt = 0;
if (x == EOF)
return EOF;
num_written = fp->vtable.write(pic, fp->vtable.cookie, (const char *) &c, 1);
2015-05-28 10:28:55 -04:00
bufsize = 1;
} else {
/* buffered write */
assert(fp->ptr);
if (x != EOF) {
*fp->ptr++ = (unsigned char) c;
}
bufsize = (int)(fp->ptr - fp->base);
while(bufsize - num_written > 0) {
int t;
t = fp->vtable.write(pic, fp->vtable.cookie, fp->base + num_written, bufsize - num_written);
2015-05-28 10:28:55 -04:00
if (t < 0)
break;
num_written += t;
}
fp->ptr = fp->base;
2015-06-18 16:13:57 -04:00
fp->cnt = XBUFSIZ - 1;
2015-05-28 10:28:55 -04:00
}
if (num_written == bufsize) {
return x;
} else {
fp->flag |= X_ERR;
return EOF;
}
}
2015-06-18 13:05:56 -04:00
int xfflush(pic_state *pic, xFILE *f) {
2015-05-28 10:28:55 -04:00
int retval;
int i;
retval = 0;
if (f == NULL) {
/* flush all output streams */
for (i = 0; i < XOPEN_MAX; i++) {
2015-06-18 14:06:57 -04:00
if ((pic->files[i].flag & X_WRITE) && (xfflush(pic, &pic->files[i]) == -1))
2015-05-28 10:28:55 -04:00
retval = -1;
}
} else {
if ((f->flag & X_WRITE) == 0)
return -1;
2015-06-18 13:05:56 -04:00
x_flushbuf(pic, EOF, f);
2015-05-28 10:28:55 -04:00
if (f->flag & X_ERR)
retval = -1;
}
return retval;
}
2015-06-18 13:05:56 -04:00
int xfputc(pic_state *pic, int x, xFILE *fp) {
return xputc(pic, x, fp);
2015-05-28 10:28:55 -04:00
}
2015-06-18 13:05:56 -04:00
int xfgetc(pic_state *pic, xFILE *fp) {
return xgetc(pic, fp);
2015-05-28 10:28:55 -04:00
}
2015-06-18 13:05:56 -04:00
int xfputs(pic_state *pic, const char *s, xFILE *stream) {
2015-05-28 10:28:55 -04:00
const char *ptr = s;
while(*ptr != '\0') {
2015-06-18 13:05:56 -04:00
if (xputc(pic, *ptr, stream) == EOF)
2015-05-28 10:28:55 -04:00
return EOF;
++ptr;
}
return (int)(ptr - s);
}
2015-06-18 13:05:56 -04:00
char *xfgets(pic_state *pic, char *s, int size, xFILE *stream) {
2015-05-28 10:28:55 -04:00
int c;
char *buf;
2015-06-18 13:05:56 -04:00
xfflush(pic, NULL);
2015-05-28 10:28:55 -04:00
if (size == 0) {
return NULL;
}
buf = s;
2015-06-18 13:05:56 -04:00
while (--size > 0 && (c = xgetc(pic, stream)) != EOF) {
2015-05-28 10:28:55 -04:00
if ((*buf++ = c) == '\n')
break;
}
*buf = '\0';
return (c == EOF && buf == s) ? NULL : s;
}
2015-06-18 13:05:56 -04:00
int xputs(pic_state *pic, const char *s) {
2015-05-28 10:28:55 -04:00
int i = 1;
while(*s != '\0') {
2015-06-18 13:05:56 -04:00
if (xputchar(pic, *s++) == EOF)
2015-05-28 10:28:55 -04:00
return EOF;
i++;
}
2015-06-18 13:05:56 -04:00
if (xputchar(pic, '\n') == EOF) {
2015-05-28 10:28:55 -04:00
return EOF;
}
return i;
}
2015-06-18 13:05:56 -04:00
char *xgets(pic_state *pic, char *s) {
2015-05-28 10:28:55 -04:00
int c;
char *buf;
2015-06-18 13:05:56 -04:00
xfflush(pic, NULL);
2015-05-28 10:28:55 -04:00
buf = s;
2015-06-18 13:05:56 -04:00
while ((c = xgetchar(pic)) != EOF && c != '\n') {
2015-05-28 10:28:55 -04:00
*buf++ = c;
}
*buf = '\0';
return (c == EOF && buf == s) ? NULL : s;
}
int xungetc(int c, xFILE *fp) {
unsigned char uc = c;
if (c == EOF || fp->base == fp->ptr) {
return EOF;
}
fp->cnt++;
return *--fp->ptr = uc;
}
2015-06-18 13:05:56 -04:00
size_t xfread(pic_state *pic, void *ptr, size_t size, size_t count, xFILE *fp) {
char *bptr = ptr;
2015-05-28 10:28:55 -04:00
long nbytes;
int c;
nbytes = size * count;
while (nbytes > fp->cnt) {
memcpy(bptr, fp->ptr, fp->cnt);
2015-05-28 10:28:55 -04:00
fp->ptr += fp->cnt;
bptr += fp->cnt;
2015-05-28 10:28:55 -04:00
nbytes -= fp->cnt;
2015-06-18 13:05:56 -04:00
if ((c = x_fillbuf(pic, fp)) == EOF) {
2015-05-28 10:28:55 -04:00
return (size * count - nbytes) / size;
} else {
xungetc(c, fp);
}
}
memcpy(bptr, fp->ptr, nbytes);
2015-05-28 10:28:55 -04:00
fp->ptr += nbytes;
fp->cnt -= nbytes;
return count;
}
2015-06-18 13:05:56 -04:00
size_t xfwrite(pic_state *pic, const void *ptr, size_t size, size_t count, xFILE *fp) {
const char *bptr = ptr;
2015-05-28 10:28:55 -04:00
long nbytes;
nbytes = size * count;
while (nbytes > fp->cnt) {
memcpy(fp->ptr, bptr, fp->cnt);
2015-05-28 10:28:55 -04:00
fp->ptr += fp->cnt;
bptr += fp->cnt;
2015-05-28 10:28:55 -04:00
nbytes -= fp->cnt;
2015-06-18 13:05:56 -04:00
if (x_flushbuf(pic, EOF, fp) == EOF) {
2015-05-28 10:28:55 -04:00
return (size * count - nbytes) / size;
}
}
memcpy(fp->ptr, bptr, nbytes);
2015-05-28 10:28:55 -04:00
fp->ptr += nbytes;
fp->cnt -= nbytes;
return count;
}
2015-06-18 13:05:56 -04:00
long xfseek(pic_state *pic, xFILE *fp, long offset, int whence) {
2015-05-28 10:28:55 -04:00
long s;
2015-06-18 13:05:56 -04:00
xfflush(pic, fp);
2015-05-28 10:28:55 -04:00
fp->ptr = fp->base;
fp->cnt = 0;
if ((s = fp->vtable.seek(pic, fp->vtable.cookie, offset, whence)) != 0)
2015-05-28 10:28:55 -04:00
return s;
fp->flag &= ~X_EOF;
return 0;
}
2015-06-18 13:05:56 -04:00
long xftell(pic_state *pic, xFILE *fp) {
return xfseek(pic, fp, 0, XSEEK_CUR);
2015-05-28 10:28:55 -04:00
}
2015-06-18 13:05:56 -04:00
void xrewind(pic_state *pic, xFILE *fp) {
xfseek(pic, fp, 0, XSEEK_SET);
2015-05-28 10:28:55 -04:00
xclearerr(fp);
}
2015-06-18 13:05:56 -04:00
int xprintf(pic_state *pic, const char *fmt, ...) {
2015-05-28 10:28:55 -04:00
va_list ap;
int n;
va_start(ap, fmt);
2015-06-18 13:05:56 -04:00
n = xvfprintf(pic, xstdout, fmt, ap);
2015-05-28 10:28:55 -04:00
va_end(ap);
return n;
}
2015-06-18 13:05:56 -04:00
int xfprintf(pic_state *pic, xFILE *stream, const char *fmt, ...) {
2015-05-28 10:28:55 -04:00
va_list ap;
int n;
va_start(ap, fmt);
2015-06-18 13:05:56 -04:00
n = xvfprintf(pic, stream, fmt, ap);
2015-05-28 10:28:55 -04:00
va_end(ap);
return n;
}
2015-06-18 13:05:56 -04:00
static int print_int(pic_state *pic, xFILE *stream, long x, int base) {
2015-05-30 07:05:04 -04:00
static const char digits[] = "0123456789abcdef";
2015-05-28 10:28:55 -04:00
char buf[20];
int i, c, neg;
neg = 0;
if (x < 0) {
neg = 1;
x = -x;
}
i = 0;
do {
buf[i++] = digits[x % base];
} while ((x /= base) != 0);
if (neg) {
buf[i++] = '-';
}
c = i;
while (i-- > 0) {
2015-06-18 13:05:56 -04:00
xputc(pic, buf[i], stream);
2015-05-28 10:28:55 -04:00
}
return c;
}
2015-06-18 13:05:56 -04:00
int xvfprintf(pic_state *pic, xFILE *stream, const char *fmt, va_list ap) {
2015-05-28 10:28:55 -04:00
const char *p;
char *sval;
int ival;
void *vp;
int cnt = 0;
for (p = fmt; *p; p++) {
if (*p != '%') {
2015-06-18 13:05:56 -04:00
xputc(pic, *p, stream);
2015-05-28 10:28:55 -04:00
cnt++;
continue;
}
switch (*++p) {
case 'd':
case 'i':
ival = va_arg(ap, int);
2015-06-18 13:05:56 -04:00
cnt += print_int(pic, stream, ival, 10);
2015-05-28 10:28:55 -04:00
break;
2015-07-19 13:19:41 -04:00
#if PIC_ENABLE_LIBC
2015-07-04 04:32:16 -04:00
case 'f': {
char buf[100];
sprintf(buf, "%g", va_arg(ap, double));
xfputs(pic, buf, stream);
break;
}
2015-07-19 13:19:41 -04:00
#else
2015-07-04 04:32:16 -04:00
case 'f': {
double dval, dint;
2015-07-02 14:56:24 -04:00
dval = modf(va_arg(ap, double), &dint);
if (dint < 0 || dval < 0) { /* either may be zero */
xputc(pic, '-', stream);
cnt++;
}
cnt += print_int(pic, stream, (long)fabs(dint), 10);
2015-06-18 13:05:56 -04:00
xputc(pic, '.', stream);
2015-05-28 10:28:55 -04:00
cnt++;
2015-07-02 14:56:24 -04:00
if ((ival = fabs(fabs(dval) * 1e4) + 0.5) == 0) {
2015-06-18 13:05:56 -04:00
cnt += xfputs(pic, "0000", stream);
2015-05-28 10:28:55 -04:00
} else {
int i;
for (i = 0; i < 3 - (int)log10(ival); ++i) {
2015-06-18 13:05:56 -04:00
xputc(pic, '0', stream);
2015-05-28 10:28:55 -04:00
cnt++;
}
2015-06-18 13:05:56 -04:00
cnt += print_int(pic, stream, ival, 10);
2015-05-28 10:28:55 -04:00
}
break;
2015-07-04 04:32:16 -04:00
}
2015-05-28 10:28:55 -04:00
#endif
case 's':
sval = va_arg(ap, char*);
2015-06-18 13:05:56 -04:00
cnt += xfputs(pic, sval, stream);
2015-05-28 10:28:55 -04:00
break;
case 'p':
vp = va_arg(ap, void*);
2015-06-18 13:05:56 -04:00
cnt += xfputs(pic, "0x", stream);
cnt += print_int(pic, stream, (long)vp, 16);
2015-05-28 10:28:55 -04:00
break;
case '%':
2015-06-18 13:05:56 -04:00
xputc(pic, *(p-1), stream);
2015-05-28 10:28:55 -04:00
cnt++;
break;
default:
2015-06-18 13:05:56 -04:00
xputc(pic, '%', stream);
xputc(pic, *(p-1), stream);
2015-05-28 10:28:55 -04:00
cnt += 2;
break;
}
}
return cnt;
}
#if 0
int main()
{
char buf[256];
xgets(buf);
xprintf("%s\n", buf);
xprintf("hello\n");
xprintf("hello\n");
// xfflush(0);
}
#endif