summaryrefslogtreecommitdiff
path: root/include/fitz/stm_stream.h
blob: 4f7463bb92dd55ae238f2c3a50a4c6a66722c39b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * Stream API for Fitz.
 * Read and write data to and from files, memory buffers and filters.
 */

typedef struct fz_stream_s fz_stream;

enum { FZ_SFILE, FZ_SBUFFER, FZ_SFILTER };
enum { FZ_SREAD, FZ_SWRITE };

struct fz_stream_s
{
	int refs;
	int kind;
	int mode;
	int dead;
	fz_buffer *buffer;
	fz_filter *filter;
	fz_stream *chain;
	fz_error *error; /* delayed error from readbyte and peekbyte */
	int file;
};

/*
 * Various stream creation functions.
 */

/* open() and creat() & co */
fz_error *fz_openrfile(fz_stream **stmp, char *filename);
fz_error *fz_openwfile(fz_stream **stmp, char *filename);
fz_error *fz_openafile(fz_stream **stmp, char *filename);

/* write to memory buffers! */
fz_error *fz_openrmemory(fz_stream **stmp, char *mem, int len);
fz_error *fz_openrbuffer(fz_stream **stmp, fz_buffer *buf);
fz_error *fz_openwbuffer(fz_stream **stmp, fz_buffer *buf);

/* almost like fork() exec() pipe() */
fz_error *fz_openrfilter(fz_stream **stmp, fz_filter *flt, fz_stream *chain);
fz_error *fz_openwfilter(fz_stream **stmp, fz_filter *flt, fz_stream *chain);

/*
 * Functions that are common to both input and output streams.
 */

fz_stream *fz_keepstream(fz_stream *stm);
void fz_dropstream(fz_stream *stm);

int fz_tell(fz_stream *stm);
fz_error * fz_seek(fz_stream *stm, int offset, int whence);

/*
 * Input stream functions.
 */

int fz_rtell(fz_stream *stm);
fz_error * fz_rseek(fz_stream *stm, int offset, int whence);

fz_error * fz_readimp(fz_stream *stm);
fz_error * fz_read(int *np, fz_stream *stm, unsigned char *buf, int len);
fz_error * fz_readall(fz_buffer **bufp, fz_stream *stm, int sizehint);
fz_error * fz_readline(fz_stream *stm, char *buf, int max);

/*
 * Error handling when reading with readbyte/peekbyte is non-standard.
 * The cause of an error is stuck into the stream struct,
 * and EOF is returned. Not good, but any other way is too painful.
 * So we have to be careful to check the error status eventually.
 */

fz_error *fz_readerror(fz_stream *stm);
int fz_readbytex(fz_stream *stm);
int fz_peekbytex(fz_stream *stm);

#ifdef DEBUG

#define fz_readbyte fz_readbytex
#define fz_peekbyte fz_peekbytex

#else

static inline int fz_readbyte(fz_stream *stm)
{
    fz_buffer *buf = stm->buffer;
    if (buf->rp < buf->wp)
	return *buf->rp++;
    return fz_readbytex(stm);
}

static inline int fz_peekbyte(fz_stream *stm)
{
    fz_buffer *buf = stm->buffer;
    if (buf->rp < buf->wp)
	return *buf->rp;
    return fz_peekbytex(stm);
}

#endif

/*
 * Output stream functions.
 */

int fz_wtell(fz_stream *stm);
fz_error * fz_wseek(fz_stream *stm, int offset, int whence);

fz_error * fz_write(fz_stream *stm, unsigned char *buf, int n);
fz_error * fz_flush(fz_stream *stm);

fz_error * fz_printstr(fz_stream *stm, char *s);
fz_error * fz_printobj(fz_stream *stm, fz_obj *obj, int tight);
fz_error * fz_print(fz_stream *stm, char *fmt, ...);