-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.h
130 lines (103 loc) · 3.14 KB
/
database.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#ifndef DATABASE_H
#define DATABASE_H
#include <setjmp.h>
struct database;
struct database_object;
typedef void (db_read_f)(struct database *);
typedef int (db_write_f)(struct database *);
enum db_source
{
SRC_FILE,
SRC_MMAP
};
struct database
{
char *name;
char *filename;
char *tmp_filename;
db_read_f *read_func;
db_write_f *write_func;
unsigned int indent;
unsigned int line;
unsigned int line_pos;
unsigned int map_pos;
enum db_source source;
size_t length;
FILE *fp;
char *map; // for mmap()
jmp_buf jbuf;
struct ptrlist *free_on_error;
struct dict *nodes;
};
enum database_type
{
DB_EMPTY,
DB_OBJECT,
DB_STRING,
DB_STRINGLIST
};
struct db_node
{
enum database_type type;
union
{
void *ptr; // since data is an union, ptr always points to the appropiate data storage
char *string;
struct dict *object;
struct stringlist *slist;
} data;
};
struct database_object
{
struct dict *current;
struct dict **stack;
unsigned int stack_size;
unsigned int stack_used;
};
enum error_codes
{
UNTERMINATED_STRING = 1,
EXPECTED_OPEN_QUOTE,
EXPECTED_OPEN_BRACE,
EXPECTED_OPEN_PAREN,
EXPECTED_COMMA,
EXPECTED_START_DATA,
EXPECTED_SEMICOLON,
EXPECTED_RECORD_DATA,
EXPECTED_COMMENT_END
};
enum ptr_types // for free_on_error ptrlist
{
PTR_STRING,
PTR_DICT,
PTR_DB_ENTRY,
PTR_STRINGLIST
};
void database_init();
void database_fini();
struct dict *database_dict();
struct dict *database_load(const char *filename); // load database from file
struct database *database_create(const char *name, db_read_f *read_func, db_write_f *write_func); // register database
void database_delete(struct database *db); // unregister and free database
void database_set_write_interval(struct database *db, time_t interval);
struct db_node *database_fetch_path(struct dict *db_nodes, const char *node_path);
void *database_fetch(struct dict *db_nodes, const char *path, enum database_type type);
int database_read(struct database *db, unsigned int free_nodes_after_read);
int database_write(struct database *db);
void database_dump(struct dict *db_nodes);
void database_free_node(struct db_node *node);
void database_begin_object(struct database *db, const char *key);
void database_end_object(struct database *db);
void database_write_long(struct database *db, const char *key, long value);
void database_write_string(struct database *db, const char *key, const char *value);
void database_write_stringlist(struct database *db, const char *key, struct stringlist *slist);
void database_write_object(struct database *db, const char *key, const struct dict *object);
struct database_object *database_obj_create();
void database_obj_free(struct database_object *dbo);
void database_obj_begin_object(struct database_object *dbo, const char *key);
void database_obj_end_object(struct database_object *dbo);
void database_obj_write_long(struct database_object *dbo, const char *key, long value);
void database_obj_write_string(struct database_object *dbo, const char *key, const char *value);
void database_obj_write_stringlist(struct database_object *dbo, const char *key, struct stringlist *slist);
struct dict *database_copy_object(const struct dict *object);
#endif