1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3 * Copyright (c) 1999-2004 Carnegie Mellon University. All rights
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
18 * This work was supported in part by funding from the Defense Advanced
19 * Research Projects Agency and the National Science Foundation of the
20 * United States of America, and the CMU Sphinx Speech Consortium.
22 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
23 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
26 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * ====================================================================
38 * pio.h -- Packaged I/O routines.
40 * **********************************************
41 * CMU ARPA Speech Project
43 * Copyright (c) 1999 Carnegie Mellon University.
44 * ALL RIGHTS RESERVED.
45 * **********************************************
49 * Revision 1.3 2005/06/22 08:00:09 arthchan2003
50 * Completed all doxygen documentation on file description for libs3decoder/libutil/libs3audio and programs.
52 * Revision 1.2 2005/06/22 03:09:52 arthchan2003
53 * 1, Fixed doxygen documentation, 2, Added keyword.
55 * Revision 1.2 2005/06/16 00:14:08 archan
56 * Added const keyword to file argument for file_open
58 * Revision 1.1 2005/06/15 06:11:03 archan
59 * sphinx3 to s3.generic: change io.[ch] to pio.[ch]
61 * Revision 1.5 2005/06/15 04:21:46 archan
62 * 1, Fixed doxygen-documentation, 2, Add keyword such that changes will be logged into a file.
64 * Revision 1.4 2005/04/20 03:49:32 archan
65 * Add const to string argument of myfopen.
67 * Revision 1.3 2005/03/30 01:22:48 archan
68 * Fixed mistakes in last updates. Add
71 * 08-Dec-1999 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
74 * 11-Mar-1999 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
75 * Added _myfopen() and myfopen macro.
77 * 05-Sep-97 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
82 #ifndef _LIBUTIL_IO_H_
83 #define _LIBUTIL_IO_H_
86 #if !defined(_WIN32_WCE) && !(defined(__ADSPBLACKFIN__) && !defined(__linux__))
90 /* Win32/WinCE DLL gunk */
91 #include <sphinxbase/sphinxbase_export.h>
92 #include <sphinxbase/prim_type.h>
95 * \brief file IO related operations.
97 * Custom fopen with error checking is implemented. fopen_comp can
98 * open a file with .z, .Z, .gz or .GZ extension
100 * WARNING: Usage of stat_retry will results in 100s of waiting time
101 * if the file doesn't exist.
113 * Like fopen, but use popen and zcat if it is determined that "file" is compressed
114 * (i.e., has a .z, .Z, .gz, or .GZ extension).
117 FILE *fopen_comp (const char *file, /**< In: File to be opened */
118 const char *mode, /**< In: "r" or "w", as with normal fopen */
119 int32 *ispipe /**< Out: On return *ispipe is TRUE iff file
120 was opened via a pipe */
124 * Close a file opened using fopen_comp.
127 void fclose_comp (FILE *fp, /**< In: File pointer to be closed */
128 int32 ispipe /**< In: ispipe argument that was returned by the
129 corresponding fopen_comp() call */
133 * Open a file for reading, but if file not present try to open compressed version (if
134 * file is uncompressed, and vice versa).
137 FILE *fopen_compchk (const char *file, /**< In: File to be opened */
138 int32 *ispipe /**< Out: On return *ispipe is TRUE iff file
139 was opened via a pipe */
143 * Wrapper around fopen to check for failure and E_FATAL if failed.
146 FILE *_myfopen(const char *file, const char *mode,
147 const char *pgm, int32 line); /* In: __FILE__, __LINE__ from where called */
148 #define myfopen(file,mode) _myfopen((file),(mode),__FILE__,__LINE__)
152 * NFS file reads seem to fail now and then. Use the following functions in place of
153 * the regular fread. It retries failed freads several times and quits only if all of
154 * them fail. Be aware, however, that even normal failures such as attempting to read
155 * beyond EOF will trigger such retries, wasting about a minute in retries.
156 * Arguments identical to regular fread.
159 int32 fread_retry(void *pointer, int32 size, int32 num_items, FILE *stream);
162 * Read a line of arbitrary length from a file and return it as a
163 * newly allocated string.
165 * @deprecated Use line iterators instead.
167 * @param stream The file handle to read from.
168 * @param out_len Output: if not NULL, length of the string read.
169 * @return allocated string containing the line, or NULL on error or EOF.
172 char *fread_line(FILE *stream, size_t *out_len);
175 * Line iterator for files.
177 typedef struct lineiter_t {
185 * Start reading lines from a file.
188 lineiter_t *lineiter_start(FILE *fh);
191 * Move to the next line in the file.
194 lineiter_t *lineiter_next(lineiter_t *li);
197 * Stop reading lines from a file.
200 void lineiter_free(lineiter_t *li);
204 /* Fake this for WinCE which has no stat() */
210 #endif /* _WIN32_WCE */
212 #if defined(__ADSPBLACKFIN__) && !defined(__linux__)
221 * Bitstream encoder - for writing compressed files.
223 typedef struct bit_encode_s bit_encode_t;
226 * Attach bitstream encoder to a file.
228 bit_encode_t *bit_encode_attach(FILE *outfh);
231 * Retain pointer to a bit encoder.
233 bit_encode_t *bit_encode_retain(bit_encode_t *be);
236 * Release pointer to a bit encoder.
238 * Note that this does NOT flush any leftover bits.
240 int bit_encode_free(bit_encode_t *be);
243 * Write bits to encoder.
245 int bit_encode_write(bit_encode_t *be, unsigned char const *bits, int nbits);
248 * Write lowest-order bits of codeword to encoder.
250 int bit_encode_write_cw(bit_encode_t *be, uint32 codeword, int nbits);
253 * Flush any unwritten bits, zero-padding if necessary.
255 int bit_encode_flush(bit_encode_t *be);
258 * There is no bitstream decoder, because a stream abstraction is too
259 * slow. Instead we read blocks of bits and treat them as bitvectors.
263 * Like fread_retry, but for stat. Arguments identical to regular stat.
264 * Return value: 0 if successful, -1 if stat failed several attempts.
267 int32 stat_retry (const char *file, struct stat *statbuf);
270 * Return time of last modification for the given file, or -1 if stat fails.
274 int32 stat_mtime (const char *file);
277 * Create a directory and all of its parent directories, as needed.
279 * @return 0 on success, <0 on failure.
282 int build_directory(const char *path);