profile.h
Go to the documentation of this file.
1 /* vim: set ts=2 expandtab: */
2 /**
3  * @file profile.h
4  * @brief a key-value store for loading configuration information
5  *
6  * @author Josh King (jheretic), jking@chambana.net
7  *
8  * @internal
9  * Created 03/07/2013
10  * Revision $Id: doxygen.commotion.templates,v 0.1 2013/01/01 09:00:00 jheretic Exp $
11  * Compiler gcc/g++
12  * Company The Open Technology Institute
13  * Copyright Copyright (c) 2013, Josh King
14  *
15  * This file is part of Commotion, Copyright (c) 2013, Josh King
16  *
17  * Commotion is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU Affero General Public License as
19  * published by the Free Software Foundation, either version 3 of the
20  * License, or (at your option) any later version.
21  *
22  * Commotion is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Affero General Public License for more details.
26  *
27  * You should have received a copy of the GNU Affero General Public License
28  * along with Commotion. If not, see <http://www.gnu.org/licenses/>.
29  *
30  * =====================================================================================
31  */
32 
33 #ifndef _PROFILE_H
34 #define _PROFILE_H
35 
36 #include <stdlib.h>
37 #include <stddef.h>
38 #include "obj.h"
39 
40 #define SCHEMA(N) static int schema_##N(co_obj_t *self, co_obj_t **output, co_obj_t *params)
41 
42 #define SCHEMA_ADD(K, V) ({ \
43  co_obj_t *val = co_str8_create(V, sizeof(V), 0); \
44  if (!co_tree_insert(self, K, sizeof(K), val)) \
45  co_obj_free(val); \
46  })
47 
48 #define SCHEMA_REGISTER(N) co_schema_register(schema_##N)
49 
50 #define SCHEMA_GLOBAL(N) co_schema_register_global(schema_##N)
51 
52 typedef struct co_cbptr_t co_cbptr_t;
53 
54 /**
55  * @struct co_cbptr_t a data structure wrapping a callback pointer
56  */
57 struct co_cbptr_t {
58  co_obj_t _header;
59  uint8_t _exttype;
60  uint8_t _len;
61  co_cb_t cb;
62 } __attribute__((packed));
63 
64 typedef struct co_profile_t co_profile_t;
65 
66 /**
67  * @struct co_profile_t a linked list for profile structs
68  */
69 struct co_profile_t {
70  co_obj_t _header;
71  uint8_t _exttype;
72  uint8_t _len;
73  co_obj_t *name; /**< command name */
74  co_obj_t *data;
75 } __attribute__((packed));
76 
77 /**
78  * @brief register a callback for populating a profile schema
79  * @param cb schema callback
80  */
81 int co_schema_register(co_cb_t cb);
82 
83 /**
84  * @brief register a callback for populating global profile
85  * @param cb schema callback
86  */
87 int co_schema_register_global(co_cb_t cb);
88 
89 /**
90  * @brief load schemas for specified profile
91  * @param profile profile to load schemas for
92  */
93 int co_schemas_load(co_obj_t *profile);
94 
95 /**
96  * @brief create global profiles list
97  * @param index_size size of index for profiles list (16 or 32 bits)
98  */
99 int co_profiles_create(const size_t index_size);
100 
101 /**
102  * @brief creates a list of available profiles
103  */
104 int co_profiles_init(const size_t index_size);
105 
106 /**
107  * @brief removes the list of available profiles
108  */
109 void co_profiles_shutdown(void);
110 
111 /**
112  * @brief imports available profiles from profiles directory
113  * @param path file path to the profiles directory
114  */
115 int co_profile_import_files(const char *path);
116 
117 /**
118  * @brief imports global profile
119  * @param path file path to the global profile
120  */
121 int co_profile_import_global(const char *path);
122 
123 /**
124  * @brief searches the profile list for a specified profile
125  * @param name profile name (search key)
126  */
128 
129 /**
130  * @brief returns the global profile
131  */
133 
134 /**
135  * @brief deletes the global profile
136  */
137 void co_profile_delete_global(void);
138 
139 /**
140  * @brief adds a new profile
141  * @param name profile name
142  * @param nlen length of profile name
143  */
144 int co_profile_add(const char *name, const size_t nlen);
145 
146 /**
147  * @brief removes profile with given name
148  * @param name profile name
149  * @param nlen length of profile name
150  */
151 int co_profile_remove(const char *name, const size_t nlen);
152 
153 /**
154  * @brief dumps profile data
155  * @param profile profile struct
156  */
157 void co_profile_dump(co_obj_t *profile);
158 
159 /**
160  * @brief returns the key value (if an int) from the profile. If no value set, returns the default value
161  * @param profile profile struct
162  * @param key key in profile
163  * @param klen key length
164  */
165 co_obj_t *co_profile_get(co_obj_t *profile, const co_obj_t *key);
166 
167 /**
168  * @brief sets a specified profile value (if a string)
169  * @param profile profile struct
170  * @param key key in profile
171  * @param klen key length
172  * @param value key's value
173  * @param vlen value length
174  */
175 int co_profile_set_str(co_obj_t *profile, const char *key, const size_t klen, const char *value, const size_t vlen);
176 
177 /**
178  * @brief returns the key value (if a string) from the profile. If no value set, returns the default value
179  * @param profile profile struct
180  * @param output output buffer
181  * @param key key in profile
182  * @param klen key length
183  */
184 size_t co_profile_get_str(co_obj_t *profile, char **output, const char *key, const size_t klen);
185 
186 /**
187  * @brief sets a specified profile value (if an int)
188  * @param profile profile struct
189  * @param key key in profile
190  * @param klen key length
191  * @param value key's value
192  */
193 int co_profile_set_int(co_obj_t *profile, const char *key, const size_t klen, const signed long value);
194 
195 /**
196  * @brief returns the key value (if an int) from the profile. If no value set, returns the default value
197  * @param profile profile struct
198  * @param key key in profile
199  * @param klen key length
200  */
201 signed long co_profile_get_int(co_obj_t *profile, const char *key, const size_t klen);
202 
203 /**
204  * @brief sets a specified profile value (if an unsigned int)
205  * @param profile profile struct
206  * @param key key in profile
207  * @param klen key length
208  * @param value key's value
209  */
210 int co_profile_set_uint(co_obj_t *profile, const char *key, const size_t klen, const unsigned long value);
211 
212 /**
213  * @brief returns the key value (if an unsigned int) from the profile. If no value set, returns the default value
214  * @param profile profile struct
215  * @param key key in profile
216  * @param klen key length
217  */
218 unsigned long co_profile_get_uint(co_obj_t *profile, const char *key, const size_t klen);
219 
220 /**
221  * @brief sets a specified profile value (if a float)
222  * @param profile profile struct
223  * @param key key in profile
224  * @param klen key length
225  * @param value key's value
226  */
227 int co_profile_set_float(co_obj_t *profile, const char *key, const size_t klen, const double value);
228 
229 /**
230  * @brief returns the key value (if a float) from the profile. If no value set, returns the default value
231  * @param profile profile struct
232  * @param key key in profile
233  * @param klen key length
234  */
235 double co_profile_get_float(co_obj_t *profile, const char *key, const size_t klen);
236 
237 /**
238  * @brief exports a profile in memory to a file
239  * @param profile profile struct
240  * @param path export path
241  */
242 int co_profile_export_file(co_obj_t *profile, const char *path);
243 
244 /**
245  * @brief processes all loaded profiles with specified iterator function
246  * @param iter iterator function callback
247  * @param context additional parameters for iterator
248  */
249 co_obj_t *co_profiles_process(co_iter_t iter, void *context);
250 #endif
Commotion object model.
co_obj_t * name
Definition: profile.h:73
int co_schema_register_global(co_cb_t cb)
register a callback for populating global profile
Definition: profile.c:80
int co_profile_set_int(co_obj_t *profile, const char *key, const size_t klen, const signed long value)
sets a specified profile value (if an int)
Definition: profile.c:510
void co_profiles_shutdown(void)
removes the list of available profiles
Definition: profile.c:112
co_obj_t * co_profile_get(co_obj_t *profile, const co_obj_t *key)
returns the key value (if an int) from the profile. If no value set, returns the default value ...
Definition: profile.c:466
co_obj_t * co_profiles_process(co_iter_t iter, void *context)
processes all loaded profiles with specified iterator function
Definition: profile.c:697
int co_profile_import_files(const char *path)
imports available profiles from profiles directory
Definition: profile.c:348
int co_profile_set_float(co_obj_t *profile, const char *key, const size_t klen, const double value)
sets a specified profile value (if a float)
Definition: profile.c:570
void co_profile_delete_global(void)
deletes the global profile
Definition: profile.c:635
Definition: profile.h:57
co_obj_t * co_profile_find(co_obj_t *name)
searches the profile list for a specified profile
Definition: profile.c:624
co_obj_t * co_profile_global(void)
returns the global profile
Definition: profile.c:642
int co_profile_import_global(const char *path)
imports global profile
Definition: profile.c:358
Definition: obj.h:131
unsigned long co_profile_get_uint(co_obj_t *profile, const char *key, const size_t klen)
returns the key value (if an unsigned int) from the profile. If no value set, returns the default val...
Definition: profile.c:553
int co_profile_set_uint(co_obj_t *profile, const char *key, const size_t klen, const unsigned long value)
sets a specified profile value (if an unsigned int)
Definition: profile.c:540
int co_profiles_init(const size_t index_size)
creates a list of available profiles
Definition: profile.c:170
size_t co_profile_get_str(co_obj_t *profile, char **output, const char *key, const size_t klen)
returns the key value (if a string) from the profile. If no value set, returns the default value ...
Definition: profile.c:495
int co_profile_set_str(co_obj_t *profile, const char *key, const size_t klen, const char *value, const size_t vlen)
sets a specified profile value (if a string)
Definition: profile.c:482
double co_profile_get_float(co_obj_t *profile, const char *key, const size_t klen)
returns the key value (if a float) from the profile. If no value set, returns the default value ...
Definition: profile.c:583
int co_profile_add(const char *name, const size_t nlen)
adds a new profile
Definition: profile.c:157
int co_profile_remove(const char *name, const size_t nlen)
removes profile with given name
Definition: profile.c:142
signed long co_profile_get_int(co_obj_t *profile, const char *key, const size_t klen)
returns the key value (if an int) from the profile. If no value set, returns the default value ...
Definition: profile.c:523
co_obj_t * name
Definition: profile.h:701
int co_profiles_create(const size_t index_size)
create global profiles list
void co_profile_dump(co_obj_t *profile)
dumps profile data
int co_schema_register(co_cb_t cb)
register a callback for populating a profile schema
Definition: profile.c:70
Definition: profile.h:69
int co_schemas_load(co_obj_t *profile)
load schemas for specified profile
int co_profile_export_file(co_obj_t *profile, const char *path)
exports a profile in memory to a file
Definition: profile.c:678