programmer's documentation
cs_tree.h
Go to the documentation of this file.
1 #ifndef __CS_TREE_H__
2 #define __CS_TREE_H__
3 
4 /*============================================================================
5  * Routines to handle a tree structure used to store data and settings
6  *============================================================================*/
7 
8 /*
9  This file is part of Code_Saturne, a general-purpose CFD tool.
10 
11  Copyright (C) 1998-2018 EDF S.A.
12 
13  This program is free software; you can redistribute it and/or modify it under
14  the terms of the GNU General Public License as published by the Free Software
15  Foundation; either version 2 of the License, or (at your option) any later
16  version.
17 
18  This program is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20  FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21  details.
22 
23  You should have received a copy of the GNU General Public License along with
24  this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
25  Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 */
27 
28 /*----------------------------------------------------------------------------*/
29 
30 #include "cs_defs.h"
31 
32 /*----------------------------------------------------------------------------
33  * Local headers
34  *----------------------------------------------------------------------------*/
35 
36 #include "cs_base.h"
37 #include "cs_log.h"
38 
39 /*----------------------------------------------------------------------------*/
40 
42 
43 /*============================================================================
44  * Macro definitions
45  *============================================================================*/
46 
47 /* Set of bit masks to tune finely the behavior of a node.
48  By default node value is assumed to be a string.
49  */
50 
51 #define CS_TREE_NODE_INTEGER (1 << 0) /* 1: value is an integer */
52 #define CS_TREE_NODE_REAL (1 << 1) /* 2: value is a cs_real_t */
53 #define CS_TREE_NODE_BOOL (1 << 2) /* 4: value is a bool */
54 
55 /*============================================================================
56  * Type definitions
57  *============================================================================*/
58 
59 typedef struct _cs_tree_node_t cs_tree_node_t;
60 
62 
63  char *name; /* name of the node */
64  char *desc; /* NULL or short description/help about this node */
65  int flag; /* metadata used to specified the node behavior */
66 
67  void *value; /* value related to this node. Cast on-the-fly */
68  int size; /* size > 1 if it is an array */
69 
70  /* Pointers to other nodes to allow an easy navigation among the tree */
71 
72  cs_tree_node_t *parent; /* Pointer to the parent node or NULL if root */
73  cs_tree_node_t *children; /* Pointer to the first children or NULL */
74  cs_tree_node_t *prev; /* Pointer to a previous node sharing the same
75  parent or NULL if this is the first children */
76  cs_tree_node_t *next; /* Pointer to a next node sharing the same parent
77  or NULL if there is no next node */
78 
79 };
80 
81 /*============================================================================
82  * Public function prototypes
83  *============================================================================*/
84 
85 /*----------------------------------------------------------------------------*/
93 /*----------------------------------------------------------------------------*/
94 
95 cs_tree_node_t *
96 cs_tree_node_create(const char *name);
97 
98 /*----------------------------------------------------------------------------*/
104 /*----------------------------------------------------------------------------*/
105 
106 void
107 cs_tree_node_free(cs_tree_node_t **pnode);
108 
109 /*----------------------------------------------------------------------------*/
116 /*----------------------------------------------------------------------------*/
117 
118 void
119 cs_tree_node_set_string_val(cs_tree_node_t *node,
120  const char *val);
121 
122 /*----------------------------------------------------------------------------*/
129 /*----------------------------------------------------------------------------*/
130 
131 void
132 cs_tree_node_set_bool(cs_tree_node_t *node,
133  bool val);
134 
135 /*----------------------------------------------------------------------------*/
143 /*----------------------------------------------------------------------------*/
144 
145 void
146 cs_tree_node_set_bool_val(cs_tree_node_t *node,
147  int size,
148  const bool *val);
149 
150 /*----------------------------------------------------------------------------*/
158 /*----------------------------------------------------------------------------*/
159 
160 void
161 cs_tree_node_set_int_val(cs_tree_node_t *node,
162  int size,
163  const int *val);
164 
165 /*----------------------------------------------------------------------------*/
174 /*----------------------------------------------------------------------------*/
175 
176 void
177 cs_tree_node_set_real_val(cs_tree_node_t *node,
178  int size,
179  const cs_real_t *val);
180 
181 /*----------------------------------------------------------------------------*/
189 /*----------------------------------------------------------------------------*/
190 
191 void
193  int depth,
194  const cs_tree_node_t *node);
195 
196 /*----------------------------------------------------------------------------*/
209 /*----------------------------------------------------------------------------*/
210 
211 cs_tree_node_t *
212 cs_tree_get_node_rw(cs_tree_node_t *root,
213  const char *path);
214 
215 /*----------------------------------------------------------------------------*/
227 /*----------------------------------------------------------------------------*/
228 
229 const cs_tree_node_t *
230 cs_tree_get_node(const cs_tree_node_t *root,
231  const char *path);
232 
233 /*----------------------------------------------------------------------------*/
245 /*----------------------------------------------------------------------------*/
246 
247 static inline const char *
248 cs_tree_get_node_string_val(const cs_tree_node_t *root,
249  const char *path)
250 {
251  const cs_tree_node_t *node = cs_tree_get_node(root, path);
252  return node->value;
253 }
254 
255 /*----------------------------------------------------------------------------*/
264 /*----------------------------------------------------------------------------*/
265 
266 cs_tree_node_t *
267 cs_tree_add_child(cs_tree_node_t *parent,
268  const char *name);
269 
270 /*----------------------------------------------------------------------------*/
281 /*----------------------------------------------------------------------------*/
282 
283 static inline cs_tree_node_t *
285  const char *name,
286  const char *val_name)
287 {
288  cs_tree_node_t *child = cs_tree_add_child(parent, name);
289  cs_tree_node_set_string_val(child, val_name);
290  return child;
291 }
292 
293 /*----------------------------------------------------------------------------*/
302 /*----------------------------------------------------------------------------*/
303 
304 cs_tree_node_t *
305 cs_tree_add_sibling(cs_tree_node_t *sibling,
306  const char *name);
307 
308 /*----------------------------------------------------------------------------*/
319 /*----------------------------------------------------------------------------*/
320 
321 static inline cs_tree_node_t *
323  const char *name,
324  bool val)
325 {
326  cs_tree_node_t *child = cs_tree_add_child(parent, name);
327  cs_tree_node_set_bool_val(child, 1, &val);
328  return child;
329 }
330 
331 /*----------------------------------------------------------------------------*/
342 /*----------------------------------------------------------------------------*/
343 
344 static inline cs_tree_node_t *
346  const char *name,
347  int val)
348 {
349  cs_tree_node_t *child = cs_tree_add_child(parent, name);
350  cs_tree_node_set_int_val(child, 1, &val);
351  return child;
352 }
353 
354 /*----------------------------------------------------------------------------*/
365 /*----------------------------------------------------------------------------*/
366 
367 static inline cs_tree_node_t *
369  const char *name,
370  cs_real_t val)
371 {
372  cs_tree_node_t *child = cs_tree_add_child(parent, name);
373  cs_tree_node_set_real_val(child, 1, &val);
374  return child;
375 }
376 
377 /*----------------------------------------------------------------------------*/
384 /*----------------------------------------------------------------------------*/
385 
386 void
387 cs_tree_free(cs_tree_node_t **proot);
388 
389 /*----------------------------------------------------------------------------*/
397 /*----------------------------------------------------------------------------*/
398 
399 void
401  int depth,
402  const cs_tree_node_t *root);
403 
404 /*----------------------------------------------------------------------------*/
405 
407 
408 #endif /* __CS_TREE_H__ */
static cs_tree_node_t * cs_tree_add_int_child(cs_tree_node_t *parent, const char *name, int val)
Create and add a node in a tree below the given parent node This node has one integer value set to va...
Definition: cs_tree.h:345
char * name
Definition: cs_tree.h:63
Definition: cs_tree.h:61
void cs_tree_node_set_string_val(cs_tree_node_t *node, const char *val)
Allocate the value member of a node and assign to it a string.
Definition: cs_tree.c:283
void cs_tree_free(cs_tree_node_t **proot)
Free a branch in a tree starting from root. If root is the real root of the tree, the whole tree is f...
Definition: cs_tree.c:684
#define BEGIN_C_DECLS
Definition: cs_defs.h:453
cs_tree_node_t * cs_tree_get_node_rw(cs_tree_node_t *root, const char *path)
Retrieve the pointer to the value member of the node. This node can be modified. This node is located...
Definition: cs_tree.c:556
const cs_tree_node_t * cs_tree_get_node(const cs_tree_node_t *root, const char *path)
Retrieve the pointer to a node in read-only access. This node is located at "path" from the given roo...
Definition: cs_tree.c:584
void cs_tree_node_set_real_val(cs_tree_node_t *node, int size, const cs_real_t *val)
Allocate the value member of a node and assign to it an array of real values.
Definition: cs_tree.c:382
int size
Definition: cs_tree.h:68
cs_tree_node_t * prev
Definition: cs_tree.h:74
void cs_tree_dump(cs_log_t log, int depth, const cs_tree_node_t *root)
Dump a cs_tree_node_t structure starting from the node "root".
Definition: cs_tree.c:717
void cs_tree_node_set_int_val(cs_tree_node_t *node, int size, const int *val)
Allocate the value member of a node and assign to it an integer.
Definition: cs_tree.c:355
void cs_tree_node_dump(cs_log_t log, int depth, const cs_tree_node_t *node)
Dump a cs_tree_node_t structure.
Definition: cs_tree.c:408
double cs_real_t
Floating-point value.
Definition: cs_defs.h:297
void cs_tree_node_set_bool_val(cs_tree_node_t *node, int size, const bool *val)
Allocate the value member of a node and assign to it a boolean.
Definition: cs_tree.c:329
cs_tree_node_t * next
Definition: cs_tree.h:76
int flag
Definition: cs_tree.h:65
void cs_tree_node_set_bool(cs_tree_node_t *node, bool val)
Allocate the value member of a node and assign to it a boolean.
Definition: cs_tree.c:306
char * desc
Definition: cs_tree.h:64
cs_log_t
Definition: cs_log.h:47
cs_tree_node_t * children
Definition: cs_tree.h:73
static cs_tree_node_t * cs_tree_add_string_child(cs_tree_node_t *parent, const char *name, const char *val_name)
Create and add a node in a tree below the given parent node This node has a string value set to val_n...
Definition: cs_tree.h:284
void * value
Definition: cs_tree.h:67
cs_tree_node_t * cs_tree_add_sibling(cs_tree_node_t *sibling, const char *name)
Create and add a node in a tree at the right of the given node.
Definition: cs_tree.c:653
static cs_tree_node_t * cs_tree_add_real_child(cs_tree_node_t *parent, const char *name, cs_real_t val)
Create and add a node in a tree below the given parent node This node has one real value set to val...
Definition: cs_tree.h:368
cs_tree_node_t * cs_tree_add_child(cs_tree_node_t *parent, const char *name)
Create and add a node in a tree below the given parent node.
Definition: cs_tree.c:609
cs_tree_node_t * cs_tree_node_create(const char *name)
Create an empty node. Only the name is assigned if given.
Definition: cs_tree.c:221
#define END_C_DECLS
Definition: cs_defs.h:454
static const char * cs_tree_get_node_string_val(const cs_tree_node_t *root, const char *path)
Retrieve the pointer to the value member of the node in case of a string. Read-only access is allowed...
Definition: cs_tree.h:248
cs_tree_node_t * parent
Definition: cs_tree.h:72
static cs_tree_node_t * cs_tree_add_bool_child(cs_tree_node_t *parent, const char *name, bool val)
Create and add a node in a tree below the given parent node This node has one boolean value set to va...
Definition: cs_tree.h:322
void cs_tree_node_free(cs_tree_node_t **pnode)
Free a cs_tree_node_t structure.
Definition: cs_tree.c:254