julius/m_jconf.c

説明を見る。
00001 
00017 /*
00018  * Copyright (c) 1991-2006 Kawahara Lab., Kyoto University
00019  * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology
00020  * Copyright (c) 2005-2006 Julius project team, Nagoya Institute of Technology
00021  * All rights reserved
00022  */
00023 
00024 #include <julius.h>
00025 
00026 #if defined(_WIN32) && !defined(__CYGWIN32__)
00027 #include <mbstring.h>
00028 #endif
00029 
00030 #define ISTOKEN(A) (A == ' ' || A == '\t' || A == '\n') 
00031 
00032 
00059 /* added by H.Banno for Windows & Mac */
00060 static char *
00061 fgets_jconf(char *buf, int size, FILE *fp)
00062 {
00063   int c, prev_c;
00064   int pos;
00065 
00066   if (fp == NULL) return NULL;
00067     
00068   pos = 0;
00069   c = '\0';
00070   prev_c = '\0';
00071   while (1) {
00072     if (pos >= size) {
00073       pos--;
00074       break;
00075     }
00076 
00077     c = fgetc(fp);
00078     if (c == EOF) {
00079       buf[pos] = '\0';
00080       if (pos <= 0) {
00081         return NULL;
00082       } else {
00083         return buf;
00084       }
00085     } else if (c == '\n' || c == '\r') {
00086       if (c == '\r' && (c = fgetc(fp)) != '\n') { /* for Mac */
00087         ungetc(c, fp);
00088       }
00089       if (prev_c == '\\') {
00090         pos--;
00091       } else {
00092         break;
00093       }
00094     } else {
00095       buf[pos] = c;
00096       pos++;
00097 
00098 #if defined(_WIN32) && !defined(__CYGWIN32__)
00099       if (c == '\\' && (_ismbblead(prev_c) && _ismbbtrail(c))) {
00100       c = '\0';
00101       }
00102 #endif
00103     }
00104   }
00105   buf[pos] = '\0';
00106 
00107   return buf;
00108 }
00109 
00126 void
00127 get_dirname(char *path)
00128 {
00129   char *p;
00130   /* /path/file -> /path/ */
00131   /* path/file  -> path/  */
00132   /* /file      -> / */
00133   /* file       ->  */
00134   /* ../file    -> ../ */
00135   p = path + strlen(path) - 1;
00136   while (*p != '/'
00137 #if defined(_WIN32) && !defined(__CYGWIN32__)
00138          && *p != '\\'
00139 #endif
00140          && p != path) p--;
00141   if (p == path && *p != '/') *p = '\0';
00142   else *(p+1) = '\0';
00143 }
00144 
00145 /* read-in and parse jconf file and process those using m_options */
00158 void
00159 config_file_parse(char *conffile)
00160 {
00161   int c_argc;
00162   char **c_argv;
00163   FILE *fp;
00164   int maxnum, step;
00165   static const int len = 512;
00166   char buf[len], cpy[len];
00167   char *p, *dst, *dst_from;
00168   char *cdir;
00169 
00170   j_printerr("include config: %s\n", conffile);
00171   
00172   /* set the content of jconf file into argument list c_argv[1..c_argc-1] */
00173   /* c_argv[0] will be the original conffile name */
00174   /* inside jconf file, quoting by ", ' and escape by '\' is supported */
00175   if ((fp = fopen(conffile, "r")) == NULL) {
00176     j_error("%s: failed to open jconf file: %s\n",EXECNAME, conffile);
00177   }
00178   step = 20;
00179   maxnum = step;
00180   c_argv = (char **)mymalloc(sizeof(char *) * maxnum);
00181   c_argv[0] = strcpy((char *)mymalloc(strlen(conffile)+1), conffile);
00182   c_argc = 1;
00183   while (fgets_jconf(buf, len, fp) != NULL) {
00184     if (buf[0] == '\0') continue;
00185     p = buf; dst = cpy;
00186     while (1) {
00187       while (*p != '\0' && ISTOKEN(*p)) p++;
00188       if (*p == '\0') break;
00189       
00190       dst_from = dst;
00191       
00192       while (*p != '\0' && (!ISTOKEN(*p))) {
00193         if (0 &&/* '\' is removed by fgets_jconf */ *p == '\\') {     /* escape by '\' */
00194           if (*(++p) == '\0') break;
00195           *(dst++) = *(p++);
00196         } else {
00197           if (*p == '"') { /* quote by "" */
00198             p++;
00199             while (*p != '\0' && *p != '"') *(dst++) = *(p++);
00200             if (*p == '\0') break;
00201             p++;
00202           } else if (*p == '\'') { /* quote by '' */
00203             p++;
00204             while (*p != '\0' && *p != '\'') *(dst++) = *(p++);
00205             if (*p == '\0') break;
00206             p++;
00207           } else if (*p == '#') { /* comment out by '#' */
00208             *p = '\0';
00209             break;
00210           } else {              /* other */
00211             *(dst++) = *(p++);
00212           }
00213         }
00214       }
00215       if (dst != dst_from) {
00216         *dst = '\0'; dst++;
00217         if (c_argc >= maxnum) {
00218           maxnum += step;
00219           c_argv = (char **)myrealloc(c_argv, sizeof(char *) * maxnum);
00220         }
00221         c_argv[c_argc++] = strcpy((char*)mymalloc(strlen(dst_from)+1), dst_from);
00222       }
00223     }
00224   }
00225   if (fclose(fp) == -1) {
00226     j_error("%s: jconf file cannot close\n", EXECNAME);
00227   }
00228 
00229   if (debug2_flag) {            /* for debug */
00230     int i;
00231     j_printf("\t");
00232     for (i=1;i<c_argc;i++) j_printf(" %s",c_argv[i]);
00233     j_printf("\n");
00234   }
00235 
00236   /* now that options are in c_argv[][], call opt_parse() to process them */
00237   /* relative paths in jconf file are relative to the jconf file (not current) */
00238   cdir = strcpy((char *)mymalloc(strlen(conffile)+1), conffile);
00239   get_dirname(cdir);
00240   opt_parse(c_argc, c_argv, (cdir[0] == '\0') ? NULL : cdir);
00241   free(cdir);
00242 
00243   /* free arguments */
00244   while (c_argc-- > 0) {
00245     free(c_argv[c_argc]);
00246   }
00247   free(c_argv);
00248 }
00249 

Juliusに対してTue Dec 26 16:19:27 2006に生成されました。  doxygen 1.5.0