/* * Copyright (c) 1999-2000 Image Power, Inc. and the University of * British Columbia. * Copyright (c) 2001-2003 Michael David Adams. * All rights reserved. */ /* __START_OF_JASPER_LICENSE__ * * JasPer License Version 2.0 * * Copyright (c) 2001-2006 Michael David Adams * Copyright (c) 1999-2000 Image Power, Inc. * Copyright (c) 1999-2000 The University of British Columbia * * All rights reserved. * * Permission is hereby granted, free of charge, to any person (the * "User") obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, * publish, distribute, and/or sell copies of the Software, and to permit * persons to whom the Software is furnished to do so, subject to the * following conditions: * * 1. The above copyright notices and this permission notice (which * includes the disclaimer below) shall be included in all copies or * substantial portions of the Software. * * 2. The name of a copyright holder shall not be used to endorse or * promote products derived from the Software without specific prior * written permission. * * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. * * __END_OF_JASPER_LICENSE__ */ /*! * @file jas_stream.h * @brief I/O Stream Class */ #ifndef JAS_STREAM_H#define JAS_STREAM_H /******************************************************************************\* Includes.\******************************************************************************/ /* The configuration header file should be included first. */#include <jasper/jas_config.h> /* IWYU pragma: export */ #if defined(JAS_HAVE_FCNTL_H)#include <fcntl.h>#endif#include <jasper/jas_types.h> #ifdef __cplusplusextern "C" {#endif /*! * @addtogroup module_iostreams * @{ */ /******************************************************************************\* Constants.\******************************************************************************/ /* On most UNIX systems, we probably need to define O_BINARY ourselves. */#ifndef O_BINARY#define O_BINARY 0#endif /* * Stream open flags. */ /* The stream was opened for reading. */#define JAS_STREAM_READ 0x0001/* The stream was opened for writing. */#define JAS_STREAM_WRITE 0x0002/* The stream was opened for appending. */#define JAS_STREAM_APPEND 0x0004/* The stream was opened in binary mode. */#define JAS_STREAM_BINARY 0x0008/* The stream should be created/truncated. */#define JAS_STREAM_CREATE 0x0010 /* * Stream buffering flags. */ /* The stream is unbuffered. */#define JAS_STREAM_UNBUF 0x0000/* The stream is line buffered. */#define JAS_STREAM_LINEBUF 0x0001/* The stream is fully buffered. */#define JAS_STREAM_FULLBUF 0x0002/* The buffering mode mask. */#define JAS_STREAM_BUFMODEMASK 0x000f /* The memory associated with the buffer needs to be deallocated when the stream is destroyed. */#define JAS_STREAM_FREEBUF 0x0008/* The buffer is currently being used for reading. */#define JAS_STREAM_RDBUF 0x0010/* The buffer is currently being used for writing. */#define JAS_STREAM_WRBUF 0x0020 /* * Stream error flags. */ /* The end-of-file has been encountered (on reading). */#define JAS_STREAM_EOF 0x0001/* An I/O error has been encountered on the stream. */#define JAS_STREAM_ERR 0x0002/* The read/write limit has been exceeded. */#define JAS_STREAM_RWLIMIT 0x0004/* The error mask. */#define JAS_STREAM_ERRMASK \ (JAS_STREAM_EOF | JAS_STREAM_ERR | JAS_STREAM_RWLIMIT) /* * Other miscellaneous constants. */ /* The default buffer size (for fully-buffered operation). */#define JAS_STREAM_BUFSIZE 8192/* The default permission mask for file creation. */#define JAS_STREAM_PERMS 0666 /* The maximum number of characters that can always be put back on a stream. */#define JAS_STREAM_MAXPUTBACK 16 /******************************************************************************\* Types.\******************************************************************************/ /* * Generic file object. */ typedef void jas_stream_obj_t; /* * Generic file object operations. */ typedef struct { /* Read characters from a file object. */ ssize_t (*read_)(jas_stream_obj_t *obj, char *buf, size_t cnt); /* Write characters to a file object. */ ssize_t (*write_)(jas_stream_obj_t *obj, const char *buf, size_t cnt); /* Set the position for a file object. */ long (*seek_)(jas_stream_obj_t *obj, long offset, int origin); /* Close a file object. */ int (*close_)(jas_stream_obj_t *obj); } jas_stream_ops_t; /*!@briefI/O stream object. @warningLibrary users should never directly access any of the members of thisclass.The functions/macros provided by the JasPer library API should alwaysbe used. */ typedef struct { /* The mode in which the stream was opened. */ int openmode_; /* The buffering mode. */ int bufmode_; /* The stream status. */ int flags_; /* The start of the buffer area to use for reading/writing. */ jas_uchar *bufbase_; /* The start of the buffer area excluding the extra initial space for character putback. */ jas_uchar *bufstart_; /* The buffer size. */ int bufsize_; /* The current position in the buffer. */ jas_uchar *ptr_; /* The number of characters that must be read/written before the buffer needs to be filled/flushed. */ int cnt_; /* A trivial buffer to be used for unbuffered operation. */ jas_uchar tinybuf_[JAS_STREAM_MAXPUTBACK + 1]; /* The operations for the underlying stream file object. */ const jas_stream_ops_t *ops_; /* The underlying stream file object. */ jas_stream_obj_t *obj_; /* The number of characters read/written. */ long rwcnt_; /* The maximum number of characters that may be read/written. */ long rwlimit_; } jas_stream_t; /* * Regular file object. */ /* * File descriptor file object. */typedef struct { int fd; int flags; char pathname[L_tmpnam + 1];} jas_stream_fileobj_t; /* Delete underlying file object upon stream close. */#define JAS_STREAM_FILEOBJ_DELONCLOSE 0x01/* Do not close underlying file object upon stream close. */#define JAS_STREAM_FILEOBJ_NOCLOSE 0x02 /* * Memory file object. */ typedef struct { /* The data associated with this file. */ jas_uchar *buf_; /* The allocated size of the buffer for holding file data. */ size_t bufsize_; /* The length of the file. */ size_t len_; /* The seek position. */ size_t pos_; /* Is the buffer growable? */ int growable_; /* Was the buffer allocated internally? */ int myalloc_; } jas_stream_memobj_t; /******************************************************************************\* Macros/functions for opening and closing streams.\******************************************************************************/ /*!@brief Open a file as a stream. @param filenameA pointer to the pathname of the file to be opened.@param modeA pointer to the string specifying the open mode.The open mode is similar to that used by the fopen function in theC standard library. @returnUpon success, a pointer to the opened stream is returned.Otherwise, a null pointer is returned.*/JAS_EXPORTjas_stream_t *jas_stream_fopen(const char *filename, const char *mode); /*!@brief Open a memory buffer as a stream. @param bufferA pointer to the buffer to be used to store stream data.@param buffer_sizeThe size of the buffer. @details<ul><li>If buffer is 0 and buffer_size > 0:a buffer is dynamically allocated with size buffer_size and this buffer isnot growable.<li>If buffer is 0 and buffer_size is 0:a buffer is dynamically allocated whose size will automatically grow toaccommodate the amount of data written.<li>If buffer is not 0:buffer_size (which, in this case, is not currently allowed to be zero) isthe size of the (nongrowable) buffer pointed to by buffer.</ul>*/JAS_EXPORTjas_stream_t *jas_stream_memopen(char *buffer, size_t buffer_size); /*!@briefDo not use.@deprecatedDo not use this function.This function is deprecated.Use jas_stream_memopen instead.*/JAS_DEPRECATEDJAS_EXPORTjas_stream_t *jas_stream_memopen2(char *buffer, size_t buffer_size); /*!@brief Open a file descriptor as a stream. @param fdThe file descriptor of the file to open as a stream.@param modeA pointer to a string specifying the open mode.The format of this string is similar to that of the fdopen functionin the C standard library. @returnUpon success, a pointer to the opened stream is returned.Otherwise, a null pointer is returned.*/JAS_EXPORTjas_stream_t *jas_stream_fdopen(int fd, const char *mode); /*!@brief Open a stdio (i.e., C standard library) stream as a stream. @param pathA pointer to a null-terminated string containing the pathname of the fileto be reopened.@param modeA pointer to a null-terminated string containing the mode to be used forreopening the file.This string is similar to that used by the fdopen function in theC standard library.@param fpA pointer to the `FILE` (i.e., stdio stream) to be reopened. @detailsIt is unspecified whether the open mode specified by mode can bechanged from the open mode used for opening the stdio stream. @returnUpon success, a pointer to the opened stream is returned.Otherwise, a null pointer is returned.*/JAS_EXPORTjas_stream_t *jas_stream_freopen(const char *path, const char *mode, FILE *fp); /*!@brief Open a temporary file as a stream. @detailsA temporary file is created and opened as a stream.The temporary file is deleted when closed via jas_stream_close().Some operating systems provide a mechanism for ensuring that a fileis removed when closed.Such functionality may be used by the implementation when available. @returnUpon success, a pointer to the opened stream is returned.Otherwise, a null pointer is returned.*/JAS_EXPORTjas_stream_t *jas_stream_tmpfile(void); /*!@brief Close a stream. @param streamA (nonnull) pointer to the stream to be closed. @detailsThe close operation will implicitly flush any pending outputto the stream before closing.If such a flush operation fails, this will be reflected inthe return value of this function.For many systems, it is likely that the main reason that this functioncan fail is due to an I/O error when flushing buffered output. @returnIf no errors are encountered when closing the stream, 0 is returned.Otherwise, a nonzero value is returned.*/JAS_EXPORTint jas_stream_close(jas_stream_t *stream); /******************************************************************************\* Macros/functions for getting/setting the stream state.\******************************************************************************/ /*!@brief Get the EOF indicator for a stream. @param streamThe stream whose EOF indicator is to be queried. @returnThe value of the EOF indicator is returned.A nonzero value indicates that the stream has encountered EOF.*/#define jas_stream_eof(stream) \ (((stream)->flags_ & JAS_STREAM_EOF) != 0) /*!@brief Get the error indicator for a stream. @param streamThe stream whose error indicator is to be queried.@returnThe value of the error indicator is returned.A nonzero value indicates that the stream has encountered an errorof some type (such as an I/O error).Note that EOF is not an error.*/#define jas_stream_error(stream) \ (((stream)->flags_ & JAS_STREAM_ERR) != 0) /*!@brief Clear the error indicator for a stream. @param streamThe stream whose error indicator is to be cleared. @todoTODO/FIXME: Should this macro evaluate to void?*/#define jas_stream_clearerr(stream) \ ((stream)->flags_ &= ~(JAS_STREAM_ERR | JAS_STREAM_EOF)) /*!@brief Get the read/write limit for a stream.@param streamA pointer to the stream whose read/write limit is to be queried.@returnThe read/write limit for the stream is returned.This operation cannot fail.A negative read/write limit indicates no limit (i.e., an limit that iseffectively infinite).*/#define jas_stream_getrwlimit(stream) \ (((const jas_stream_t *)(stream))->rwlimit_) /*!@brief Set the read/write limit for a stream. @param streamA pointer to the stream whose read/write limit is to be set.@param rwlimitThe new value for the read/write limit. @detailsA negative read/write limit is treated as if it were infinity(i.e., there is no read/write limit).Lines 1–500 of 1106. Load more
/* * Copyright (c) 1999-2000 Image Power, Inc. and the University of * British Columbia. * Copyright (c) 2001-2003 Michael David Adams. * All rights reserved. */ /* __START_OF_JASPER_LICENSE__ * * JasPer License Version 2.0 * * Copyright (c) 2001-2006 Michael David Adams * Copyright (c) 1999-2000 Image Power, Inc. * Copyright (c) 1999-2000 The University of British Columbia * * All rights reserved. * * Permission is hereby granted, free of charge, to any person (the * "User") obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, * publish, distribute, and/or sell copies of the Software, and to permit * persons to whom the Software is furnished to do so, subject to the * following conditions: * * 1. The above copyright notices and this permission notice (which * includes the disclaimer below) shall be included in all copies or * substantial portions of the Software. * * 2. The name of a copyright holder shall not be used to endorse or * promote products derived from the Software without specific prior * written permission. * * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. * * __END_OF_JASPER_LICENSE__ */ /*! * @file jas_stream.h * @brief I/O Stream Class */ #ifndef JAS_STREAM_H #define JAS_STREAM_H /******************************************************************************\ * Includes. \******************************************************************************/ /* The configuration header file should be included first. */ #include <jasper/jas_config.h> /* IWYU pragma: export */ #if defined(JAS_HAVE_FCNTL_H) #include <fcntl.h> #endif #include <jasper/jas_types.h> #ifdef __cplusplus extern "C" { #endif /*! * @addtogroup module_iostreams * @{ */ /******************************************************************************\ * Constants. \******************************************************************************/ /* On most UNIX systems, we probably need to define O_BINARY ourselves. */ #ifndef O_BINARY #define O_BINARY 0 #endif /* * Stream open flags. */ /* The stream was opened for reading. */ #define JAS_STREAM_READ 0x0001 /* The stream was opened for writing. */ #define JAS_STREAM_WRITE 0x0002 /* The stream was opened for appending. */ #define JAS_STREAM_APPEND 0x0004 /* The stream was opened in binary mode. */ #define JAS_STREAM_BINARY 0x0008 /* The stream should be created/truncated. */ #define JAS_STREAM_CREATE 0x0010 /* * Stream buffering flags. */ /* The stream is unbuffered. */ #define JAS_STREAM_UNBUF 0x0000 /* The stream is line buffered. */ #define JAS_STREAM_LINEBUF 0x0001 /* The stream is fully buffered. */ #define JAS_STREAM_FULLBUF 0x0002 /* The buffering mode mask. */ #define JAS_STREAM_BUFMODEMASK 0x000f /* The memory associated with the buffer needs to be deallocated when the stream is destroyed. */ #define JAS_STREAM_FREEBUF 0x0008 /* The buffer is currently being used for reading. */ #define JAS_STREAM_RDBUF 0x0010 /* The buffer is currently being used for writing. */ #define JAS_STREAM_WRBUF 0x0020 /* * Stream error flags. */ /* The end-of-file has been encountered (on reading). */ #define JAS_STREAM_EOF 0x0001 /* An I/O error has been encountered on the stream. */ #define JAS_STREAM_ERR 0x0002 /* The read/write limit has been exceeded. */ #define JAS_STREAM_RWLIMIT 0x0004 /* The error mask. */ #define JAS_STREAM_ERRMASK \ (JAS_STREAM_EOF | JAS_STREAM_ERR | JAS_STREAM_RWLIMIT) /* * Other miscellaneous constants. */ /* The default buffer size (for fully-buffered operation). */ #define JAS_STREAM_BUFSIZE 8192 /* The default permission mask for file creation. */ #define JAS_STREAM_PERMS 0666 /* The maximum number of characters that can always be put back on a stream. */ #define JAS_STREAM_MAXPUTBACK 16 /******************************************************************************\ * Types. \******************************************************************************/ /* * Generic file object. */ typedef void jas_stream_obj_t; /* * Generic file object operations. */ typedef struct { /* Read characters from a file object. */ ssize_t (*read_)(jas_stream_obj_t *obj, char *buf, size_t cnt); /* Write characters to a file object. */ ssize_t (*write_)(jas_stream_obj_t *obj, const char *buf, size_t cnt); /* Set the position for a file object. */ long (*seek_)(jas_stream_obj_t *obj, long offset, int origin); /* Close a file object. */ int (*close_)(jas_stream_obj_t *obj); } jas_stream_ops_t; /*! @brief I/O stream object. @warning Library users should never directly access any of the members of this class. The functions/macros provided by the JasPer library API should always be used. */ typedef struct { /* The mode in which the stream was opened. */ int openmode_; /* The buffering mode. */ int bufmode_; /* The stream status. */ int flags_; /* The start of the buffer area to use for reading/writing. */ jas_uchar *bufbase_; /* The start of the buffer area excluding the extra initial space for character putback. */ jas_uchar *bufstart_; /* The buffer size. */ int bufsize_; /* The current position in the buffer. */ jas_uchar *ptr_; /* The number of characters that must be read/written before the buffer needs to be filled/flushed. */ int cnt_; /* A trivial buffer to be used for unbuffered operation. */ jas_uchar tinybuf_[JAS_STREAM_MAXPUTBACK + 1]; /* The operations for the underlying stream file object. */ const jas_stream_ops_t *ops_; /* The underlying stream file object. */ jas_stream_obj_t *obj_; /* The number of characters read/written. */ long rwcnt_; /* The maximum number of characters that may be read/written. */ long rwlimit_; } jas_stream_t; /* * Regular file object. */ /* * File descriptor file object. */ typedef struct { int fd; int flags; char pathname[L_tmpnam + 1]; } jas_stream_fileobj_t; /* Delete underlying file object upon stream close. */ #define JAS_STREAM_FILEOBJ_DELONCLOSE 0x01 /* Do not close underlying file object upon stream close. */ #define JAS_STREAM_FILEOBJ_NOCLOSE 0x02 /* * Memory file object. */ typedef struct { /* The data associated with this file. */ jas_uchar *buf_; /* The allocated size of the buffer for holding file data. */ size_t bufsize_; /* The length of the file. */ size_t len_; /* The seek position. */ size_t pos_; /* Is the buffer growable? */ int growable_; /* Was the buffer allocated internally? */ int myalloc_; } jas_stream_memobj_t; /******************************************************************************\ * Macros/functions for opening and closing streams. \******************************************************************************/ /*! @brief Open a file as a stream. @param filename A pointer to the pathname of the file to be opened. @param mode A pointer to the string specifying the open mode. The open mode is similar to that used by the fopen function in the C standard library. @return Upon success, a pointer to the opened stream is returned. Otherwise, a null pointer is returned. */ JAS_EXPORT jas_stream_t *jas_stream_fopen(const char *filename, const char *mode); /*! @brief Open a memory buffer as a stream. @param buffer A pointer to the buffer to be used to store stream data. @param buffer_size The size of the buffer. @details <ul> <li> If buffer is 0 and buffer_size > 0: a buffer is dynamically allocated with size buffer_size and this buffer is not growable. <li> If buffer is 0 and buffer_size is 0: a buffer is dynamically allocated whose size will automatically grow to accommodate the amount of data written. <li> If buffer is not 0: buffer_size (which, in this case, is not currently allowed to be zero) is the size of the (nongrowable) buffer pointed to by buffer. </ul> */ JAS_EXPORT jas_stream_t *jas_stream_memopen(char *buffer, size_t buffer_size); /*! @brief Do not use. @deprecated Do not use this function. This function is deprecated. Use jas_stream_memopen instead. */ JAS_DEPRECATED JAS_EXPORT jas_stream_t *jas_stream_memopen2(char *buffer, size_t buffer_size); /*! @brief Open a file descriptor as a stream. @param fd The file descriptor of the file to open as a stream. @param mode A pointer to a string specifying the open mode. The format of this string is similar to that of the fdopen function in the C standard library. @return Upon success, a pointer to the opened stream is returned. Otherwise, a null pointer is returned. */ JAS_EXPORT jas_stream_t *jas_stream_fdopen(int fd, const char *mode); /*! @brief Open a stdio (i.e., C standard library) stream as a stream. @param path A pointer to a null-terminated string containing the pathname of the file to be reopened. @param mode A pointer to a null-terminated string containing the mode to be used for reopening the file. This string is similar to that used by the fdopen function in the C standard library. @param fp A pointer to the `FILE` (i.e., stdio stream) to be reopened. @details It is unspecified whether the open mode specified by mode can be changed from the open mode used for opening the stdio stream. @return Upon success, a pointer to the opened stream is returned. Otherwise, a null pointer is returned. */ JAS_EXPORT jas_stream_t *jas_stream_freopen(const char *path, const char *mode, FILE *fp); /*! @brief Open a temporary file as a stream. @details A temporary file is created and opened as a stream. The temporary file is deleted when closed via jas_stream_close(). Some operating systems provide a mechanism for ensuring that a file is removed when closed. Such functionality may be used by the implementation when available. @return Upon success, a pointer to the opened stream is returned. Otherwise, a null pointer is returned. */ JAS_EXPORT jas_stream_t *jas_stream_tmpfile(void); /*! @brief Close a stream. @param stream A (nonnull) pointer to the stream to be closed. @details The close operation will implicitly flush any pending output to the stream before closing. If such a flush operation fails, this will be reflected in the return value of this function. For many systems, it is likely that the main reason that this function can fail is due to an I/O error when flushing buffered output. @return If no errors are encountered when closing the stream, 0 is returned. Otherwise, a nonzero value is returned. */ JAS_EXPORT int jas_stream_close(jas_stream_t *stream); /******************************************************************************\ * Macros/functions for getting/setting the stream state. \******************************************************************************/ /*! @brief Get the EOF indicator for a stream. @param stream The stream whose EOF indicator is to be queried. @return The value of the EOF indicator is returned. A nonzero value indicates that the stream has encountered EOF. */ #define jas_stream_eof(stream) \ (((stream)->flags_ & JAS_STREAM_EOF) != 0) /*! @brief Get the error indicator for a stream. @param stream The stream whose error indicator is to be queried. @return The value of the error indicator is returned. A nonzero value indicates that the stream has encountered an error of some type (such as an I/O error). Note that EOF is not an error. */ #define jas_stream_error(stream) \ (((stream)->flags_ & JAS_STREAM_ERR) != 0) /*! @brief Clear the error indicator for a stream. @param stream The stream whose error indicator is to be cleared. @todo TODO/FIXME: Should this macro evaluate to void? */ #define jas_stream_clearerr(stream) \ ((stream)->flags_ &= ~(JAS_STREAM_ERR | JAS_STREAM_EOF)) /*! @brief Get the read/write limit for a stream. @param stream A pointer to the stream whose read/write limit is to be queried. @return The read/write limit for the stream is returned. This operation cannot fail. A negative read/write limit indicates no limit (i.e., an limit that is effectively infinite). */ #define jas_stream_getrwlimit(stream) \ (((const jas_stream_t *)(stream))->rwlimit_) /*! @brief Set the read/write limit for a stream. @param stream A pointer to the stream whose read/write limit is to be set. @param rwlimit The new value for the read/write limit. @details A negative read/write limit is treated as if it were infinity (i.e., there is no read/write limit).