Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages | Examples

CfgParse.cpp

Go to the documentation of this file.
00001 /////////////////////////////////////////////////////////////////////////////
00002 /*! \file CfgParse.cpp
00003  *  \brief The configuration parser will read the configuration file and
00004  *  break it up into groups of tokens for handling by specific modules.
00005  *  \author     $Author: mavandyk $
00006  *  \version $Revision: 1.4 $
00007  *  \date    $Date: 2003/08/01 19:58:11 $
00008 */////////////////////////////////////////////////////////////////////////////
00009 
00010 #include "CfgParse.h"
00011 #include <stdio.h>
00012 #include <stdlib.h>
00013 #include <string.h>
00014 
00015 cfgBody *CfgParse::go(char *fn) {
00016   FILE *fp;
00017   char *line, *ptr, *rest, *gotln;
00018   int i, dev=-1, keynum, valnum;
00019   cfgBody *retval;
00020   
00021 
00022   if (!(fp = fopen(fn, "r")))
00023           return(NULL);
00024   line = (char *)malloc(16384);
00025 
00026   // TODO: Obviously, this will need to be done more smartly in the future...
00027   retval = (cfgBody *)malloc(sizeof(cfgBody)*100);
00028 
00029   while ((gotln=fgets(line, 16833, fp))) {
00030           // TODO: Need to be more sophisticated about stripping out
00031           //    all whitespace before checking for useless lines.
00032           strip(line, ' ', 2);
00033           if (line[0] == '#' || line[0] == '\n' || line[0] == '\r' ||
00034                           !(strchr(line, ':')) )
00035                   continue;
00036           if (!(ptr = strchr(line, ':')))
00037                   continue;
00038           // Now we are processing a device block...
00039           // TODO: Need to do this dynamically, or at least smarter
00040           dev++;
00041           retval[dev].keys = (char **)malloc(sizeof(char *)*20);        // 20 keys
00042           retval[dev].vals = (char ***)malloc(sizeof(char **)*20);      // 20 val-lines
00043           for (i = 0; i < 20; i++)
00044                   retval[dev].vals[i] = (char **)malloc(sizeof(char *)*20);     // 20 words max
00045           ptr[0] = 0;
00046           strip(line, ' ', 2);
00047           allocCpy(&(retval[dev].devTy), line);
00048           // Set the devTyNum (device index no) member
00049           // Loop through all possible device types
00050           for (i = 0, retval[dev].devTyNum = (wdev_t)-1; DEV_TYPES[i][0]; i++)
00051                   if (!strcasecmp(DEV_TYPES[i],retval[dev].devTy)) {
00052                           retval[dev].devTyNum = (wdev_t)i;
00053                           break;
00054                   }
00055           ptr++;
00056           strip(ptr, ' ', 2);
00057           crstrip(ptr);
00058           allocCpy(&(retval[dev].devNm), ptr);
00059           // TODO: Assuming '{' on line by itself...
00060           while (!feof(fp) && !strchr(line, '{'))
00061                   fgets(line, 16383, fp);
00062           // TODO: Assumed '}' on line by itself and one key per line
00063           for (keynum = 0, fgets(line, 16383, fp);
00064                           !strchr(line, '}');
00065                           keynum++, fgets(line, 16383, fp)) {
00066                   if (!(ptr = strchr(line, ':'))) {
00067                           keynum--;
00068                           continue;
00069                   }
00070                   ptr[0]=0;
00071                   strip(line, ' ', 2);
00072                   allocCpy(&(retval[dev].keys[keynum]), line);
00073                   rest = ptr+1;
00074                   for (valnum = 0; (ptr=strtok(rest, ",")); valnum++, rest=NULL) {
00075                           // TODO: clean this up
00076                           strip(ptr, ' ', 2);
00077                           crstrip(ptr);
00078                           strip(ptr, ' ', 2);
00079                           allocCpy(&(retval[dev].vals[keynum][valnum]), ptr);
00080                   }
00081                   allocCpy(&(retval[dev].vals[keynum][valnum]), "");
00082           }
00083           allocCpy(&(retval[dev].keys[keynum]), "");
00084   }
00085   allocCpy(&(retval[dev].devTy), "");
00086   return(retval);
00087 }
00088 

Generated on Wed Sep 5 12:54:18 2007 for DSACSS Operational Code by  doxygen 1.3.9.1