From f0386657be8ec72a4b816aaecd0cb310298d20d6 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Feb 2016 14:50:12 +0900 Subject: [PATCH] cosmetic changes --- extlib/benz/file.c | 72 ++++++++----------------------- extlib/benz/include/picrin.h | 56 ++++++++++++------------ extlib/benz/include/picrin/file.h | 71 +++++++++--------------------- extlib/benz/port.c | 3 ++ extlib/benz/read.c | 3 ++ 5 files changed, 74 insertions(+), 131 deletions(-) diff --git a/extlib/benz/file.c b/extlib/benz/file.c index 4b4b274e..d5dc60b9 100644 --- a/extlib/benz/file.c +++ b/extlib/benz/file.c @@ -1,5 +1,9 @@ #include "picrin.h" +#ifndef EOF +# define EOF (-1) +#endif + 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 *)) { xFILE *fp; @@ -137,6 +141,15 @@ int xfflush(pic_state *pic, xFILE *f) { return retval; } +#define xgetc(pic, p) \ + ((--(p)->cnt >= 0) \ + ? (unsigned char) *(p)->ptr++ \ + : x_fillbuf((pic), p)) +#define xputc(pic, x, p) \ + ((--(p)->cnt >= 0 && !(((p)->flag & X_LNBUF) && (x) == '\n')) \ + ? *(p)->ptr++ = (x) \ + : x_flushbuf((pic), (x), (p))) + int xfputc(pic_state *pic, int x, xFILE *fp) { return xputc(pic, x, fp); } @@ -174,35 +187,6 @@ char *xfgets(pic_state *pic, char *s, int size, xFILE *stream) { return (c == EOF && buf == s) ? NULL : s; } -int xputs(pic_state *pic, const char *s) { - int i = 1; - - while(*s != '\0') { - if (xputchar(pic, *s++) == EOF) - return EOF; - i++; - } - if (xputchar(pic, '\n') == EOF) { - return EOF; - } - return i; -} - -char *xgets(pic_state *pic, char *s) { - int c; - char *buf; - - xfflush(pic, NULL); - - buf = s; - while ((c = xgetchar(pic)) != EOF && c != '\n') { - *buf++ = c; - } - *buf = '\0'; - - return (c == EOF && buf == s) ? NULL : s; -} - int xungetc(int c, xFILE *fp) { unsigned char uc = c; @@ -270,25 +254,6 @@ long xfseek(pic_state *pic, xFILE *fp, long offset, int whence) { return 0; } -long xftell(pic_state *pic, xFILE *fp) { - return xfseek(pic, fp, 0, XSEEK_CUR); -} - -void xrewind(pic_state *pic, xFILE *fp) { - xfseek(pic, fp, 0, XSEEK_SET); - xclearerr(fp); -} - -int xprintf(pic_state *pic, const char *fmt, ...) { - va_list ap; - int n; - - va_start(ap, fmt); - n = xvfprintf(pic, xstdout, fmt, ap); - va_end(ap); - return n; -} - int xfprintf(pic_state *pic, xFILE *stream, const char *fmt, ...) { va_list ap; int n; @@ -435,12 +400,13 @@ file_close(pic_state PIC_UNUSED(*pic), void *cookie) { } xFILE *xfopen_file(pic_state *pic, FILE *fp, const char *mode) { - switch (*mode) { - case 'r': - return xfunopen(pic, fp, file_read, 0, file_seek, file_close); - default: - return xfunopen(pic, fp, 0, file_write, file_seek, file_close); + xFILE *f; + if (*mode == 'r') { + f = xfunopen(pic, fp, file_read, 0, file_seek, file_close); + } else { + f = xfunopen(pic, fp, 0, file_write, file_seek, file_close); } + return f; } #endif diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 03d5b6f0..b32f641c 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -149,33 +149,35 @@ struct pic_string *pic_vstrf_value(pic_state *, const char *fmt, va_list ap); struct pic_blob *pic_blob_value(pic_state *, const unsigned char *buf, int len); struct pic_data *pic_data_value(pic_state *, void *ptr, const pic_data_type *type); -#define PIC_TYPE_INVALID 1 -#define PIC_TYPE_FLOAT 2 -#define PIC_TYPE_INT 3 -#define PIC_TYPE_CHAR 4 -#define PIC_TYPE_EOF 5 -#define PIC_TYPE_UNDEF 6 -#define PIC_TYPE_TRUE 8 -#define PIC_TYPE_NIL 7 -#define PIC_TYPE_FALSE 9 -#define PIC_IVAL_END 10 -/* --------------------- */ -#define PIC_TYPE_STRING 16 -#define PIC_TYPE_VECTOR 17 -#define PIC_TYPE_BLOB 18 -#define PIC_TYPE_PROC 19 -#define PIC_TYPE_PORT 20 -#define PIC_TYPE_ERROR 21 -#define PIC_TYPE_ID 22 -#define PIC_TYPE_ENV 23 -#define PIC_TYPE_DATA 24 -#define PIC_TYPE_DICT 25 -#define PIC_TYPE_WEAK 26 -#define PIC_TYPE_RECORD 27 -#define PIC_TYPE_SYMBOL 28 -#define PIC_TYPE_PAIR 29 -#define PIC_TYPE_CXT 30 -#define PIC_TYPE_CP 31 +enum { + PIC_TYPE_INVALID = 1, + PIC_TYPE_FLOAT = 2, + PIC_TYPE_INT = 3, + PIC_TYPE_CHAR = 4, + PIC_TYPE_EOF = 5, + PIC_TYPE_UNDEF = 6, + PIC_TYPE_TRUE = 8, + PIC_TYPE_NIL = 7, + PIC_TYPE_FALSE = 9, + PIC_IVAL_END = 10, +/* -------------------- */ + PIC_TYPE_STRING = 16, + PIC_TYPE_VECTOR = 17, + PIC_TYPE_BLOB = 18, + PIC_TYPE_PROC = 19, + PIC_TYPE_PORT = 20, + PIC_TYPE_ERROR = 21, + PIC_TYPE_ID = 22, + PIC_TYPE_ENV = 23, + PIC_TYPE_DATA = 24, + PIC_TYPE_DICT = 25, + PIC_TYPE_WEAK = 26, + PIC_TYPE_RECORD = 27, + PIC_TYPE_SYMBOL = 28, + PIC_TYPE_PAIR = 29, + PIC_TYPE_CXT = 30, + PIC_TYPE_CP = 31 +}; #define pic_undef_p(pic,v) (pic_type(pic,v) == PIC_TYPE_UNDEF) #define pic_int_p(pic,v) (pic_type(pic,v) == PIC_TYPE_INT) diff --git a/extlib/benz/include/picrin/file.h b/extlib/benz/include/picrin/file.h index d23479ad..2118934c 100644 --- a/extlib/benz/include/picrin/file.h +++ b/extlib/benz/include/picrin/file.h @@ -5,10 +5,6 @@ extern "C" { #endif -#ifndef EOF -# define EOF (-1) -#endif - #define XBUFSIZ 1024 #define XOPEN_MAX 1024 @@ -33,7 +29,7 @@ typedef struct { #define xstdout (&pic->files[1]) #define xstderr (&pic->files[2]) -enum _flags { +enum { X_READ = 01, X_WRITE = 02, X_UNBUF = 04, @@ -45,23 +41,29 @@ enum _flags { #define xclearerr(p) ((p)->flag &= ~(X_EOF | X_ERR)) #define xfeof(p) (((p)->flag & X_EOF) != 0) #define xferror(p) (((p)->flag & X_ERR) != 0) -#define xfileno(p) ((p)->fd) -#define xgetc(pic, p) \ - ((--(p)->cnt >= 0) \ - ? (unsigned char) *(p)->ptr++ \ - : x_fillbuf((pic), p)) -#define xputc(pic, x, p) \ - ((--(p)->cnt >= 0 && !(((p)->flag & X_LNBUF) && (x) == '\n')) \ - ? *(p)->ptr++ = (x) \ - : x_flushbuf((pic), (x), (p))) -#define xgetchar(pic) xgetc((pic), xstdin) -#define xputchar(pic, x) xputc((pic), (x), xstdout) - -/* resource aquisition */ xFILE *xfunopen(pic_state *, 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 *)); + +enum { + XSEEK_CUR, + XSEEK_END, + XSEEK_SET +}; + +size_t xfread(pic_state *, void *, size_t, size_t, xFILE *); +size_t xfwrite(pic_state *, const void *, size_t, size_t, xFILE *); +long xfseek(pic_state *, xFILE *, long, int); +int xfflush(pic_state *, xFILE *); int xfclose(pic_state *, xFILE *); +int xfputc(pic_state *, int, xFILE *); +int xfgetc(pic_state *, xFILE *); +int xfputs(pic_state *, const char *, xFILE *); +char *xfgets(pic_state *, char *, int, xFILE *); +int xungetc(int, xFILE *); +int xfprintf(pic_state *, xFILE *, const char *, ...); +int xvfprintf(pic_state *, xFILE *, const char *, va_list); + #if PIC_ENABLE_STDIO xFILE *xfopen_file(pic_state *, FILE *, const char *mode); #endif @@ -69,39 +71,6 @@ xFILE *xfopen_buf(pic_state *, const char *buf, int len, const char *mode); int xfget_buf(pic_state *, xFILE *file, const char **buf, int *len); xFILE *xfopen_null(pic_state *, const char *mode); -/* buffer management */ -int x_fillbuf(pic_state *, xFILE *); -int x_flushbuf(pic_state *, int, xFILE *); -int xfflush(pic_state *, xFILE *); - -/* direct IO */ -size_t xfread(pic_state *, void *, size_t, size_t, xFILE *); -size_t xfwrite(pic_state *, const void *, size_t, size_t, xFILE *); - -enum { - XSEEK_CUR, - XSEEK_END, - XSEEK_SET -}; - -/* indicator positioning */ -long xfseek(pic_state *, xFILE *, long, int); -long xftell(pic_state *, xFILE *); -void xrewind(pic_state *, xFILE *); - -/* character IO */ -int xfputc(pic_state *, int, xFILE *); -int xfgetc(pic_state *, xFILE *); -int xfputs(pic_state *, const char *, xFILE *); -char *xfgets(pic_state *, char *, int, xFILE *); -int xputs(pic_state *, const char *); -int xungetc(int, xFILE *); - -/* formatted I/O */ -int xprintf(pic_state *, const char *, ...); -int xfprintf(pic_state *, xFILE *, const char *, ...); -int xvfprintf(pic_state *, xFILE *, const char *, va_list); - #if defined(__cplusplus) } #endif diff --git a/extlib/benz/port.c b/extlib/benz/port.c index 86373553..136de293 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -5,6 +5,9 @@ #include "picrin.h" #include "picrin/object.h" +#undef EOF +#define EOF (-1) + struct pic_port * pic_make_port(pic_state *pic, xFILE *file) { diff --git a/extlib/benz/read.c b/extlib/benz/read.c index fbad22f7..11d46184 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -5,6 +5,9 @@ #include "picrin.h" #include "picrin/object.h" +#undef EOF +#define EOF (-1) + KHASH_DEFINE(read, int, pic_value, kh_int_hash_func, kh_int_hash_equal) static pic_value read(pic_state *pic, struct pic_port *port, int c);