My Project: jas_stream.h Source File

My Project 2.0.16 |

jas_stream.h
1 /*
2 * Copyright (c) 1999-2000 Image Power, Inc. and the University of
3 * British Columbia.
4 * Copyright (c) 2001-2003 Michael David Adams.
5 * All rights reserved.
6 */
7
8 /* __START_OF_JASPER_LICENSE__
9 *
10 * JasPer License Version 2.0
11 *
12 * Copyright (c) 2001-2006 Michael David Adams
13 * Copyright (c) 1999-2000 Image Power, Inc.
14 * Copyright (c) 1999-2000 The University of British Columbia
15 *
16 * All rights reserved.
17 *
18 * Permission is hereby granted, free of charge, to any person (the
19 * "User") obtaining a copy of this software and associated documentation
20 * files (the "Software"), to deal in the Software without restriction,
21 * including without limitation the rights to use, copy, modify, merge,
22 * publish, distribute, and/or sell copies of the Software, and to permit
23 * persons to whom the Software is furnished to do so, subject to the
24 * following conditions:
25 *
26 * 1. The above copyright notices and this permission notice (which
27 * includes the disclaimer below) shall be included in all copies or
28 * substantial portions of the Software.
29 *
30 * 2. The name of a copyright holder shall not be used to endorse or
31 * promote products derived from the Software without specific prior
32 * written permission.
33 *
34 * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
35 * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
36 * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
37 * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
38 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
39 * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO
40 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
41 * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
42 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
43 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
44 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE
45 * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
46 * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
47 * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
48 * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
49 * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS
50 * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
51 * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE
52 * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
53 * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
54 * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
55 * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
56 * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
57 * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
58 * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
59 * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
60 *
61 * __END_OF_JASPER_LICENSE__
62 */
63
64 /*
65 * I/O Stream Class
66 *
67 * $Id$
68 */
69
70 #ifndef JAS_STREAM_H
71 #define JAS_STREAM_H
72
73 /******************************************************************************\
74 * Includes.
75 \******************************************************************************/
76
77 /* The configuration header file should be included first. */
78 #include <jasper/jas_config.h>
79
80 #include <stdio.h>
81 #include <limits.h>
82 #if defined(JAS_HAVE_FCNTL_H)
83 #include <fcntl.h>
84 #endif
85 #include <string.h>
86 #if defined(JAS_HAVE_UNISTD_H)
87 #include <unistd.h>
88 #endif
89 #include <jasper/jas_types.h>
90
91 #ifdef __cplusplus
92 extern "C" {
93 #endif
94
95 /******************************************************************************\
96 * Constants.
97 \******************************************************************************/
98
99 /* On most UNIX systems, we probably need to define O_BINARY ourselves. */
100 #ifndef O_BINARY
101 #define O_BINARY 0
102 #endif
103
104 #ifdef PATH_MAX
105 #define JAS_PATH_MAX PATH_MAX
106 #else
107 #define JAS_PATH_MAX 4096
108 #endif
109
110 /*
111 * Stream open flags.
112 */
113
114 /* The stream was opened for reading. */
115 #define JAS_STREAM_READ 0x0001
116 /* The stream was opened for writing. */
117 #define JAS_STREAM_WRITE 0x0002
118 /* The stream was opened for appending. */
119 #define JAS_STREAM_APPEND 0x0004
120 /* The stream was opened in binary mode. */
121 #define JAS_STREAM_BINARY 0x0008
122 /* The stream should be created/truncated. */
123 #define JAS_STREAM_CREATE 0x0010
124
125
126 /*
127 * Stream buffering flags.
128 */
129
130 /* The stream is unbuffered. */
131 #define JAS_STREAM_UNBUF 0x0000
132 /* The stream is line buffered. */
133 #define JAS_STREAM_LINEBUF 0x0001
134 /* The stream is fully buffered. */
135 #define JAS_STREAM_FULLBUF 0x0002
136 /* The buffering mode mask. */
137 #define JAS_STREAM_BUFMODEMASK 0x000f
138
139 /* The memory associated with the buffer needs to be deallocated when the
140 stream is destroyed. */
141 #define JAS_STREAM_FREEBUF 0x0008
142 /* The buffer is currently being used for reading. */
143 #define JAS_STREAM_RDBUF 0x0010
144 /* The buffer is currently being used for writing. */
145 #define JAS_STREAM_WRBUF 0x0020
146
147 /*
148 * Stream error flags.
149 */
150
151 /* The end-of-file has been encountered (on reading). */
152 #define JAS_STREAM_EOF 0x0001
153 /* An I/O error has been encountered on the stream. */
154 #define JAS_STREAM_ERR 0x0002
155 /* The read/write limit has been exceeded. */
156 #define JAS_STREAM_RWLIMIT 0x0004
157 /* The error mask. */
158 #define JAS_STREAM_ERRMASK \
159 (JAS_STREAM_EOF | JAS_STREAM_ERR | JAS_STREAM_RWLIMIT)
160
161 /*
162 * Other miscellaneous constants.
163 */
164
165 /* The default buffer size (for fully-buffered operation). */
166 #define JAS_STREAM_BUFSIZE 8192
167 /* The default permission mask for file creation. */
168 #define JAS_STREAM_PERMS 0666
169
170 /* The maximum number of characters that can always be put back on a stream. */
171 #define JAS_STREAM_MAXPUTBACK 16
172
173 /******************************************************************************\
174 * Types.
175 \******************************************************************************/
176
177 /*
178 * Generic file object.
179 */
180
181 typedef void jas_stream_obj_t;
182
183 /*
184 * Generic file object operations.
185 */
186
187 typedef struct {
188
189 /* Read characters from a file object. */
190 int (*read_)(jas_stream_obj_t *obj, char *buf, int cnt);
191
192 /* Write characters to a file object. */
193 int (*write_)(jas_stream_obj_t *obj, char *buf, int cnt);
194
195 /* Set the position for a file object. */
196 long (*seek_)(jas_stream_obj_t *obj, long offset, int origin);
197
198 /* Close a file object. */
199 int (*close_)(jas_stream_obj_t *obj);
200
201 } jas_stream_ops_t;
202
203 /*
204 * Stream object.
205 */
206
207 typedef struct {
208
209 /* The mode in which the stream was opened. */
210 int openmode_;
211
212 /* The buffering mode. */
213 int bufmode_;
214
215 /* The stream status. */
216 int flags_;
217
218 /* The start of the buffer area to use for reading/writing. */
219 jas_uchar *bufbase_;
220
221 /* The start of the buffer area excluding the extra initial space for
222 character putback. */
223 jas_uchar *bufstart_;
224
225 /* The buffer size. */
226 int bufsize_;
227
228 /* The current position in the buffer. */
229 jas_uchar *ptr_;
230
231 /* The number of characters that must be read/written before
232 the buffer needs to be filled/flushed. */
233 int cnt_;
234
235 /* A trivial buffer to be used for unbuffered operation. */
236 jas_uchar tinybuf_[JAS_STREAM_MAXPUTBACK + 1];
237
238 /* The operations for the underlying stream file object. */
239 jas_stream_ops_t *ops_;
240
241 /* The underlying stream file object. */
242 jas_stream_obj_t *obj_;
243
244 /* The number of characters read/written. */
245 long rwcnt_;
246
247 /* The maximum number of characters that may be read/written. */
248 long rwlimit_;
249
250 } jas_stream_t;
251
252 /*
253 * Regular file object.
254 */
255
256 /*
257 * File descriptor file object.
258 */
259 typedef struct {
260 int fd;
261 int flags;
262 char pathname[JAS_PATH_MAX + 1];
263 } jas_stream_fileobj_t;
264
265 #define JAS_STREAM_FILEOBJ_DELONCLOSE 0x01
266 #define JAS_STREAM_FILEOBJ_NOCLOSE 0x02
267
268 /*
269 * Memory file object.
270 */
271
272 typedef struct {
273
274 /* The data associated with this file. */
275 jas_uchar *buf_;
276
277 /* The allocated size of the buffer for holding file data. */
278 size_t bufsize_;
279
280 /* The length of the file. */
281 int_fast32_t len_;
282
283 /* The seek position. */
284 int_fast32_t pos_;
285
286 /* Is the buffer growable? */
287 int growable_;
288
289 /* Was the buffer allocated internally? */
290 int myalloc_;
291
292 } jas_stream_memobj_t;
293
294 /******************************************************************************\
295 * Macros/functions for opening and closing streams.
296 \******************************************************************************/
297
298 /* Open a file as a stream. */
299 JAS_DLLEXPORT jas_stream_t *jas_stream_fopen(const char *filename, const char *mode);
300
301 /* Open a memory buffer as a stream. */
302 JAS_DLLEXPORT jas_stream_t *jas_stream_memopen(char *buf, int bufsize);
303
304 /* Do not use this function.
305 It will eventually replace jas_stream_memopen. */
306 JAS_DLLEXPORT jas_stream_t *jas_stream_memopen2(char *buf, size_t bufsize);
307
308 /* Open a file descriptor as a stream. */
309 JAS_DLLEXPORT jas_stream_t *jas_stream_fdopen(int fd, const char *mode);
310
311 /* Open a stdio stream as a stream. */
312 JAS_DLLEXPORT jas_stream_t *jas_stream_freopen(const char *path, const char *mode, FILE *fp);
313
314 /* Open a temporary file as a stream. */
315 JAS_DLLEXPORT jas_stream_t *jas_stream_tmpfile(void);
316
317 /* Close a stream. */
318 JAS_DLLEXPORT int jas_stream_close(jas_stream_t *stream);
319
320 /******************************************************************************\
321 * Macros/functions for getting/setting the stream state.
322 \******************************************************************************/
323
324 /* Get the EOF indicator for a stream. */
325 #define jas_stream_eof(stream) \
326 (((stream)->flags_ & JAS_STREAM_EOF) != 0)
327
328 /* Get the error indicator for a stream. */
329 #define jas_stream_error(stream) \
330 (((stream)->flags_ & JAS_STREAM_ERR) != 0)
331
332 /* Clear the error indicator for a stream. */
333 #define jas_stream_clearerr(stream) \
334 ((stream)->flags_ &= ~(JAS_STREAM_ERR | JAS_STREAM_EOF))
335
336 /* Get the read/write limit for a stream. */
337 #define jas_stream_getrwlimit(stream) \
338 (((const jas_stream_t *)(stream))->rwlimit_)
339
340 /* Set the read/write limit for a stream. */
341 JAS_DLLEXPORT int jas_stream_setrwlimit(jas_stream_t *stream, long rwlimit);
342
343 /* Get the read/write count for a stream. */
344 #define jas_stream_getrwcount(stream) \
345 (((const jas_stream_t *)(stream))->rwcnt_)
346
347 /* Set the read/write count for a stream. */
348 JAS_DLLEXPORT long jas_stream_setrwcount(jas_stream_t *stream, long rwcnt);
349
350 /******************************************************************************\
351 * Macros/functions for I/O.
352 \******************************************************************************/
353
354 /* Read a character from a stream. */
355 #if defined(DEBUG)
356 #define jas_stream_getc(stream) jas_stream_getc_func(stream)
357 #else
358 #define jas_stream_getc(stream) jas_stream_getc_macro(stream)
359 #endif
360
361 /* Write a character to a stream. */
362 #if defined(DEBUG)
363 #define jas_stream_putc(stream, c) jas_stream_putc_func(stream, c)
364 #else
365 #define jas_stream_putc(stream, c) jas_stream_putc_macro(stream, c)
366 #endif
367
368 /* Read characters from a stream into a buffer. */
369 JAS_DLLEXPORT int jas_stream_read(jas_stream_t *stream, void *buf, int cnt);
370
371 /* Write characters from a buffer to a stream. */
372 JAS_DLLEXPORT int jas_stream_write(jas_stream_t *stream, const void *buf, int cnt);
373
374 /* Write formatted output to a stream. */
375 JAS_DLLEXPORT int jas_stream_printf(jas_stream_t *stream, const char *fmt, ...);
376
377 /* Write a string to a stream. */
378 JAS_DLLEXPORT int jas_stream_puts(jas_stream_t *stream, const char *s);
379
380 /* Read a line of input from a stream. */
381 JAS_DLLEXPORT char *jas_stream_gets(jas_stream_t *stream, char *buf, int bufsize);
382
383 /* Look at the next character to be read from a stream without actually
384 removing it from the stream. */
385 #define jas_stream_peekc(stream) \
386 (((stream)->cnt_ <= 0) ? jas_stream_fillbuf(stream, 0) : \
387 ((int)(*(stream)->ptr_)))
388
389 /* Put a character back on a stream. */
390 JAS_DLLEXPORT int jas_stream_ungetc(jas_stream_t *stream, int c);
391
392 /******************************************************************************\
393 * Macros/functions for getting/setting the stream position.
394 \******************************************************************************/
395
396 /* Is it possible to seek on this stream? */
397 JAS_DLLEXPORT int jas_stream_isseekable(jas_stream_t *stream);
398
399 /* Set the current position within the stream. */
400 JAS_DLLEXPORT long jas_stream_seek(jas_stream_t *stream, long offset, int origin);
401
402 /* Get the current position within the stream. */
403 JAS_DLLEXPORT long jas_stream_tell(jas_stream_t *stream);
404
405 /* Seek to the beginning of a stream. */
406 JAS_DLLEXPORT int jas_stream_rewind(jas_stream_t *stream);
407
408 /******************************************************************************\
409 * Macros/functions for flushing.
410 \******************************************************************************/
411
412 /* Flush any pending output to a stream. */
413 JAS_DLLEXPORT int jas_stream_flush(jas_stream_t *stream);
414
415 /******************************************************************************\
416 * Miscellaneous macros/functions.
417 \******************************************************************************/
418
419 /* Copy data from one stream to another. */
420 JAS_DLLEXPORT int jas_stream_copy(jas_stream_t *dst, jas_stream_t *src, int n);
421
422 /* Display stream contents (for debugging purposes). */
423 JAS_DLLEXPORT int jas_stream_display(jas_stream_t *stream, FILE *fp, int n);
424
425 /* Consume (i.e., discard) characters from stream. */
426 JAS_DLLEXPORT int jas_stream_gobble(jas_stream_t *stream, int n);
427
428 /* Write a character multiple times to a stream. */
429 JAS_DLLEXPORT int jas_stream_pad(jas_stream_t *stream, int n, int c);
430
431 /* Get the size of the file associated with the specified stream.
432 The specified stream must be seekable. */
433 JAS_DLLEXPORT long jas_stream_length(jas_stream_t *stream);
434
435 /******************************************************************************\
436 * Internal functions.
437 \******************************************************************************/
438
439 /* The following functions are for internal use only! If you call them
440 directly, you will die a horrible, miserable, and painful death! */
441
442 /* Read a character from a stream. */
443 #define jas_stream_getc_macro(stream) \
444 ((!((stream)->flags_ & (JAS_STREAM_ERR | JAS_STREAM_EOF | \
445 JAS_STREAM_RWLIMIT))) ? \
446 (((stream)->rwlimit_ >= 0 && (stream)->rwcnt_ >= (stream)->rwlimit_) ? \
447 (stream->flags_ |= JAS_STREAM_RWLIMIT, EOF) : \
448 jas_stream_getc2(stream)) : EOF)
449 #define jas_stream_getc2(stream) \
450 ((--(stream)->cnt_ < 0) ? jas_stream_fillbuf(stream, 1) : \
451 (++(stream)->rwcnt_, (int)(*(stream)->ptr_++)))
452
453 /* Write a character to a stream. */
454 #define jas_stream_putc_macro(stream, c) \
455 ((!((stream)->flags_ & (JAS_STREAM_ERR | JAS_STREAM_EOF | \
456 JAS_STREAM_RWLIMIT))) ? \
457 (((stream)->rwlimit_ >= 0 && (stream)->rwcnt_ >= (stream)->rwlimit_) ? \
458 (stream->flags_ |= JAS_STREAM_RWLIMIT, EOF) : \
459 jas_stream_putc2(stream, c)) : EOF)
460 #define jas_stream_putc2(stream, c) \
461 (((stream)->bufmode_ |= JAS_STREAM_WRBUF, --(stream)->cnt_ < 0) ? \
462 jas_stream_flushbuf((stream), (jas_uchar)(c)) : \
463 (++(stream)->rwcnt_, (int)(*(stream)->ptr_++ = (c))))
464
465 /* These prototypes need to be here for the sake of the stream_getc and
466 stream_putc macros. */
467 JAS_DLLEXPORT int jas_stream_fillbuf(jas_stream_t *stream, int getflag);
468 JAS_DLLEXPORT int jas_stream_flushbuf(jas_stream_t *stream, int c);
469 JAS_DLLEXPORT int jas_stream_getc_func(jas_stream_t *stream);
470 JAS_DLLEXPORT int jas_stream_putc_func(jas_stream_t *stream, int c);
471
472 #ifdef __cplusplus
473 }
474 #endif
475
476 #endif