summaryrefslogtreecommitdiff
path: root/fitz/stm_misc.c
blob: a95833b4193916ecf8de113e37659a5ba44ab20f (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
/*
 * Miscellaneous I/O functions
 */

#include "fitz_base.h"
#include "fitz_stream.h"

/*
 * Read a line terminated by LF or CR or CRLF.
 */

fz_error
fz_readline(fz_stream *stm, char *mem, int n)
{
	fz_error error;

	char *s = mem;
	int c = EOF;
	while (n > 1)
	{
		c = fz_readbyte(stm);
		if (c == EOF)
			break;
		if (c == '\r') {
			c = fz_peekbyte(stm);
			if (c == '\n')
				c = fz_readbyte(stm);
			break;
		}
		if (c == '\n')
			break;
		*s++ = c;
		n--;
	}
	if (n)
		*s = '\0';

	error = fz_readerror(stm);
	if (error)
		return fz_rethrow(error, "cannot read line");
	return fz_okay;
}

static inline int fz_fillbuf(fz_stream *stm, fz_buffer *buf)
{
	int remaining = buf->ep - buf->wp;
	int available = stm->buffer->wp - stm->buffer->rp;

	if (available == 0)
	{
		int c = fz_readbytex(stm);
		if (c == EOF)
			return EOF;

		*buf->wp++ = c;

		remaining = buf->ep - buf->wp;
		available = stm->buffer->wp - stm->buffer->rp;
	}

	memmove(buf->wp, stm->buffer->rp, MIN(remaining, available));
	buf->wp += MIN(remaining, available);
	stm->buffer->rp += MIN(remaining, available);

	if (buf->rp == buf->wp && stm->buffer->eof)
		return EOF;
	return 0;
}

/*
 * Utility function to consume all the contents of an input stream into
 * a freshly allocated buffer.
 */

fz_error
fz_readall(fz_buffer **bufp, fz_stream *stm, int sizehint)
{
	fz_error error;
	fz_buffer *buf;

	if (sizehint == 0)
	    sizehint = 4 * 1024;

	error = fz_newbuffer(&buf, sizehint);
	if (error)
	    return fz_rethrow(error, "cannot create scratch buffer");

	while (fz_fillbuf(stm, buf) != EOF)
	{
		if (buf->wp == buf->ep)
		{
		    error = fz_growbuffer(buf);
		    if (error)
		    {
			fz_dropbuffer(buf);
			return fz_rethrow(error, "cannot resize scratch buffer");
		    }
		}
	}

	*bufp = buf;
	return fz_okay;
}