Path: blob/master/3rdparty/libjasper/jasper/jas_stream.h
16337 views
/*1* Copyright (c) 1999-2000 Image Power, Inc. and the University of2* British Columbia.3* Copyright (c) 2001-2003 Michael David Adams.4* All rights reserved.5*/67/* __START_OF_JASPER_LICENSE__8*9* JasPer License Version 2.010*11* Copyright (c) 2001-2006 Michael David Adams12* Copyright (c) 1999-2000 Image Power, Inc.13* Copyright (c) 1999-2000 The University of British Columbia14*15* All rights reserved.16*17* Permission is hereby granted, free of charge, to any person (the18* "User") obtaining a copy of this software and associated documentation19* files (the "Software"), to deal in the Software without restriction,20* including without limitation the rights to use, copy, modify, merge,21* publish, distribute, and/or sell copies of the Software, and to permit22* persons to whom the Software is furnished to do so, subject to the23* following conditions:24*25* 1. The above copyright notices and this permission notice (which26* includes the disclaimer below) shall be included in all copies or27* substantial portions of the Software.28*29* 2. The name of a copyright holder shall not be used to endorse or30* promote products derived from the Software without specific prior31* written permission.32*33* THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS34* LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER35* THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS36* "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING37* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A38* PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO39* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL40* INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING41* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,42* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION43* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE44* PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE45* THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.46* EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS47* BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL48* PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS49* GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE50* ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE51* IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL52* SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,53* AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL54* SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH55* THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,56* PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH57* RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY58* EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.59*60* __END_OF_JASPER_LICENSE__61*/6263/*64* I/O Stream Class65*66* $Id: jas_stream.h,v 1.2 2008-05-26 09:41:51 vp153 Exp $67*/6869#ifndef JAS_STREAM_H70#define JAS_STREAM_H7172/******************************************************************************\73* Includes.74\******************************************************************************/7576#include <jasper/jas_config.h>7778#include <stdio.h>79#include <limits.h>80#if defined(HAVE_FCNTL_H)81#include <fcntl.h>82#endif83#include <string.h>84#if defined(HAVE_UNISTD_H)85#include <unistd.h>86#endif87#include <jasper/jas_types.h>8889#ifdef __cplusplus90extern "C" {91#endif9293/******************************************************************************\94* Constants.95\******************************************************************************/9697/* On most UNIX systems, we probably need to define O_BINARY ourselves. */98#ifndef O_BINARY99#define O_BINARY 0100#endif101102/*103* Stream open flags.104*/105106/* The stream was opened for reading. */107#define JAS_STREAM_READ 0x0001108/* The stream was opened for writing. */109#define JAS_STREAM_WRITE 0x0002110/* The stream was opened for appending. */111#define JAS_STREAM_APPEND 0x0004112/* The stream was opened in binary mode. */113#define JAS_STREAM_BINARY 0x0008114/* The stream should be created/truncated. */115#define JAS_STREAM_CREATE 0x0010116117118/*119* Stream buffering flags.120*/121122/* The stream is unbuffered. */123#define JAS_STREAM_UNBUF 0x0000124/* The stream is line buffered. */125#define JAS_STREAM_LINEBUF 0x0001126/* The stream is fully buffered. */127#define JAS_STREAM_FULLBUF 0x0002128/* The buffering mode mask. */129#define JAS_STREAM_BUFMODEMASK 0x000f130131/* The memory associated with the buffer needs to be deallocated when the132stream is destroyed. */133#define JAS_STREAM_FREEBUF 0x0008134/* The buffer is currently being used for reading. */135#define JAS_STREAM_RDBUF 0x0010136/* The buffer is currently being used for writing. */137#define JAS_STREAM_WRBUF 0x0020138139/*140* Stream error flags.141*/142143/* The end-of-file has been encountered (on reading). */144#define JAS_STREAM_EOF 0x0001145/* An I/O error has been encountered on the stream. */146#define JAS_STREAM_ERR 0x0002147/* The read/write limit has been exceeded. */148#define JAS_STREAM_RWLIMIT 0x0004149/* The error mask. */150#define JAS_STREAM_ERRMASK \151(JAS_STREAM_EOF | JAS_STREAM_ERR | JAS_STREAM_RWLIMIT)152153/*154* Other miscellaneous constants.155*/156157/* The default buffer size (for fully-buffered operation). */158#define JAS_STREAM_BUFSIZE 8192159/* The default permission mask for file creation. */160#define JAS_STREAM_PERMS 0666161162/* The maximum number of characters that can always be put back on a stream. */163#define JAS_STREAM_MAXPUTBACK 16164165/******************************************************************************\166* Types.167\******************************************************************************/168169/*170* Generic file object.171*/172173typedef void jas_stream_obj_t;174175/*176* Generic file object operations.177*/178179typedef struct {180181/* Read characters from a file object. */182int (*read_)(jas_stream_obj_t *obj, char *buf, int cnt);183184/* Write characters to a file object. */185int (*write_)(jas_stream_obj_t *obj, char *buf, int cnt);186187/* Set the position for a file object. */188long (*seek_)(jas_stream_obj_t *obj, long offset, int origin);189190/* Close a file object. */191int (*close_)(jas_stream_obj_t *obj);192193} jas_stream_ops_t;194195/*196* Stream object.197*/198199typedef struct {200201/* The mode in which the stream was opened. */202int openmode_;203204/* The buffering mode. */205int bufmode_;206207/* The stream status. */208int flags_;209210/* The start of the buffer area to use for reading/writing. */211uchar *bufbase_;212213/* The start of the buffer area excluding the extra initial space for214character putback. */215uchar *bufstart_;216217/* The buffer size. */218int bufsize_;219220/* The current position in the buffer. */221uchar *ptr_;222223/* The number of characters that must be read/written before224the buffer needs to be filled/flushed. */225int cnt_;226227/* A trivial buffer to be used for unbuffered operation. */228uchar tinybuf_[JAS_STREAM_MAXPUTBACK + 1];229230/* The operations for the underlying stream file object. */231jas_stream_ops_t *ops_;232233/* The underlying stream file object. */234jas_stream_obj_t *obj_;235236/* The number of characters read/written. */237long rwcnt_;238239/* The maximum number of characters that may be read/written. */240long rwlimit_;241242} jas_stream_t;243244/*245* Regular file object.246*/247248/*249* File descriptor file object.250*/251typedef struct {252int fd;253int flags;254#if defined _WIN32 && !defined __MINGW__ && !defined __MINGW32__255char pathname[MAX_PATH + 1];256#else257char pathname[PATH_MAX + 1];258#endif259} jas_stream_fileobj_t;260261#define JAS_STREAM_FILEOBJ_DELONCLOSE 0x01262#define JAS_STREAM_FILEOBJ_NOCLOSE 0x02263264/*265* Memory file object.266*/267268typedef struct {269270/* The data associated with this file. */271uchar *buf_;272273/* The allocated size of the buffer for holding file data. */274int bufsize_;275276/* The length of the file. */277int_fast32_t len_;278279/* The seek position. */280int_fast32_t pos_;281282/* Is the buffer growable? */283int growable_;284285/* Was the buffer allocated internally? */286int myalloc_;287288} jas_stream_memobj_t;289290/******************************************************************************\291* Macros/functions for opening and closing streams.292\******************************************************************************/293294/* Open a file as a stream. */295jas_stream_t *jas_stream_fopen(const char *filename, const char *mode);296297/* Open a memory buffer as a stream. */298jas_stream_t *jas_stream_memopen(char *buf, int bufsize);299300/* Open a file descriptor as a stream. */301jas_stream_t *jas_stream_fdopen(int fd, const char *mode);302303/* Open a stdio stream as a stream. */304jas_stream_t *jas_stream_freopen(const char *path, const char *mode, FILE *fp);305306/* Open a temporary file as a stream. */307jas_stream_t *jas_stream_tmpfile(void);308309/* Close a stream. */310int jas_stream_close(jas_stream_t *stream);311312/******************************************************************************\313* Macros/functions for getting/setting the stream state.314\******************************************************************************/315316/* Get the EOF indicator for a stream. */317#define jas_stream_eof(stream) \318(((stream)->flags_ & JAS_STREAM_EOF) != 0)319320/* Get the error indicator for a stream. */321#define jas_stream_error(stream) \322(((stream)->flags_ & JAS_STREAM_ERR) != 0)323324/* Clear the error indicator for a stream. */325#define jas_stream_clearerr(stream) \326((stream)->flags_ &= ~(JAS_STREAM_ERR | JAS_STREAM_EOF))327328/* Get the read/write limit for a stream. */329#define jas_stream_getrwlimit(stream) \330(((const jas_stream_t *)(stream))->rwlimit_)331332/* Set the read/write limit for a stream. */333int jas_stream_setrwlimit(jas_stream_t *stream, long rwlimit);334335/* Get the read/write count for a stream. */336#define jas_stream_getrwcount(stream) \337(((const jas_stream_t *)(stream))->rwcnt_)338339/* Set the read/write count for a stream. */340long jas_stream_setrwcount(jas_stream_t *stream, long rwcnt);341342/******************************************************************************\343* Macros/functions for I/O.344\******************************************************************************/345346/* Read a character from a stream. */347#if defined(DEBUG)348#define jas_stream_getc(stream) jas_stream_getc_func(stream)349#else350#define jas_stream_getc(stream) jas_stream_getc_macro(stream)351#endif352353/* Write a character to a stream. */354#if defined(DEBUG)355#define jas_stream_putc(stream, c) jas_stream_putc_func(stream, c)356#else357#define jas_stream_putc(stream, c) jas_stream_putc_macro(stream, c)358#endif359360/* Read characters from a stream into a buffer. */361int jas_stream_read(jas_stream_t *stream, void *buf, int cnt);362363/* Write characters from a buffer to a stream. */364int jas_stream_write(jas_stream_t *stream, const void *buf, int cnt);365366/* Write formatted output to a stream. */367int jas_stream_printf(jas_stream_t *stream, const char *fmt, ...);368369/* Write a string to a stream. */370int jas_stream_puts(jas_stream_t *stream, const char *s);371372/* Read a line of input from a stream. */373char *jas_stream_gets(jas_stream_t *stream, char *buf, int bufsize);374375/* Look at the next character to be read from a stream without actually376removing it from the stream. */377#define jas_stream_peekc(stream) \378(((stream)->cnt_ <= 0) ? jas_stream_fillbuf(stream, 0) : \379((int)(*(stream)->ptr_)))380381/* Put a character back on a stream. */382int jas_stream_ungetc(jas_stream_t *stream, int c);383384/******************************************************************************\385* Macros/functions for getting/setting the stream position.386\******************************************************************************/387388/* Is it possible to seek on this stream? */389int jas_stream_isseekable(jas_stream_t *stream);390391/* Set the current position within the stream. */392long jas_stream_seek(jas_stream_t *stream, long offset, int origin);393394/* Get the current position within the stream. */395long jas_stream_tell(jas_stream_t *stream);396397/* Seek to the beginning of a stream. */398int jas_stream_rewind(jas_stream_t *stream);399400/******************************************************************************\401* Macros/functions for flushing.402\******************************************************************************/403404/* Flush any pending output to a stream. */405int jas_stream_flush(jas_stream_t *stream);406407/******************************************************************************\408* Miscellaneous macros/functions.409\******************************************************************************/410411/* Copy data from one stream to another. */412int jas_stream_copy(jas_stream_t *dst, jas_stream_t *src, int n);413414/* Display stream contents (for debugging purposes). */415int jas_stream_display(jas_stream_t *stream, FILE *fp, int n);416417/* Consume (i.e., discard) characters from stream. */418int jas_stream_gobble(jas_stream_t *stream, int n);419420/* Write a character multiple times to a stream. */421int jas_stream_pad(jas_stream_t *stream, int n, int c);422423/* Get the size of the file associated with the specified stream.424The specified stream must be seekable. */425long jas_stream_length(jas_stream_t *stream);426427/******************************************************************************\428* Internal functions.429\******************************************************************************/430431/* The following functions are for internal use only! If you call them432directly, you will die a horrible, miserable, and painful death! */433434/* Read a character from a stream. */435#define jas_stream_getc_macro(stream) \436((!((stream)->flags_ & (JAS_STREAM_ERR | JAS_STREAM_EOF | \437JAS_STREAM_RWLIMIT))) ? \438(((stream)->rwlimit_ >= 0 && (stream)->rwcnt_ >= (stream)->rwlimit_) ? \439(stream->flags_ |= JAS_STREAM_RWLIMIT, EOF) : \440jas_stream_getc2(stream)) : EOF)441#define jas_stream_getc2(stream) \442((--(stream)->cnt_ < 0) ? jas_stream_fillbuf(stream, 1) : \443(++(stream)->rwcnt_, (int)(*(stream)->ptr_++)))444445/* Write a character to a stream. */446#define jas_stream_putc_macro(stream, c) \447((!((stream)->flags_ & (JAS_STREAM_ERR | JAS_STREAM_EOF | \448JAS_STREAM_RWLIMIT))) ? \449(((stream)->rwlimit_ >= 0 && (stream)->rwcnt_ >= (stream)->rwlimit_) ? \450(stream->flags_ |= JAS_STREAM_RWLIMIT, EOF) : \451jas_stream_putc2(stream, c)) : EOF)452#define jas_stream_putc2(stream, c) \453(((stream)->bufmode_ |= JAS_STREAM_WRBUF, --(stream)->cnt_ < 0) ? \454jas_stream_flushbuf((stream), (uchar)(c)) : \455(++(stream)->rwcnt_, (int)(*(stream)->ptr_++ = (c))))456457/* These prototypes need to be here for the sake of the stream_getc and458stream_putc macros. */459int jas_stream_fillbuf(jas_stream_t *stream, int getflag);460int jas_stream_flushbuf(jas_stream_t *stream, int c);461int jas_stream_getc_func(jas_stream_t *stream);462int jas_stream_putc_func(jas_stream_t *stream, int c);463464#ifdef __cplusplus465}466#endif467468#endif469470471