Skip to content

Commit

Permalink
Revert file checks to more portable versions
Browse files Browse the repository at this point in the history
git-svn-id: svn+ssh://svn.code.sf.net/p/cmusphinx/code/trunk/pocketsphinx@12126 94700074-3cef-4d97-a70e-9c8c206c02f5
  • Loading branch information
nshmyrev committed Dec 21, 2013
1 parent 4bd66c6 commit b8cba67
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions src/libpocketsphinx/pocketsphinx.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,27 @@ static const arg_t ps_args_def[] = {
CMDLN_EMPTY_OPTION
};

/* I'm not sure what the portable way to do this is. */
static int
dir_exists(const char *path)
file_exists(const char *path)
{
struct stat sb;
FILE *tmp;

E_SYSCALL(stat(path, &sb), "cannot stat `%s'", path);
return S_ISDIR(sb.st_mode);
tmp = fopen(path, "rb");
if (tmp) fclose(tmp);
return (tmp != NULL);
}

static int
hmmdir_exists(const char *path)
{
FILE *tmp;
char *mdef = string_join(path, "/mdef", NULL);

tmp = fopen(mdef, "rb");
if (tmp) fclose(tmp);
ckd_free(mdef);
return (tmp != NULL);
}

static void
Expand All @@ -81,7 +95,7 @@ ps_add_file(ps_decoder_t *ps, const char *arg,
{
char *tmp = string_join(hmmdir, "/", file, NULL);

if (cmd_ln_str_r(ps->config, arg) == NULL && !access(tmp, F_OK))
if (cmd_ln_str_r(ps->config, arg) == NULL && file_exists(tmp))
cmd_ln_set_str_r(ps->config, arg, tmp);
ckd_free(tmp);
}
Expand Down Expand Up @@ -146,44 +160,44 @@ ps_default_search_args(cmd_ln_t *config)
#ifdef MODELDIR
/* Set default acoustic and language models. */
const char *hmmdir = cmd_ln_str_r(config, "-hmm");
if (hmmdir == NULL && dir_exists(MODELDIR "/hmm/en_US/hub4wsj_sc_8k")) {
if (hmmdir == NULL && hmmdir_exists(MODELDIR "/hmm/en_US/hub4wsj_sc_8k")) {
hmmdir = MODELDIR "/hmm/en_US/hub4wsj_sc_8k";
cmd_ln_set_str_r(config, "-hmm", hmmdir);
}

const char *lmfile = cmd_ln_str_r(config, "-lm");
if (lmfile == NULL && !cmd_ln_str_r(config, "-fsg")
&& !cmd_ln_str_r(config, "-jsgf")
&& !access(MODELDIR "/lm/en_US/hub4.5000.DMP", F_OK))
&& file_exists(MODELDIR "/lm/en_US/hub4.5000.DMP"))
{
lmfile = MODELDIR "/lm/en_US/hub4.5000.DMP";
cmd_ln_set_str_r(config, "-lm", lmfile);
}

const char *dictfile = cmd_ln_str_r(config, "-dict");
if (dictfile == NULL && !access(MODELDIR "/lm/en_US/cmu07a.dic", F_OK)) {
if (dictfile == NULL && file_exists(MODELDIR "/lm/en_US/cmu07a.dic")) {
dictfile = MODELDIR "/lm/en_US/cmu07a.dic";
cmd_ln_set_str_r(config, "-dict", dictfile);
}

/* Expand acoustic and language model filenames relative to installation
* path. */
if (hmmdir && !path_is_absolute(hmmdir) && !dir_exists(hmmdir)) {
if (hmmdir && !path_is_absolute(hmmdir) && !hmmdir_exists(hmmdir)) {
char *tmphmm = string_join(MODELDIR "/hmm/", hmmdir, NULL);
if (dir_exists(tmphmm)) {
if (hmmdir_exists(tmphmm)) {
cmd_ln_set_str_r(config, "-hmm", tmphmm);
} else {
E_ERROR("Failed to find mdef file inside the model folder "
"specified with -hmm `%s'\n", hmmdir);
}
ckd_free(tmphmm);
}
if (lmfile && !path_is_absolute(lmfile) && access(lmfile, F_OK)) {
if (lmfile && !path_is_absolute(lmfile) && !file_exists(lmfile)) {
char *tmplm = string_join(MODELDIR "/lm/", lmfile, NULL);
cmd_ln_set_str_r(config, "-lm", tmplm);
ckd_free(tmplm);
}
if (dictfile && !path_is_absolute(dictfile) && access(dictfile, F_OK)) {
if (dictfile && !path_is_absolute(dictfile) && !file_exists(lmfile)) {
char *tmpdict = string_join(MODELDIR "/lm/", dictfile, NULL);
cmd_ln_set_str_r(config, "-dict", tmpdict);
ckd_free(tmpdict);
Expand Down

0 comments on commit b8cba67

Please sign in to comment.