libjulius/src/useropt.c

Go to the documentation of this file.
00001 
00018 /*
00019  * Copyright (c) 1991-2007 Kawahara Lab., Kyoto University
00020  * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology
00021  * Copyright (c) 2005-2007 Julius project team, Nagoya Institute of Technology
00022  * All rights reserved
00023  */
00024 
00025 #include <julius/juliuslib.h>
00026 
00031 static USEROPT *useropt_root = NULL;
00032 
00044 static USEROPT *
00045 useropt_new()
00046 {
00047   USEROPT *new;
00048 
00049   new = (USEROPT *)mymalloc(sizeof(USEROPT));
00050   new->optstr = NULL;
00051   new->desc = NULL;
00052   new->argnum = 0;
00053   new->reqargnum = 0;
00054   new->next = NULL;
00055 
00056   return new;
00057 }
00058 
00070 static void
00071 useropt_free(USEROPT *x)
00072 {
00073   if (x->optstr) free(x->optstr);
00074   if (x->desc) free(x->desc);
00075   free(x);
00076 }
00077 
00089 void
00090 useropt_free_all()
00091 {
00092   USEROPT *x, *tmp;
00093 
00094   x = useropt_root;
00095   while(x) {
00096     tmp = x->next;
00097     useropt_free(x);
00098     x = tmp;
00099   }
00100   useropt_root = NULL;
00101 }
00102   
00128 boolean
00129 j_add_option(char *fmt, int argnum, int reqargnum, char *desc, boolean (*func)(Jconf *jconf, char *arg[], int argnum))
00130 {
00131   USEROPT *new;
00132 
00133   if (fmt[0] != '-') {
00134     jlog("ERROR: j_add_option: option string must start with \'-\': %s\n", fmt);
00135     return FALSE;
00136   }
00137   if (argnum < reqargnum) {     /* error */
00138     jlog("ERROR: j_add_option: number of required argument (%d) larger than total (%d)\n", reqargnum, argnum);
00139     return FALSE;
00140   }
00141 
00142   /* allocate new */
00143   new = useropt_new();
00144   /* set option string */
00145   new->optstr = strcpy((char *)mymalloc(strlen(fmt)+1), fmt);
00146   /* set number of arguments */
00147   new->argnum = argnum;
00148   /* set number of required arguments.
00149      If less than argnum, the latter options should be optional */
00150   new->reqargnum = reqargnum;
00151   /* set description string */
00152   new->desc = strcpy((char*)mymalloc(strlen(desc)+1),desc);
00153 
00154   /* set user-given function to process this option */
00155   new->func = func;
00156 
00157   /* add to list */
00158   new->next = useropt_root;
00159   useropt_root = new;
00160 
00161   return TRUE;
00162 }
00163 
00164 
00186 int
00187 useropt_exec(Jconf *jconf, char *argv[], int argc, int *n)
00188 {
00189   USEROPT *x;
00190   int narg, i;
00191 
00192   for(x=useropt_root;x;x=x->next) {
00193     if (strmatch(argv[*n], x->optstr)) {
00194       i = *n + 1;
00195       while(i < argc && (argv[i][0] != '-' || (argv[i][1] >= '0' && argv[i][1] <= '9'))) i++;
00196       
00197       narg = i - *n - 1;
00198       if (narg > x->argnum || narg < x->reqargnum) {
00199         if (x->reqargnum != x->argnum) {
00200           jlog("ERROR: useropt_exec: \"%s\" should have at least %d argument(s)\n", x->optstr, x->reqargnum);
00201         } else {
00202           jlog("ERROR: useropt_exec: \"%s\" should have %d argument(s)\n", x->optstr, x->argnum);
00203         }
00204         return -1;              /* error */
00205       }
00206 
00207       if ((*(x->func))(jconf, &(argv[(*n)+1]), narg) == FALSE) {
00208         jlog("ERROR: useropt_exec: \"%s\" function returns FALSE\n", x->optstr);
00209         return -1;              /* error */
00210       }
00211       *n += narg;
00212       return 1;                 /* processed */
00213     }
00214   }
00215 
00216   return 0;                     /* nothing processed */
00217 }
00218 
00232 void
00233 useropt_show_desc(FILE *fp)
00234 {
00235   USEROPT *x;
00236   int i;
00237 
00238   if (! useropt_root) return;
00239   fprintf(fp, "\n Additional options for application:\n");
00240   for(x=useropt_root;x;x=x->next) {
00241     fprintf(fp, "    [%s", x->optstr);
00242     for(i=0;i<x->reqargnum;i++) fprintf(fp, " arg");
00243     for(i=x->reqargnum;i<x->argnum;i++) fprintf(fp, " (arg)");
00244     fprintf(fp, "]\t%s\n", x->desc);
00245   }
00246 }
00247 
00248 /* end of file */

Generated on Tue Dec 18 15:59:53 2007 for Julius by  doxygen 1.5.4