1 /*
2  * wzdftpd - a modular and cool ftp server
3  * Copyright (C) 2002-2004  Pierre Chifflier
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * As a special exemption, Pierre Chifflier
20  * and other respective copyright holders give permission to link this program
21  * with OpenSSL, and distribute the resulting executable, without including
22  * the source code for OpenSSL in the source distribution.
23  */
24 
25 #ifndef __WZD_CONFIGFILE__
26 #define __WZD_CONFIGFILE__
27 
28 /** \file wzd_configfile.h
29  * \brief Simple config file parser (.ini like)
30  *
31  * \addtogroup libwzd_core
32  * @{
33  */
34 
35 typedef struct _wzd_configfile_t wzd_configfile_t;
36 
37 typedef enum {
38   CF_OK = 0,
39   CF_ERROR_INVALID_ARGS = -1,
40   CF_ERROR_GROUP_NOT_FOUND = -2,
41   CF_ERROR_NO_CURRENT_GROUP = -3,
42   CF_ERROR_PARSE = -4,
43   CF_ERROR_NOT_FOUND = -5,
44   CF_ERROR_INVALID_ENCODING = -6,
45   CF_ERROR_FILE = -7,
46   CF_ERROR_KEY_ALREADY_EXISTS = -8,
47 
48   CF_ERROR_NEED_MORE_DATA = -20,
49 } cf_error_t;
50 
51 typedef enum {
52   CF_FILE_NONE           = 0,
53   CF_FILE_MERGE_MULTIPLE = 1 << 0,
54 
55   CF_FILE_DEBUG = 1 << 7,
56 } cf_flags_t;
57 
58 /** \brief Creates a new empty wzd_configfile_t object.
59  */
60 wzd_configfile_t * config_new(void);
61 
62 /** \brief Frees a wzd_configfile_t
63  */
64 void config_free(wzd_configfile_t * file);
65 
66 /** \brief Returns the list of groups contained in \a file
67  */
68 wzd_string_t ** config_get_groups(const wzd_configfile_t * file);
69 
70 /** \brief Returns the list of keys contained in \a group
71  */
72 wzd_string_t ** config_get_keys(const wzd_configfile_t * file, const char * groupname, int * errcode);
73 
74 /** \brief Looks whether the config file has the group \a groupname.
75  * \return 1 if \a groupname is part of \a file
76  */
77 int config_has_group(wzd_configfile_t * file, const char * groupname);
78 
79 /** \brief Looks whether the config file has the key \a key in the group \a groupname.
80  * \return 1 if \a key is part of \a groupname
81  */
82 int config_has_key(wzd_configfile_t * file, const char * groupname, const char * key);
83 
84 /** \brief Returns the value associated with \a key under \a groupname.
85  * \return the value, or NULL if the key is not found
86  * The returned value is a pointer to the object, it must not be freed.
87  */
88 char * config_get_value(const wzd_configfile_t * file, const char * groupname, const char * key);
89 
90 /** \brief Associates a new value with \a key under \a groupname.
91  *
92  * If \a key cannot be found then it is created. If \a groupname cannot be found then it is
93  * created.
94  */
95 int config_set_value(wzd_configfile_t * file, const char * groupname, const char * key, const char * value);
96 
97 /** \brief Returns the value associated with \a key under \a groupname as a string.
98  * \return the value, else \a errcode is set to nonzero.
99  */
100 wzd_string_t * config_get_string(const wzd_configfile_t * file, const char * groupname, const char * key, int * errcode);
101 
102 /** \brief Associates a new string value with \a key under \a groupname.
103  *
104  * If \a key cannot be found then it is created.
105  */
106 int config_set_string(wzd_configfile_t * file, const char * groupname, const char * key, wzd_string_t * value);
107 
108 /** \brief Returns the value associated with \a key under \a groupname as a string.
109  * \return a NULL-terminated string array,, or NULL and set \a errcode to nonzero.
110  * The array should be freed using str_deallocate_array()
111  */
112 wzd_string_t ** config_get_string_list(wzd_configfile_t * file, const char * groupname, const char * key, int * errcode);
113 
114 /** \brief Associates a list of string values with \a key under \a groupname.
115  *
116  * If \a key cannot be found then it is created.
117  */
118 int config_set_string_list(wzd_configfile_t * file, const char * groupname, const char * key, wzd_string_t ** value, size_t length);
119 
120 /** \brief Returns the value associated with \a key under \a groupname as a boolean.
121  * \return the value, else \a errcode is set to nonzero.
122  */
123 int config_get_boolean(wzd_configfile_t * file, const char * groupname, const char * key, int * errcode);
124 
125 /** \brief Associates a new boolean value with \a key under \a groupname.
126  *
127  * If \a key cannot be found then it is created.
128  */
129 int config_set_boolean(wzd_configfile_t * file, const char * groupname, const char * key, int value);
130 
131 /** \brief Returns the value associated with \a key under \a groupname as an integer.
132  * \return the value, else \a errcode is set to nonzero.
133  */
134 int config_get_integer(wzd_configfile_t * file, const char * groupname, const char * key, int * errcode);
135 
136 /** \brief Associates a new integer value with \a key under \a groupname.
137  *
138  * If \a key cannot be found then it is created.
139  */
140 int config_set_integer(wzd_configfile_t * file, const char * groupname, const char * key, int value);
141 
142 /** \brief Places a comment above \a key from \a groupname.
143  *
144  * If \a key is NULL then \a comment will be written above \a groupname.
145  * If both \a key and \a groupname are NULL, then \a comment will be written
146  * above the first group in the file.
147  */
148 int config_set_comment(wzd_configfile_t * file, const char * groupname, const char * key, const char * comment);
149 
150 /** \brief Removes a comment above \a key from \a groupname
151  *
152  * If \a key is \a NULL then the comment above \a groupname is removed.
153  * If both \a key and \a groupname are \a NULL, then the comment before the
154  * first group is removed.
155  */
156 int config_remove_comment(wzd_configfile_t * file, const char * groupname, const char * key);
157 
158 /** \brief Removes a \a key in \a groupname from the key file.
159  */
160 int config_remove_key(wzd_configfile_t * file, const char * groupname, const char * key);
161 
162 /** \brief Removes a \a groupname (and all associated keys and comments) from the key file.
163  */
164 int config_remove_group(wzd_configfile_t * file, const char * groupname);
165 
166 /** \brief Loads a key file from disk into an empty wzd_configfile_t structure.
167  *
168  * If the object cannot be created then the return value is non-zero.
169  */
170 int config_load_from_file (wzd_configfile_t * config, const char * file, unsigned long flags);
171 
172 /** \brief Loads a key file from an opened file descriptor into an empty
173  * wzd_configfile_t structure.
174  *
175  * If the object cannot be created then the return value is non-zero.
176  */
177 int config_load_from_fd (wzd_configfile_t * config, int fd, unsigned long flags);
178 
179 /** \brief Loads a key file from memory into an empty wzd_configfile_t structure.
180  *
181  * If the object cannot be created then the return value is non-zero.
182  */
183 int config_load_from_data (wzd_configfile_t * config, const char * data, size_t length, unsigned long flags);
184 
185 /** \brief Outputs \a config as a wzd_string_t.
186  */
187 wzd_string_t * config_to_data (wzd_configfile_t * config, size_t * length);
188 
189 /** @} */
190 
191 #endif /* __WZD_CONFIGFILE__ */
192