]> granicus.if.org Git - vnstat/commitdiff
refactor loadcfg()
authorTeemu Toivola <git@humdi.net>
Fri, 9 Jan 2015 18:10:38 +0000 (20:10 +0200)
committerTeemu Toivola <git@humdi.net>
Fri, 9 Jan 2015 18:10:38 +0000 (20:10 +0200)
src/cfg.c
src/cfg.h

index 58ac4b46520151073fe27e6780b2e820378cd5c3..576c356f7d2346f40159062525592d11c3d27370 100644 (file)
--- a/src/cfg.c
+++ b/src/cfg.c
@@ -166,7 +166,7 @@ void printcfgfile(void)
 int loadcfg(const char *cfgfile)
 {
        FILE *fd;
-       int i, j, k, linelen, cfglen;
+       int i, linelen, cfglen;
 
        char value[512], cfgline[512];
 
@@ -240,85 +240,45 @@ int loadcfg(const char *cfgfile)
        while (!feof(fd)) {
 
                cfgline[0] = '\0';
-
-               /* get current line */
                if (fgets(cfgline, 512, fd)==NULL) {
                        break;
                }
 
                linelen = (int)strlen(cfgline);
-               if (linelen>2 && cfgline[0]!='#') {
-
-                       for (i=0; cset[i].name!=0; i++) {
-
-                               if (cset[i].found) {
-                                       continue;
-                               }
-
-                               cfglen = (int)strlen(cset[i].name);
-                               if ( (linelen>=(cfglen+2)) && (strncasecmp(cfgline, cset[i].name, cfglen)==0) ) {
-
-                                       /* clear value buffer */
-                                       for (j=0; j<512; j++) {
-                                               value[j]='\0';
-                                       }
-
-                                       /* search value */
-                                       j = 0;
-                                       for (k=cfglen; k<linelen; k++) {
-                                               if (cfgline[k]=='\n' || cfgline[k]=='\r') {
-                                                       break;
-                                               } else if (cfgline[k]=='\"') {
-                                                       if (j==0) {
-                                                               continue;
-                                                       } else {
-                                                               break;
-                                                       }
-                                               } else {
-                                                       if (j==0 && (cfgline[k]==' ' || cfgline[k]=='=' || cfgline[k]=='\t')) {
-                                                               continue;
-                                                       } else {
-                                                               value[j] = cfgline[k];
-                                                               j++;
-                                                       }
-                                               }
-                                       }
-
-                                       /* set value and get new line if valid value was found */
-                                       if (strlen(value)!=0) {
-
-                                               if (cset[i].namelen>0) {
-                                                       strncpy_nt(cset[i].locc, value, cset[i].namelen);
-                                                       cset[i].locc[cset[i].namelen-1]='\0';
-                                                       if (debug)
-                                                               printf("  c: %s   -> \"%s\": \"%s\"\n", cfgline, cset[i].name, cset[i].locc);
-                                               } else if (isdigit(value[0])) {
-                                                       *cset[i].loci = strtol(value, (char **)NULL, 0);
-                                                       if (debug)
-                                                               printf("  i: %s   -> \"%s\": %d\n", cfgline, cset[i].name, *cset[i].loci);
-                                               } else {
-                                                       continue;
-                                               }
-
-                                               cset[i].found = 1;
-                                               break;
-                                       } else if (strlen(value)==0) {
-                                               if (debug)
-                                                       printf("  c: %s   -> \"%s\" with no value, keeping default.\n", cfgline, cset[i].name);
-                                               cset[i].found = 1;
-                                               break;
-                                       }
-
-                               } /* if */
-
-                       } /* for */
-
-                       if ((debug) && (!cset[i].found) && (strncasecmp(cfgline, "MaxBW", 5)!=0))
-                               printf("Unknown configuration line: %s", cfgline);
-
-               } /* if */
-
-       } /* while */
+               if (linelen<=2 || cfgline[0]=='#') {
+                       continue;
+               }
+
+               for (i=0; cset[i].name!=0; i++) {
+
+                       if (cset[i].found) {
+                               continue;
+                       }
+
+                       cfglen = (int)strlen(cset[i].name);
+                       if ( (linelen<(cfglen+2)) || (strncasecmp(cfgline, cset[i].name, cfglen)!=0) ) {
+                               continue;
+                       }
+
+                       if (!extractcfgvalue(value, cfgline, cfglen)) {
+                               if (debug)
+                                       printf("  c: %s   -> \"%s\" with no value, keeping default.\n", cfgline, cset[i].name);
+                               cset[i].found = 1;
+                               break;
+                       }
+
+                       if (!setcfgvalue(&cset[i], value, cfgline)) {
+                               continue;
+                       }
+
+                       cset[i].found = 1;
+                       break;
+               }
+
+               if ((debug) && (!cset[i].found) && (strncasecmp(cfgline, "MaxBW", 5)!=0))
+                       printf("Unknown configuration line: %s", cfgline);
+
+       }
 
        fclose(fd);
 
@@ -613,3 +573,54 @@ int opencfgfile(const char *cfgfile, FILE **fd)
 
        return 2;
 }
+
+int extractcfgvalue(char *value, const char *cfgline, int cfglen) {
+
+       int i, j, linelen;
+
+       linelen = (int)strlen(cfgline);
+
+       for (i=0; i<512; i++) {
+               value[i]='\0';
+       }
+
+       i = 0;
+       for (j=cfglen; j<linelen; j++) {
+               if (cfgline[j]=='\n' || cfgline[j]=='\r') {
+                       break;
+               } else if (cfgline[j]=='\"') {
+                       if (i==0) {
+                               continue;
+                       } else {
+                               break;
+                       }
+               } else {
+                       if (i==0 && (cfgline[j]==' ' || cfgline[j]=='=' || cfgline[j]=='\t')) {
+                               continue;
+                       } else {
+                               value[i] = cfgline[j];
+                               i++;
+                       }
+               }
+       }
+
+       return (int)strlen(value);
+}
+
+int setcfgvalue(struct cfgsetting *cset, const char *value, const char *cfgline)
+{
+       if (cset->namelen>0) {
+               strncpy_nt(cset->locc, value, cset->namelen);
+               cset->locc[cset->namelen-1]='\0';
+               if (debug)
+                       printf("  c: %s   -> \"%s\": \"%s\"\n", cfgline, cset->name, cset->locc);
+       } else if (isdigit(value[0])) {
+               *cset->loci = strtol(value, (char **)NULL, 0);
+               if (debug)
+                       printf("  i: %s   -> \"%s\": %d\n", cfgline, cset->name, *cset->loci);
+       } else {
+               return 0;
+       }
+
+       return 1;
+}
index a9ec3a6d464f901a07e6ac07edd46b8c8b1a22e1..91da24b84ba701f812e8156033fb678031c62231 100644 (file)
--- a/src/cfg.h
+++ b/src/cfg.h
@@ -1,12 +1,6 @@
 #ifndef CFG_H
 #define CFG_H
 
-void printcfgfile(void);
-int loadcfg(const char *cfgfile);
-void validatecfg(void);
-void defaultcfg(void);
-int opencfgfile(const char *cfgfile, FILE **fd);
-
 struct cfgsetting {
        const char *name;
        char *locc;
@@ -15,4 +9,12 @@ struct cfgsetting {
        short found;
 };
 
+void printcfgfile(void);
+int loadcfg(const char *cfgfile);
+void validatecfg(void);
+void defaultcfg(void);
+int opencfgfile(const char *cfgfile, FILE **fd);
+int extractcfgvalue(char *value, const char *cfgline, int cfglen);
+int setcfgvalue(struct cfgsetting *cset, const char *value, const char *cfgline);
+
 #endif