summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorGuillaume Friloux <guillaume.friloux@gmail.com>2013-12-17 16:04:54 +0100
committerGuillaume Friloux <guillaume.friloux@gmail.com>2013-12-17 16:04:54 +0100
commit7a94df818b590d0918b978aae5a60b911fa79ce0 (patch)
treec42b232fff654620b55c558862edd00dcb2dc3a2 /src/lib
parent6b427971ed5c8c26786e77fb33140cca117737c1 (diff)
Delete old libconf file.
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/conf.c215
1 files changed, 0 insertions, 215 deletions
diff --git a/src/lib/conf.c b/src/lib/conf.c
deleted file mode 100644
index acca01e..0000000
--- a/src/lib/conf.c
+++ /dev/null
@@ -1,215 +0,0 @@
1/*
2 * Copyright © 2013 Guillaume Friloux <kuri@efl.so>
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19/**
20 * @file libconf.c
21 * @brief Allows reading of a configuration file
22 * @author Guillaume Friloux <kuri@efl.so>
23 * @version 1.0
24 *
25 * Allows reading of a configuration file.
26 * Every valid line of the configuration will be stored in an
27 * Eina list, and can be retrieved using callbacks
28 * @todo Being able to specify separation character between variable and value
29 *
30 */
31
32/**
33 * @page tutorial_libconf Libconf Documentation
34 * Libconf will allow you to parse basic configuration files of type :
35 *
36 * variable = value
37 *
38 *
39 * All you need to do is initialise the library, give fullpath to configuration
40 * file and read it.
41 *
42 * Example code :
43 * @code
44 * #include <libconf.h>
45 *
46 * int show_conf(char *variable, char *value)
47 * {
48 * printf("Variable=%s\tValue=%s\n", variable, value);
49 * return(0);
50 * }
51 *
52 * int main(int argc, char **argv)
53 * {
54 * if( argc < 2 ) return(1);
55 * struct libconfig myconf;
56 * eina_init();
57 * libconfig_init(argv[1], &myconf);
58 * libconfig_load(&myconf);
59 * libconfig_list(&myconf, show_conf);
60 * libconfig_free(&myconf);
61 * return(0);
62 * }
63 * @endcode
64 *
65 * @verbatim gcc -o main main.c `pkg-config --cflags libconf` `pkg-config --libs libconf` `pkg-config --cflags eina` @endverbatim
66 * @verbatim EINA_LOG_LEVEL=5 ./main /etc/docupack.conf@endverbatim
67 */
68#include <Conf.h>
69
70/**
71 * \brief Inits lib.
72 *
73 * \param file full path to configuration file
74 * \param myconf Pointer to a libconfig structure
75 * \return 0 if no error, 1 otherwise
76 */
77int libconfig_init(char *file, struct libconfig *myconf)
78{
79 strcpy(myconf->file, file);
80 myconf->lconfig = NULL;
81
82 ligconfig_einadom = eina_log_domain_register("Libconf", EINA_COLOR_ORANGE);
83
84 EINA_LOG_DOM_DBG(ligconfig_einadom, "Using configuration file %s", myconf->file);
85
86 return(0);
87}
88
89
90/**
91 * \brief Loads configuration file to store every variable into our Eina list
92 *
93 * \param myconf Pointer to a libconfig structure
94 * \return 0
95 */
96int libconfig_load(struct libconfig *myconf)
97{
98 FILE *fd;
99 char line[1024],
100 *ptr;
101 int nb;
102
103 EINA_LOG_DOM_DBG(ligconfig_einadom, "Loading configuration file");
104
105 fd = fopen(myconf->file, "r");
106 if( !fd )
107 {
108 EINA_LOG_DOM_ERR(ligconfig_einadom, "Can't open conf file \"%s\" : %s", myconf->file, strerror(errno));
109 return(-1);
110 }
111
112 while( 1 )
113 {
114 struct libconfig_entry *entrytmp;
115 int size;
116
117 nb = fscanf(fd, "%1024[^\n]\n", line);
118
119 // Did not got anything
120 if( nb != 1 )
121 break;
122
123 // Check if last char is '\r', if so we delete it
124 ptr = &line[strlen(line)-1];
125 if( ptr[0] == '\r' ) ptr[0] = 0;
126
127 // If this line is a comment, we go to next line
128 if( line[0] == '#' ) continue;
129
130 // We check that line has a '='
131 ptr = strchr(line, '=');
132 if( !ptr )
133 {
134 EINA_LOG_DOM_INFO(ligconfig_einadom, "Config entry is invalid : %s", line);
135 continue;
136 }
137
138 ptr[0] = 0;
139
140 ptr = &ptr[1];
141
142 // We delete undesired spaces
143 while( ( ptr[0] == 32 ) || ( ptr[0] == 9 ) )
144 ptr = &ptr[1];
145
146 size = strlen(ptr);
147 while( ( ptr[size-1] == 32 ) || ( ptr[size-1] == 9 ) )
148 {
149 ptr[size-1] = 0;
150 size = strlen(ptr);
151 }
152
153 size = strlen(line);
154 while( ( line[size-1] == 32 ) || ( line[size-1] == 9 ) )
155 {
156 line[size-1] = 0;
157 size = strlen(line);
158 }
159
160
161 entrytmp = malloc( sizeof(struct libconfig_entry) );
162
163 entrytmp->var = malloc( sizeof(char) * ( strlen(line) + 1 ) );
164 strcpy(entrytmp->var, line);
165
166 entrytmp->value = malloc( sizeof(char) * ( strlen(ptr) + 1 ) );
167 strcpy(entrytmp->value, ptr);
168
169 EINA_LOG_DOM_DBG(ligconfig_einadom, "Adding %s<->%s", entrytmp->var, entrytmp->value);
170 myconf->lconfig = eina_list_prepend(myconf->lconfig, entrytmp);
171 }
172
173 fclose(fd);
174
175 return(0);
176}
177
178/**
179 * \brief Will send to a callback every entry of the Eina list
180 *
181 * \param myconf Pointer to a libconfig structure
182 * \param callback_function Callback to call for each entry
183 * \return 0
184 */
185int libconfig_list(struct libconfig *myconf, int (*callback_function)(char *variable, char *value))
186{
187 Eina_List *l;
188 struct libconfig_entry *myentry;
189
190 EINA_LIST_FOREACH(myconf->lconfig, l, myentry)
191 callback_function(myentry->var, myentry->value);
192
193 return(0);
194}
195
196/**
197 * \brief Will free our eina list
198 *
199 * \param myconf Pointer to a libconfig structure
200 * \return 0
201 */
202int libconfig_free(struct libconfig *myconf)
203{
204 Eina_List *l;
205 struct libconfig_entry *myentry;
206
207 EINA_LIST_FOREACH(myconf->lconfig, l, myentry)
208 {
209 free(myentry->var);
210 free(myentry->value);
211 free(myentry);
212 }
213 eina_list_free(myconf->lconfig);
214 return(0);
215}