summaryrefslogtreecommitdiffstats
path: root/common/eurephia_cfgfile.c
blob: ac47ee7f47ae4077c69ab5c8fae31316406a76ed (plain)
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
/* eurephia_cfgfile.c
 *
 *  Simple generic ini-style config file parser
 *
 *  GPLv2 only - Copyright (C) 2011
 *               David Sommerseth <dazo@users.sourceforge.net>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; version 2
 *  of the License.
 *
 *  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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

/**
 * @file   eurephia_cfgfile.c
 * @author David Sommerseth <dazo@users.sourceforge.net>
 * @date   2011-07-23
 *
 * @brief A simple, generic ini-style config file parser
 *
 */

#include <stdio.h>
#include <string.h>
#include <sys/stat.h>

#include <eurephia_nullsafe.h>
#include <eurephia_context.h>
#include <eurephia_log.h>
#include <eurephia_values.h>


/**
 * Parse one single configuration line into a eurephiaVALUES key/value
 * pair.  It will also ignore comment lines, and also remove the
 * comments on the line of the configuration line so that only the
 * key/value information is extracted.
 *
 * @param line Input configuration line
 *
 * @return eurephiaVALUES pointer containing the parsed result.  On
 *         error or if no valid config line was found, NULL is returned.
 */
static inline eurephiaVALUES *parse_config_line(eurephiaCTX *ctx, const char *line) {
        char *cp = NULL, *key = NULL, *val = NULL, *ptr = NULL;
        eurephiaVALUES *ret = NULL;

        if( *line == '#' ) {
                return NULL;
        }

        cp = strdup(line);
        key = cp;
        val = strpbrk(cp, "=");
        if( val == NULL ) {
                free_nullsafe(NULL, cp);
                return NULL;
        }
        *val = '\0'; val++;

        // Discard comments at the end of a line
        if( (ptr = strpbrk(val, "#")) != NULL ) {
                *ptr = '\0';
        }

        // Left trim
        while( ((*key == 0x20) || (*key == 0x0A) || (*key == 0x0D)) ) {
                key++;
        }
        while( ((*val == 0x20) || (*val == 0x0A) || (*val == 0x0D)) ) {
                val++;
        }

        // Right trim
        ptr = key + strlen_nullsafe(key) - 1;
        while( ((*ptr == 0x20) || (*ptr == 0x0A) || (*ptr == 0x0D)) && (ptr > key) ) {
                ptr--;
        }
        ptr++;
        *ptr = '\0';

        ptr = val + strlen_nullsafe(val) - 1;
        while( ((*ptr == 0x20) || (*ptr == 0x0A) || (*ptr == 0x0D)) && (ptr > val) ) {
                ptr--;
        }
        ptr++;
        *ptr = '\0';

        // Put key/value into a eurephiaVALUES struct and return it
        ret = eCreate_value_space(ctx, 20);
        ret->key = strdup(key);
        ret->val = strdup(val);

        free_nullsafe(ctx, cp);
        return ret;
}


/**
 * Parses a complete config file and puts it into an eurephiaVALUES
 * key/value stack
 *
 * @param fname Complete file name with full path to the configuration
 *        file to parse
 *
 * @return Returns a pointer to an eurephiaVALUES stack containing the
 *         configuration on success, otherwise NULL.
 */
eurephiaVALUES *ecfg_ReadConfig(eurephiaCTX *ctx, const char *fname)
{
        FILE *fp = NULL;
        char *buf = NULL;
        eurephiaVALUES *cfg = NULL;
        struct stat fi;

        if( stat(fname, &fi) == -1 ) {
                eurephia_log(ctx, LOG_CRITICAL, 0,
                             "Could not open the config file: %s\n", fname);
                return NULL;
        }

        if( (fp = fopen(fname, "r")) == NULL ) {
                eurephia_log(ctx, LOG_CRITICAL, 0,
                             "Could not open the config file: %s\n", fname);
                return NULL;
        }

        buf = (char *) malloc_nullsafe(ctx, fi.st_size+2);

        cfg = eCreate_value_space(ctx, 20);
        while( fgets(buf, fi.st_size, fp) != NULL ) {
                eurephiaVALUES *prm = parse_config_line(ctx, buf);
                if( prm != NULL ) {
                        eAdd_valuestruct(ctx, cfg, prm);
                }
        };
        free_nullsafe(ctx, buf);
        fclose(fp); fp = NULL;

        return cfg;
}