2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2013-01-19 18:42:09 +01:00
|
|
|
gbx_stream.h
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-02-12 02:53:46 +01:00
|
|
|
(c) 2000-2017 Benoît Minisini <g4mba5@gmail.com>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-01-19 18:42:09 +01:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-01-19 18:42:09 +01:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-01-19 18:42:09 +01:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __GBX_STREAM_H
|
|
|
|
#define __GBX_STREAM_H
|
|
|
|
|
|
|
|
#include "gbx_value.h"
|
|
|
|
#include "gbx_archive.h"
|
|
|
|
|
|
|
|
union STREAM;
|
|
|
|
|
|
|
|
typedef
|
2013-01-19 18:42:09 +01:00
|
|
|
struct STREAM_CLASS {
|
|
|
|
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 (*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);
|
|
|
|
int (*flush)(union STREAM *stream);
|
|
|
|
int (*eof)(union STREAM *stream);
|
|
|
|
int (*lof)(union STREAM *stream, int64_t *len);
|
|
|
|
int (*handle)(union STREAM *stream);
|
|
|
|
}
|
|
|
|
STREAM_CLASS;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2020-02-11 01:45:10 +01:00
|
|
|
typedef
|
|
|
|
struct {
|
|
|
|
union STREAM *redirect;
|
|
|
|
char *buffer;
|
|
|
|
short buffer_pos;
|
|
|
|
short buffer_len;
|
|
|
|
char *unread;
|
|
|
|
int unread_pos;
|
|
|
|
int unread_len;
|
|
|
|
}
|
|
|
|
STREAM_EXTRA;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
typedef
|
2013-01-19 18:42:09 +01:00
|
|
|
struct {
|
|
|
|
STREAM_CLASS *type;
|
|
|
|
short mode;
|
|
|
|
unsigned swap : 1;
|
|
|
|
unsigned eol : 2;
|
|
|
|
unsigned eof : 1;
|
|
|
|
unsigned no_fionread : 1;
|
|
|
|
unsigned no_lseek : 1;
|
|
|
|
unsigned available_now : 1;
|
2009-02-04 23:51:20 +01:00
|
|
|
unsigned standard : 1;
|
2010-02-27 23:08:04 +01:00
|
|
|
unsigned blocking : 1;
|
2011-04-04 02:40:32 +02:00
|
|
|
unsigned redirected : 1;
|
2015-03-20 00:23:47 +01:00
|
|
|
unsigned no_read_ahead : 1;
|
2019-12-18 06:29:33 +01:00
|
|
|
unsigned null_terminated : 1;
|
2019-12-16 13:32:49 +01:00
|
|
|
unsigned _reserved : 4;
|
2020-02-11 01:45:10 +01:00
|
|
|
#if __WORDSIZE == 64
|
|
|
|
unsigned _reserved2 : 32;
|
|
|
|
#endif
|
|
|
|
STREAM_EXTRA *extra;
|
2019-12-16 13:32:49 +01:00
|
|
|
#if DEBUG_STREAM
|
|
|
|
int tag;
|
|
|
|
#endif
|
2013-01-19 18:42:09 +01:00
|
|
|
}
|
|
|
|
STREAM_COMMON;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
typedef
|
2013-01-19 18:42:09 +01:00
|
|
|
struct {
|
|
|
|
STREAM_COMMON common;
|
|
|
|
int _reserved[6];
|
|
|
|
}
|
|
|
|
STREAM_RESERVED;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
typedef
|
2013-01-19 18:42:09 +01:00
|
|
|
struct {
|
|
|
|
STREAM_COMMON common;
|
|
|
|
int64_t size;
|
|
|
|
int fd;
|
2011-09-18 17:21:11 +02:00
|
|
|
unsigned watch : 1;
|
2018-08-27 23:57:30 +02:00
|
|
|
unsigned has_size : 1;
|
|
|
|
unsigned use_size : 1;
|
2013-01-19 18:42:09 +01:00
|
|
|
}
|
|
|
|
STREAM_DIRECT;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
typedef
|
2013-01-19 18:42:09 +01:00
|
|
|
struct {
|
|
|
|
STREAM_COMMON common;
|
|
|
|
FILE *file;
|
|
|
|
}
|
|
|
|
STREAM_BUFFER;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
typedef
|
2013-01-19 18:42:09 +01:00
|
|
|
struct {
|
|
|
|
STREAM_COMMON common;
|
|
|
|
void *addr;
|
|
|
|
intptr_t pos;
|
|
|
|
}
|
|
|
|
STREAM_MEMORY;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
typedef
|
2013-01-19 18:42:09 +01:00
|
|
|
struct {
|
|
|
|
STREAM_COMMON common;
|
|
|
|
ARCHIVE *arch;
|
|
|
|
int size;
|
|
|
|
int start;
|
|
|
|
int pos;
|
|
|
|
}
|
|
|
|
STREAM_ARCH;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
typedef
|
2013-01-19 18:42:09 +01:00
|
|
|
struct {
|
|
|
|
STREAM_COMMON common;
|
|
|
|
void *process;
|
|
|
|
}
|
|
|
|
STREAM_PROCESS;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-27 01:06:43 +01:00
|
|
|
typedef
|
2013-01-19 18:42:09 +01:00
|
|
|
struct {
|
|
|
|
STREAM_COMMON common;
|
|
|
|
char *buffer;
|
2013-11-04 16:17:01 +01:00
|
|
|
int pos;
|
|
|
|
int size;
|
2013-01-19 18:42:09 +01:00
|
|
|
}
|
|
|
|
STREAM_STRING;
|
2010-11-27 01:06:43 +01:00
|
|
|
|
2019-12-18 06:59:21 +01:00
|
|
|
typedef
|
|
|
|
struct {
|
|
|
|
STREAM_COMMON common;
|
|
|
|
intptr_t pos;
|
|
|
|
}
|
|
|
|
STREAM_NULL;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
typedef
|
2013-01-19 18:42:09 +01:00
|
|
|
union STREAM {
|
|
|
|
STREAM_CLASS *type;
|
|
|
|
STREAM_COMMON common;
|
|
|
|
STREAM_RESERVED _reserved;
|
|
|
|
STREAM_DIRECT direct;
|
|
|
|
STREAM_BUFFER buffer;
|
|
|
|
STREAM_DIRECT pipe;
|
|
|
|
STREAM_MEMORY memory;
|
|
|
|
STREAM_ARCH arch;
|
|
|
|
STREAM_PROCESS process;
|
2010-11-27 01:06:43 +01:00
|
|
|
STREAM_STRING string;
|
2019-12-18 06:59:21 +01:00
|
|
|
STREAM_NULL null;
|
2013-01-19 18:42:09 +01:00
|
|
|
}
|
|
|
|
STREAM;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2020-11-09 17:38:28 +01:00
|
|
|
/*enum {
|
2018-08-30 13:33:13 +02:00
|
|
|
STO_READ = (1 << 0),
|
|
|
|
STO_WRITE = (1 << 1),
|
|
|
|
STO_READ_WRITE = STO_READ + STO_WRITE,
|
|
|
|
STO_MODE = 0x3,
|
|
|
|
STO_APPEND = (1 << 2),
|
|
|
|
STO_CREATE = (1 << 3),
|
|
|
|
STO_ACCESS = 0xF,
|
|
|
|
STO_DIRECT = (1 << 4),
|
|
|
|
STO_LOCK = (1 << 5),
|
|
|
|
STO_WATCH = (1 << 6),
|
|
|
|
STO_PIPE = (1 << 7),
|
|
|
|
STO_MEMORY = (1 << 8),
|
2019-12-18 06:59:21 +01:00
|
|
|
STO_STRING = (1 << 9),
|
|
|
|
STO_NULL = (1 << 10)
|
2020-11-09 17:38:28 +01:00
|
|
|
};*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
enum {
|
|
|
|
ST_EOL_UNIX = 0,
|
|
|
|
ST_EOL_WINDOWS = 1,
|
|
|
|
ST_EOL_MAC = 2
|
|
|
|
};
|
|
|
|
|
2017-09-27 02:48:00 +02:00
|
|
|
//EXTERN int STREAM_eff_read;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
#ifndef __STREAM_IMPL_C
|
|
|
|
|
|
|
|
EXTERN STREAM_CLASS STREAM_direct;
|
2017-12-18 13:02:03 +01:00
|
|
|
EXTERN STREAM_CLASS STREAM_lock;
|
2007-12-30 17:41:49 +01:00
|
|
|
EXTERN STREAM_CLASS STREAM_buffer;
|
|
|
|
EXTERN STREAM_CLASS STREAM_pipe;
|
|
|
|
EXTERN STREAM_CLASS STREAM_memory;
|
|
|
|
EXTERN STREAM_CLASS STREAM_arch;
|
|
|
|
EXTERN STREAM_CLASS STREAM_process;
|
2010-11-27 01:06:43 +01:00
|
|
|
EXTERN STREAM_CLASS STREAM_string;
|
2019-12-18 06:59:21 +01:00
|
|
|
EXTERN STREAM_CLASS STREAM_null;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define DECLARE_STREAM(stream) \
|
2008-01-17 22:39:26 +01:00
|
|
|
STREAM_CLASS stream = \
|
2007-12-30 17:41:49 +01:00
|
|
|
{ \
|
2013-01-19 18:42:09 +01:00
|
|
|
(void *)stream_open, \
|
|
|
|
(void *)stream_close, \
|
|
|
|
(void *)stream_read, \
|
|
|
|
(void *)stream_write, \
|
|
|
|
(void *)stream_seek, \
|
|
|
|
(void *)stream_tell, \
|
|
|
|
(void *)stream_flush, \
|
|
|
|
(void *)stream_eof, \
|
|
|
|
(void *)stream_lof, \
|
|
|
|
(void *)stream_handle \
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2014-07-28 07:35:58 +02:00
|
|
|
#define STREAM_BUFFER_SIZE 1024
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
bool STREAM_in_archive(const char *path);
|
|
|
|
//int STREAM_get_readable(int fd, long *len);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void STREAM_open(STREAM *stream, const char *path, int mode);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-01-27 15:00:04 +01:00
|
|
|
void STREAM_release(STREAM *stream);
|
2008-01-17 22:39:26 +01:00
|
|
|
void STREAM_close(STREAM *stream);
|
|
|
|
void STREAM_write(STREAM *stream, void *addr, int len);
|
2011-02-16 23:01:35 +01:00
|
|
|
char *STREAM_line_input(STREAM *stream, char *escape);
|
2010-06-09 01:08:04 +02:00
|
|
|
char *STREAM_input(STREAM *stream);
|
2008-01-17 22:39:26 +01:00
|
|
|
int64_t STREAM_tell(STREAM *stream);
|
|
|
|
void STREAM_seek(STREAM *stream, int64_t pos, int whence);
|
2017-09-27 02:48:00 +02:00
|
|
|
int STREAM_read(STREAM *stream, void *addr, int len);
|
2020-02-11 01:45:10 +01:00
|
|
|
void STREAM_peek(STREAM *stream, void *addr, int len);
|
2010-04-05 19:02:16 +02:00
|
|
|
int STREAM_read_max(STREAM *stream, void *addr, int len);
|
2014-11-30 23:41:41 +01:00
|
|
|
bool STREAM_read_ahead(STREAM *stream);
|
2014-11-29 16:29:10 +01:00
|
|
|
//char STREAM_getchar(STREAM *stream);
|
2010-12-28 22:58:42 +01:00
|
|
|
void STREAM_read_type(STREAM *stream, TYPE type, VALUE *value);
|
2008-01-17 22:39:26 +01:00
|
|
|
void STREAM_write(STREAM *stream, void *addr, int len);
|
2010-12-28 22:58:42 +01:00
|
|
|
void STREAM_write_zeros(STREAM *stream, int len);
|
|
|
|
void STREAM_write_type(STREAM *stream, TYPE type, VALUE *value);
|
2008-01-17 22:39:26 +01:00
|
|
|
void STREAM_write_eol(STREAM *stream);
|
|
|
|
void STREAM_flush(STREAM *stream);
|
|
|
|
int STREAM_handle(STREAM *stream);
|
|
|
|
void STREAM_lof(STREAM *stream, int64_t *len);
|
|
|
|
bool STREAM_eof(STREAM *stream);
|
2018-08-27 23:57:30 +02:00
|
|
|
bool STREAM_default_eof(STREAM *stream);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void STREAM_load(const char *path, char **buffer, int *len);
|
2008-12-28 23:41:55 +01:00
|
|
|
|
|
|
|
bool STREAM_map(const char *path, char **paddr, int *plen);
|
|
|
|
void STREAM_unmap(char *addr, int len);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2017-12-18 13:02:03 +01:00
|
|
|
bool STREAM_lock_all(STREAM *stream);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
#define STREAM_is_closed(_stream) ((_stream)->type == NULL)
|
2013-07-06 21:00:41 +02:00
|
|
|
#define STREAM_is_closed_for_writing(_stream) (STREAM_is_closed(_stream) && !(_stream)->common.redirected)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-10-29 19:26:56 +01:00
|
|
|
void STREAM_blocking(STREAM *stream, bool block);
|
2010-04-07 20:39:45 +02:00
|
|
|
#define STREAM_is_blocking(_stream) ((_stream)->common.blocking)
|
|
|
|
void STREAM_check_blocking(STREAM *stream);
|
2008-10-29 19:26:56 +01:00
|
|
|
|
2012-08-25 17:13:39 +02:00
|
|
|
int STREAM_get_readable(STREAM *stream, int *len);
|
|
|
|
|
2008-12-31 16:55:40 +01:00
|
|
|
void STREAM_exit(void);
|
|
|
|
|
2011-04-04 02:40:32 +02:00
|
|
|
void STREAM_begin(STREAM *stream);
|
|
|
|
void STREAM_cancel(STREAM *stream);
|
|
|
|
void STREAM_end(STREAM *stream);
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
#endif
|