Change the translation of the "Help" menu item to "?", so that the menu can be displa...
[reactos.git] / rosapps / smartpdf / fitz / stream / stm_misc.c
1 /*
2 * Miscellaneous I/O functions
3 */
4
5 #include "fitz-base.h"
6 #include "fitz-stream.h"
7
8 int fz_tell(fz_stream *stm)
9 {
10 if (stm->mode == FZ_SREAD)
11 return fz_rtell(stm);
12 return fz_wtell(stm);
13 }
14
15 int fz_seek(fz_stream *stm, int offset, int whence)
16 {
17 if (stm->mode == FZ_SREAD)
18 return fz_rseek(stm, offset, whence);
19 return fz_wseek(stm, offset, whence);
20 }
21
22 /*
23 * Read a line terminated by LF or CR or CRLF.
24 */
25
26 int fz_readline(fz_stream *stm, char *mem, int n)
27 {
28 char *s = mem;
29 int c = EOF;
30 while (n > 1)
31 {
32 c = fz_readbyte(stm);
33 if (c == EOF)
34 break;
35 if (c == '\r') {
36 c = fz_peekbyte(stm);
37 if (c == '\n')
38 c = fz_readbyte(stm);
39 break;
40 }
41 if (c == '\n')
42 break;
43 *s++ = c;
44 n--;
45 }
46 if (n)
47 *s = '\0';
48 return s - mem;
49 }
50
51 struct fz_linkedbuf_s
52 {
53 int len;
54 struct fz_linkedbuf_s * next;
55 };
56
57 typedef struct fz_linkedbuf_s fz_linkedbuf;
58
59 fz_error *fz_newlinkedbuf(fz_linkedbuf **bufp, int len, unsigned char **data)
60 {
61 fz_linkedbuf *buf;
62
63 buf = *bufp = fz_malloc(sizeof(fz_linkedbuf) + len);
64 if (!buf) return fz_outofmem;
65 buf->next = nil;
66 buf->len = len;
67 *data = (unsigned char*)buf + sizeof(fz_linkedbuf);
68 return nil;
69 }
70
71 fz_error *fz_growlinkedbuf(fz_linkedbuf *buf, int len, unsigned char **data)
72 {
73 fz_linkedbuf *newbuf;
74 fz_error *error;
75
76 error = fz_newlinkedbuf(&newbuf, len, data);
77 if (error) return error;
78 while (buf->next)
79 buf = buf->next;
80 buf->next = newbuf;
81 return nil;
82 }
83
84 void fz_droplinkedbuf(fz_linkedbuf *buf)
85 {
86 fz_linkedbuf *next;
87 while (buf) {
88 next = buf->next;
89 fz_free(buf);
90 buf = next;
91 }
92 }
93
94 int fz_linkedbuflen(fz_linkedbuf *buf)
95 {
96 int len = 0;
97 while (buf) {
98 len += buf->len;
99 buf = buf->next;
100 }
101 return len;
102 }
103
104 fz_error *fz_linearizelinkedbuf(fz_linkedbuf *buf, int len, unsigned char **datap)
105 {
106 unsigned char *data, *bufdata;
107 int tocopy;
108 int buflen = fz_linkedbuflen(buf);
109 assert(len <= buflen);
110 data = *datap = fz_malloc(len);
111 if (!data) return fz_outofmem;
112
113 while (len > 0)
114 {
115 bufdata = (unsigned char*)buf + sizeof(fz_linkedbuf);
116 tocopy = MIN(len, buf->len);
117 memmove(data, bufdata, tocopy);
118 buf = buf->next;
119 data += tocopy;
120 len -= tocopy;
121 }
122 return nil;
123 }
124
125 /*
126 * Utility function to consume all the contents of an input stream into
127 * a freshly allocated buffer; realloced and trimmed to size.
128 */
129
130 enum { MINCHUNKSIZE = 1024 * 8 };
131 enum { MAXCHUNKSIZE = MINCHUNKSIZE * 10 };
132
133 int fz_readall(fz_buffer **bufp, fz_stream *stm)
134 {
135 fz_buffer *real;
136 fz_error *error;
137 fz_linkedbuf *buf;
138 unsigned char *data;
139 int totallen;
140 int n;
141 int chunksize = MINCHUNKSIZE;
142
143 *bufp = nil;
144
145 totallen = 0;
146 error = fz_newlinkedbuf(&buf, chunksize, &data);
147 if (error)
148 return -1;
149
150 while (1)
151 {
152 n = fz_read(stm, data, chunksize);
153 if (n < 0)
154 {
155 fz_free(buf);
156 return -1;
157 }
158
159 totallen += n;
160 if (n != chunksize)
161 break;
162
163 if (chunksize < MAXCHUNKSIZE)
164 chunksize = chunksize + MINCHUNKSIZE;
165 error = fz_growlinkedbuf(buf, chunksize, &data);
166 if (error)
167 {
168 fz_droplinkedbuf(buf);
169 return -1;
170 }
171 }
172
173 error = fz_linearizelinkedbuf(buf, totallen, &data);
174 fz_droplinkedbuf(buf);
175 if (error)
176 return -1;
177
178 real = *bufp = fz_malloc(sizeof(fz_buffer));
179 if (!real)
180 {
181 fz_free(data);
182 return -1;
183 }
184
185 real->refs = 1;
186 real->ownsdata = 1;
187 real->bp = data;
188 real->rp = data;
189 real->wp = data + totallen;
190 real->ep = data + totallen;
191 real->eof = 1;
192 return totallen;
193 }
194