1 /*
2  * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * on the rights to use, copy, modify, merge, publish, distribute, sub
8  * license, and/or sell copies of the Software, and to permit persons to whom
9  * the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21  * USE OR OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *      Jerome Glisse
25  */
26 #ifndef BOF_H
27 #define BOF_H
28 
29 #include <stdio.h>
30 #include <stdint.h>
31 
32 #define BOF_TYPE_STRING		0
33 #define BOF_TYPE_NULL		1
34 #define BOF_TYPE_BLOB		2
35 #define BOF_TYPE_OBJECT		3
36 #define BOF_TYPE_ARRAY		4
37 #define BOF_TYPE_INT32		5
38 
39 struct bof;
40 
41 typedef struct bof {
42 	struct bof	**array;
43 	unsigned	centry;
44 	unsigned	nentry;
45 	unsigned	refcount;
46 	FILE		*file;
47 	uint32_t	type;
48 	uint32_t	size;
49 	uint32_t	array_size;
50 	void		*value;
51 	long		offset;
52 } bof_t;
53 
54 extern int bof_file_flush(bof_t *root);
55 extern bof_t *bof_file_new(const char *filename);
56 extern int bof_object_dump(bof_t *object, const char *filename);
57 
58 /* object */
59 extern bof_t *bof_object(void);
60 extern bof_t *bof_object_get(bof_t *object, const char *keyname);
61 extern int bof_object_set(bof_t *object, const char *keyname, bof_t *value);
62 /* array */
63 extern bof_t *bof_array(void);
64 extern int bof_array_append(bof_t *array, bof_t *value);
65 extern bof_t *bof_array_get(bof_t *bof, unsigned i);
66 extern unsigned bof_array_size(bof_t *bof);
67 /* blob */
68 extern bof_t *bof_blob(unsigned size, void *value);
69 extern unsigned bof_blob_size(bof_t *bof);
70 extern void *bof_blob_value(bof_t *bof);
71 /* string */
72 extern bof_t *bof_string(const char *value);
73 /* int32 */
74 extern bof_t *bof_int32(int32_t value);
75 extern int32_t bof_int32_value(bof_t *bof);
76 /* common functions */
77 extern void bof_decref(bof_t *bof);
78 extern void bof_incref(bof_t *bof);
79 extern bof_t *bof_load_file(const char *filename);
80 extern int bof_dump_file(bof_t *bof, const char *filename);
81 extern void bof_print(bof_t *bof);
82 
bof_is_object(bof_t * bof)83 static inline int bof_is_object(bof_t *bof){return (bof->type == BOF_TYPE_OBJECT);}
bof_is_blob(bof_t * bof)84 static inline int bof_is_blob(bof_t *bof){return (bof->type == BOF_TYPE_BLOB);}
bof_is_null(bof_t * bof)85 static inline int bof_is_null(bof_t *bof){return (bof->type == BOF_TYPE_NULL);}
bof_is_int32(bof_t * bof)86 static inline int bof_is_int32(bof_t *bof){return (bof->type == BOF_TYPE_INT32);}
bof_is_array(bof_t * bof)87 static inline int bof_is_array(bof_t *bof){return (bof->type == BOF_TYPE_ARRAY);}
bof_is_string(bof_t * bof)88 static inline int bof_is_string(bof_t *bof){return (bof->type == BOF_TYPE_STRING);}
89 
90 #endif
91