summaryrefslogtreecommitdiff
path: root/src/bin/libconf.c
blob: 0be5ebacf6dc5ecb7c265d057c8c6ef7112683e0 (plain) (blame)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
 * Copyright © 2013 Guillaume Friloux <kuri@efl.so>
 *
 * 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, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 *
 */

/**
 * @file libconf.c
 * @brief Allows reading of a configuration file
 * @author Guillaume Friloux <kuri@efl.so>
 * @version 1.0
 *
 * Allows reading of a configuration file.
 * Every valid line of the configuration will be stored in an
 * Eina list, and can be retrieved using callbacks
 * @todo Being able to specify separation character between variable and value
 *
 */

/**
 * @page tutorial_libconf Libconf Documentation
 * Libconf will allow you to parse basic configuration files of type :
 *
 * variable = value
 *
 *
 * All you need to do is initialise the library, give fullpath to configuration
 * file and read it.
 *
 * Example code :
 * @code
 * #include <libconf.h>
 *
 * int show_conf(char *variable, char *value)
 * {
 * 	printf("Variable=%s\tValue=%s\n", variable, value);
 * 	return(0);
 * }
 *
 * int main(int argc, char **argv)
 * {
 * 	if( argc < 2 ) return(1);
 * 	struct libconfig myconf;
 * 	eina_init();
 * 	libconfig_init(argv[1], &myconf);
 * 	libconfig_load(&myconf);
 * 	libconfig_list(&myconf, show_conf);
 * 	libconfig_free(&myconf);
 * 	return(0);
 * }
 * @endcode
 *
 * @verbatim  gcc -o main main.c `pkg-config --cflags libconf` `pkg-config --libs libconf`  `pkg-config --cflags eina` @endverbatim
 * @verbatim  EINA_LOG_LEVEL=5 ./main /etc/docupack.conf@endverbatim
 */
#include "libconf.h"

/**
 * \fn int libconfig_init(char *file, struct libconfig *myconf)
 * \brief Inits lib.
 *
 * \param file full path to configuration file
 * \param myconf Pointer to a libconfig structure
 * \return 0 if no error, 1 otherwise
 */
int libconfig_init(char *file, struct libconfig *myconf)
{
	strcpy(myconf->file, file);
	myconf->lconfig = NULL;

	ligconfig_einadom = eina_log_domain_register("Libconf", EINA_COLOR_ORANGE);	

	EINA_LOG_DOM_DBG(ligconfig_einadom, "Using configuration file %s", myconf->file);

	return(0);
}


/**
 * \fn int libconfig_load(struct libconfig *myconf)
 * \brief Loads configuration file to store every variable into our Eina list
 *
 * \param myconf Pointer to a libconfig structure
 * \return 0
 */
int libconfig_load(struct libconfig *myconf)
{
	FILE *fd;
	char line[1024],
	     *ptr;
	int nb;

	EINA_LOG_DOM_DBG(ligconfig_einadom, "Loading configuration file");

	fd = fopen(myconf->file, "r");
	if( !fd )
	{
		EINA_LOG_DOM_ERR(ligconfig_einadom, "Can't open conf file \"%s\" : %s", myconf->file, strerror(errno));
		return(-1);
	}

	while( 1 )
	{
		struct libconfig_entry *entrytmp;
		int size;

		nb = fscanf(fd, "%1024[^\n]\n", line);

		// Did not got anything
		if( nb != 1 )
 			break;

		// Check if last char is '\r', if so we delete it
		ptr = &line[strlen(line)-1];
		if( ptr[0] == '\r' ) ptr[0] = 0;

		// If this line is a comment, we go to next line
		if( line[0] == '#' ) continue;

		// We check that line has a '='
		ptr = strchr(line, '=');
		if( !ptr )
		{
			EINA_LOG_DOM_INFO(ligconfig_einadom, "Config entry is invalid : %s", line);
			continue;
		}

		ptr[0] = 0;

		ptr = &ptr[1];

		// We delete undesired spaces
		while( ( ptr[0] == 32 ) || ( ptr[0] == 9 ) )
			ptr = &ptr[1];

		size = strlen(ptr);
		while( ( ptr[size-1] == 32 ) || ( ptr[size-1] == 9 ) )
		{
			ptr[size-1] = 0;
			size = strlen(ptr);
		}

		size = strlen(line);
		while( ( line[size-1] == 32 ) || ( line[size-1] == 9 ) )
		{
			line[size-1] = 0;
			size = strlen(line);
		}


		entrytmp = malloc( sizeof(struct libconfig_entry) );

		entrytmp->var = malloc( sizeof(char) * ( strlen(line) + 1 ) );
		strcpy(entrytmp->var, line);

		entrytmp->value = malloc( sizeof(char) * ( strlen(ptr) + 1 ) );
		strcpy(entrytmp->value, ptr);

		EINA_LOG_DOM_DBG(ligconfig_einadom, "Adding %s<->%s", entrytmp->var, entrytmp->value);
		myconf->lconfig = eina_list_prepend(myconf->lconfig, entrytmp);
	}

	fclose(fd);

	return(0);
}

/**
 * \fn int libconfig_list(struct libconfig *myconf, int (*callback_function)(char *variable, char *value))
 * \brief Will send to a callback every entry of the Eina list
 *
 * \param myconf Pointer to a libconfig structure
 * \param callback_function Callback to call for each entry
 * \return 0
 */
int libconfig_list(struct libconfig *myconf, int (*callback_function)(char *variable, char *value))
{
	Eina_List *l;
	struct libconfig_entry *myentry;

	EINA_LIST_FOREACH(myconf->lconfig, l, myentry)
		callback_function(myentry->var, myentry->value);

	return(0);
}

/**
 * \fn int libconfig_free(struct libconfig *myconf)
 * \brief Will free our eina list
 *
 * \param myconf Pointer to a libconfig structure
 * \return 0
 */
int libconfig_free(struct libconfig *myconf)
{
	Eina_List *l;
	struct libconfig_entry *myentry;

	EINA_LIST_FOREACH(myconf->lconfig, l, myentry)
	{
		free(myentry->var);
		free(myentry->value);
		free(myentry);
	}
	eina_list_free(myconf->lconfig);
	return(0);
}