libmcs 0.6.0
src/libmcs/mcs.h
Go to the documentation of this file.
00001 /*
00002  * This is mcs; a modular configuration system.
00003  *
00004  * Copyright (c) 2007 William Pitcock <nenolod -at- sacredspiral.co.uk>
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are
00008  * met:
00009  *
00010  * 1. Redistributions of source code must retain the above copyright notice,
00011  *    this list of conditions and the following disclaimer.
00012  *
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  *
00017  * 3. The name of the author may not be used to endorse or promote products
00018  *    derived from this software without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00021  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00022  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00023  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
00024  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00025  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00026  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00027  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
00028  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00029  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00030  * POSSIBILITY OF SUCH DAMAGE.
00031  */
00032 
00033 #ifndef __LIBMCS_MCS_H__
00034 #define __LIBMCS_MCS_H__
00035 
00036 #include <unistd.h>
00037 #include <stdlib.h>
00038 #include <string.h>
00039 #include <sys/stat.h>
00040 #include <sys/types.h>
00041 #include <dirent.h>
00042 #include <stdio.h>
00043 #include <limits.h>
00044 #include <stdarg.h>
00045 #include <errno.h>
00046 
00047 #ifndef __WIN32__
00048 #include <dlfcn.h>
00049 #endif
00050 
00051 #include <mowgli.h>
00052 
00053 #ifdef _MCS_CORE
00054 # include <libmcs/mcs_config.h>
00055 #endif
00056 
00057 #ifndef PATH_MAX
00058 #define PATH_MAX 4096
00059 #endif
00060 
00062 typedef enum {
00063         MCS_FAIL, 
00064         MCS_OK    
00065 } mcs_response_t;
00066 
00068 typedef struct mcs_handle_ mcs_handle_t;
00069 
00083 typedef struct {
00084         void *handle; 
00097         const char *name;
00098 
00099         /* constructors and destructors */
00100 
00114         mcs_handle_t *(*mcs_new)(char *domain);
00115 
00125         void (*mcs_destroy)(mcs_handle_t *handle);
00126 
00127         /* retrieval */
00128 
00137         mcs_response_t (*mcs_get_string)(mcs_handle_t *handle,
00138                                          const char *section,
00139                                          const char *key,
00140                                          char **value);
00141 
00150         mcs_response_t (*mcs_get_int)(mcs_handle_t *handle,
00151                                       const char *section,
00152                                       const char *key,
00153                                       int *value);
00154 
00163         mcs_response_t (*mcs_get_bool)(mcs_handle_t *handle,
00164                                        const char *section,
00165                                        const char *key,
00166                                        int *value);
00167 
00176         mcs_response_t (*mcs_get_float)(mcs_handle_t *handle,
00177                                         const char *section,
00178                                         const char *key,
00179                                         float *value);
00180 
00189         mcs_response_t (*mcs_get_double)(mcs_handle_t *handle,
00190                                          const char *section,
00191                                          const char *key,
00192                                          double *value);
00193 
00194         /* setting data */
00195 
00204         mcs_response_t (*mcs_set_string)(mcs_handle_t *handle,
00205                                          const char *section,
00206                                          const char *key,
00207                                          const char *value);
00208 
00217         mcs_response_t (*mcs_set_int)(mcs_handle_t *handle,
00218                                       const char *section,
00219                                       const char *key,
00220                                       int value);
00221 
00230         mcs_response_t (*mcs_set_bool)(mcs_handle_t *handle,
00231                                        const char *section,
00232                                        const char *key,
00233                                        int value);
00234 
00243         mcs_response_t (*mcs_set_float)(mcs_handle_t *handle,
00244                                         const char *section,
00245                                         const char *key,
00246                                         float value);
00247 
00256         mcs_response_t (*mcs_set_double)(mcs_handle_t *handle,
00257                                          const char *section,
00258                                          const char *key,
00259                                          double value);
00260 
00261         /* unset */
00262 
00270         mcs_response_t (*mcs_unset_key)(mcs_handle_t *handle,
00271                                         const char *section,
00272                                         const char *key);
00273 
00274         /* key request */
00275 
00282         mowgli_queue_t *(*mcs_get_keys)(mcs_handle_t *handle,
00283                                     const char *section);
00284 
00285         /* sections request */
00286 
00292         mowgli_queue_t *(*mcs_get_sections)(mcs_handle_t *handle);
00293 } mcs_backend_t;
00294 
00298 struct mcs_handle_ {
00299         mowgli_object_t object;  
00300         mcs_backend_t *base;     
00301         void *mcs_priv_handle;   
00302 };
00303 
00304 /*
00305  * These functions have to do with initialization of the
00306  * library.
00307  */
00308 
00309 extern void  mcs_init(void);
00310 extern void  mcs_fini(void);
00311 extern char *mcs_version(void);
00312 extern void  mcs_handle_class_init(void);
00313  
00314 /*
00315  * These functions have to do with registration of MCS backends.
00316  */
00317 extern mcs_response_t mcs_backend_register(mcs_backend_t *backend);
00318 extern mcs_response_t mcs_backend_unregister(mcs_backend_t *backend);
00319 extern mowgli_queue_t *   mcs_backend_get_list(void);
00320 extern const char *       mcs_backend_select(void);
00321 
00322 /*
00323  * These functions provide the public interface for creating and closing MCS
00324  * handles.
00325  *
00326  * Please note that if a handle is not closed, the data may not be saved to
00327  * disk.
00328  */
00329 extern mcs_handle_t *mcs_new(char *domain);
00330 extern void mcs_destroy(mcs_handle_t *handle);
00331 
00332 /*
00333  * These functions provide the public interface for querying and setting data.
00334  */
00335 /* retrieval */
00336 extern mcs_response_t mcs_get_string(mcs_handle_t *handle,
00337                                  const char *section,
00338                                  const char *key,
00339                                  char **value);
00340 
00341 extern mcs_response_t mcs_get_int(mcs_handle_t *handle,
00342                               const char *section,
00343                               const char *key,
00344                               int *value);
00345 
00346 extern mcs_response_t mcs_get_bool(mcs_handle_t *handle,
00347                                const char *section,
00348                                const char *key,
00349                                int *value);
00350 
00351 extern mcs_response_t mcs_get_float(mcs_handle_t *handle,
00352                                 const char *section,
00353                                 const char *key,
00354                                 float *value);
00355 
00356 extern mcs_response_t mcs_get_double(mcs_handle_t *handle,
00357                                  const char *section,
00358                                  const char *key,
00359                                  double *value);
00360 
00361 /* setting data */
00362 extern mcs_response_t mcs_set_string(mcs_handle_t *handle,
00363                                  const char *section,
00364                                  const char *key,
00365                                  const char *value);
00366 
00367 extern mcs_response_t mcs_set_int(mcs_handle_t *handle,
00368                               const char *section,
00369                               const char *key,
00370                               int value);
00371 
00372 extern mcs_response_t mcs_set_bool(mcs_handle_t *handle,
00373                                const char *section,
00374                                const char *key,
00375                                int value);
00376 
00377 extern mcs_response_t mcs_set_float(mcs_handle_t *handle,
00378                                 const char *section,
00379                                 const char *key,
00380                                 float value);
00381 
00382 extern mcs_response_t mcs_set_double(mcs_handle_t *handle,
00383                                  const char *section,
00384                                  const char *key,
00385                                  double value);
00386 
00387 /* unset */
00388 extern mcs_response_t mcs_unset_key(mcs_handle_t *handle,
00389                                 const char *section,
00390                                 const char *key);
00391 
00392 /* key request */
00393 extern mowgli_queue_t *mcs_get_keys(mcs_handle_t *handle,
00394                                 const char *section);
00395 
00396 extern mowgli_queue_t *mcs_get_sections(mcs_handle_t *handle);
00397 
00398 /*
00399  * These functions have to do with the plugin loader.
00400  */
00401 extern void mcs_load_plugins(void);
00402 extern void mcs_unload_plugins(mowgli_queue_t *l);
00403 
00404 /*
00405  * These functions are utility functions.
00406  */
00407 extern size_t mcs_strnlen(const char *str, size_t len);
00408 extern char * mcs_strndup(const char *str, size_t len);
00409 extern int mcs_create_directory(const char *path, mode_t mode);
00410 extern size_t mcs_strlcat(char *dest, const char *src, size_t count);
00411 extern size_t mcs_strlcpy(char *dest, const char *src, size_t count);
00412 
00413 #endif