1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
/*****************************************************************
parsecfg.c -- routines for parsing a key-based config file
Copyright (c) 1999 by Mike Oliphant - oliphant@gtk.org
http://www.ling.ed.ac.uk/~oliphant
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
*****************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <glib.h>
#include "parsecfg.h"
static gboolean ParseLine(char *buf,CFGEntry *cfg);
static char *ChopWhite(char *buf);
/* Get rid of whitespace at the beginning or end of a string */
static char *ChopWhite(char *buf)
{
int pos;
for(pos=strlen(buf)-1;(pos>=0)&&isspace(buf[pos]);pos--);
buf[pos+1]='\0';
for(;isspace(*buf);buf++);
return buf;
}
static gboolean ParseLine(char *buf,CFGEntry *cfg)
{
int cfgent;
gboolean found=FALSE;
char *tok;
tok=strtok(buf," ");
if(tok)
for(cfgent=0;cfg[cfgent].type!=CFG_ENTRY_LAST;cfgent++) {
if(!strcasecmp(tok,cfg[cfgent].name)) {
tok=strtok(NULL,"");
if(tok)
switch(cfg[cfgent].type) {
case CFG_ENTRY_STRING:
strncpy((char *)cfg[cfgent].destvar,
ChopWhite(tok),cfg[cfgent].length);
break;
case CFG_ENTRY_BOOL:
*((gboolean *)cfg[cfgent].destvar)=(atoi(tok)==1);
break;
case CFG_ENTRY_INT:
*((int *)cfg[cfgent].destvar)=atoi(tok);
break;
default:
printf("Error: Bad entry type\n");
break;
}
found=TRUE;
break;
}
}
/* if(!found) printf("Error: Unrecognized line: %s\n",buf);*/
return found;
}
gboolean LoadConfig(char *filename,char *name,int ver,int reqver,CFGEntry *cfg)
{
char buf[1024];
FILE *cfp;
char *tok;
cfp=fopen(filename,"r");
if(!cfp) return FALSE;
fgets(buf,1024,cfp);
tok=strtok(buf," ");
if(!tok||(strcasecmp(tok,name))) {
printf("Error: Invalid config file\n");
return FALSE;
}
tok=strtok(NULL,"");
if(!tok||(atoi(tok)<reqver)) {
printf("Error: Config file out of date\n");
return FALSE;
}
while(fgets(buf,1024,cfp)) {
ParseLine(buf,cfg);
}
fclose(cfp);
return TRUE;
}
gboolean SaveConfig(char *filename,char *name,int ver,CFGEntry *cfg)
{
FILE *cfp;
int cfgent;
cfp=fopen(filename,"w");
if(!cfp) return FALSE;
fprintf(cfp,"%s %d\n",name,ver);
for(cfgent=0;cfg[cfgent].type!=CFG_ENTRY_LAST;cfgent++) {
fprintf(cfp,"%s ",cfg[cfgent].name);
switch(cfg[cfgent].type) {
case CFG_ENTRY_STRING:
fprintf(cfp,"%s\n",(char *)cfg[cfgent].destvar);
break;
case CFG_ENTRY_INT:
fprintf(cfp,"%d\n",*((int *)cfg[cfgent].destvar));
break;
case CFG_ENTRY_BOOL:
fprintf(cfp,"%d\n",*((int *)cfg[cfgent].destvar)==1);
break;
default:
break;
}
}
fclose(cfp);
return TRUE;
}
|