2002-03-20 23:21:22 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002
|
|
|
|
* Antti Tapaninen <aet@cc.hut.fi>
|
|
|
|
*
|
|
|
|
* Originally based on source by Timo Sirainen <tss@iki.fi>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _SC_CONF_H
|
|
|
|
#define _SC_CONF_H
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2002-03-31 11:30:54 +00:00
|
|
|
#define SCCONF_BOOLEAN 11
|
|
|
|
#define SCCONF_INTEGER 12
|
|
|
|
#define SCCONF_STRING 13
|
|
|
|
|
2002-03-20 23:21:22 +00:00
|
|
|
typedef struct _scconf_block scconf_block;
|
|
|
|
|
|
|
|
typedef struct _scconf_list {
|
|
|
|
struct _scconf_list *next;
|
|
|
|
char *data;
|
|
|
|
} scconf_list;
|
|
|
|
|
2002-11-11 14:08:45 +00:00
|
|
|
#define SCCONF_ITEM_TYPE_COMMENT 0 /* key = NULL, comment */
|
|
|
|
#define SCCONF_ITEM_TYPE_BLOCK 1 /* key = key, block */
|
|
|
|
#define SCCONF_ITEM_TYPE_VALUE 2 /* key = key, list */
|
2002-03-20 23:21:22 +00:00
|
|
|
|
|
|
|
typedef struct _scconf_item {
|
|
|
|
struct _scconf_item *next;
|
|
|
|
int type;
|
|
|
|
char *key;
|
|
|
|
union {
|
|
|
|
char *comment;
|
|
|
|
scconf_block *block;
|
|
|
|
scconf_list *list;
|
|
|
|
} value;
|
|
|
|
} scconf_item;
|
|
|
|
|
|
|
|
struct _scconf_block {
|
|
|
|
scconf_block *parent;
|
|
|
|
scconf_list *name;
|
|
|
|
scconf_item *items;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
char *filename;
|
2002-03-31 11:30:54 +00:00
|
|
|
int debug;
|
2002-03-20 23:21:22 +00:00
|
|
|
scconf_block *root;
|
2003-12-03 14:09:15 +00:00
|
|
|
char *errmsg;
|
2002-03-20 23:21:22 +00:00
|
|
|
} scconf_context;
|
|
|
|
|
2002-03-26 20:56:13 +00:00
|
|
|
/* Allocate scconf_context
|
2002-03-25 20:10:44 +00:00
|
|
|
* The filename can be NULL
|
|
|
|
*/
|
2002-03-26 20:56:13 +00:00
|
|
|
extern scconf_context *scconf_new(const char *filename);
|
2002-03-25 20:10:44 +00:00
|
|
|
|
2002-03-26 20:56:13 +00:00
|
|
|
/* Free scconf_context
|
2002-03-25 20:10:44 +00:00
|
|
|
*/
|
2002-03-26 20:56:13 +00:00
|
|
|
extern void scconf_free(scconf_context * config);
|
2002-03-20 23:21:22 +00:00
|
|
|
|
2002-03-25 20:10:44 +00:00
|
|
|
/* Parse configuration
|
|
|
|
* Returns 1 = ok, 0 = error, -1 = error opening config file
|
|
|
|
*/
|
2003-12-03 14:09:15 +00:00
|
|
|
extern int scconf_parse(scconf_context * config);
|
2002-03-20 23:21:22 +00:00
|
|
|
|
2003-01-03 11:54:02 +00:00
|
|
|
/* Parse a static configuration string
|
|
|
|
* Returns 1 = ok, 0 = error
|
|
|
|
*/
|
2003-12-03 14:09:15 +00:00
|
|
|
extern int scconf_parse_string(scconf_context * config, const char *string);
|
2003-01-03 11:54:02 +00:00
|
|
|
|
2002-03-25 20:10:44 +00:00
|
|
|
/* Write config to a file
|
|
|
|
* If the filename is NULL, use the config->filename
|
2002-03-20 23:21:22 +00:00
|
|
|
* Returns 0 = ok, else = errno
|
|
|
|
*/
|
|
|
|
extern int scconf_write(scconf_context * config, const char *filename);
|
|
|
|
|
2002-03-31 11:30:54 +00:00
|
|
|
/* Find a block by the item_name
|
2002-03-25 20:10:44 +00:00
|
|
|
* If the block is NULL, the root block is used
|
|
|
|
*/
|
2002-11-11 14:08:45 +00:00
|
|
|
extern const scconf_block *scconf_find_block(const scconf_context * config, const scconf_block * block, const char *item_name);
|
2002-03-25 20:10:44 +00:00
|
|
|
|
2002-03-31 11:30:54 +00:00
|
|
|
/* Find blocks by the item_name
|
2002-03-25 20:10:44 +00:00
|
|
|
* If the block is NULL, the root block is used
|
2002-03-26 20:56:13 +00:00
|
|
|
* The key can be used to specify what the blocks first name should be
|
2002-03-25 20:10:44 +00:00
|
|
|
*/
|
2002-11-11 14:08:45 +00:00
|
|
|
extern scconf_block **scconf_find_blocks(const scconf_context * config, const scconf_block * block, const char *item_name, const char *key);
|
2002-03-25 20:10:44 +00:00
|
|
|
|
|
|
|
/* Get a list of values for option
|
|
|
|
*/
|
2002-03-25 22:39:22 +00:00
|
|
|
extern const scconf_list *scconf_find_list(const scconf_block * block, const char *option);
|
2002-03-20 23:21:22 +00:00
|
|
|
|
2002-03-25 22:39:22 +00:00
|
|
|
/* Return the first string of the option
|
2002-03-31 11:30:54 +00:00
|
|
|
* If no option found, return def
|
2002-03-25 20:10:44 +00:00
|
|
|
*/
|
2002-03-25 22:39:22 +00:00
|
|
|
extern const char *scconf_get_str(const scconf_block * block, const char *option, const char *def);
|
|
|
|
|
|
|
|
/* Return the first value of the option as integer
|
2002-03-31 11:30:54 +00:00
|
|
|
* If no option found, return def
|
2002-03-25 22:39:22 +00:00
|
|
|
*/
|
|
|
|
extern int scconf_get_int(const scconf_block * block, const char *option, int def);
|
|
|
|
|
|
|
|
/* Return the first value of the option as boolean
|
2002-03-31 11:30:54 +00:00
|
|
|
* If no option found, return def
|
2002-03-25 22:39:22 +00:00
|
|
|
*/
|
|
|
|
extern int scconf_get_bool(const scconf_block * block, const char *option, int def);
|
2002-03-20 23:21:22 +00:00
|
|
|
|
2002-11-11 22:26:06 +00:00
|
|
|
/* Write value to a block as a string
|
|
|
|
*/
|
|
|
|
extern const char *scconf_put_str(scconf_block * block, const char *option, const char *value);
|
|
|
|
|
|
|
|
/* Write value to a block as an integer
|
|
|
|
*/
|
|
|
|
extern int scconf_put_int(scconf_block * block, const char *option, int value);
|
|
|
|
|
|
|
|
/* Write value to a block as a boolean
|
|
|
|
*/
|
|
|
|
extern int scconf_put_bool(scconf_block * block, const char *option, int value);
|
|
|
|
|
2002-11-11 14:08:45 +00:00
|
|
|
/* Add block structure
|
|
|
|
* If the block is NULL, the root block is used
|
|
|
|
*/
|
|
|
|
extern scconf_block *scconf_block_add(scconf_context * config, scconf_block * block, const char *key, const scconf_list *name);
|
|
|
|
|
|
|
|
/* Copy block structure (recursive)
|
|
|
|
*/
|
|
|
|
extern scconf_block *scconf_block_copy(const scconf_block * src, scconf_block ** dst);
|
|
|
|
|
|
|
|
/* Free block structure (recursive)
|
2002-03-25 20:10:44 +00:00
|
|
|
*/
|
2002-03-20 23:21:22 +00:00
|
|
|
extern void scconf_block_destroy(scconf_block * block);
|
|
|
|
|
2002-11-11 14:08:45 +00:00
|
|
|
/* Add item to block structure
|
|
|
|
* If the block is NULL, the root block is used
|
|
|
|
*/
|
|
|
|
extern scconf_item *scconf_item_add(scconf_context * config, scconf_block * block, scconf_item * item, int type, const char *key, const void *data);
|
|
|
|
|
|
|
|
/* Copy item structure (recursive)
|
|
|
|
*/
|
|
|
|
extern scconf_item *scconf_item_copy(const scconf_item * src, scconf_item ** dst);
|
|
|
|
|
|
|
|
/* Free item structure (recursive)
|
|
|
|
*/
|
|
|
|
extern void scconf_item_destroy(scconf_item * item);
|
|
|
|
|
2002-03-31 11:30:54 +00:00
|
|
|
/* Add a new value to the list
|
|
|
|
*/
|
|
|
|
extern scconf_list *scconf_list_add(scconf_list ** list, const char *value);
|
|
|
|
|
2002-11-11 14:08:45 +00:00
|
|
|
/* Copy list structure
|
|
|
|
*/
|
|
|
|
extern scconf_list *scconf_list_copy(const scconf_list * src, scconf_list ** dst);
|
|
|
|
|
2002-03-31 11:30:54 +00:00
|
|
|
/* Free list structure
|
|
|
|
*/
|
|
|
|
extern void scconf_list_destroy(scconf_list * list);
|
|
|
|
|
2002-03-25 20:10:44 +00:00
|
|
|
/* Return the length of an list array
|
|
|
|
*/
|
|
|
|
extern int scconf_list_array_length(const scconf_list * list);
|
2002-03-20 23:21:22 +00:00
|
|
|
|
2002-03-25 20:10:44 +00:00
|
|
|
/* Return the combined length of the strings on all arrays
|
|
|
|
*/
|
|
|
|
extern int scconf_list_strings_length(const scconf_list * list);
|
2002-03-20 23:21:22 +00:00
|
|
|
|
2002-03-25 20:10:44 +00:00
|
|
|
/* Return an allocated string that contains all
|
|
|
|
* the strings in a list separated by the filler
|
2002-03-31 11:30:54 +00:00
|
|
|
* The filler can be NULL
|
2002-03-25 20:10:44 +00:00
|
|
|
*/
|
|
|
|
extern char *scconf_list_strdup(const scconf_list * list, const char *filler);
|
2002-03-20 23:21:22 +00:00
|
|
|
|
2003-11-20 14:13:08 +00:00
|
|
|
/* Returns an allocated array of const char *pointers to
|
|
|
|
* list elements.
|
|
|
|
* Last pointer is NULL
|
|
|
|
* Array must be freed, but pointers to strings belong to scconf_list
|
|
|
|
*/
|
|
|
|
extern const char **scconf_list_toarray(const scconf_list * list);
|
|
|
|
|
2002-03-20 23:21:22 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|