📄 m_options.c
字号:
/** * @file m_options.c * * <JA> * @brief オプション借妄 * * ここにある簇眶は·jconfファイルおよびコマンドラインからのオプション回年を * 界に粕み哈み·猛を呈羌する. * </JA> * * <EN> * @brief Option parsing. * * These functions read option strings from jconf file or command line * and set values to the configuration structure. * </EN> * * @author Akinobu Lee * @date Thu May 12 18:52:07 2005 * * $Revision: 1.17 $ * *//* * Copyright (c) 1991-2007 Kawahara Lab., Kyoto University * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology * Copyright (c) 2005-2007 Julius project team, Nagoya Institute of Technology * All rights reserved */#include <julius/julius.h>/** * <JA> * @brief 陵滦パスをフルパスに恃垂する. * * ファイルのパス叹が陵滦パスであれば·カレントディレクトリをつけた * フルパスに恃垂して手す. 冷滦パスであれば·そのまま手す. * * @param filename [in] ファイルのパス叹 * @param dirname [in] カレントディレクトリのパス叹 * * @return 冷滦パス叹の掐った·糠たに充り烧けられたバッファ * </JA> * <EN> * @brief Change relative path to full path. * * If the file path is given as relative, prepend the dirname to it. * If the file path is full, just copy it to new buffer and return. * * @param filename [in] file path name * @param dirname [in] full path of current directory * * @return newly malloced buffer holding the full path name. * </EN> */char *filepath(char *filename, char *dirname){ char *p; if (dirname != NULL && filename[0] != '/'#if defined(_WIN32) && filename[0] != '\\' && !(strlen(filename) >= 3 && filename[1] == ':')#endif ) { p = (char *)mymalloc(strlen(filename) + strlen(dirname) + 1); strcpy(p, dirname); strcat(p, filename); } else { p = strcpy((char *)mymalloc(strlen(filename)+1), filename); } return p;}/** * <EN> * Returns next argument string. * </EN> * <JA> * 肌の苞眶の矢机误を手す. * </JA> * * @param cur [i/o] pointer to current point of the argment array * @param argc [in] total number of argments * @param argv [in] argment array * * @return pointer to the next argument, or NULL if no more argument vailable. * */static char *next_arg(int *cur, int argc, char *argv[]){ (*cur)++; if (*cur >= argc) { jlog("ERROR: m_options: option requires argument -- %s\n", argv[*cur-1]); return NULL; } return(argv[*cur]);}static booleancheck_section(Jconf *jconf, char *optname, short sec){ if (! jconf->optsectioning) return TRUE; if (jconf->optsection == sec) return TRUE; if (jconf->optsection == JCONF_OPT_DEFAULT) return TRUE; switch(sec) { case JCONF_OPT_GLOBAL: jlog("ERROR: \"%s\" is global option (should be before any instance declaration)", optname); break; case JCONF_OPT_AM: jlog("ERROR: \"%s\" is AM option", optname); break; case JCONF_OPT_LM: jlog("ERROR: \"%s\" is LM option", optname); break; case JCONF_OPT_SR: jlog("ERROR: \"%s\" is SR (search) option", optname); break; } switch(jconf->optsection) { case JCONF_OPT_GLOBAL: jlog(", but exists at global section (-GLOBAL)\n"); break; case JCONF_OPT_AM: jlog(", but exists at AM section (-AM \"%s\")\n", jconf->amnow->name); break; case JCONF_OPT_LM: jlog(", but exists at LM section (-LM \"%s\")\n", jconf->lmnow->name); break; case JCONF_OPT_SR: jlog(", but exists at recognizer section (-SR \"%s\")\n", jconf->searchnow->name); break; } jlog("ERROR: fix it, or you can disable this check by \"-nosectioncheck\"\n"); return FALSE;}/** * <JA> * メモリ挝拌を豺庶し NULL で虽める. * @param p [i/o] メモリ挝拌の黎片を回すポインタ恃眶へのポインタ * @note @a p が NULL の眷圭は部も弹こらない。 * </JA> * <EN> * Free memory and fill it with NULL. * @param p [i/o] pointer to pointer that holds allocated address * @note Nothing will happen if @a p equals to NULL. * </EN> */#define FREE_MEMORY(p) \ {if (p) {free(p); p = NULL;}}/** * <JA> * オプション豺老. * * @param argc [in] @a argv に崔まれる苞眶の眶 * @param argv [in] 苞眶猛∈矢机误∷の芹误 * @param cwd [in] カレントディレクトリ * @param jconf [out] 猛を呈羌するjconf菇陇挛 * * </JA> * <EN> * Option parsing. * * @param argc [in] number of elements in @a argv * @param argv [in] array of argument strings * @param cwd [in] current directory * @param jconf [out] jconf structure to store data * * </EN> * @return TRUE on success, or FALSE on error. * * @callgraph * @callergraph */booleanopt_parse(int argc, char *argv[], char *cwd, Jconf *jconf){ char *tmparg; int i; boolean unknown_opt; JCONF_AM *amconf, *atmp; JCONF_LM *lmconf, *ltmp; JCONF_SEARCH *sconf; char sname[JCONF_MODULENAME_MAXLEN];#ifdef ENABLE_PLUGIN int sid; FUNC_INT func;#endif#define GET_TMPARG if ((tmparg = next_arg(&i, argc, argv)) == NULL) return FALSE for (i=1;i<argc;i++) { unknown_opt = FALSE; if (strmatch(argv[i],"-C")) { /* include jconf file */ GET_TMPARG; tmparg = filepath(tmparg, cwd); if (config_file_parse(tmparg, jconf) == FALSE) { return FALSE; } free(tmparg); continue; } else if (strmatch(argv[i],"-AM") || strmatch(argv[i], "[AM]")) { GET_TMPARG; if (tmparg[0] == '-') { jlog("ERROR: m_options: -AM needs an argument as module name\n"); return FALSE; } if (tmparg[0] >= '0' && tmparg[0] <= '9') { jlog("ERROR: m_options: AM name \"%s\" not acceptable: first character should not be a digit\n", tmparg); return FALSE; } /* if not first time, create new module instance and switch to it */ /* and switch current to this */ amconf = j_jconf_am_new(); if (j_jconf_am_regist(jconf, amconf, tmparg) == FALSE) { jlog("ERROR: failed to add new amconf as \"%s\"\n", tmparg); jlog("ERROR: m_options: failed to create amconf\n"); j_jconf_am_free(amconf); return FALSE; } jconf->amnow = amconf; jconf->optsection = JCONF_OPT_AM; continue; } else if (strmatch(argv[i],"-AM_GMM") || strmatch(argv[i], "[AM_GMM]")) { /* switch current to GMM */ if (jconf->gmm == NULL) { /* if new, allocate jconf for GMM */ jconf->gmm = j_jconf_am_new(); } jconf->amnow = jconf->gmm; jconf->optsection = JCONF_OPT_AM; continue; } else if (strmatch(argv[i],"-LM") || strmatch(argv[i], "[LM]")) { GET_TMPARG; if (tmparg[0] == '-') { jlog("ERROR: m_options: -LM needs an argument as module name\n"); return FALSE; } if (tmparg[0] >= '0' && tmparg[0] <= '9') { jlog("ERROR: m_options: LM name \"%s\" not acceptable: first character should not be a digit\n", tmparg); return FALSE; } /* create new module instance and switch to it */ /* and switch current to this */ lmconf = j_jconf_lm_new(); if (j_jconf_lm_regist(jconf, lmconf, tmparg) == FALSE) { jlog("ERROR: failed to add new lmconf as \"%s\"\n", tmparg); jlog("ERROR: m_options: failed to create lmconf\n"); j_jconf_lm_free(lmconf); return FALSE; } jconf->lmnow = lmconf; jconf->optsection = JCONF_OPT_LM; continue; } else if (strmatch(argv[i],"-SR") || strmatch(argv[i], "[SR]")) { GET_TMPARG; if (tmparg[0] == '-') { jlog("ERROR: m_options: -SR needs three arguments: module name, AM name and LM name\n"); return FALSE; } if (tmparg[0] >= '0' && tmparg[0] <= '9') { jlog("ERROR: m_options: SR name \"%s\" not acceptable: first character should not be a digit\n", tmparg); return FALSE; } /* store name temporarly */ strncpy(sname, tmparg, JCONF_MODULENAME_MAXLEN); /* get link to jconf_am and jconf_lm */ GET_TMPARG; if (tmparg[0] == '-') { jlog("ERROR: m_options: -SR needs three arguments: module name, AM name and LM name\n"); return FALSE; } if (tmparg[0] >= '0' && tmparg[0] <= '9') { /* arg is number */ if ((amconf = j_get_amconf_by_id(jconf, atoi(tmparg))) == NULL) return FALSE; } else { /* name string */ if ((amconf = j_get_amconf_by_name(jconf, tmparg)) == NULL) return FALSE; } GET_TMPARG; if (tmparg[0] == '-') { jlog("ERROR: m_options: -SR needs three arguments: module name, AM name and LM name\n"); return FALSE; } if (tmparg[0] >= '0' && tmparg[0] <= '9') { /* arg is number */ if ((lmconf = j_get_lmconf_by_id(jconf, atoi(tmparg))) == NULL) return FALSE; } else { /* name string */ if ((lmconf = j_get_lmconf_by_name(jconf, tmparg)) == NULL) return FALSE; } /* check to avoid assigning an LM for multiple SR */ for(sconf=jconf->search_root;sconf;sconf=sconf->next) { if (sconf->lmconf == lmconf) { jlog("ERROR: you are going to share LM \"%s\" among multiple SRs\n"); jlog("ERROR: current Julius cannot share LM among SRs\n"); jlog("ERROR: you should define LM for each SR\n"); return FALSE; } } /* if not first time, create new module instance and switch to it */ sconf = j_jconf_search_new(); sconf->amconf = amconf; sconf->lmconf = lmconf; if (j_jconf_search_regist(jconf, sconf, sname) == FALSE) { jlog("ERROR: failed to add new amconf as \"%s\"\n", sname); jlog("ERROR: m_options: failed to create search conf\n"); j_jconf_search_free(sconf); return FALSE; } jconf->searchnow = sconf; jconf->optsection = JCONF_OPT_SR; continue; } else if (strmatch(argv[i],"-GLOBAL")) { jconf->optsection = JCONF_OPT_GLOBAL; continue; } else if (strmatch(argv[i],"-sectioncheck")) { /* enable section check */ jconf->optsectioning = TRUE; continue; } else if (strmatch(argv[i],"-nosectioncheck")) { /* disable section check */ jconf->optsectioning = FALSE; continue; } else if (strmatch(argv[i],"-input")) { /* speech input */ if (!check_section(jconf, argv[i], JCONF_OPT_GLOBAL)) return FALSE; GET_TMPARG; jconf->input.plugin_source = -1; if (strmatch(tmparg,"file") || strmatch(tmparg,"rawfile")) { jconf->input.type = INPUT_WAVEFORM; jconf->input.speech_input = SP_RAWFILE; jconf->decodeopt.realtime_flag = FALSE; } else if (strmatch(tmparg,"htkparam") || strmatch(tmparg,"mfcfile") || strmatch(tmparg,"mfc")) { jconf->input.type = INPUT_VECTOR; jconf->input.speech_input = SP_MFCFILE; jconf->decodeopt.realtime_flag = FALSE; } else if (strmatch(tmparg,"stdin")) { jconf->input.type = INPUT_WAVEFORM; jconf->input.speech_input = SP_STDIN; jconf->decodeopt.realtime_flag = FALSE; } else if (strmatch(tmparg,"adinnet")) { jconf->input.type = INPUT_WAVEFORM; jconf->input.speech_input = SP_ADINNET; jconf->decodeopt.realtime_flag = TRUE;#ifdef USE_NETAUDIO } else if (strmatch(tmparg,"netaudio")) { jconf->input.type = INPUT_WAVEFORM; jconf->input.speech_input = SP_NETAUDIO; jconf->decodeopt.realtime_flag = TRUE;#endif#ifdef USE_MIC } else if (strmatch(tmparg,"mic")) { jconf->input.type = INPUT_WAVEFORM; jconf->input.speech_input = SP_MIC; jconf->input.device = SP_INPUT_DEFAULT; jconf->decodeopt.realtime_flag = TRUE; } else if (strmatch(tmparg,"alsa")) {#ifdef HAS_ALSA jconf->input.type = INPUT_WAVEFORM; jconf->input.speech_input = SP_MIC; jconf->input.device = SP_INPUT_ALSA; jconf->decodeopt.realtime_flag = TRUE;#else jlog("ERROR: m_options: \"-input alsa\": ALSA support is not built-in\n"); return FALSE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -