代码改变世界

(转:htk中命令行参数的读取)

2012-10-12 11:28  钱吉  阅读(839)  评论(0编辑  收藏  举报

由于不知道来源出处了,请原作者见谅,如有需要请告知源地址。这里只是贴出来mark一下,以后便于查看。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
static int argcount;    /* total args = argc */
static int nextarg=1;    /* next arg to return in GetxxxArg */
static char **arglist;
/* ----------- Command Line Argument Handling ------------ */
typedef enum {FALSE=0, TRUE=1} Boolean;
typedef enum {SWITCHARG, STRINGARG, INTARG, FLOATARG, NOARG} ArgKind;
void InitShell(int argc, char *argv[])
{
    int i;
    argcount = argc;
    arglist = (char **) malloc(argc*sizeof(char *));
    for (i=0;i<argc;i++){
        arglist[i] = argv[i];
    }
}
/* NumHead: returns TRUE if the first two chars of a string
            are ('+'|'-') digit | digit */
Boolean NumHead(char *s)
{
    if (*s!='/0')
        if (isdigit((int) *s))
            return TRUE;
    if (((*s=='+') || (*s=='-')) && (isdigit((int) *(s+1))))
            return TRUE;
    return FALSE;
}
int NumArgs(void)
{
    return argcount-nextarg;
}
char * GetNextArg(Boolean step)
{
    char *s;
    if (argcount>nextarg) {
        s = arglist[nextarg];    
    }
    if (step) {
        ++nextarg;
    }
    return s;
}
/* EXPORT->NextArg;Kind of next argument */
ArgKind NextArg(void)
{
    char *s,*p;
    if (NumArgs() == 0) return NOARG;
    s = GetNextArg(FALSE);
    if ( NumHead(s)) {
        strtod(s,&p);
        if ( p==NULL || *p=='/0' ) {
            if (strchr(s,'.') == NULL)
                return INTARG;
            else
                return FLOATARG; 
        }
    }
    if (*s=='-')
        return SWITCHARG;
    return STRINGARG;
}
char  *GetStrArg(void)
{
    char *s;
    if (NextArg() != STRINGARG ){
        printf("%s is not string/n",GetNextArg(FALSE));
        exit(1);
    }
    return GetNextArg(TRUE);
}
char  *GetSwtArg(void)
{
    if (NextArg() != SWITCHARG ){
        printf("%s is not switch/n",GetNextArg(FALSE));
        exit(1);
    }
    return GetNextArg(TRUE)+1;
}
int  GetIntArg(void)
{
    int i;
    char *s;
    if (NextArg() != INTARG ){
        printf("%s is not int/n",GetNextArg(FALSE));
        exit(1);
    }
    s = GetNextArg(TRUE);
    if (sscanf(s,"%i",&i) != 1){
        ;
    }
    return i;
}
float GetFltArg(void)
{
    int k = NextArg();
    if (k != INTARG && k != FLOATARG) {
        printf("%s is not float/n",GetNextArg(FALSE));
        exit(1);
    }
    return atof(GetNextArg(TRUE));
}
void ReportUsage(void)
{
    printf("/n USAGE: HERest [options] hmmList .../n/n");
    printf(" Option                              /n/n");
    printf(" -A      Command Line Printed        /n");
    printf(" -H mmf  LOAD HMM macro file mmf           ./MMF/n");
    printf(" -w f    set mix weight floor to f*MINMI    0.0/n");
    printf(" -m N    set min examples needed per model  3/n");
}
int main(int argc, char *argv[])
{
    if( argc == 1 ) {
        ReportUsage();
        exit(1);
    }
    InitShell(argc, argv);
    char *s;
    while (NextArg() == SWITCHARG) {
        s = GetSwtArg();
        if (strlen(s)!=1) {
            printf("switch must be single letter/n");
            exit(1);
        }
        switch(s[0]) {
        case 'A':
            printf("-A/n");
            break;
        case 'H':
            printf("-H/t%s/n",GetStrArg());
            break;
        case 'w':
            printf("-w/t%f/n",GetFltArg());
            break;
        case 'm':
            printf("-m/t%d/n",GetIntArg());
            break;
        default:
            printf("unkown switch %s/n",s);
            exit(1);
        }
    }
    if (NextArg() != STRINGARG) {
        printf("hmmlist expected/n");
        exit(1);    
    }
    printf("/t%s/n",GetStrArg());
    exit(0);
}

HTK里面实现的参数读取与此类似,但是加入了更多的安全检查机制,以及默认统一参数的解析。以后研究清楚了更新一下。