GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
gp_data_storage.h
1// SPDX-License-Identifier: LGPL-2.1-or-later
2/*
3 * Copyright (C) 2009-2014 Cyril Hrubis <metan@ucw.cz>
4 */
5
6/*
7
8 Data storage is an abstraction for recusive, typed, name -> value pairs.
9
10 It's designed to be common layer for metadata such as exif, etc.
11
12 */
13
14#ifndef LOADERS_DATA_STORAGE_H
15#define LOADERS_DATA_STORAGE_H
16
17enum gp_data_type {
18 GP_DATA_INT,
19 GP_DATA_STRING,
20 GP_DATA_DOUBLE,
21 GP_DATA_RATIONAL,
22 GP_DATA_DICT,
23};
24
25struct gp_data_rational {
26 long num;
27 long den;
28};
29
30typedef struct gp_storage gp_storage;
31typedef struct gp_data_dict gp_data_dict;
32
33union gp_data_value {
34 long i;
35 double d;
36 const char *str;
37 struct gp_data_rational rat;
38 gp_data_dict *dict;
39};
40
41typedef struct gp_data_node {
42 enum gp_data_type type;
43 union gp_data_value value;
44 const char *id;
45 struct gp_data_node *next;
46} gp_data_node;
47
48/*
49 * Creates a data storage
50 */
51gp_storage *gp_storage_create(void);
52
53/*
54 * Destroys a data storage and all its data
55 */
56void gp_storage_destroy(gp_storage *self);
57
58/*
59 * Returns storage root node.
60 */
61gp_data_node *gp_storage_root(gp_storage *self);
62
63/*
64 * Returns first node in a dict node list.
65 */
66gp_data_node *gp_data_dict_first(gp_data_node *node);
67
68/*
69 * Clears all data in storage.
70 */
71void gp_storage_clear(gp_storage *self);
72
73/*
74 * Prints a data
75 */
76void gp_data_print(const gp_data_node *node);
77
78static inline void gp_storage_print(gp_storage *self)
79{
80 gp_data_print(gp_storage_root(self));
81}
82
83const char *gp_data_type_name(enum gp_data_type type);
84
85/*
86 * Returns subnode of a given id (or NULL) starting at node.
87 *
88 * If node is NULL storage root is used.
89 */
90gp_data_node *gp_storage_get(gp_storage *self,
91 gp_data_node *node, const char *id);
92
93/*
94 * Returns data node by a path in the data storage.
95 *
96 * Example path: "/Exif/Orientation"
97 *
98 * The path works like filesystem path. The equivalent to working
99 * directory is the node pointer.
100 */
101gp_data_node *gp_storage_get_by_path(gp_storage *self, gp_data_node *node,
102 const char *path);
103
104/*
105 * Adds data into a dict.
106 *
107 * If node is NULL, data storage root is used.
108 *
109 * The data holds information to be used for the node addition.
110 *
111 * Returns newly created node or NULL in case of failure.
112 */
113gp_data_node *gp_storage_add(gp_storage *self,
114 gp_data_node *node, gp_data_node *data);
115
116/*
117 * Shortcuts for adding a int/string/double/rational
118 *
119 * If dict is NULL data is added to the root.
120 */
121gp_data_node *gp_storage_add_int(gp_storage *self, gp_data_node *node,
122 const char *id, long i);
123
124gp_data_node *gp_storage_add_string(gp_storage *self, gp_data_node *node,
125 const char *id, const char *str);
126
127gp_data_node *gp_storage_add_double(gp_storage *self, gp_data_node *node,
128 const char *id, double d);
129
130gp_data_node *gp_storage_add_rational(gp_storage *self, gp_data_node *node,
131 const char *id, long num, long den);
132
133gp_data_node *gp_storage_add_dict(gp_storage *self, gp_data_node *node,
134 const char *id);
135
136#endif /* LOADERS_GP_DATA_STORAGE_H */