PhoenixYml  0.5.0
Yml parser for Phoenix
Loading...
Searching...
No Matches
vec_value_utils.cpp
Go to the documentation of this file.
1/***************************************
2 Auteur : Pierre Aubert
3 Mail : pierre.aubert@lapp.in2p3.fr
4 Licence : CeCILL-C
5****************************************/
6
7
8#include "vec_value_utils.h"
9
11
15VecValue * getLastVecValue(VecValue & vecVal, size_t depth){
16 VecVecValue & vecChildren = vecVal.getVecChild();
17 if(vecChildren.size() == 0lu || depth == 0lu){return &vecVal;}
18 else{return getLastVecValue(vecChildren.back(), depth - 1lu);}
19}
20
22
27VecValue * getParentVecValue(VecValue & vecAllVal, const std::vector<size_t> & vecIndentation, size_t currentIndentation){
28 if(vecIndentation.size() == 0lu){
29 return getLastVecValue(vecAllVal);
30 }
31 size_t depth(0lu);
32 std::vector<size_t>::const_iterator it(vecIndentation.begin());
33 bool isCurrentLower(true);
34 while(isCurrentLower && it != vecIndentation.end()){
35 isCurrentLower = *it < currentIndentation;
36 if(isCurrentLower){
37 ++depth;
38 }
39 ++it;
40 }
41 VecValue * out = getLastVecValue(vecAllVal, depth);
42 return out;
43}
44
46
49void getVecParentPtr(std::vector<VecValue*> & vecParentPtr, VecValue & vecAllVal){
50 vecParentPtr.push_back(&vecAllVal);
51 if(vecAllVal.getVecChild().size() != 0lu){
52 getVecParentPtr(vecParentPtr, vecAllVal.getVecChild().back());
53 }
54}
55
57
60std::vector<VecValue*> getVecParentPtr(VecValue & vecAllVal){
61 std::vector<VecValue*> vecParentPtr;
62 getVecParentPtr(vecParentPtr, vecAllVal);
63 return vecParentPtr;
64}
65
67
73VecValue * getParentVecValueListItem(VecValue & vecAllVal, const std::vector<size_t> & vecIndentation, size_t currentIndentation, const VecValue & child){
74 if(vecIndentation.size() == 0lu){
75 return getLastVecValue(vecAllVal);
76 }
77 size_t childIndentation(child.getIndentation());
78 VecValue * out = NULL;
79 std::vector<VecValue*> vecParentPtr = getVecParentPtr(vecAllVal);
80 std::vector<VecValue*>::const_reverse_iterator itParent(vecParentPtr.rbegin());
81 bool isCurrentGreater(true);
82 while(isCurrentGreater && itParent != vecParentPtr.rend()){
83 if((*itParent)->getType() == VecValueType::KEY && child.getType() == VecValueType::LIST_ITEM){
84 isCurrentGreater = (*itParent)->getIndentation() > childIndentation;
85// if(!isCurrentGreater){
86// std::cout << "getParentVecValueListItem : Parent("<<(*itParent)->getKey()<<").getType() : " << (*itParent)->getType() << ", ParentIndentation = "<<(*itParent)->getIndentation()<<", child.getType() = " << child.getType() << ", key = '"<<child.getKey()<<"', value = '"<<child.getValue()<<"', childIndentation = "<<childIndentation << std::endl;
87// }
88 }else{
89 if((*itParent)->getType() != VecValueType::LIST_ITEM && (*itParent)->getType() != VecValueType::VALUE &&
91 {
92 isCurrentGreater = (*itParent)->getIndentation() >= childIndentation;
93 }
94 }
95 //If we check with a > instead a >=, we have to check if the parent of a LIST_ITEM is a KEY
96 out = *itParent;
97 ++itParent;
98 }
99 return out;
100}
101
103
110VecValue * addChildToParentVecValue(VecValue & mainVecValue, const std::vector<size_t> & vecIndentation, bool isCompactMode, const VecValue & child, size_t currentIndentation){
111 VecValue * parent = NULL;
112 if(isCompactMode){
113 parent = &mainVecValue;
114 }else{
115 if(currentIndentation != -1lu){
116 parent = getParentVecValue(mainVecValue, vecIndentation, currentIndentation);
117 }else{
118 parent = getLastVecValue(mainVecValue);
119 }
120 }
121 parent->getVecChild().push_back(child);
122 return &(parent->getVecChild().back());
123}
124
126
133VecValue * addChildToParentVecValueAddListItem(VecValue & mainVecValue, const std::vector<size_t> & vecIndentation, bool isCompactMode, const VecValue & child, size_t currentIndentation){
134 VecValue * parent = NULL;
135 if(isCompactMode){
136 parent = &mainVecValue;
137 }else{
138 if(currentIndentation != -1lu){
139 parent = getParentVecValueListItem(mainVecValue, vecIndentation, currentIndentation, child);
140 }else{
141 parent = getLastVecValue(mainVecValue);
142 }
143 }
144 parent->getVecChild().push_back(child);
145 return &(parent->getVecChild().back());
146}
147
149
153void vecValueToDicoValue(DicoValue & dicoValue, const VecValue & vecVal, bool isMainValue){
154 PString key(vecVal.getKey());
155 const VecVecValue & vecChildren = vecVal.getVecChild();
156 if(vecChildren.size() == 0lu){
157 PString value(vecVal.getValue());
158 if(key == ""){ //The vecVal is a value only
159 DicoValue subDico;
160 subDico.setValue(value);
161 dicoValue.getVecChild().push_back(subDico);
162 }else{ //The vecVal is a key-value
163 DicoValue subDico;
164 subDico.setKey(key);
165 subDico.setValue(value);
166 dicoValue.getMapChild()[key] = subDico;
167 }
168 }else{
169 if(key != ""){
170 DicoValue subDico;
171 subDico.setKey(vecVal.getKey());
172 for(VecVecValue::const_iterator it(vecChildren.begin()); it != vecChildren.end(); ++it){
173 vecValueToDicoValue(subDico, *it, false);
174 }
175 dicoValue.getMapChild()[key] = subDico;
176 }else{
177 if(isMainValue){
178 for(VecVecValue::const_iterator it(vecChildren.begin()); it != vecChildren.end(); ++it){
179 vecValueToDicoValue(dicoValue, *it, false);
180 }
181 }else{
182 DicoValue dashValue;
183 for(VecVecValue::const_iterator it(vecChildren.begin()); it != vecChildren.end(); ++it){
184 vecValueToDicoValue(dashValue, *it, false);
185 }
186 dicoValue.getVecChild().push_back(dashValue);
187 }
188 }
189 }
190}
191
192
Vector of keys and values.
Definition VecValue.h:15
const VecValueType::VecValueType & getType() const
Gets the type of the VecValue.
Definition VecValue.cpp:118
size_t getIndentation() const
Gets the indentation of the VecValue.
Definition VecValue.cpp:132
const std::vector< VecValue > & getVecChild() const
Gets the vecChild of the VecValue.
Definition VecValue.cpp:104
const PString & getValue() const
Gets the value of the VecValue.
Definition VecValue.cpp:76
const PString & getKey() const
Gets the key of the VecValue.
Definition VecValue.cpp:90
void getVecParentPtr(std::vector< VecValue * > &vecParentPtr, VecValue &vecAllVal)
Get the parent pointer vector.
VecValue * getParentVecValue(VecValue &vecAllVal, const std::vector< size_t > &vecIndentation, size_t currentIndentation)
Get the parent VecValue by respect to its indentation.
VecValue * addChildToParentVecValueAddListItem(VecValue &mainVecValue, const std::vector< size_t > &vecIndentation, bool isCompactMode, const VecValue &child, size_t currentIndentation)
Add the given child to the main VecValue and return a pointer to the added child.
VecValue * getLastVecValue(VecValue &vecVal, size_t depth)
Get the last VecValue added at the specified depth.
VecValue * addChildToParentVecValue(VecValue &mainVecValue, const std::vector< size_t > &vecIndentation, bool isCompactMode, const VecValue &child, size_t currentIndentation)
Add the given child to the main VecValue and return a pointer to the added child.
void vecValueToDicoValue(DicoValue &dicoValue, const VecValue &vecVal, bool isMainValue)
Convert a VecValue into a DicoValue.
VecValue * getParentVecValueListItem(VecValue &vecAllVal, const std::vector< size_t > &vecIndentation, size_t currentIndentation, const VecValue &child)
Get the parent VecValue by respect to its indentation.
std::vector< VecValue > VecVecValue