#include "parsecfgfile.h" Parser::Parser() { nntpserver = NULL; user = NULL; passwd = NULL; regex = NULL; cfgfilename = strdup( ".bgrabrc" ); parsefile(); } Parser::Parser( char *cfn ) { nntpserver = NULL; user = NULL; passwd = NULL; regex = NULL; cfgfilename = strdup( cfn ); parsefile(); } void Parser::parsefile() { nntpserver = user = passwd = NULL; char lnbuf[BUFFERLEN]; char var[VARLEN+1]; char val[VALLEN+1]; *spamsenders = *spamsubjects = '\0'; int n; FILE *file = fopen( cfgfilename, "r" ); if (file == NULL ) return; while ( fgets(lnbuf,BUFFERLEN,file) ) { *var=*val='\0'; /* n==2 if input ok */ if (lnbuf[0] != '#') { // # starts a comment line n=sscanf(lnbuf,"%60[^=\n\r]=%160[^\n\r]",var,val); notrspaces(var); notrspaces(val); // printf("%d: [%s] = [%s]\n",n,var,val); if(n==2) { if ( strcmp(var,"nntpserver") == 0 ) { nntpserver = strdup(val); } if ( strcmp(var,"user") == 0 ) { user = strdup(val); } if ( strcmp(var,"passwd") == 0 ) { passwd = strdup(val); } if ( strcmp(var,"regex") == 0 ) { regex = strdup(val); } if ( strcmp(var,"spamsender") == 0 ) { if (strcmp(spamsenders,"") != 0) strcat(spamsenders,"\377"); strcat(spamsenders,val); } if ( strcmp(var,"spamsubject") == 0 ) { if (strcmp(spamsubjects,"") != 0) strcat(spamsubjects,"\377"); strcat(spamsubjects,val); } } } } /* if (nntpserver) printf("SERVER: %s\n",nntpserver); if (user) printf("USER: %s\n",user); if (passwd) printf("PASSWD: %s\n",passwd);*/ } void Parser::notrspaces(char *src) { /* remove leading/trailing spaces and other garbage */ char *s; if (!src || ! *src ) return; for(s=src+strlen(src)-1; s>src && isspace(*s); --s) ; *(s+1)='\0'; for(s=src; *s && isspace(*s); ++s) ; if (s==src) return; for( ; *s; *src++ = *s++); *src='\0'; } /* int main() { Parser *p; p = new Parser(); } */