51 #ifdef HAVE_SYS_TYPES_H 52 #include <sys/types.h> 55 #ifdef HAVE_SYS_STAT_H 59 #if defined(_WIN32) && !defined(CYGWIN) 81 guess_comptype(
char const *file, int32 *ispipe, int32 *isgz)
89 && ((strcmp(file + k - 2,
".Z") == 0)
90 || (strcmp(file + k - 2,
".z") == 0))) {
92 *isgz = COMP_COMPRESS;
94 else if ((k > 3) && ((strcmp(file + k - 3,
".gz") == 0)
95 || (strcmp(file + k - 3,
".GZ") == 0))) {
99 else if ((k > 4) && ((strcmp(file + k - 4,
".bz2") == 0)
100 || (strcmp(file + k - 4,
".BZ2") == 0))) {
107 fopen_comp(
const char *file,
const char *mode, int32 * ispipe)
115 guess_comptype(file, ispipe, &isgz);
121 E_FATAL(
"No popen() on WinCE\n");
123 if (strcmp(mode,
"r") == 0) {
127 command =
string_join(
"gunzip" EXEEXT,
" -c ", file, NULL);
130 command =
string_join(
"zcat" EXEEXT,
" ", file, NULL);
133 command =
string_join(
"bunzip2" EXEEXT,
" -c ", file, NULL);
137 E_FATAL(
"Unknown compression type %d\n", isgz);
139 if ((fp = popen(command, mode)) == NULL) {
140 E_ERROR_SYSTEM(
"Failed to open a pipe for a command '%s' mode '%s'", command, mode);
146 else if (strcmp(mode,
"w") == 0) {
150 command =
string_join(
"gzip" EXEEXT,
" > ", file, NULL);
153 command =
string_join(
"compress" EXEEXT,
" -c > ", file, NULL);
156 command =
string_join(
"bzip2" EXEEXT,
" > ", file, NULL);
160 E_FATAL(
"Unknown compression type %d\n", isgz);
162 if ((fp = popen(command, mode)) == NULL) {
163 E_ERROR_SYSTEM(
"Failed to open a pipe for a command '%s' mode '%s'", command, mode);
170 E_ERROR(
"Compressed file operation for mode %s is not supported\n", mode);
176 fp = fopen(file, mode);
188 #if defined(_WIN32) && (!defined(__SYMBIAN32__)) 212 if ((fh =
fopen_comp(file,
"r", ispipe)) != NULL)
219 guess_comptype(file, ispipe, &isgz);
222 strcpy(tmpfile, file);
225 tmpfile[k - 3] =
'\0';
228 tmpfile[k - 4] =
'\0';
231 tmpfile[k - 2] =
'\0';
234 strcpy(tmpfile + k,
".gz");
235 if ((fh =
fopen_comp(tmpfile,
"r", ispipe)) != NULL) {
236 E_WARN(
"Using %s instead of %s\n", tmpfile, file);
240 strcpy(tmpfile + k,
".bz2");
241 if ((fh =
fopen_comp(tmpfile,
"r", ispipe)) != NULL) {
242 E_WARN(
"Using %s instead of %s\n", tmpfile, file);
246 strcpy(tmpfile + k,
".Z");
247 if ((fh =
fopen_comp(tmpfile,
"r", ispipe)) != NULL) {
248 E_WARN(
"Using %s instead of %s\n", tmpfile, file);
255 E_WARN(
"Using %s instead of %s\n", tmpfile, file);
279 if (li && 0 == strncmp(li->buf,
"\xef\xbb\xbf", 3)) {
280 memmove(li->buf, li->buf + 3, strlen(li->buf + 1));
299 if (li->buf && li->buf[0] ==
'#') {
316 if (fgets(li->buf, li->bsiz, li->fh) == NULL) {
322 li->len = (int32)strlen(li->buf);
323 if (li->len < li->bsiz - 1 || li->buf[li->len - 1] ==
'\n')
331 if (fgets(li->buf + li->len, li->bsiz - li->len, li->fh) == NULL) {
332 li->len += strlen(li->buf + li->len);
335 li->len += strlen(li->buf + li->len);
337 if (li->len < li->bsiz - 1 || li->buf[li->len - 1] ==
'\n')
350 return lineiter_next_plain(li);
352 for (li = lineiter_next_plain(li); li; li = lineiter_next_plain(li)) {
355 if (li->buf[0] != 0 && li->buf[0] !=
'#')
379 char *output, *outptr;
382 output = outptr = NULL;
383 while (fgets(buf,
sizeof(buf), stream)) {
384 size_t len = strlen(buf);
386 if (output == NULL) {
391 size_t cur = outptr - output;
392 output = (
char *)
ckd_realloc(output, cur + len + 1);
393 outptr = output + cur;
395 memcpy(outptr, buf, len + 1);
398 if (len <
sizeof(buf)-1 || buf[len-1] ==
'\n')
401 if (out_len) *out_len = outptr - output;
405 #define FREAD_RETRY_COUNT 60 408 fread_retry(
void *pointer, int32 size, int32 num_items, FILE * stream)
416 n_retry_rem = FREAD_RETRY_COUNT;
418 data = (
char *)pointer;
420 n_items_rem = num_items;
423 n_items_read = fread(&data[loc], size, n_items_rem, stream);
425 n_items_rem -= n_items_read;
427 if (n_items_rem > 0) {
430 if (n_retry_rem == 0)
433 if (n_retry_rem == FREAD_RETRY_COUNT) {
439 loc += n_items_read * size;
440 #if !defined(_WIN32) && defined(HAVE_UNISTD_H) 444 }
while (n_items_rem > 0);
452 stat_retry(
const char *file,
struct stat * statbuf)
454 WIN32_FIND_DATAW file_data;
459 len = mbstowcs(NULL, file, 0) + 1;
461 mbstowcs(wfile, file, len);
462 if ((h = FindFirstFileW(wfile, &file_data)) == INVALID_HANDLE_VALUE) {
467 memset(statbuf, 0,
sizeof(*statbuf));
468 statbuf->st_mtime = file_data.ftLastWriteTime.dwLowDateTime;
469 statbuf->st_size = file_data.nFileSizeLow;
484 return ((int32) statbuf.st_mtime);
487 #define STAT_RETRY_COUNT 10 493 for (i = 0; i < STAT_RETRY_COUNT; i++) {
494 #ifndef HAVE_SYS_STAT_H 497 if ((fp = (FILE *)fopen(file,
"r")) != 0) {
498 fseek(fp, 0, SEEK_END);
499 statbuf->st_size = ftell(fp);
504 if (stat(file, statbuf) == 0)
523 #ifdef HAVE_SYS_STAT_H 524 if (stat(file, &statbuf) != 0)
531 return ((int32) statbuf.st_mtime);
537 unsigned char buf, bbits;
564 if (--be->refcount > 0)
576 tbits = nbits + be->bbits;
579 be->buf |= ((bits[0] >> (8 - nbits)) << (8 - tbits));
585 fputc(be->buf | ((bits[i]) >> be->bbits), be->fh);
587 be->buf = (bits[i] << (8 - be->bbits)) & 0xff;
601 unsigned char bits[4];
602 codeword <<= (32 - nbits);
603 bits[0] = (codeword >> 24) & 0xff;
604 bits[1] = (codeword >> 16) & 0xff;
605 bits[2] = (codeword >> 8) & 0xff;
606 bits[3] = codeword & 0xff;
614 fputc(be->buf, be->fh);
626 if (strlen(path) == 0)
629 #if defined(_WIN32) && !defined(CYGWIN) 630 else if ((rv = _mkdir(path)) == 0)
632 #elif defined(HAVE_SYS_STAT_H) 633 else if ((rv = mkdir(path, 0777)) == 0)
638 else if (errno == EEXIST)
640 else if (errno != ENOENT) {
650 #if defined(_WIN32) && !defined(CYGWIN) 652 #elif defined(HAVE_SYS_STAT_H) 653 return mkdir(path, 0777);
#define E_ERROR_SYSTEM(...)
Print error text; Call perror("");.
Miscellaneous useful string functions.
int bit_encode_write(bit_encode_t *be, unsigned char const *bits, int nbits)
Write bits to encoder.
bit_encode_t * bit_encode_attach(FILE *outfh)
Attach bitstream encoder to a file.
SPHINXBASE_EXPORT int lineiter_lineno(lineiter_t *li)
Returns current line number.
#define ckd_calloc(n, sz)
Macros to simplify the use of above functions.
#define E_ERROR(...)
Print error message to error log.
Sphinx's memory allocation/deallocation routines.
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 uncompres...
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 lineiter_t * lineiter_start_clean(FILE *fh)
Start reading lines from a file, skip comments and trim lines.
File names related operation.
#define ckd_salloc(ptr)
Macro for ckd_salloc
SPHINXBASE_EXPORT void ckd_free(void *ptr)
Test and free a 1-D array.
bit_encode_t * bit_encode_retain(bit_encode_t *be)
Retain pointer to a bit encoder.
SPHINXBASE_EXPORT int build_directory(const char *path)
Create a directory and all of its parent directories, as needed.
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 void lineiter_free(lineiter_t *li)
Stop reading lines from a file.
SPHINXBASE_EXPORT lineiter_t * lineiter_next(lineiter_t *li)
Move to the next line in the file.
SPHINXBASE_EXPORT lineiter_t * lineiter_start(FILE *fh)
Start reading lines from a file.
int bit_encode_write_cw(bit_encode_t *be, uint32 codeword, int nbits)
Write lowest-order bits of codeword to encoder.
Implementation of logging routines.
int bit_encode_free(bit_encode_t *be)
Release pointer to a bit encoder.
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).
#define E_WARN(...)
Print warning message to error log.
SPHINXBASE_EXPORT int32 fread_retry(void *pointer, int32 size, int32 num_items, FILE *stream)
NFS file reads seem to fail now and then.
#define ckd_malloc(sz)
Macro for ckd_malloc
SPHINXBASE_EXPORT char * string_join(const char *base,...)
Concatenate a NULL-terminated argument list of strings, returning a newly allocated string...
int bit_encode_flush(bit_encode_t *be)
Flush any unwritten bits, zero-padding if necessary.
#define E_FATAL(...)
Exit with non-zero status after error message.
SPHINXBASE_EXPORT char * string_trim(char *string, enum string_edge_e which)
Remove whitespace from a string, modifying it in-place.
SPHINXBASE_EXPORT int32 stat_mtime(const char *file)
Return time of last modification for the given file, or -1 if stat fails.
SPHINXBASE_EXPORT void fclose_comp(FILE *fp, int32 ispipe)
Close a file opened using fopen_comp.
SPHINXBASE_EXPORT void path2dirname(const char *path, char *dir)
Strip off filename from the given path and copy the directory name into dir Caller must have allocate...
#define ckd_realloc(ptr, sz)
Macro for ckd_realloc
file IO related operations.