Initial import (again)
[srvx.git] / src / recdb.h
1 /* recdb.h - recursive/record database implementation
2  * Copyright 2000-2004 srvx Development Team
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 2 of the License, or
7  * (at your option) any later version.  Important limitations are
8  * listed in the COPYING file that accompanies this software.
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, email srvx-maintainers@srvx.net.
17  */
18
19 #ifndef _recdb_h
20 #define _recdb_h
21
22 #include "common.h"
23 #include "dict.h"
24
25 enum recdb_type {
26     RECDB_INVALID,
27     RECDB_QSTRING,
28     RECDB_OBJECT,
29     RECDB_STRING_LIST
30 };
31
32 struct record_data {
33     enum recdb_type type;
34     union {
35         char *qstring;
36         dict_t object;
37         struct string_list *slist;
38         void *whatever;
39     } d;
40 };
41
42 #define SET_RECORD_QSTRING(rec, qs) do { const char *s = (qs); (rec)->type = RECDB_QSTRING; (rec)->d.qstring = (s) ? strdup(s) : NULL; } while (0)
43 #define GET_RECORD_QSTRING(rec) (((rec)->type == RECDB_QSTRING) ? (rec)->d.qstring : 0)
44 #define SET_RECORD_OBJECT(rec, obj) do { (rec)->type = RECDB_OBJECT; (rec)->d.object = (obj); } while (0)
45 #define GET_RECORD_OBJECT(rec) (((rec)->type == RECDB_OBJECT) ? (rec)->d.object : 0)
46 #define SET_RECORD_STRING_LIST(rec, sl) do { (rec)->type = RECDB_STRING_LIST; (rec)->d.slist = (sl); } while (0)
47 #define GET_RECORD_STRING_LIST(rec) (((rec)->type == RECDB_STRING_LIST) ? (rec)->d.slist : 0)
48
49 struct string_list {
50     unsigned int used, size;
51     char **list;
52 };
53 void string_list_append(struct string_list *slist, char *string);
54 struct string_list *string_list_copy(struct string_list *orig);
55 void string_list_sort(struct string_list *slist);
56 #define string_list_delete(slist, n) (free((slist)->list[n]), (slist)->list[n] = (slist)->list[--(slist)->used])
57
58 /* allocation functions */
59 struct string_list *alloc_string_list(int size);
60 struct record_data *alloc_record_data_qstring(const char *string);
61 struct record_data *alloc_record_data_object(dict_t obj);
62 struct record_data *alloc_record_data_string_list(struct string_list *slist);
63 dict_t alloc_database(void);
64 #define alloc_object() alloc_database()
65
66 /* misc operations */
67 /* note: once you give a string list a string, it frees it automatically */
68 struct record_data *database_get_path(dict_t db, const char *path);
69 void *database_get_data(dict_t db, const char *path, enum recdb_type type);
70
71 /* freeing data */
72 void free_string_list(struct string_list *slist);
73 void free_record_data(void *rdata);
74 #define free_object(obj) dict_delete(obj)
75 #define free_database(db) dict_delete(db)
76
77 /* parsing stuff from disk */
78 const char *parse_record(const char *text, char **pname, struct record_data **prd);
79 dict_t parse_database(const char *filename);
80
81 #endif