From 3e0398dbd169787dc3da35dc584ef3ad51b79a84 Mon Sep 17 00:00:00 2001 From: gambas Date: Wed, 18 Dec 2019 06:59:21 +0100 Subject: [PATCH] OPEN NULL is a new syntax that opens a null stream. [INTERPRETER] * NEW: OPEN NULL is a new syntax that opens a null stream. Reading always returns end of file, and writing does nothing except advancing the file pointer, allowing to know how many bytes were written. [COMPILER] * NEW: OPEN NULL is a new syntax that opens a null stream. --- main/gbc/gbc_trans.h | 2 +- main/gbc/gbc_trans_subr.c | 113 ++++++++++++++++++++++---------------- main/gbx/Makefile.am | 2 +- main/gbx/gbx_stream.c | 2 + main/gbx/gbx_stream.h | 13 ++++- 5 files changed, 81 insertions(+), 51 deletions(-) diff --git a/main/gbc/gbc_trans.h b/main/gbc/gbc_trans.h index 93f0eb2c8..d59fe13b7 100644 --- a/main/gbc/gbc_trans.h +++ b/main/gbc/gbc_trans.h @@ -49,6 +49,7 @@ enum { TS_MODE_PIPE = (1 << 7), TS_MODE_MEMORY = (1 << 8), TS_MODE_STRING = (1 << 9), + TS_MODE_NULL = (1 << 10), }; enum { @@ -220,7 +221,6 @@ void TRANS_write(void); void TRANS_open(void); void TRANS_pipe(void); void TRANS_memory(void); -void TRANS_open_string(void); void TRANS_close(void); void TRANS_lock(void); void TRANS_unlock(void); diff --git a/main/gbc/gbc_trans_subr.c b/main/gbc/gbc_trans_subr.c index 3a0885e39..0d850a59e 100644 --- a/main/gbc/gbc_trans_subr.c +++ b/main/gbc/gbc_trans_subr.c @@ -342,6 +342,7 @@ void TRANS_input(void) } } + void TRANS_read_old(void) { PATTERN *save_var; @@ -375,6 +376,7 @@ void TRANS_read_old(void) JOB->current = save_current; } + void TRANS_read(void) { bool def = trans_stream_no_check(TS_STDIN); @@ -394,6 +396,60 @@ void TRANS_read(void) } } + +static void TRANS_open_null(void) +{ + int mode = TS_MODE_READ; + + // file name + + CODE_push_null(); + + // open mode + + if (TRANS_is(RS_FOR)) + { + if (TRANS_is(RS_READ)) + mode |= TS_MODE_READ | TS_MODE_DIRECT; + + if (TRANS_is(RS_WRITE)) + mode |= TS_MODE_WRITE | TS_MODE_DIRECT; + } + + CODE_push_number(mode | TS_MODE_NULL); + + TRANS_subr(TS_SUBR_OPEN, 2); +} + + +static void TRANS_open_string(void) +{ + int mode = TS_MODE_READ; + + // file name + + if (!PATTERN_is(*JOB->current, RS_FOR)) + TRANS_expression(FALSE); + else + CODE_push_null(); + + // open mode + + if (TRANS_is(RS_FOR)) + { + if (TRANS_is(RS_READ)) + mode |= TS_MODE_READ | TS_MODE_DIRECT; + + if (TRANS_is(RS_WRITE)) + mode |= TS_MODE_WRITE | TS_MODE_DIRECT; + } + + CODE_push_number(mode | TS_MODE_STRING); + + TRANS_subr(TS_SUBR_OPEN, 2); +} + + void TRANS_open(void) { int mode = TS_MODE_READ; @@ -413,13 +469,17 @@ void TRANS_open(void) TRANS_open_string(); return; } + else if (TRANS_is(RS_NULL)) + { + TRANS_open_null(); + return; + } - - /* Nom du fichier */ + // file name TRANS_expression(FALSE); - /* mode d'ouverture */ + // open mode if (TRANS_is(RS_FOR)) { @@ -438,9 +498,6 @@ void TRANS_open(void) else if (TRANS_is(RS_APPEND)) mode |= TS_MODE_APPEND; - //if (TRANS_is(RS_DIRECT)) - // mode |= TS_MODE_DIRECT; - if (TRANS_is(RS_WATCH)) mode |= TS_MODE_WATCH; @@ -458,16 +515,6 @@ void TRANS_open(void) CODE_push_number(mode); TRANS_subr(TS_SUBR_OPEN, 2); - - /*if (!TRANS_in_assignment) - { - //CODE_drop(); - - TRANS_want(RS_AS, NULL); - TRANS_ignore(RS_SHARP); - - TRANS_reference(); - }*/ } @@ -475,11 +522,11 @@ void TRANS_pipe(void) { int mode = TS_MODE_READ; - /* Nom du fichier */ + // file name TRANS_expression(FALSE); - /* mode d'ouverture */ + // open mode if (TRANS_is(RS_FOR)) { @@ -528,34 +575,6 @@ void TRANS_memory(void) } -void TRANS_open_string(void) -{ - int mode = TS_MODE_READ; - - /* Nom du fichier */ - - if (!PATTERN_is(*JOB->current, RS_FOR)) - TRANS_expression(FALSE); - else - CODE_push_null(); - - /* mode d'ouverture */ - - if (TRANS_is(RS_FOR)) - { - if (TRANS_is(RS_READ)) - mode |= TS_MODE_READ | TS_MODE_DIRECT; - - if (TRANS_is(RS_WRITE)) - mode |= TS_MODE_WRITE | TS_MODE_DIRECT; - } - - CODE_push_number(mode | TS_MODE_STRING); - - TRANS_subr(TS_SUBR_OPEN, 2); -} - - void TRANS_close(void) { if (PATTERN_is_newline(*JOB->current)) @@ -923,7 +942,7 @@ void TRANS_rmdir(void) CODE_drop(); } -void TRANS_mid() +void TRANS_mid(void) { PATTERN *str; PATTERN *pos; diff --git a/main/gbx/Makefile.am b/main/gbx/Makefile.am index 306a0d3da..d444d4859 100644 --- a/main/gbx/Makefile.am +++ b/main/gbx/Makefile.am @@ -38,7 +38,7 @@ gbx3_SOURCES = \ gbx_class_load.c gbx_class_load.h \ gbx_event.h gbx_event.c \ gb_file.h gb_file.c \ - gbx_stream.h gbx_stream.c gbx_stream_direct.c gbx_stream_lock.c gbx_stream_buffer.c gbx_stream_memory.c \ + gbx_stream.h gbx_stream.c gbx_stream_direct.c gbx_stream_lock.c gbx_stream_buffer.c gbx_stream_memory.c gbx_stream_null.c \ gbx_stream_arch.c gbx_stream_process.c gbx_stream_pipe.c gbx_stream_string.c \ gbx_project.h gbx_project.c \ gbx_library.h gbx_library.c \ diff --git a/main/gbx/gbx_stream.c b/main/gbx/gbx_stream.c index 6a04304a5..3828107d4 100644 --- a/main/gbx/gbx_stream.c +++ b/main/gbx/gbx_stream.c @@ -176,6 +176,8 @@ void STREAM_open(STREAM *stream, const char *path, int mode) sclass = &STREAM_string; else if (mode & STO_LOCK) sclass = &STREAM_lock; + else if (mode & STO_NULL) + sclass = &STREAM_null; else { // ".99" is used for opening a file descriptor in direct mode diff --git a/main/gbx/gbx_stream.h b/main/gbx/gbx_stream.h index ba5a9a231..30c68ed92 100644 --- a/main/gbx/gbx_stream.h +++ b/main/gbx/gbx_stream.h @@ -129,6 +129,13 @@ typedef } STREAM_STRING; +typedef + struct { + STREAM_COMMON common; + intptr_t pos; + } + STREAM_NULL; + typedef union STREAM { STREAM_CLASS *type; @@ -141,6 +148,7 @@ typedef STREAM_ARCH arch; STREAM_PROCESS process; STREAM_STRING string; + STREAM_NULL null; } STREAM; @@ -157,7 +165,8 @@ enum { STO_WATCH = (1 << 6), STO_PIPE = (1 << 7), STO_MEMORY = (1 << 8), - STO_STRING = (1 << 9) + STO_STRING = (1 << 9), + STO_NULL = (1 << 10) }; enum { @@ -178,7 +187,7 @@ EXTERN STREAM_CLASS STREAM_memory; EXTERN STREAM_CLASS STREAM_arch; EXTERN STREAM_CLASS STREAM_process; EXTERN STREAM_CLASS STREAM_string; -/*EXTERN STREAM_CLASS STREAM_null;*/ +EXTERN STREAM_CLASS STREAM_null; #else