From aa67ba1309bde70e8b528be833ba06a8d124cbd1 Mon Sep 17 00:00:00 2001 From: gambas Date: Wed, 27 Sep 2017 02:48:00 +0200 Subject: [PATCH] Rework the stream interface, so that the stream read & write methods just have to actually read or write the data. [INTERPRETER] * NEW: Rework the stream interface, so that the stream read & write methods just have to actually read or write the data. --- gb.compress.bzlib2/src/main.c | 14 +- gb.compress.bzlib2/src/main.h | 1 - gb.compress.zlib/src/main.c | 19 +-- gb.db.odbc/src/main.c | 4 +- gb.db.postgresql/src/main.c | 4 +- gb.net.curl/src/CCurl.c | 4 +- gb.net/src/CSocket.c | 22 +-- gb.net/src/CUdpSocket.c | 15 +- main/gbx/gbx_api.c | 11 +- main/gbx/gbx_api.h | 2 +- main/gbx/gbx_archive.c | 4 +- main/gbx/gbx_archive.h | 2 +- main/gbx/gbx_c_task.c | 8 +- main/gbx/gbx_stream.c | 257 +++++++++++++++++++--------------- main/gbx/gbx_stream.h | 10 +- main/gbx/gbx_stream_arch.c | 15 +- main/gbx/gbx_stream_buffer.c | 33 ++--- main/gbx/gbx_stream_direct.c | 7 +- main/gbx/gbx_stream_memory.c | 11 +- main/gbx/gbx_stream_pipe.c | 6 +- main/gbx/gbx_stream_process.c | 6 +- main/gbx/gbx_stream_string.c | 10 +- main/gbx/gbx_subr_file.c | 11 +- main/share/gambas.h | 1 - main/share/gb_arch_temp.h | 7 - 25 files changed, 219 insertions(+), 265 deletions(-) diff --git a/gb.compress.bzlib2/src/main.c b/gb.compress.bzlib2/src/main.c index 45b6f450e..a29ecdb53 100644 --- a/gb.compress.bzlib2/src/main.c +++ b/gb.compress.bzlib2/src/main.c @@ -72,7 +72,6 @@ GB_STREAM_DESC BZStream = open: BZ_stream_open, close: BZ_stream_close, read: BZ_stream_read, - getchar: BZ_stream_getchar, write: BZ_stream_write, seek: BZ_stream_seek, tell: BZ_stream_tell, @@ -481,7 +480,7 @@ static int BZ_stream_write(GB_STREAM *stream, char *buffer, int len) if (P_MODE==MODE_READ) return -1; BZ2_bzWrite ( &bzerror,P_BZ, (void*)buffer, len); - if (!bzerror) { P_POS+=len; return 0; } + if (!bzerror) { P_POS += len; return len; } //BZ2_bzWriteClose (&bzerror,P_BZ,0,NULL,NULL); //fclose(P_FILE); @@ -508,18 +507,16 @@ static int BZ_stream_read(GB_STREAM *stream, char *buffer, int len) if (!bzerror) { - GB.Stream.SetBytesRead(stream, len2); P_POS += len2; - return 0; + return len2; } else { if ((len2 == len) && (bzerror == BZ_STREAM_END)) { - GB.Stream.SetBytesRead(stream, len2); P_POS += len2; P_EOF = 1; - return 0; + return len2; } } @@ -530,11 +527,6 @@ static int BZ_stream_read(GB_STREAM *stream, char *buffer, int len) return -1; } -static int BZ_stream_getchar(GB_STREAM *stream, char *buffer) -{ - return BZ_stream_read(stream,buffer,1); -} - /**************************************************************************** This array of functions defines what functions the compression component diff --git a/gb.compress.bzlib2/src/main.h b/gb.compress.bzlib2/src/main.h index bc0ff5edd..f4f7c9f13 100644 --- a/gb.compress.bzlib2/src/main.h +++ b/gb.compress.bzlib2/src/main.h @@ -44,7 +44,6 @@ static int BZ_stream_close(GB_STREAM *stream); static int BZ_stream_write(GB_STREAM *stream, char *buffer, int len); static int BZ_stream_eof(GB_STREAM *stream); static int BZ_stream_read(GB_STREAM *stream, char *buffer, int len); -static int BZ_stream_getchar(GB_STREAM *stream, char *buffer); #endif /* __MAIN_H */ diff --git a/gb.compress.zlib/src/main.c b/gb.compress.zlib/src/main.c index 7daa911a0..8fc71f301 100644 --- a/gb.compress.zlib/src/main.c +++ b/gb.compress.zlib/src/main.c @@ -454,14 +454,9 @@ static int CZ_stream_write(GB_STREAM *stream, char *buffer, int len) STREAM_COMPRESS *s = (STREAM_COMPRESS *)stream; if (s->mode == MODE_READ) - return TRUE; + return -1; - if ( gzwrite (s->handle, (voidp)buffer, (unsigned)len) == len) - return FALSE; - - //gzclose (s->handle); - //stream->desc=NULL; - return TRUE; + return gzwrite(s->handle, (voidp)buffer, (unsigned)len); } static int CZ_stream_eof(GB_STREAM *stream) @@ -472,19 +467,11 @@ static int CZ_stream_eof(GB_STREAM *stream) static int CZ_stream_read(GB_STREAM *stream, char *buffer, int len) { STREAM_COMPRESS *s = (STREAM_COMPRESS *)stream; - int n; if (s->mode == MODE_WRITE) return -1; - n = gzread(s->handle, (voidp)buffer, (unsigned)len); - if (n > 0) - { - GB.Stream.SetBytesRead(stream, n); - return 0; - } - else - return -1; + return gzread(s->handle, (voidp)buffer, (unsigned)len); } diff --git a/gb.db.odbc/src/main.c b/gb.db.odbc/src/main.c index 40a2bf47f..e255cdaa1 100644 --- a/gb.db.odbc/src/main.c +++ b/gb.db.odbc/src/main.c @@ -2569,7 +2569,7 @@ fflush(stderr); else comma = TRUE; - DB.Query.Add(fp->name); + DB.Query.Add(fp->name); //AB autoincrement field is mapped to Integer because this is Database dependent if (fp->type == DB_T_SERIAL) @@ -3363,4 +3363,4 @@ int EXPORT GB_INIT(void) void EXPORT GB_EXIT() { -} \ No newline at end of file +} diff --git a/gb.db.postgresql/src/main.c b/gb.db.postgresql/src/main.c index b99582383..c64482efc 100644 --- a/gb.db.postgresql/src/main.c +++ b/gb.db.postgresql/src/main.c @@ -1259,7 +1259,7 @@ static int begin_transaction(DB_DATABASE *db) { char buffer[8]; sprintf(buffer, "%d", trans - 1); - return do_query(db, "Unable to begin transaction: &1", NULL, "SAVEPOINT t&1", 1, buffer); + return do_query(db, "Unable to begin transaction: Unable to define savepoint: &1", NULL, "SAVEPOINT t&1", 1, buffer); } } @@ -1290,7 +1290,7 @@ static int commit_transaction(DB_DATABASE *db) { char buffer[8]; sprintf(buffer, "%d", trans); - return do_query(db, "Unable to begin transaction: &1", NULL, "RELEASE SAVEPOINT t&1", 1, buffer); + return do_query(db, "Unable to commit transaction: Unable to release savepoint: &1", NULL, "RELEASE SAVEPOINT t&1", 1, buffer); } } diff --git a/gb.net.curl/src/CCurl.c b/gb.net.curl/src/CCurl.c index c39a3f778..2d0fc53ee 100644 --- a/gb.net.curl/src/CCurl.c +++ b/gb.net.curl/src/CCurl.c @@ -162,9 +162,7 @@ int CCURL_stream_read(GB_STREAM *stream, char *buffer, int len) GB.FreeString(&THIS->data); THIS->data = new_data; - GB.Stream.SetBytesRead(stream, len); - - return 0; + return len; } static void raise_event(void *_object, int event) diff --git a/gb.net/src/CSocket.c b/gb.net/src/CSocket.c index 787413b50..e8c18549f 100644 --- a/gb.net/src/CSocket.c +++ b/gb.net/src/CSocket.c @@ -448,16 +448,12 @@ int CSocket_stream_read(GB_STREAM *stream, char *buffer, int len) if (bytes < len) len = bytes; - USE_MSG_NOSIGNAL(npos=recv(SOCKET->socket,(void*)buffer,len*sizeof(char),MSG_NOSIGNAL)); - - GB.Stream.SetBytesRead(stream, npos); - - if (npos==len) return 0; + USE_MSG_NOSIGNAL(npos = recv(SOCKET->socket,(void*)buffer,len*sizeof(char),MSG_NOSIGNAL)); if (npos < 0 && errno != EAGAIN) CSocket_stream_internal_error(THIS, NET_CANNOT_READ, FALSE); - return -1; + return npos; } int CSocket_stream_write(GB_STREAM *stream, char *buffer, int len) @@ -467,14 +463,7 @@ int CSocket_stream_write(GB_STREAM *stream, char *buffer, int len) if (!THIS) return -1; - while (len > 0) - { - USE_MSG_NOSIGNAL(npos=send(SOCKET->socket,(void*)buffer,len*sizeof(char),MSG_NOSIGNAL)); - if (npos <= 0) - break; - len -= npos; - buffer += npos; - } + USE_MSG_NOSIGNAL(npos = send(SOCKET->socket,(void*)buffer,len*sizeof(char),MSG_NOSIGNAL)); if (npos >= 0 || errno == EAGAIN) { @@ -486,13 +475,10 @@ int CSocket_stream_write(GB_STREAM *stream, char *buffer, int len) } } - if (npos >= 0) - return 0; - if (errno != EAGAIN) CSocket_stream_internal_error(THIS, NET_CANNOT_WRITE, FALSE); - return -1; + return npos; } diff --git a/gb.net/src/CUdpSocket.c b/gb.net/src/CUdpSocket.c index d25f94738..f3c20909f 100644 --- a/gb.net/src/CUdpSocket.c +++ b/gb.net/src/CUdpSocket.c @@ -226,9 +226,7 @@ int CUdpSocket_stream_read(GB_STREAM *stream, char *buffer, int len) memcpy(buffer, &THIS->buffer[THIS->buffer_pos], len); THIS->buffer_pos += len; - GB.Stream.SetBytesRead(stream, len); - - return 0; + return len; } int CUdpSocket_stream_write(GB_STREAM *stream, char *buffer, int len) @@ -275,12 +273,13 @@ int CUdpSocket_stream_write(GB_STREAM *stream, char *buffer, int len) USE_MSG_NOSIGNAL(retval = sendto(SOCKET->socket, (void*)buffer, len, MSG_NOSIGNAL, addr, size)); - if (retval >= 0) - return 0; + if (retval < 0) + { + CUdpSocket_stream_close(stream); + SOCKET->status= NET_CANNOT_WRITE; + } - CUdpSocket_stream_close(stream); - SOCKET->status= NET_CANNOT_WRITE; - return -1; + return retval; } /************************************************************************************************ diff --git a/main/gbx/gbx_api.c b/main/gbx/gbx_api.c index 27e7dc569..4d3be7287 100644 --- a/main/gbx/gbx_api.c +++ b/main/gbx/gbx_api.c @@ -280,7 +280,6 @@ const void *const GAMBAS_Api[] = (void *)GB_HashTableFirst, (void *)GB_StreamGet, - (void *)GB_StreamSetBytesRead, (void *)GB_StreamSetSwapping, (void *)GB_StreamSetAvailableNow, (void *)GB_StreamBlock, @@ -2267,11 +2266,6 @@ GB_STREAM *GB_StreamGet(void *object) return (GB_STREAM *)CSTREAM_stream(object); } -void GB_StreamSetBytesRead(GB_STREAM *stream, int length) -{ - STREAM_eff_read += length; -} - void GB_StreamSetSwapping(GB_STREAM *stream, int v) { ((STREAM *)stream)->common.swap = v; @@ -2297,10 +2291,7 @@ int GB_StreamRead(GB_STREAM *stream, void *addr, int len) if (len < 0) ret = STREAM_read_max((STREAM *)stream, addr, -len); else - { - STREAM_read((STREAM *)stream, addr, len); - ret = STREAM_eff_read; - } + ret = STREAM_read((STREAM *)stream, addr, len); } END_CATCH_ERROR_INT } diff --git a/main/gbx/gbx_api.h b/main/gbx/gbx_api.h index 82bb71e62..6e59216d1 100644 --- a/main/gbx/gbx_api.h +++ b/main/gbx/gbx_api.h @@ -200,7 +200,7 @@ int GB_CountArray(void *data); void *GB_Add(void *pdata); GB_STREAM *GB_StreamGet(void *); -void GB_StreamSetBytesRead(GB_STREAM *, int); +//void GB_StreamSetBytesRead(GB_STREAM *, int); void GB_StreamSetSwapping(GB_STREAM *, int); void GB_StreamSetAvailableNow(GB_STREAM *, int); bool GB_StreamBlock(GB_STREAM *, int); diff --git a/main/gbx/gbx_archive.c b/main/gbx/gbx_archive.c index cca7d92d5..3c7b9e028 100644 --- a/main/gbx/gbx_archive.c +++ b/main/gbx/gbx_archive.c @@ -524,9 +524,9 @@ void ARCHIVE_stat(ARCHIVE *arch, const char *path, FILE_STAT *info) } -bool ARCHIVE_read(ARCHIVE *arch, int pos, void *buffer, int len) +void ARCHIVE_read(ARCHIVE *arch, int pos, void *buffer, int len) { - return ARCH_read(arch->arch, pos, buffer, len); + ARCH_read(arch->arch, pos, buffer, len); } diff --git a/main/gbx/gbx_archive.h b/main/gbx/gbx_archive.h index 7a563b5cf..6250dec6a 100644 --- a/main/gbx/gbx_archive.h +++ b/main/gbx/gbx_archive.h @@ -79,7 +79,7 @@ void ARCHIVE_load_exported_class(ARCHIVE *arch, int pass); bool ARCHIVE_get(ARCHIVE *arch, const char **ppath, ARCHIVE_FIND *find); -bool ARCHIVE_read(ARCHIVE *arch, int pos, void *buffer, int len); +void ARCHIVE_read(ARCHIVE *arch, int pos, void *buffer, int len); bool ARCHIVE_exist(ARCHIVE *arch, const char *path); void ARCHIVE_stat(ARCHIVE *arch, const char *path, FILE_STAT *info); diff --git a/main/gbx/gbx_c_task.c b/main/gbx/gbx_c_task.c index 2a3188957..3bbad7201 100644 --- a/main/gbx/gbx_c_task.c +++ b/main/gbx/gbx_c_task.c @@ -578,6 +578,7 @@ BEGIN_PROPERTY(Task_Value) FILE_STAT info; char *err = NULL; int fd; + int n; if (!THIS->child && THIS->stopped) { @@ -629,7 +630,12 @@ BEGIN_PROPERTY(Task_Value) fd = open(path, O_RDONLY); if (fd < 0) goto __ERROR; - STREAM_read_direct(fd, err, info.size); + for(;;) + { + n = read(fd, err, info.size); + if (n == info.size || errno != EINTR) + break; + } close(fd); GB_Error("Task has failed: &1", err); diff --git a/main/gbx/gbx_stream.c b/main/gbx/gbx_stream.c index 4701d8a99..93f2e1e55 100644 --- a/main/gbx/gbx_stream.c +++ b/main/gbx/gbx_stream.c @@ -54,8 +54,6 @@ #include "gbx_struct.h" #include "gbx_stream.h" -int STREAM_eff_read; - static STREAM _temp_stream = { 0 }; static STREAM *_temp_save = NULL; static int _temp_level; @@ -319,52 +317,68 @@ void STREAM_flush(STREAM *stream) (*(stream->type->flush))(stream); } -static void read_buffer(STREAM *stream, void **addr, int *len) +static int read_buffer(STREAM *stream, void *addr, int len) { int l = stream->common.buffer_len - stream->common.buffer_pos; - if (l > *len) - l = *len; + if (l > len) + l = len; if (l > 0) { - memcpy(*addr, stream->common.buffer + stream->common.buffer_pos, l); - *addr = (char *)*addr + l; - *len -= l; - STREAM_eff_read += l; + memcpy(addr, stream->common.buffer + stream->common.buffer_pos, l); stream->common.buffer_pos += l; + return l; } + else + return 0; } -void STREAM_read(STREAM *stream, void *addr, int len) +int STREAM_read(STREAM *stream, void *addr, int len) { - STREAM_eff_read = 0; - + int eff = 0; + int n; + if (STREAM_is_closed(stream)) THROW(E_CLOSED); if (len <= 0) - return; + return 0; if (stream->common.buffer) - read_buffer(stream, &addr, &len); - - while ((*(stream->type->read))(stream, addr, len)) { - if (errno == EINTR) - continue; - - stop_watching(stream, GB_WATCH_READ); - - switch(errno) - { - case 0: - case EAGAIN: - THROW(E_EOF); - case EIO: - THROW(E_READ); - default: - THROW_SYSTEM(errno, NULL); - } + eff = read_buffer(stream, addr, len); + addr += eff; + len -= eff; } + + while (len > 0) + { + n = (*(stream->type->read))(stream, addr, len); + + if ((n <= 0) && errno != EINTR) + { + stop_watching(stream, GB_WATCH_READ); + + if (n == 0) + THROW(E_EOF); + + switch(errno) + { + case 0: + case EAGAIN: + THROW(E_EOF); + case EIO: + THROW(E_READ); + default: + THROW_SYSTEM(errno, NULL); + } + } + + eff += n; + addr += n; + len -= n; + } + + return eff; } #if 0 @@ -386,7 +400,7 @@ char STREAM_getchar(STREAM *stream) else ret = (*(stream->type->read))(stream, &c, 1); - if (!ret) + if (ret == 1) break; if (errno == EINTR) @@ -413,74 +427,82 @@ char STREAM_getchar(STREAM *stream) int STREAM_read_max(STREAM *stream, void *addr, int len) { - bool err; + int eff = 0; + int n; int flags, handle; int save_errno; if (!stream->type) THROW(E_CLOSED); - - STREAM_eff_read = 0; + + if (len <= 0) + return 0; if (stream->common.buffer) - read_buffer(stream, &addr, &len); - - if (len > 0) { - for(;;) + eff += read_buffer(stream, addr, len); + addr += eff; + len -= eff; + } + + while (len > 0) + { + if (stream->common.available_now) { - if (stream->common.available_now) + n = (*(stream->type->read))(stream, addr, len); + } + else + { + handle = STREAM_handle(stream); + flags = fcntl(handle, F_GETFL); + if ((flags & O_NONBLOCK) == 0) { - err = (*(stream->type->read))(stream, addr, len); - } - else - { - handle = STREAM_handle(stream); - flags = fcntl(handle, F_GETFL); - if ((flags & O_NONBLOCK) == 0) - { - wait_for_fd_ready_to_read(handle); - fcntl(handle, F_SETFL, flags | O_NONBLOCK); - } - - errno = 0; - err = (*(stream->type->read))(stream, addr, len); - save_errno = errno; - - if ((flags & O_NONBLOCK) == 0) - fcntl(handle, F_SETFL, flags); - - errno = save_errno; + wait_for_fd_ready_to_read(handle); + fcntl(handle, F_SETFL, flags | O_NONBLOCK); } - if (!err) - break; + errno = 0; + n = (*(stream->type->read))(stream, addr, len); + save_errno = errno; - if (errno == EINTR) - continue; + if ((flags & O_NONBLOCK) == 0) + fcntl(handle, F_SETFL, flags); - if (errno == 0) + errno = save_errno; + } + + if (n > 0) + eff += n; + + if (n <= 0 && errno != EINTR) + { + if (n == 0 || errno == 0) + { stop_watching(stream, GB_WATCH_READ); - + return eff; + } + switch(errno) { - case 0: case EAGAIN: - return STREAM_eff_read; - case EIO: - return -1; //THROW(E_READ); + return eff; default: THROW_SYSTEM(errno, NULL); } } + + addr += n; + len -= n; } - return STREAM_eff_read; + return eff; } void STREAM_write(STREAM *stream, void *addr, int len) { + int n; + if (STREAM_is_closed_for_writing(stream)) THROW(E_CLOSED); @@ -490,17 +512,24 @@ void STREAM_write(STREAM *stream, void *addr, int len) if (stream->common.redirected) stream = stream->common.redirect; - while ((*(stream->type->write))(stream, addr, len)) + while (len > 0) { - switch(errno) + n = ((*(stream->type->write))(stream, addr, len)); + + if (n <= 0 && errno != EINTR) { - case EINTR: - break; - case EIO: - THROW(E_WRITE); - default: - THROW_SYSTEM(errno, NULL); + switch(errno) + { + case 0: + case EIO: + THROW(E_WRITE); + default: + THROW_SYSTEM(errno, NULL); + } } + + addr += n; + len -= n; } } @@ -565,18 +594,20 @@ void STREAM_seek(STREAM *stream, int64_t pos, int whence) release_buffer(stream); } -static void fill_buffer(STREAM *stream, char *addr, bool do_not_wait_ready) +static int fill_buffer(STREAM *stream, char *addr, bool do_not_wait_ready) { - bool err; + int n; int flags, fd; + int len; + int eff = 0; - STREAM_eff_read = 0; fd = STREAM_handle(stream); + len = STREAM_BUFFER_SIZE; - for(;;) + while (len > 0) { if (stream->common.available_now) - err = (*(stream->type->read))(stream, addr, STREAM_BUFFER_SIZE); + n = (*(stream->type->read))(stream, addr, len); else { if (!do_not_wait_ready) @@ -586,7 +617,7 @@ static void fill_buffer(STREAM *stream, char *addr, bool do_not_wait_ready) if ((flags & O_NONBLOCK) == 0) fcntl(fd, F_SETFL, flags | O_NONBLOCK); - err = (*(stream->type->read))(stream, addr, STREAM_BUFFER_SIZE); + n = (*(stream->type->read))(stream, addr, len); if ((flags & O_NONBLOCK) == 0) { @@ -596,31 +627,36 @@ static void fill_buffer(STREAM *stream, char *addr, bool do_not_wait_ready) } } - if (!err) - break; - - if (errno == EINTR) - continue; - - //if (STREAM_eff_read == 0 && stream->common.available_now) - // WATCH_need_little_sleep = TRUE; - - switch(errno) + if (n == 0) + return eff; + + if (n > 0) + eff += n; + + if (n < 0 && errno != EINTR) { - case 0: - case EAGAIN: - case EINPROGRESS: - return; - case EIO: - return; //THROW(E_READ); - default: - THROW_SYSTEM(errno, NULL); + switch(errno) + { + case EAGAIN: + case EINPROGRESS: + case EIO: + return eff; + default: + THROW_SYSTEM(errno, NULL); + } } + + addr += n; + len -= n; } + + return eff; } bool STREAM_read_ahead(STREAM *stream) { + int eff; + if (stream->common.no_read_ahead) return FALSE; @@ -630,14 +666,12 @@ bool STREAM_read_ahead(STREAM *stream) if (!stream->common.buffer) ALLOC(&stream->common.buffer, STREAM_BUFFER_SIZE); - fill_buffer(stream, stream->common.buffer, TRUE); + eff = fill_buffer(stream, stream->common.buffer, TRUE); stream->common.buffer_pos = 0; - stream->common.buffer_len = STREAM_eff_read; + stream->common.buffer_len = eff; - //fprintf(stderr, "STREAM_read_ahead: %d\n", STREAM_eff_read); - - if (STREAM_eff_read == 0) + if (eff == 0) { stream->common.eof = TRUE; return TRUE; @@ -816,10 +850,8 @@ static char *input(STREAM *stream, bool line, char *escape) stream->common.buffer_pos = buffer_pos; stream->common.buffer_len = buffer_len; - fill_buffer(stream, buffer, FALSE); - buffer_pos = 0; - buffer_len = STREAM_eff_read; + buffer_len = fill_buffer(stream, buffer, FALSE); if (!buffer_len) { @@ -1671,6 +1703,7 @@ bool STREAM_eof(STREAM *stream) } +#if 0 int STREAM_read_direct(int fd, char *buffer, int len) { ssize_t eff_read; @@ -1722,7 +1755,7 @@ int STREAM_write_direct(int fd, char *buffer, int len) return FALSE; } - +#endif void STREAM_blocking(STREAM *stream, bool block) { diff --git a/main/gbx/gbx_stream.h b/main/gbx/gbx_stream.h index 799115b5a..d08b8669c 100644 --- a/main/gbx/gbx_stream.h +++ b/main/gbx/gbx_stream.h @@ -34,7 +34,6 @@ typedef int (*open)(union STREAM *stream, const char *path, int mode, void *data); int (*close)(union STREAM *stream); int (*read)(union STREAM *stream, char *buffer, int len); - int (*getchar)(union STREAM *stream, char *buffer); int (*write)(union STREAM *stream, char *buffer, int len); int (*seek)(union STREAM *stream, int64_t pos, int whence); int (*tell)(union STREAM *stream, int64_t *pos); @@ -165,7 +164,7 @@ enum { ST_EOL_MAC = 2 }; -EXTERN int STREAM_eff_read; +//EXTERN int STREAM_eff_read; #ifndef __STREAM_IMPL_C @@ -186,7 +185,6 @@ STREAM_CLASS stream = \ (void *)stream_open, \ (void *)stream_close, \ (void *)stream_read, \ - (void *)stream_getchar, \ (void *)stream_write, \ (void *)stream_seek, \ (void *)stream_tell, \ @@ -212,7 +210,7 @@ char *STREAM_line_input(STREAM *stream, char *escape); char *STREAM_input(STREAM *stream); int64_t STREAM_tell(STREAM *stream); void STREAM_seek(STREAM *stream, int64_t pos, int whence); -void STREAM_read(STREAM *stream, void *addr, int len); +int STREAM_read(STREAM *stream, void *addr, int len); int STREAM_read_max(STREAM *stream, void *addr, int len); bool STREAM_read_ahead(STREAM *stream); //char STREAM_getchar(STREAM *stream); @@ -231,8 +229,8 @@ void STREAM_load(const char *path, char **buffer, int *len); bool STREAM_map(const char *path, char **paddr, int *plen); void STREAM_unmap(char *addr, int len); -int STREAM_read_direct(int fd, char *buffer, int len); -int STREAM_write_direct(int fd, char *buffer, int len); +//int STREAM_read_direct(int fd, char *buffer, int len); +//int STREAM_write_direct(int fd, char *buffer, int len); bool STREAM_lock(STREAM *stream); diff --git a/main/gbx/gbx_stream_arch.c b/main/gbx/gbx_stream_arch.c index 93528f2b9..e11e2a5fd 100644 --- a/main/gbx/gbx_stream_arch.c +++ b/main/gbx/gbx_stream_arch.c @@ -79,31 +79,25 @@ static int stream_close(STREAM *stream) static int stream_read(STREAM *stream, char *buffer, int len) { - bool strip = FALSE; int max; max = stream->arch.size - stream->arch.pos; if (len > max) { - strip = TRUE; len = max; errno = 0; } - if (ARCHIVE_read(stream->arch.arch, stream->arch.start + stream->arch.pos, buffer, len)) - strip = TRUE; - + ARCHIVE_read(stream->arch.arch, stream->arch.start + stream->arch.pos, buffer, len); stream->arch.pos += len; - STREAM_eff_read = len; - - return strip; + return len; } static int stream_write(STREAM *stream, char *buffer, int len) { - return TRUE; + return -1; } @@ -136,9 +130,6 @@ static int stream_seek(STREAM *stream, int64_t pos, int whence) return FALSE; } -#define stream_getchar NULL - - static int stream_tell(STREAM *stream, int64_t *pos) { *pos = stream->arch.pos; diff --git a/main/gbx/gbx_stream_buffer.c b/main/gbx/gbx_stream_buffer.c index 70353e130..5ee9cedeb 100644 --- a/main/gbx/gbx_stream_buffer.c +++ b/main/gbx/gbx_stream_buffer.c @@ -100,9 +100,18 @@ static int stream_close(STREAM *stream) static int stream_read(STREAM *stream, char *buffer, int len) { - size_t eff_read; - size_t len_read; + int eff; + eff = (int)fread(buffer, 1, len, FD); + if (eff < len) + { + if (ferror(FD) == 0) + errno = 0; + } + + return eff; + + /* while (len > 0) { len_read = Min(len, MAX_IO); @@ -128,18 +137,7 @@ static int stream_read(STREAM *stream, char *buffer, int len) } return FALSE; -} - - -static int stream_getchar(STREAM *stream, char *buffer) -{ - int c = getc(FD); - - if (c == EOF) - return TRUE; - - *buffer = (char)c; - return FALSE; + */ } @@ -151,10 +149,9 @@ static int stream_flush(STREAM *stream) static int stream_write(STREAM *stream, char *buffer, int len) { - size_t eff_write; - size_t len_write; + return fwrite(buffer, 1, len, FD); - while (len > 0) + /*while (len > 0) { len_write = Min(len, MAX_IO); eff_write = fwrite(buffer, 1, len_write, FD); @@ -172,7 +169,7 @@ static int stream_write(STREAM *stream, char *buffer, int len) if (EXEC_debug) return stream_flush(stream); else - return FALSE; + return FALSE;*/ } diff --git a/main/gbx/gbx_stream_direct.c b/main/gbx/gbx_stream_direct.c index 8aed1ec54..d8a70c1cb 100644 --- a/main/gbx/gbx_stream_direct.c +++ b/main/gbx/gbx_stream_direct.c @@ -139,15 +139,12 @@ static int stream_close(STREAM *stream) static int stream_read(STREAM *stream, char *buffer, int len) { - return STREAM_read_direct(FD, buffer, len); + return read(FD, buffer, len); } -#define stream_getchar NULL - - static int stream_write(STREAM *stream, char *buffer, int len) { - return STREAM_write_direct(FD, buffer, len); + return write(FD, buffer, len); } diff --git a/main/gbx/gbx_stream_memory.c b/main/gbx/gbx_stream_memory.c index d41f9ebf3..cf378f359 100644 --- a/main/gbx/gbx_stream_memory.c +++ b/main/gbx/gbx_stream_memory.c @@ -85,18 +85,15 @@ static int stream_read(STREAM *stream, char *buffer, int len) if (CHECK_got_error()) { errno = EIO; - return TRUE; + return -1; } else { stream->memory.pos += len; - STREAM_eff_read = len; - return FALSE; + return len; } } -#define stream_getchar NULL - static int stream_write(STREAM *stream, char *buffer, int len) { if ((stream->common.mode & ST_WRITE) == 0) @@ -112,12 +109,12 @@ static int stream_write(STREAM *stream, char *buffer, int len) if (CHECK_got_error()) { errno = EIO; - return TRUE; + return -1; } else { stream->memory.pos += len; - return FALSE; + return len; } } diff --git a/main/gbx/gbx_stream_pipe.c b/main/gbx/gbx_stream_pipe.c index 12bef01ba..3df119315 100644 --- a/main/gbx/gbx_stream_pipe.c +++ b/main/gbx/gbx_stream_pipe.c @@ -84,15 +84,13 @@ static int stream_close(STREAM *stream) static int stream_read(STREAM *stream, char *buffer, int len) { - return STREAM_read_direct(FD, buffer, len); + return read(FD, buffer, len); } -#define stream_getchar NULL - static int stream_write(STREAM *stream, char *buffer, int len) { - return STREAM_write_direct(FD, buffer, len); + return write(FD, buffer, len); } diff --git a/main/gbx/gbx_stream_process.c b/main/gbx/gbx_stream_process.c index 96ab00eee..6812a97c6 100644 --- a/main/gbx/gbx_stream_process.c +++ b/main/gbx/gbx_stream_process.c @@ -68,14 +68,12 @@ static int stream_close(STREAM *stream) static int stream_read(STREAM *stream, char *buffer, int len) { - return STREAM_read_direct(FDR, buffer, len); + return read(FDR, buffer, len); } -#define stream_getchar NULL - static int stream_write(STREAM *stream, char *buffer, int len) { - return STREAM_write_direct(FDW, buffer, len); + return write(FDW, buffer, len); } diff --git a/main/gbx/gbx_stream_string.c b/main/gbx/gbx_stream_string.c index d533f9806..cc0db4f1e 100644 --- a/main/gbx/gbx_stream_string.c +++ b/main/gbx/gbx_stream_string.c @@ -62,14 +62,12 @@ static int stream_close(STREAM *stream) static int stream_read(STREAM *stream, char *buffer, int len) { - bool strip = FALSE; int max; max = stream->string.size - stream->string.pos; if (len > max) { - strip = TRUE; len = max; errno = 0; } @@ -78,13 +76,9 @@ static int stream_read(STREAM *stream, char *buffer, int len) memcpy(buffer, stream->string.buffer + stream->string.pos, len); stream->string.pos += len; - STREAM_eff_read = len; - - return strip; + return len; } -#define stream_getchar NULL - static int stream_write(STREAM *stream, char *buffer, int len) { if ((stream->common.mode & ST_WRITE) == 0) @@ -95,7 +89,7 @@ static int stream_write(STREAM *stream, char *buffer, int len) stream->string.size += len; stream->string.pos = stream->string.size; - return FALSE; + return len; } diff --git a/main/gbx/gbx_subr_file.c b/main/gbx/gbx_subr_file.c index 7c99e94a4..ff7c37581 100755 --- a/main/gbx/gbx_subr_file.c +++ b/main/gbx/gbx_subr_file.c @@ -454,6 +454,7 @@ void SUBR_read(ushort code) { STREAM *stream; int len = 0; + int eff; SUBR_ENTER_PARAM(2); @@ -482,19 +483,19 @@ void SUBR_read(ushort code) RETURN->_string.start = 0; RETURN->_string.len = len; - STREAM_read_max(stream, RETURN->_string.addr, len); + eff = STREAM_read_max(stream, RETURN->_string.addr, len); - if (STREAM_eff_read == 0) + if (eff == 0) { RETURN->type = T_NULL; STRING_free(&RETURN->_string.addr); } else { - if (STREAM_eff_read < len) + if (eff < len) { - RETURN->_string.addr = STRING_extend(RETURN->_string.addr, STREAM_eff_read); - RETURN->_string.len = STREAM_eff_read; + RETURN->_string.addr = STRING_extend(RETURN->_string.addr, eff); + RETURN->_string.len = eff; } STRING_extend_end(RETURN->_string.addr); } diff --git a/main/share/gambas.h b/main/share/gambas.h index ac2804260..30d10ec33 100644 --- a/main/share/gambas.h +++ b/main/share/gambas.h @@ -1142,7 +1142,6 @@ typedef struct { GB_STREAM *(*Get)(void *object); - void (*SetBytesRead)(GB_STREAM *stream, int length); void (*SetSwapping)(GB_STREAM *stream, int swap); void (*SetAvailableNow)(GB_STREAM *stream, int available_now); bool (*Block)(GB_STREAM *stream, int block); diff --git a/main/share/gb_arch_temp.h b/main/share/gb_arch_temp.h index e93b512f4..d445e23f6 100644 --- a/main/share/gb_arch_temp.h +++ b/main/share/gb_arch_temp.h @@ -357,13 +357,6 @@ bool ARCH_find(ARCH *arch, const char *path, int len_path, ARCH_FIND *find) bool ARCH_read(ARCH *arch, int pos, void *buffer, int len) { - /*if (lseek(arch->fd, pos, SEEK_SET) < 0) - return TRUE; - - if (read(arch->fd, buffer, len) != len) - return TRUE;*/ - memcpy(buffer, &arch->addr[pos], len); - return FALSE; }