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.
This commit is contained in:
gambas 2019-12-18 06:59:21 +01:00
parent a0af9b3990
commit 3e0398dbd1
5 changed files with 81 additions and 51 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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 \

View File

@ -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

View File

@ -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