blob: 53511ebd829bed7c839fdee161c7ac6cb190f8ac [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;
11 node->nodeAttr.sibling = NULL;
12 node->nChildren = 0;
13 node->firstChild = NULL;
14 node->lastChild = NULL;
15}
16
17void bejTreeInitSet(struct RedfishPropertyParent* node, const char* name)
18{
19 bejTreeInitParent(node, name, bejSet);
20}
21
22void bejTreeInitArray(struct RedfishPropertyParent* node, const char* name)
23{
24 bejTreeInitParent(node, name, bejArray);
25}
26
27void bejTreeInitPropertyAnnotated(struct RedfishPropertyParent* node,
28 const char* name)
29{
30 bejTreeInitParent(node, name, bejPropertyAnnotation);
31}
32
kasunathdc3f2142023-01-31 15:09:10 -080033bool bejTreeIsParentType(struct RedfishPropertyNode* node)
34{
35 return node->format.principalDataType == bejSet ||
36 node->format.principalDataType == bejArray ||
37 node->format.principalDataType == bejPropertyAnnotation;
38}
39
kasunath2ebe82f2023-01-31 13:59:22 -080040static void bejTreeInitChildNode(struct RedfishPropertyLeaf* node,
41 const char* name,
42 enum BejPrincipalDataType type)
43{
44 node->nodeAttr.name = name;
45 node->nodeAttr.format.principalDataType = type;
46 node->nodeAttr.format.deferredBinding = 0;
47 node->nodeAttr.format.readOnlyProperty = 0;
48 node->nodeAttr.format.nullableProperty = 0;
49 node->nodeAttr.sibling = NULL;
50}
51
52void bejTreeAddInteger(struct RedfishPropertyParent* parent,
53 struct RedfishPropertyLeafInt* child, const char* name,
54 int64_t value)
55{
56 bejTreeInitChildNode((struct RedfishPropertyLeaf*)child, name, bejInteger);
57 child->value = value;
58 bejTreeLinkChildToParent(parent, child);
59}
60
61void bejTreeSetInteger(struct RedfishPropertyLeafInt* node, int64_t newValue)
62{
63 node->value = newValue;
64}
65
66void bejTreeAddEnum(struct RedfishPropertyParent* parent,
67 struct RedfishPropertyLeafEnum* child, const char* name,
68 const char* value)
69{
70 bejTreeInitChildNode((struct RedfishPropertyLeaf*)child, name, bejEnum);
71 child->value = value;
72 bejTreeLinkChildToParent(parent, child);
73}
74
kasunathdc3f2142023-01-31 15:09:10 -080075void bejTreeAddString(struct RedfishPropertyParent* parent,
76 struct RedfishPropertyLeafString* child, const char* name,
77 const char* value)
78{
79 bejTreeInitChildNode((struct RedfishPropertyLeaf*)child, name, bejString);
80 child->value = value;
81 bejTreeLinkChildToParent(parent, child);
82}
83
84void bejTreeAddReal(struct RedfishPropertyParent* parent,
85 struct RedfishPropertyLeafReal* child, const char* name,
86 double value)
87{
88 bejTreeInitChildNode((struct RedfishPropertyLeaf*)child, name, bejReal);
89 child->value = value;
90 bejTreeLinkChildToParent(parent, child);
91}
92
93void bejTreeSetReal(struct RedfishPropertyLeafReal* node, double newValue)
94{
95 node->value = newValue;
96}
97
98void bejTreeAddBool(struct RedfishPropertyParent* parent,
99 struct RedfishPropertyLeafBool* child, const char* name,
100 bool value)
101{
102 bejTreeInitChildNode((struct RedfishPropertyLeaf*)child, name, bejBoolean);
103 child->value = value;
104 bejTreeLinkChildToParent(parent, child);
105}
106
kasunath2ebe82f2023-01-31 13:59:22 -0800107void bejTreeLinkChildToParent(struct RedfishPropertyParent* parent, void* child)
108{
109 // A new node is added at the end of the list.
110 if (parent->firstChild == NULL)
111 {
112 parent->firstChild = child;
113 }
114 else
115 {
116 struct RedfishPropertyNode* lastChild = parent->lastChild;
117 lastChild->sibling = child;
118 }
119 parent->lastChild = child;
120 parent->nChildren += 1;
121}
kasunathdc3f2142023-01-31 15:09:10 -0800122
123void bejTreeUpdateNodeFlags(struct RedfishPropertyNode* node,
124 bool deferredBinding, bool readOnlyProperty,
125 bool nullableProperty)
126{
127 node->format.deferredBinding = deferredBinding;
128 node->format.readOnlyProperty = readOnlyProperty;
129 node->format.nullableProperty = nullableProperty;
130}
kasunath99bd6c92023-07-30 18:19:00 -0700131
132void* bejParentGoToNextChild(struct RedfishPropertyParent* parent,
133 struct RedfishPropertyNode* currentChild)
134{
135 if (parent == NULL || currentChild == NULL)
136 {
137 return NULL;
138 }
139
140 parent->metaData.nextChildIndex += 1;
141 parent->metaData.nextChild = currentChild->sibling;
142 return currentChild->sibling;
143}