libmcs  0.6.0
mcs.h
Go to the documentation of this file.
1 /*
2  * This is mcs; a modular configuration system.
3  *
4  * Copyright (c) 2007 William Pitcock <nenolod -at- sacredspiral.co.uk>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * 3. The name of the author may not be used to endorse or promote products
18  * derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef __LIBMCS_MCS_H__
34 #define __LIBMCS_MCS_H__
35 
36 #include <unistd.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sys/stat.h>
40 #include <sys/types.h>
41 #include <dirent.h>
42 #include <stdio.h>
43 #include <limits.h>
44 #include <stdarg.h>
45 #include <errno.h>
46 
47 #ifndef __WIN32__
48 #include <dlfcn.h>
49 #endif
50 
51 #include <mowgli.h>
52 
53 #ifdef _MCS_CORE
54 # include <libmcs/mcs_config.h>
55 #endif
56 
57 #ifndef PATH_MAX
58 #define PATH_MAX 4096
59 #endif
60 
62 typedef enum {
66 
68 typedef struct mcs_handle_ mcs_handle_t;
69 
83 typedef struct {
84  void *handle;
97  const char *name;
98 
99  /* constructors and destructors */
100 
114  mcs_handle_t *(*mcs_new)(char *domain);
115 
125  void (*mcs_destroy)(mcs_handle_t *handle);
126 
127  /* retrieval */
128 
138  const char *section,
139  const char *key,
140  char **value);
141 
151  const char *section,
152  const char *key,
153  int *value);
154 
164  const char *section,
165  const char *key,
166  int *value);
167 
177  const char *section,
178  const char *key,
179  float *value);
180 
190  const char *section,
191  const char *key,
192  double *value);
193 
194  /* setting data */
195 
205  const char *section,
206  const char *key,
207  const char *value);
208 
218  const char *section,
219  const char *key,
220  int value);
221 
231  const char *section,
232  const char *key,
233  int value);
234 
244  const char *section,
245  const char *key,
246  float value);
247 
257  const char *section,
258  const char *key,
259  double value);
260 
261  /* unset */
262 
271  const char *section,
272  const char *key);
273 
274  /* key request */
275 
282  mowgli_queue_t *(*mcs_get_keys)(mcs_handle_t *handle,
283  const char *section);
284 
285  /* sections request */
286 
292  mowgli_queue_t *(*mcs_get_sections)(mcs_handle_t *handle);
293 } mcs_backend_t;
294 
298 struct mcs_handle_ {
299  mowgli_object_t object;
302 };
303 
304 /*
305  * These functions have to do with initialization of the
306  * library.
307  */
308 
309 extern void mcs_init(void);
310 extern void mcs_fini(void);
311 extern char *mcs_version(void);
312 extern void mcs_handle_class_init(void);
313 
314 /*
315  * These functions have to do with registration of MCS backends.
316  */
319 extern mowgli_queue_t * mcs_backend_get_list(void);
320 extern const char * mcs_backend_select(void);
321 
322 /*
323  * These functions provide the public interface for creating and closing MCS
324  * handles.
325  *
326  * Please note that if a handle is not closed, the data may not be saved to
327  * disk.
328  */
329 extern mcs_handle_t *mcs_new(char *domain);
330 extern void mcs_destroy(mcs_handle_t *handle);
331 
332 /*
333  * These functions provide the public interface for querying and setting data.
334  */
335 /* retrieval */
337  const char *section,
338  const char *key,
339  char **value);
340 
342  const char *section,
343  const char *key,
344  int *value);
345 
347  const char *section,
348  const char *key,
349  int *value);
350 
352  const char *section,
353  const char *key,
354  float *value);
355 
357  const char *section,
358  const char *key,
359  double *value);
360 
361 /* setting data */
363  const char *section,
364  const char *key,
365  const char *value);
366 
368  const char *section,
369  const char *key,
370  int value);
371 
373  const char *section,
374  const char *key,
375  int value);
376 
378  const char *section,
379  const char *key,
380  float value);
381 
383  const char *section,
384  const char *key,
385  double value);
386 
387 /* unset */
389  const char *section,
390  const char *key);
391 
392 /* key request */
393 extern mowgli_queue_t *mcs_get_keys(mcs_handle_t *handle,
394  const char *section);
395 
396 extern mowgli_queue_t *mcs_get_sections(mcs_handle_t *handle);
397 
398 /*
399  * These functions have to do with the plugin loader.
400  */
401 extern void mcs_load_plugins(void);
402 extern void mcs_unload_plugins(mowgli_queue_t *l);
403 
404 /*
405  * These functions are utility functions.
406  */
407 extern size_t mcs_strnlen(const char *str, size_t len);
408 extern char * mcs_strndup(const char *str, size_t len);
409 extern int mcs_create_directory(const char *path, mode_t mode);
410 extern size_t mcs_strlcat(char *dest, const char *src, size_t count);
411 extern size_t mcs_strlcpy(char *dest, const char *src, size_t count);
412 
413 #endif
char * mcs_strndup(const char *str, size_t len)
Duplicates a string, limited to a specific length.
Definition: mcs_util.c:62
mowgli_queue_t * mcs_backend_get_list(void)
Returns a mowgli.queue representing the backends list.
Definition: mcs_backends.c:83
Contains the vtable and some references for an mcs storage backend.
Definition: mcs.h:83
mcs_response_t mcs_set_bool(mcs_handle_t *handle, const char *section, const char *key, int value)
Public function to set a boolean value in a configuration database.
Definition: mcs_handle_factory.c:280
size_t mcs_strlcat(char *dest, const char *src, size_t count)
Concatenates a string, limited to a maximum buffer size.
Definition: mcs_util.c:145
void mcs_fini(void)
Releases resources used by the mcs backend plugins.
Definition: mcs_init.c:68
void * mcs_priv_handle
Definition: mcs.h:301
Definition: mcs.h:64
mcs_response_t mcs_set_float(mcs_handle_t *handle, const char *section, const char *key, float value)
Public function to set a floating point value in a configuration database.
Definition: mcs_handle_factory.c:303
mcs_response_t mcs_set_double(mcs_handle_t *handle, const char *section, const char *key, double value)
Public function to set a double-precision floating point value in a configuration database...
Definition: mcs_handle_factory.c:327
Definition: mcs.h:63
mcs_handle_t * mcs_new(char *domain)
Definition: mcs_handle_factory.c:60
size_t mcs_strnlen(const char *str, size_t len)
Determines the length of a string, limited by an arbitrary length.
Definition: mcs_util.c:45
char * mcs_version(void)
Retrieves the version of the mcs implementation.
Definition: mcs_init.c:83
Represents an MCS object handle.
Definition: mcs.h:298
size_t mcs_strlcpy(char *dest, const char *src, size_t count)
Copies a string, limited to a maximum buffer size.
Definition: mcs_util.c:179
void mcs_destroy(mcs_handle_t *handle)
Destroys an mcs.handle object.
Definition: mcs_handle_factory.c:91
void mcs_handle_class_init(void)
Initialises the mowgli.object.class for the mcs.handle object.
Definition: mcs_handle_factory.c:51
const char * name
The unique name identifying the backend.
Definition: mcs.h:97
void mcs_init(void)
Initialises the mcs library classes and loads the backend plugins.
Definition: mcs_init.c:53
void * handle
Definition: mcs.h:84
void mcs_unload_plugins(mowgli_queue_t *l)
Unloads a given list of plugins.
Definition: mcs_loader.c:92
void mcs_load_plugins(void)
Loads all of the plugins in -DPLUGIN_DIR.
Definition: mcs_loader.c:39
mcs_response_t mcs_set_int(mcs_handle_t *handle, const char *section, const char *key, int value)
Public function to set an integer value in a configuration database.
Definition: mcs_handle_factory.c:257
mcs_response_t mcs_get_int(mcs_handle_t *handle, const char *section, const char *key, int *value)
Public function to retrieve an integer value from a configuration database.
Definition: mcs_handle_factory.c:137
mcs_response_t mcs_get_string(mcs_handle_t *handle, const char *section, const char *key, char **value)
Public function to retrieve a string value from a configuration database.
Definition: mcs_handle_factory.c:113
mcs_response_t mcs_backend_unregister(mcs_backend_t *backend)
Unregisters a custom mcs.backend.
Definition: mcs_backends.c:68
const char * mcs_backend_select(void)
Determines the backend that should be used.
Definition: mcs_backends.c:98
mcs_response_t mcs_set_string(mcs_handle_t *handle, const char *section, const char *key, const char *value)
Public function to set a string value in a configuration database.
Definition: mcs_handle_factory.c:234
mcs_response_t mcs_get_float(mcs_handle_t *handle, const char *section, const char *key, float *value)
Public function to retrieve a floating point value from a configuration database. ...
Definition: mcs_handle_factory.c:185
mcs_response_t mcs_unset_key(mcs_handle_t *handle, const char *section, const char *key)
Public function to remove a value from a configuration database.
Definition: mcs_handle_factory.c:351
mowgli_queue_t * mcs_get_keys(mcs_handle_t *handle, const char *section)
Public function to retrieve a mowgli.queue of keys from a configuration database. ...
Definition: mcs_handle_factory.c:373
int mcs_create_directory(const char *path, mode_t mode)
A utility function which creates a directory.
Definition: mcs_util.c:87
mcs_response_t mcs_get_double(mcs_handle_t *handle, const char *section, const char *key, double *value)
Public function to retrieve a double-precision floating point value from a configuration database...
Definition: mcs_handle_factory.c:209
mowgli_object_t object
Definition: mcs.h:299
mcs_response_t mcs_backend_register(mcs_backend_t *backend)
Registers a custom mcs.backend.
Definition: mcs_backends.c:49
mcs_response_t mcs_get_bool(mcs_handle_t *handle, const char *section, const char *key, int *value)
Public function to retrieve a boolean value from a configuration database.
Definition: mcs_handle_factory.c:161
mcs_backend_t * base
Definition: mcs.h:300
mowgli_queue_t * mcs_get_sections(mcs_handle_t *handle)
Public function to retrieve a mowgli.queue of sections from a configuration database.
Definition: mcs_handle_factory.c:393
mcs_response_t
Definition: mcs.h:62