blob: 2d49adaef3c442db734b1c45931410c69dd42787 [file] [log] [blame]
kasunath2ebe82f2023-01-31 13:59:22 -08001#include "bej_tree.h"
2
3static void bejTreeInitParent(struct RedfishPropertyParent* node,
4 const char* name, enum BejPrincipalDataType type)
5{
6 node->nodeAttr.name = name;
7 node->nodeAttr.format.principalDataType = type;
8 node->nodeAttr.format.deferredBinding = 0;
9 node->nodeAttr.format.readOnlyProperty = 0;
10 node->nodeAttr.format.nullableProperty = 0;
Brian Maa46f9852024-09-27 16:57:20 +080011 node->nodeAttr.format.reserved = 0;
kasunath2ebe82f2023-01-31 13:59:22 -080012 node->nodeAttr.sibling = NULL;
13 node->nChildren = 0;
14 node->firstChild = NULL;
15 node->lastChild = NULL;
16}
17
18void bejTreeInitSet(struct RedfishPropertyParent* node, const char* name)
19{
20 bejTreeInitParent(node, name, bejSet);
21}
22
23void bejTreeInitArray(struct RedfishPropertyParent* node, const char* name)
24{
25 bejTreeInitParent(node, name, bejArray);
26}
27
28void bejTreeInitPropertyAnnotated(struct RedfishPropertyParent* node,
29 const char* name)
30{
31 bejTreeInitParent(node, name, bejPropertyAnnotation);
32}
33
kasunathdc3f2142023-01-31 15:09:10 -080034bool bejTreeIsParentType(struct RedfishPropertyNode* node)
35{
36 return node->format.principalDataType == bejSet ||
37 node->format.principalDataType == bejArray ||
38 node->format.principalDataType == bejPropertyAnnotation;
39}
40
kasunath2ebe82f2023-01-31 13:59:22 -080041static void bejTreeInitChildNode(struct RedfishPropertyLeaf* node,
42 const char* name,
43 enum BejPrincipalDataType type)
44{
45 node->nodeAttr.name = name;
46 node->nodeAttr.format.principalDataType = type;
47 node->nodeAttr.format.deferredBinding = 0;
48 node->nodeAttr.format.readOnlyProperty = 0;
49 node->nodeAttr.format.nullableProperty = 0;
Brian Maa46f9852024-09-27 16:57:20 +080050 node->nodeAttr.format.reserved = 0;
kasunath2ebe82f2023-01-31 13:59:22 -080051 node->nodeAttr.sibling = NULL;
52}
53
kasunathbde1bfa2023-08-18 00:38:36 -070054void bejTreeAddNull(struct RedfishPropertyParent* parent,
55 struct RedfishPropertyLeafNull* child, const char* name)
56{
57 bejTreeInitChildNode((struct RedfishPropertyLeaf*)child, name, bejNull);
58 bejTreeLinkChildToParent(parent, child);
59}
60
kasunath2ebe82f2023-01-31 13:59:22 -080061void bejTreeAddInteger(struct RedfishPropertyParent* parent,
62 struct RedfishPropertyLeafInt* child, const char* name,
63 int64_t value)
64{
65 bejTreeInitChildNode((struct RedfishPropertyLeaf*)child, name, bejInteger);
66 child->value = value;
67 bejTreeLinkChildToParent(parent, child);
68}
69
70void bejTreeSetInteger(struct RedfishPropertyLeafInt* node, int64_t newValue)
71{
72 node->value = newValue;
73}
74
75void bejTreeAddEnum(struct RedfishPropertyParent* parent,
76 struct RedfishPropertyLeafEnum* child, const char* name,
77 const char* value)
78{
79 bejTreeInitChildNode((struct RedfishPropertyLeaf*)child, name, bejEnum);
80 child->value = value;
81 bejTreeLinkChildToParent(parent, child);
82}
83
kasunathdc3f2142023-01-31 15:09:10 -080084void bejTreeAddString(struct RedfishPropertyParent* parent,
85 struct RedfishPropertyLeafString* child, const char* name,
86 const char* value)
87{
88 bejTreeInitChildNode((struct RedfishPropertyLeaf*)child, name, bejString);
89 child->value = value;
90 bejTreeLinkChildToParent(parent, child);
91}
92
93void bejTreeAddReal(struct RedfishPropertyParent* parent,
94 struct RedfishPropertyLeafReal* child, const char* name,
95 double value)
96{
97 bejTreeInitChildNode((struct RedfishPropertyLeaf*)child, name, bejReal);
98 child->value = value;
99 bejTreeLinkChildToParent(parent, child);
100}
101
102void bejTreeSetReal(struct RedfishPropertyLeafReal* node, double newValue)
103{
104 node->value = newValue;
105}
106
107void bejTreeAddBool(struct RedfishPropertyParent* parent,
108 struct RedfishPropertyLeafBool* child, const char* name,
109 bool value)
110{
111 bejTreeInitChildNode((struct RedfishPropertyLeaf*)child, name, bejBoolean);
112 child->value = value;
113 bejTreeLinkChildToParent(parent, child);
114}
115
kasunath2ebe82f2023-01-31 13:59:22 -0800116void bejTreeLinkChildToParent(struct RedfishPropertyParent* parent, void* child)
117{
118 // A new node is added at the end of the list.
119 if (parent->firstChild == NULL)
120 {
121 parent->firstChild = child;
122 }
123 else
124 {
125 struct RedfishPropertyNode* lastChild = parent->lastChild;
126 lastChild->sibling = child;
127 }
128 parent->lastChild = child;
129 parent->nChildren += 1;
130}
kasunathdc3f2142023-01-31 15:09:10 -0800131
132void bejTreeUpdateNodeFlags(struct RedfishPropertyNode* node,
133 bool deferredBinding, bool readOnlyProperty,
134 bool nullableProperty)
135{
136 node->format.deferredBinding = deferredBinding;
137 node->format.readOnlyProperty = readOnlyProperty;
138 node->format.nullableProperty = nullableProperty;
139}
kasunath99bd6c92023-07-30 18:19:00 -0700140
141void* bejParentGoToNextChild(struct RedfishPropertyParent* parent,
142 struct RedfishPropertyNode* currentChild)
143{
144 if (parent == NULL || currentChild == NULL)
145 {
146 return NULL;
147 }
148
149 parent->metaData.nextChildIndex += 1;
150 parent->metaData.nextChild = currentChild->sibling;
151 return currentChild->sibling;
152}