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). More...
SPHINXBASE_EXPORT void | fclose_comp (FILE *fp, int32 ispipe)
| Close a file opened using fopen_comp. More...
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). More...
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. More...
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. More...
SPHINXBASE_EXPORT lineiter_t * | lineiter_start (FILE *fh)
| Start reading lines from a file.
SPHINXBASE_EXPORT lineiter_t * | lineiter_start_clean (FILE *fh)
| Start reading lines from a file, skip comments and trim lines.
SPHINXBASE_EXPORT lineiter_t * | lineiter_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_t * | bit_encode_attach (FILE *outfh)
| Attach bitstream encoder to a file.
bit_encode_t * | bit_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. More...
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. More...
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. More...

## 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.

## Function Documentation

## 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 556 of file pio.c.

Referenced by huff_code_detach().

## 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 653 of file pio.c.

References E_ERROR.

Referenced by bit_encode_flush().

## fclose_comp()

SPHINXBASE_EXPORT void fclose_comp | ( | FILE * | fp,
|  | int32 | ispipe
| ) |  |

Close a file opened using fopen_comp.

Parameters

  fp | In: File pointer to be closed
  ispipe | In: ispipe argument that was returned by the corresponding fopen_comp() call

Definition at line 175 of file pio.c.

## 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

  file | In: File to be opened
  mode | In: "r" or "w", as with normal fopen
  ispipe | Out: On return *ispipe is TRUE iff file was opened via a pipe

Definition at line 98 of file pio.c.

References E_FATAL, and string_join().

Referenced by 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

  file | In: File to be opened
  ispipe | Out: On return *ispipe is TRUE iff file was opened via a pipe

Definition at line 192 of file pio.c.

References fopen_comp().

## 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

  stream | The file handle to read from.
  out_len | Output: if not NULL, length of the string read.

Returns
  allocated string containing the line, or NULL on error or EOF.

Definition at line 367 of file pio.c.

## 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 398 of file pio.c.

References E_ERROR_SYSTEM, and 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 480 of file pio.c.

References E_ERROR_SYSTEM.

Referenced by fread_retry(), and stat_mtime().