Back to index

im-sdk  12.3.91
config-reader.c
Go to the documentation of this file.
00001 /*
00002  * Copyright 2003 Sun Microsystems Inc.
00003  *
00004  * This is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library; if not, write to the
00016  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017  * Boston, MA 02111-1307, USA.
00018  *
00019  * Authors: Karl Park <karl.park@sun.com>
00020  */
00021 
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <alloca.h> /* for alloca() */
00025 #include <string.h>
00026 #include <assert.h>
00027 #include <ctype.h>
00028 #include <le-options.h>
00029 #include "configapi.h"
00030 #include "util.h"
00031 
00032 static Bool parse_line (const char *line, KOLE_config **option);
00033 
00034 Bool
00035 get_configuration (const char *fname, int *ret_noptions, KOLE_config ***options)
00036 {
00037   FILE *fp;
00038   char *line;
00039   KOLE_config *pconf;
00040   
00041   /* read file */
00042   assert (fname != NULL);
00043   if (!fname){
00044     /* TO DO: log error */
00045     return False;
00046   }
00047 
00048   assert (ret_noptions != NULL);
00049   if (!ret_noptions){
00050     /* TO DO: log error */
00051     return False;
00052   }
00053   
00054   fp = fopen (fname, "r");
00055 
00056   if (!fp)
00057     return False;
00058   *ret_noptions = 0;
00059   *options = NULL;
00060   while ((line = (char *)safe_getline (fp))){
00061     if (parse_line (line, &pconf)){
00062       *options =
00063        (KOLE_config **) realloc (*options, sizeof (KOLE_config *) * *ret_noptions);
00064       (*options)[*ret_noptions] = pconf;
00065       (*ret_noptions)++;
00066     } else
00067       continue;
00068   }
00069   return True;
00070 }
00071 
00072 
00073 static Bool
00074 parse_line (const char *line, KOLE_config **option)
00075 {
00076   char *buffer;
00077   char *p, *delim;
00078   char *name, *value;
00079   
00080   assert (line != NULL);
00081   if (!line)
00082     return False;
00083 
00084   assert (option != NULL);
00085   if (!option)
00086     return False;
00087   
00088   buffer = alloca (strlen (line) + 1);
00089   strncpy (buffer, line, strlen (line) + 1);
00090   p = buffer;
00091 
00092   /* ':' is delimeter */
00093   delim = strchr (buffer, ':');
00094   if (!delim) /* invalid format */
00095     return False;
00096   
00097 
00098   while (*p && isspace (*p)) p++;
00099   /* start of name */
00100 
00101   if (!*p || *p == '#')
00102     return False;
00103   else 
00104     name = p;
00105 
00106 #if 0  
00107   while (*p && p < delim && !isspace (*p)) p++;
00108   
00109   
00110   /* end of name */
00111   if (!*p)
00112     return False;
00113   else if (*p == ':' || isspace (*p))
00114     /* even if p == delim, it doesn't matter */
00115     *p = '\0';
00116   else
00117     return False;
00118   /* done with name, now start getting value */
00119 #endif
00120 
00121   p = delim - 1;
00122   while ( p > name && isspace (*p)) p--;
00123   
00124   if (p == name) /* error */
00125     return False;
00126   *(p + 1) = '\0';
00127   /* done with name, now start getting value */
00128   
00129   p = delim + 1;
00130 
00131   while (*p && isspace (*p)) p++;
00132   /* start of value */
00133   if (!*p) /* invalid format(no value) */
00134     return False;
00135   else
00136     value = p;
00137 
00138   while (*p && !isspace (*p)) p++;
00139 
00140   if (*p == '\n')
00141     *p = '\0';
00142   /* I don't need to check EOL here */
00143   *option = leoption_make_option_with_string_value (name, value);
00144 
00145   if (!*option)
00146     return False;
00147   else
00148     return True;
00149 }
00150 
00151 
00152 #ifdef TEST_RUN
00153 
00154 int
00155 main (int argc, char **argv)
00156 {
00157   int nopt;
00158   KOLE_config **pconf;
00159   Bool ret;
00160 
00161   if (argc != 2){
00162     fprintf (stderr, "provide file name\n");
00163     return 0;
00164   }
00165 
00166   ret = get_configuration (argv[1], &nopt, &pconf);
00167   if (!ret){
00168     fprintf (stderr,"error\n");
00169   } else {
00170     int i;
00171     KOLE_config *p;
00172     fprintf (stdout, "%d options returned...\n", nopt);
00173     for (i = 0 ; i < nopt; i++){
00174       p = pconf[i];
00175       leoption_debug_print (p);
00176     }
00177   }
00178   return 0;
00179 }
00180 
00181 #endif