dynwatch

diff config_parser.c @ 0:ce3c5e4c75bf

dynwatch DynDNS updater for windows
author John Tsiombikas <nuclear@siggraph.org>
date Wed, 18 May 2011 05:53:29 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/config_parser.c	Wed May 18 05:53:29 2011 +0300
     1.3 @@ -0,0 +1,141 @@
     1.4 +/*
     1.5 +This file is part of dynwatch, a win32 system tray applet which
     1.6 +updates automatically the dyndns entry of quake.gr.
     1.7 +
     1.8 +Copyright (c) 2005 John Tsiombikas <nuclear@siggraph.org>
     1.9 +
    1.10 +This program is free software; you can redistribute it and/or modify
    1.11 +it under the terms of the GNU General Public License as published by
    1.12 +the Free Software Foundation; either version 2 of the License, or
    1.13 +(at your option) any later version.
    1.14 +
    1.15 +This program is distributed in the hope that it will be useful,
    1.16 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.17 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.18 +GNU General Public License for more details.
    1.19 +
    1.20 +You should have received a copy of the GNU General Public License
    1.21 +along with this program; if not, write to the Free Software
    1.22 +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.23 +*/
    1.24 +#include <stdio.h>
    1.25 +#include <string.h>
    1.26 +#include <stdlib.h>
    1.27 +#include <ctype.h>
    1.28 +#include "config_parser.h"
    1.29 +
    1.30 +/* state variables */
    1.31 +static char sym_assign = '=';
    1.32 +static char sym_comment = ';';
    1.33 +static char max_line_len = 100;
    1.34 +static char seperators[257] = " \t";
    1.35 +static struct ConfigOption cfg_opt;
    1.36 +
    1.37 +static char *config_file, *cfgptr;
    1.38 +
    1.39 +void SetParserState(enum ParserState state, int value) {
    1.40 +	switch(state) {
    1.41 +	case PS_AssignmentSymbol:
    1.42 +		sym_assign = (char)value;
    1.43 +		break;
    1.44 +
    1.45 +	case PS_CommentSymbol:
    1.46 +		sym_comment = (char)value;
    1.47 +		break;
    1.48 +
    1.49 +	case PS_MaxLineLen:
    1.50 +		max_line_len = value;
    1.51 +		break;
    1.52 +
    1.53 +	case PS_Seperators:
    1.54 +		strncpy(seperators, (char*)value, 257);
    1.55 +		break;
    1.56 +	}
    1.57 +}
    1.58 +
    1.59 +int LoadConfigFile(const char *fname) {
    1.60 +	FILE *fp;
    1.61 +	int fsize;
    1.62 +	char *temp;
    1.63 +
    1.64 +	if(!fname) return -1;
    1.65 +	if(!(fp = fopen(fname, "r"))) return -1;
    1.66 +
    1.67 +	fseek(fp, 0, SEEK_END);
    1.68 +	fsize = ftell(fp);
    1.69 +	fseek(fp, 0, SEEK_SET);
    1.70 +
    1.71 +	if(!(temp = realloc(config_file, fsize))) return -1;
    1.72 +	config_file = temp;
    1.73 +
    1.74 +	cfgptr = config_file;
    1.75 +	temp = malloc(max_line_len + 1);
    1.76 +	while(fgets(temp, max_line_len, fp)) {
    1.77 +		char *ptr = temp;
    1.78 +
    1.79 +		if(*ptr == '\n') continue;	/* kill empty lines, they irritate the parser */
    1.80 +
    1.81 +		while(ptr && *ptr && *ptr != sym_comment) {
    1.82 +			if(!strchr(seperators, *ptr)) {	/* not a seperator */
    1.83 +				*cfgptr++ = *ptr;
    1.84 +			}
    1.85 +			ptr++;
    1.86 +		}
    1.87 +
    1.88 +		if(*ptr == sym_comment && ptr != temp) {
    1.89 +			*cfgptr++ = '\n';
    1.90 +		}
    1.91 +	}
    1.92 +
    1.93 +	*cfgptr = 0;
    1.94 +
    1.95 +	memset(&cfg_opt, 0, sizeof(struct ConfigOption));
    1.96 +	cfgptr = config_file;
    1.97 +	free(temp);
    1.98 +	return 0;
    1.99 +}
   1.100 +
   1.101 +const struct ConfigOption *GetNextOption() {
   1.102 +	char *tmpbuf = malloc(max_line_len + 1);
   1.103 +	char *ptr = tmpbuf;
   1.104 +
   1.105 +	if(!(*cfgptr)) {
   1.106 +		free(tmpbuf);
   1.107 +		return 0;
   1.108 +	}
   1.109 +
   1.110 +	while(*cfgptr != '\n') {
   1.111 +		*ptr++ = *cfgptr++;
   1.112 +	}
   1.113 +	*ptr = 0;
   1.114 +	cfgptr++;
   1.115 +
   1.116 +	if(!(ptr = strchr(tmpbuf, sym_assign))) {
   1.117 +		free(tmpbuf);
   1.118 +		return 0;
   1.119 +	}
   1.120 +	*ptr++ = 0;
   1.121 +
   1.122 +	cfg_opt.flags = 0;
   1.123 +
   1.124 +	cfg_opt.option = realloc(cfg_opt.option, strlen(tmpbuf) + 1);
   1.125 +	strcpy(cfg_opt.option, tmpbuf);
   1.126 +
   1.127 +	cfg_opt.str_value = realloc(cfg_opt.str_value, strlen(ptr) + 1);
   1.128 +	strcpy(cfg_opt.str_value, ptr);
   1.129 +
   1.130 +	if(isdigit(cfg_opt.str_value[0])) {
   1.131 +		cfg_opt.flags |= CFGOPT_INT;
   1.132 +		cfg_opt.int_value = atoi(cfg_opt.str_value);
   1.133 +		cfg_opt.flt_value = atof(cfg_opt.str_value);
   1.134 +	}
   1.135 +
   1.136 +	free(tmpbuf);
   1.137 +	return &cfg_opt;
   1.138 +}
   1.139 +
   1.140 +void DestroyConfigParser() {
   1.141 +	if(cfg_opt.str_value) free(cfg_opt.str_value);
   1.142 +	if(cfg_opt.option) free(cfg_opt.option);
   1.143 +	if(config_file) free(config_file);
   1.144 +}