1 /* recdb.h - recursive/record database implementation 2 * Copyright 2000-2004 srvx Development Team 3 * 4 * This file is part of srvx. 5 * 6 * srvx is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with srvx; if not, write to the Free Software Foundation, 18 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 19 */ 20 21 #ifndef _recdb_h 22 #define _recdb_h 23 24 #include "common.h" 25 #include "dict.h" 26 27 enum recdb_type { 28 RECDB_INVALID, 29 RECDB_QSTRING, 30 RECDB_OBJECT, 31 RECDB_STRING_LIST 32 }; 33 34 struct record_data { 35 enum recdb_type type; 36 union { 37 char *qstring; 38 dict_t object; 39 struct string_list *slist; 40 void *whatever; 41 } d; 42 }; 43 44 #define SET_RECORD_QSTRING(rec, qs) do { const char *s = (qs); (rec)->type = RECDB_QSTRING; (rec)->d.qstring = (s) ? strdup(s) : NULL; } while (0) 45 #define GET_RECORD_QSTRING(rec) (((rec)->type == RECDB_QSTRING) ? (rec)->d.qstring : 0) 46 #define SET_RECORD_OBJECT(rec, obj) do { (rec)->type = RECDB_OBJECT; (rec)->d.object = (obj); } while (0) 47 #define GET_RECORD_OBJECT(rec) (((rec)->type == RECDB_OBJECT) ? (rec)->d.object : 0) 48 #define SET_RECORD_STRING_LIST(rec, sl) do { (rec)->type = RECDB_STRING_LIST; (rec)->d.slist = (sl); } while (0) 49 #define GET_RECORD_STRING_LIST(rec) (((rec)->type == RECDB_STRING_LIST) ? (rec)->d.slist : 0) 50 51 struct string_list { 52 unsigned int used, size; 53 char **list; 54 }; 55 void string_list_append(struct string_list *slist, char *string); 56 struct string_list *string_list_copy(struct string_list *orig); 57 void string_list_sort(struct string_list *slist); 58 #define string_list_delete(slist, n) (free((slist)->list[n]), (slist)->list[n] = (slist)->list[--(slist)->used]) 59 60 /* allocation functions */ 61 struct string_list *alloc_string_list(int size); 62 struct record_data *alloc_record_data_qstring(const char *string); 63 struct record_data *alloc_record_data_object(dict_t obj); 64 struct record_data *alloc_record_data_string_list(struct string_list *slist); 65 dict_t alloc_database(void); 66 #define alloc_object() alloc_database() 67 68 /* misc operations */ 69 /* note: once you give a string list a string, it frees it automatically */ 70 struct record_data *database_get_path(dict_t db, const char *path); 71 void *database_get_data(dict_t db, const char *path, enum recdb_type type); 72 73 /* freeing data */ 74 void free_string_list(struct string_list *slist); 75 void free_record_data(void *rdata); 76 #define free_object(obj) dict_delete(obj) 77 #define free_database(db) dict_delete(db) 78 79 /* parsing stuff from disk */ 80 const char *parse_record(const char *text, char **pname, struct record_data **prd); 81 dict_t parse_database(const char *filename); 82 83 #endif 84