summaryrefslogtreecommitdiffstats
path: root/include/cfg.c
diff options
context:
space:
mode:
authorhugetoad <hugetoad@97f52cf1-0a1b-0410-bd0e-c28be96e8082>2002-02-20 15:48:50 +0000
committerhugetoad <hugetoad@97f52cf1-0a1b-0410-bd0e-c28be96e8082>2002-02-20 15:48:50 +0000
commitee623dee6ae3742b1f6957c06433b075622da828 (patch)
treece75ecc7884dc7b7a386947b0f3d6579ed6bc467 /include/cfg.c
parentb863640625033bf4d27399fbc3f9b2d708a91e82 (diff)
downloadzabbix-ee623dee6ae3742b1f6957c06433b075622da828.tar.gz
zabbix-ee623dee6ae3742b1f6957c06433b075622da828.tar.xz
zabbix-ee623dee6ae3742b1f6957c06433b075622da828.zip
Common code for parsing of configuration files.
git-svn-id: svn://svn.zabbix.com/trunk@319 97f52cf1-0a1b-0410-bd0e-c28be96e8082
Diffstat (limited to 'include/cfg.c')
-rw-r--r--include/cfg.c106
1 files changed, 106 insertions, 0 deletions
diff --git a/include/cfg.c b/include/cfg.c
new file mode 100644
index 00000000..9f77a817
--- /dev/null
+++ b/include/cfg.c
@@ -0,0 +1,106 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <syslog.h>
+
+#include "common.h"
+#include "cfg.h"
+
+/* struct cfg_line
+ {
+ char *parameter,
+ void *variable,
+ int type;
+ int mandatory;
+ int min;
+ int max;
+ };
+*/
+
+/* struct cfg_line cfg[]=
+ {*/
+/* PARAMETER ,VAR ,FUNC, TYPE(0i,1s),MANDATORY,MIN,MAX */
+/* {"StartSuckers",&Suckers,0, 0 ,1 ,2,255},
+ {0}
+ };*/
+
+int parse_cfg_file(char *cfg_file,struct cfg_line *cfg)
+{
+ FILE *file;
+ char line[1024];
+ char parameter[1024];
+ char *value;
+ int lineno;
+ int i,var;
+ int *pointer;
+ char *c;
+
+
+ file=fopen(cfg_file,"r");
+ if(NULL == file)
+ {
+ syslog( LOG_CRIT, "Cannot open config file [%s] [%m]",cfg_file);
+ return FAIL;
+ }
+
+ lineno=0;
+ while(fgets(line,1024,file) != NULL)
+ {
+ lineno++;
+
+ if(line[0]=='#') continue;
+ if(strlen(line)==1) continue;
+
+ strcpy(parameter,line);
+
+ value=strstr(line,"=");
+
+ if(NULL == value)
+ {
+ syslog( LOG_CRIT, "Error in line [%s] Line %d", line, lineno);
+ return FAIL;
+ }
+ value++;
+ value[strlen(value)-1]=0;
+
+ parameter[value-line-1]=0;
+
+// syslog( LOG_WARNING, "Parameter [%s] Value [%s]", parameter, value);
+
+ i=0;
+ while(cfg[i].parameter != 0)
+ {
+ if(strcmp(cfg[i].parameter, parameter) == 0)
+ {
+ if(cfg[i].type == TYPE_INT)
+ {
+ var=atoi(value);
+ if( (cfg[i].min!=0) || (cfg[i].max!=0))
+ {
+ if( (var<cfg[i].min) || (var>cfg[i].max) )
+ {
+ syslog( LOG_CRIT, "Wrong value of [%s] in line %d. Should be between %d and %d.", cfg[i].parameter, lineno, cfg[i].min, cfg[i].max);
+ return FAIL;
+ }
+
+ }
+/* Can this be done without "pointer" ? */
+ pointer=(int *)cfg[i].variable;
+ *pointer=var;
+ syslog( LOG_WARNING, "Parameter [%s] [%d]", parameter, *pointer);
+ }
+ else
+ {
+/* Can this be done without "c" ? */
+ /* c=(char *)cfg[i].variable;
+ syslog( LOG_WARNING, "ZZZ [%d] [%s]", *c, *c);
+ *c=strdup(value);
+ syslog( LOG_WARNING, "ZZZ [%d] [%s]", c, *c);*/
+// syslog( LOG_WARNING, "Parameter [%s] [%s]", parameter, *c);
+ }
+ }
+ i++;
+ }
+ }
+ return SUCCEED;
+}