SphinxBase 5prealpha
pio.h File Reference

file IO related operations. More...

#include <stdio.h>
#include <sys/stat.h>
#include <sphinxbase/sphinxbase_export.h>
#include <sphinxbase/prim_type.h>

Go to the source code of this file.

Data Structures

struct  lineiter_t
 Line iterator for files. More...
 

Macros

#define myfopen(file, mode)   _myfopen((file),(mode),__FILE__,__LINE__)
 

Typedefs

typedef struct lineiter_t lineiter_t
 Line iterator for files.
 
typedef struct bit_encode_s bit_encode_t
 Bitstream encoder - for writing compressed files.
 

Functions

SPHINXBASE_EXPORT FILE * fopen_comp (const char *file, const char *mode, int32 *ispipe)
 Like fopen, but use popen and zcat if it is determined that "file" is compressed (i.e., has a .z, .Z, .gz, or .GZ extension).
 
SPHINXBASE_EXPORT void fclose_comp (FILE *fp, int32 ispipe)
 Close a file opened using fopen_comp.
 
SPHINXBASE_EXPORT FILE * fopen_compchk (const char *file, int32 *ispipe)
 Open a file for reading, but if file not present try to open compressed version (if file is uncompressed, and vice versa).
 
SPHINXBASE_EXPORT FILE * _myfopen (const char *file, const char *mode, const char *pgm, int32 line)
 Wrapper around fopen to check for failure and E_FATAL if failed.
 
SPHINXBASE_EXPORT int32 fread_retry (void *pointer, int32 size, int32 num_items, FILE *stream)
 NFS file reads seem to fail now and then.
 
SPHINXBASE_EXPORT char * fread_line (FILE *stream, size_t *out_len)
 Read a line of arbitrary length from a file and return it as a newly allocated string.
 
SPHINXBASE_EXPORT lineiter_tlineiter_start (FILE *fh)
 Start reading lines from a file.
 
SPHINXBASE_EXPORT lineiter_tlineiter_start_clean (FILE *fh)
 Start reading lines from a file, skip comments and trim lines.
 
SPHINXBASE_EXPORT lineiter_tlineiter_next (lineiter_t *li)
 Move to the next line in the file.
 
SPHINXBASE_EXPORT void lineiter_free (lineiter_t *li)
 Stop reading lines from a file.
 
SPHINXBASE_EXPORT int lineiter_lineno (lineiter_t *li)
 Returns current line number.
 
bit_encode_tbit_encode_attach (FILE *outfh)
 Attach bitstream encoder to a file.
 
bit_encode_tbit_encode_retain (bit_encode_t *be)
 Retain pointer to a bit encoder.
 
int bit_encode_free (bit_encode_t *be)
 Release pointer to a bit encoder.
 
int bit_encode_write (bit_encode_t *be, unsigned char const *bits, int nbits)
 Write bits to encoder.
 
int bit_encode_write_cw (bit_encode_t *be, uint32 codeword, int nbits)
 Write lowest-order bits of codeword to encoder.
 
int bit_encode_flush (bit_encode_t *be)
 Flush any unwritten bits, zero-padding if necessary.
 
SPHINXBASE_EXPORT int32 stat_retry (const char *file, struct stat *statbuf)
 There is no bitstream decoder, because a stream abstraction is too slow.
 
SPHINXBASE_EXPORT int32 stat_mtime (const char *file)
 Return time of last modification for the given file, or -1 if stat fails.
 
SPHINXBASE_EXPORT int build_directory (const char *path)
 Create a directory and all of its parent directories, as needed.
 

Detailed Description

file IO related operations.


Custom fopen with error checking is implemented. fopen_comp can open a file with .z, .Z, .gz or .GZ extension

WARNING: Usage of stat_retry will results in 100s of waiting time if the file doesn't exist.

Definition in file pio.h.

Macro Definition Documentation

◆ myfopen

#define myfopen (   file,
  mode 
)    _myfopen((file),(mode),__FILE__,__LINE__)

Definition at line 148 of file pio.h.

Typedef Documentation

◆ bit_encode_t

typedef struct bit_encode_s bit_encode_t

Bitstream encoder - for writing compressed files.

Definition at line 237 of file pio.h.

Function Documentation

◆ bit_encode_attach()

bit_encode_t * bit_encode_attach ( FILE *  outfh)

Attach bitstream encoder to a file.

Definition at line 542 of file pio.c.

References bit_encode_attach(), and ckd_calloc.

Referenced by bit_encode_attach().

◆ bit_encode_flush()

int bit_encode_flush ( bit_encode_t be)

Flush any unwritten bits, zero-padding if necessary.

Definition at line 611 of file pio.c.

References bit_encode_flush().

Referenced by bit_encode_flush().

◆ bit_encode_free()

int bit_encode_free ( bit_encode_t be)

Release pointer to a bit encoder.

Note that this does NOT flush any leftover bits.

Definition at line 560 of file pio.c.

References bit_encode_free(), and ckd_free().

Referenced by bit_encode_free().

◆ bit_encode_retain()

bit_encode_t * bit_encode_retain ( bit_encode_t be)

Retain pointer to a bit encoder.

Definition at line 553 of file pio.c.

References bit_encode_retain().

Referenced by bit_encode_retain().

◆ bit_encode_write()

int bit_encode_write ( bit_encode_t be,
unsigned char const *  bits,
int  nbits 
)

Write bits to encoder.

Definition at line 572 of file pio.c.

References bit_encode_write().

Referenced by bit_encode_write(), and bit_encode_write_cw().

◆ bit_encode_write_cw()

int bit_encode_write_cw ( bit_encode_t be,
uint32  codeword,
int  nbits 
)

Write lowest-order bits of codeword to encoder.

Definition at line 599 of file pio.c.

References bit_encode_write(), and bit_encode_write_cw().

Referenced by bit_encode_write_cw().

◆ build_directory()

SPHINXBASE_EXPORT int build_directory ( const char *  path)

Create a directory and all of its parent directories, as needed.

Returns
0 on success, <0 on failure.

Definition at line 621 of file pio.c.

References build_directory(), ckd_free(), ckd_salloc, E_ERROR_SYSTEM, and path2dirname().

Referenced by build_directory().

◆ fclose_comp()

SPHINXBASE_EXPORT void fclose_comp ( FILE *  fp,
int32  ispipe 
)

Close a file opened using fopen_comp.

Parameters
fpIn: File pointer to be closed
ispipeIn: ispipe argument that was returned by the corresponding fopen_comp() call

Definition at line 184 of file pio.c.

References fclose_comp().

Referenced by fclose_comp().

◆ fopen_comp()

SPHINXBASE_EXPORT FILE * fopen_comp ( const char *  file,
const char *  mode,
int32 *  ispipe 
)

Like fopen, but use popen and zcat if it is determined that "file" is compressed (i.e., has a .z, .Z, .gz, or .GZ extension).

Parameters
fileIn: File to be opened
modeIn: "r" or "w", as with normal fopen
ispipeOut: On return *ispipe is TRUE iff file was opened via a pipe

Definition at line 107 of file pio.c.

References ckd_free(), E_ERROR, E_ERROR_SYSTEM, E_FATAL, fopen_comp(), and string_join().

Referenced by fopen_comp(), and fopen_compchk().

◆ fopen_compchk()

SPHINXBASE_EXPORT FILE * fopen_compchk ( const char *  file,
int32 *  ispipe 
)

Open a file for reading, but if file not present try to open compressed version (if file is uncompressed, and vice versa).

Parameters
fileIn: File to be opened
ispipeOut: On return *ispipe is TRUE iff file was opened via a pipe

Definition at line 201 of file pio.c.

References ckd_calloc, ckd_free(), E_WARN, fopen_comp(), and fopen_compchk().

Referenced by fopen_compchk().

◆ fread_line()

SPHINXBASE_EXPORT char * fread_line ( FILE *  stream,
size_t *  out_len 
)

Read a line of arbitrary length from a file and return it as a newly allocated string.

Deprecated:
Use line iterators instead.
Parameters
streamThe file handle to read from.
out_lenOutput: if not NULL, length of the string read.
Returns
allocated string containing the line, or NULL on error or EOF.

Definition at line 377 of file pio.c.

References ckd_malloc, ckd_realloc, and fread_line().

Referenced by fread_line().

◆ fread_retry()

SPHINXBASE_EXPORT int32 fread_retry ( void *  pointer,
int32  size,
int32  num_items,
FILE *  stream 
)

NFS file reads seem to fail now and then.

Use the following functions in place of the regular fread. It retries failed freads several times and quits only if all of them fail. Be aware, however, that even normal failures such as attempting to read beyond EOF will trigger such retries, wasting about a minute in retries. Arguments identical to regular fread.

Definition at line 408 of file pio.c.

References E_ERROR_SYSTEM, and fread_retry().

Referenced by fread_retry().

◆ lineiter_free()

SPHINXBASE_EXPORT void lineiter_free ( lineiter_t li)

Stop reading lines from a file.

Definition at line 368 of file pio.c.

References ckd_free(), and lineiter_free().

Referenced by lineiter_free().

◆ lineiter_lineno()

SPHINXBASE_EXPORT int lineiter_lineno ( lineiter_t li)

Returns current line number.

Definition at line 362 of file pio.c.

References lineiter_lineno().

Referenced by lineiter_lineno().

◆ lineiter_next()

SPHINXBASE_EXPORT lineiter_t * lineiter_next ( lineiter_t li)

Move to the next line in the file.

Definition at line 347 of file pio.c.

References lineiter_next(), STRING_BOTH, and string_trim().

Referenced by lineiter_next(), lineiter_start(), and lineiter_start_clean().

◆ lineiter_start()

SPHINXBASE_EXPORT lineiter_t * lineiter_start ( FILE *  fh)

Start reading lines from a file.

Definition at line 264 of file pio.c.

References ckd_calloc, ckd_malloc, lineiter_next(), and lineiter_start().

Referenced by lineiter_start(), and lineiter_start_clean().

◆ lineiter_start_clean()

SPHINXBASE_EXPORT lineiter_t * lineiter_start_clean ( FILE *  fh)

Start reading lines from a file, skip comments and trim lines.

Definition at line 288 of file pio.c.

References lineiter_next(), lineiter_start(), lineiter_start_clean(), STRING_BOTH, and string_trim().

Referenced by lineiter_start_clean().

◆ stat_mtime()

SPHINXBASE_EXPORT int32 stat_mtime ( const char *  file)

Return time of last modification for the given file, or -1 if stat fails.

Definition at line 519 of file pio.c.

References stat_retry().

◆ stat_retry()

SPHINXBASE_EXPORT int32 stat_retry ( const char *  file,
struct stat *  statbuf 
)

There is no bitstream decoder, because a stream abstraction is too slow.

Instead we read blocks of bits and treat them as bitvectors. Like fread_retry, but for stat. Arguments identical to regular stat. Return value: 0 if successful, -1 if stat failed several attempts.

Definition at line 489 of file pio.c.

References E_ERROR_SYSTEM.

Referenced by stat_mtime().