PhoenixFileParser  1.0.0
Set of tools to ease file parsing
Loading...
Searching...
No Matches
dico_replace_var.cpp File Reference
#include "PFileParser.h"
#include "dico_replace_var.h"
+ Include dependency graph for dico_replace_var.cpp:

Go to the source code of this file.

Typedefs

typedef std::map< PString, std::pair< PNestedCall, DicoValue * > > MapVarWithNestedCall
 Map of the variables which uses nested call.
 
typedef std::vector< std::pair< PNestedCall, DicoValue * > > PVecReplaceVar
 Map used to replace variable value in nested calls (VariableName, PNestedCall)
 

Functions

bool createNestedCallFromStr (PNestedCall &call, const PString &value, const PString varName, const PString &varBegin, const PString &varEnd)
 Create the PNestedCall from the given value.
 
void dico_create_nested_call (PNestedCall &call, const PString &baseStr, const PString &varBegin, const PString &varEnd)
 Create the nested calls of the input base string.
 
void dico_find_all_var (PMapKnownVar &mapReadyVar, PVecReplaceVar &mapNestedVar, MapVarWithNestedCall &mapVarWithNestedCall, DicoValue &dico, const PString &varIdentifier)
 Get the variable which contains only a value and those with nested calls.
 
void dico_replace_nested_call (PString &out, const PNestedCall &call, const PMapKnownVar &mapKeyVariable, const PString &varBegin, const PString &varEnd)
 Replace the nested call by the variables in map.
 
void dico_replace_var (DicoValue &dico, const PString &varIdentifier)
 Replace all the variables which are string in the given DicoValue, when ${variable} apprears in the value.
 
PString dico_replace_var_str (const PString &baseStr, const PMapKnownVar &mapKeyVariable, const PString &varBegin, const PString &varEnd)
 Update the suffix of the file.
 
void dico_update_all_nestedCall (PMapKnownVar &mapReadyVar, PVecReplaceVar &mapNestedVar, MapVarWithNestedCall &mapVarWithNestedCall, PNestedCall &nestedCall, DicoValue *dico, const PString &varIdentifier)
 Update variables with nested calls.
 
void dico_update_all_var (PMapKnownVar &mapReadyVar, PVecReplaceVar &mapNestedVar, MapVarWithNestedCall &mapVarWithNestedCall, const PString &varIdentifier)
 Update the variable which contains nested calls.
 

Typedef Documentation

◆ MapVarWithNestedCall

typedef std::map<PString, std::pair<PNestedCall, DicoValue*> > MapVarWithNestedCall

Map of the variables which uses nested call.

Definition at line 13 of file dico_replace_var.cpp.

◆ PVecReplaceVar

typedef std::vector<std::pair<PNestedCall, DicoValue*> > PVecReplaceVar

Map used to replace variable value in nested calls (VariableName, PNestedCall)

Definition at line 11 of file dico_replace_var.cpp.

Function Documentation

◆ createNestedCallFromStr()

bool createNestedCallFromStr ( PNestedCall & call,
const PString & value,
const PString varName,
const PString & varBegin,
const PString & varEnd )

Create the PNestedCall from the given value.

Parameters
[out]call: PNestedCall
value: value to be used
varName: name of the variable to be defined
varBegin: characters which announce the begining of a variable (example ${)
varEnd: characters which announce the ending of a variable (example })
Returns
true if the value contains a nested call, false otherwise

Definition at line 89 of file dico_replace_var.cpp.

89 {
90 call.setName(varName);
91 PFileParser parser;
92 parser.setFileContent(value);
93 bool hasNestedCall(false);
94 while(!parser.isEndOfFile()){
95 PString prevCall(parser.getUntilKeyWithoutPatern(varBegin));
96 if(prevCall != ""){
97 PNestedStr str;
98 str.setValue(prevCall);
99 str.setIsVarCall(false);
100 call.getVecNestedStr().push_back(str);
101 }
102 PString varNameCall(parser.getUntilKeyWithoutPatern(varEnd));
103 if(varNameCall != ""){
104 PNestedStr str;
105 str.setValue(varNameCall);
106 str.setIsVarCall(true);
107 call.getVecNestedStr().push_back(str);
108 hasNestedCall = true;
109 }
110 }
111 return hasNestedCall;
112}
classe qui permet de parser des fichiers texte en renvoyant les tokens les uns après les autres
Definition PFileParser.h:20
PString getUntilKeyWithoutPatern(const PString &patern)
Renvoie la chaine de caractère du caractère courant jusqu'à patern exclu.
void setFileContent(const PString &fileContent)
Set the file content.
bool isEndOfFile() const
Dit si on est à la fin du fichier.
void setName(const PString &name)
Sets the name of the PNestedCall.
const std::vector< PNestedStr > & getVecNestedStr() const
Gets the vecNestedStr of the PNestedCall.
Nested string or variable call.
Definition PNestedCall.h:14
void setValue(const PString &value)
Sets the value of the PNestedStr.
void setIsVarCall(bool isVarCall)
Sets the isVarCall of the PNestedStr.

References PFileParser::getUntilKeyWithoutPatern(), PNestedCall::getVecNestedStr(), PFileParser::isEndOfFile(), PFileParser::setFileContent(), PNestedStr::setIsVarCall(), PNestedCall::setName(), and PNestedStr::setValue().

Referenced by dico_find_all_var().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dico_create_nested_call()

void dico_create_nested_call ( PNestedCall & call,
const PString & baseStr,
const PString & varBegin,
const PString & varEnd )

Create the nested calls of the input base string.

Parameters
[out]call: PNestedCall to be created
baseStr: basic suffix to be used
varBegin: characters which announce the begining of a variable (example ${)
varEnd: characters which announce the ending of a variable (example })

Definition at line 21 of file dico_replace_var.cpp.

21 {
22 PFileParser parser;
23 parser.setFileContent(baseStr);
24 while(!parser.isEndOfFile()){
25 PString prevCall(parser.getUntilKeyWithoutPatern(varBegin));
26 if(prevCall != ""){
27 PNestedStr str;
28 str.setValue(prevCall);
29 str.setIsVarCall(false);
30 call.getVecNestedStr().push_back(str);
31 }
32 PString varNameCall(parser.getUntilKeyWithoutPatern(varEnd));
33 if(varNameCall != ""){
34 PNestedStr str;
35 str.setValue(varNameCall);
36 str.setIsVarCall(true);
37 call.getVecNestedStr().push_back(str);
38 }
39 }
40}

References PFileParser::getUntilKeyWithoutPatern(), PNestedCall::getVecNestedStr(), PFileParser::isEndOfFile(), PFileParser::setFileContent(), PNestedStr::setIsVarCall(), and PNestedStr::setValue().

Referenced by dico_replace_var_str().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dico_find_all_var()

void dico_find_all_var ( PMapKnownVar & mapReadyVar,
PVecReplaceVar & mapNestedVar,
MapVarWithNestedCall & mapVarWithNestedCall,
DicoValue & dico,
const PString & varIdentifier )

Get the variable which contains only a value and those with nested calls.

Parameters
[out]mapReadyVar: map of variables with values only
[out]mapNestedVar: map of variables with nested calls
[out]mapVarWithNestedCall: map of the variables which use nested calls
varIdentifier: String used to detect the variable names (example: '$' for ${varName}, § for §{varName})
dico: DicoValue to be used

Definition at line 121 of file dico_replace_var.cpp.

121 {
122 if(dico.hasMap()){
123 MapDicoValue & mapDico = dico.getMapChild();
124 for(MapDicoValue::iterator it(mapDico.begin()); it != mapDico.end(); ++it){
125 dico_find_all_var(mapReadyVar, mapNestedVar, mapVarWithNestedCall, it->second, varIdentifier);
126 }
127 }else if(dico.hasVec()){
128 VecDicoValue & vecDico = dico.getVecChild();
129 for(VecDicoValue::iterator it(vecDico.begin()); it != vecDico.end(); ++it){
130 dico_find_all_var(mapReadyVar, mapNestedVar, mapVarWithNestedCall, *it, varIdentifier);
131 }
132 }else if(dico.hasKey()){ //No map, no vector but a key, this is a string variable
133 PString strValue(dico.getString()), varName(dico.getKey());
134 //Let's check is the value contains nested calls
135 //We will create a class to handle the split between string part and nested calls
136 PNestedCall call;
137 //We add the class in the proper map
138 if((createNestedCallFromStr(call, strValue, varName, varIdentifier + "{", "}"))){ //There is (at least) one nested call
139 mapNestedVar.push_back(std::pair<PNestedCall, DicoValue*>(call, &dico));
140 mapVarWithNestedCall[varName] = std::pair<PNestedCall, DicoValue*>(call, &dico);
141 }else{ //There is no nested call
142 mapReadyVar[varName] = strValue;
143 }
144 }
145}
std::map< PString, DicoValue > MapDicoValue
Vector of DicoValue.
Definition DicoValue.h:82
std::vector< DicoValue > VecDicoValue
Vector of DicoValue.
Definition DicoValue.h:80
const PString & getKey() const
Gets the key of the DicoValue.
bool hasKey() const
Say if the DicoValue has a key.
Definition DicoValue.cpp:92
const std::vector< DicoValue > & getVecChild() const
Gets the vecChild of the DicoValue.
bool hasMap() const
Say if the DicoValue has a map of children.
Definition DicoValue.cpp:97
const std::map< PString, DicoValue > & getMapChild() const
Gets the mapChild of the DicoValue.
PString getString() const
Get a string value without the first and/or last quote or double quote in there are some.
bool hasVec() const
Say if the DicoValue has a vector of children.
Class used to parse nested call variables.
Definition PNestedCall.h:36
bool createNestedCallFromStr(PNestedCall &call, const PString &value, const PString varName, const PString &varBegin, const PString &varEnd)
Create the PNestedCall from the given value.
void dico_find_all_var(PMapKnownVar &mapReadyVar, PVecReplaceVar &mapNestedVar, MapVarWithNestedCall &mapVarWithNestedCall, DicoValue &dico, const PString &varIdentifier)
Get the variable which contains only a value and those with nested calls.

References createNestedCallFromStr(), dico_find_all_var(), DicoValue::getKey(), DicoValue::getMapChild(), DicoValue::getString(), DicoValue::getVecChild(), DicoValue::hasKey(), DicoValue::hasMap(), and DicoValue::hasVec().

Referenced by dico_find_all_var(), and dico_replace_var().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dico_replace_nested_call()

void dico_replace_nested_call ( PString & out,
const PNestedCall & call,
const PMapKnownVar & mapKeyVariable,
const PString & varBegin,
const PString & varEnd )

Replace the nested call by the variables in map.

Parameters
[out]out: output string with replaced variables
call: nested call to create the output string
mapKeyVariable: map of all defined variabled to be used
varBegin: characters which announce the begining of a variable (example ${) (in case variable is not found)
varEnd: characters which announce the ending of a variable (example }) (in case variable is not found)

Definition at line 49 of file dico_replace_var.cpp.

49 {
50 const std::vector<PNestedStr> & vecNestedStr = call.getVecNestedStr();
51 for(std::vector<PNestedStr>::const_iterator it(vecNestedStr.begin()); it != vecNestedStr.end(); ++it){
52 if(it->getIsVarCall()){
53 PMapKnownVar::const_iterator itCall(mapKeyVariable.find(varBegin + it->getValue() + varEnd));
54 if(itCall != mapKeyVariable.end()){
55 out += itCall->second;
56 }else{
57 out += varBegin + it->getValue() + varEnd;
58 }
59 }else{
60 out += it->getValue();
61 }
62 }
63}

References PNestedCall::getVecNestedStr().

Referenced by dico_replace_var_str().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dico_replace_var()

void dico_replace_var ( DicoValue & dico,
const PString & varIdentifier )

Replace all the variables which are string in the given DicoValue, when ${variable} apprears in the value.

Parameters
dico: DicoValue to be updated
varIdentifier: String used to detect the variable names (example: '$' for ${varName}, § for §{varName})

Definition at line 218 of file dico_replace_var.cpp.

218 {
219 //Let's find all the defined variables, linked to the DicoValue, string only
220 PMapKnownVar mapReadyVar;
221 PVecReplaceVar mapNestedVar;
222 MapVarWithNestedCall mapVarWithNestedCall;
223 dico_find_all_var(mapReadyVar, mapNestedVar, mapVarWithNestedCall, dico, varIdentifier);
224
225 //Update variables with nested call, separate those with nested call from the other
226 dico_update_all_var(mapReadyVar, mapNestedVar, mapVarWithNestedCall, varIdentifier);
227}
std::vector< std::pair< PNestedCall, DicoValue * > > PVecReplaceVar
Map used to replace variable value in nested calls (VariableName, PNestedCall)
void dico_update_all_var(PMapKnownVar &mapReadyVar, PVecReplaceVar &mapNestedVar, MapVarWithNestedCall &mapVarWithNestedCall, const PString &varIdentifier)
Update the variable which contains nested calls.
std::map< PString, std::pair< PNestedCall, DicoValue * > > MapVarWithNestedCall
Map of the variables which uses nested call.
std::map< PString, PString > PMapKnownVar
Map of known variables.

References dico_find_all_var(), and dico_update_all_var().

+ Here is the call graph for this function:

◆ dico_replace_var_str()

PString dico_replace_var_str ( const PString & baseStr,
const PMapKnownVar & mapKeyVariable,
const PString & varBegin,
const PString & varEnd )

Update the suffix of the file.

Parameters
baseStr: basic suffix to be used
mapKeyVariable: map of all defined variabled to be used
varBegin: characters which announce the begining of a variable (example ${)
varEnd: characters which announce the ending of a variable (example })
Returns
updated string

Definition at line 72 of file dico_replace_var.cpp.

72 {
73 if(varBegin == "" || varEnd == ""){return baseStr;}
74 PNestedCall call;
75 dico_create_nested_call(call, baseStr, varBegin, varEnd);
76 PString out("");
77 dico_replace_nested_call(out, call, mapKeyVariable, varBegin, varEnd);
78 return out;
79}
void dico_replace_nested_call(PString &out, const PNestedCall &call, const PMapKnownVar &mapKeyVariable, const PString &varBegin, const PString &varEnd)
Replace the nested call by the variables in map.
void dico_create_nested_call(PNestedCall &call, const PString &baseStr, const PString &varBegin, const PString &varEnd)
Create the nested calls of the input base string.

References dico_create_nested_call(), and dico_replace_nested_call().

+ Here is the call graph for this function:

◆ dico_update_all_nestedCall()

void dico_update_all_nestedCall ( PMapKnownVar & mapReadyVar,
PVecReplaceVar & mapNestedVar,
MapVarWithNestedCall & mapVarWithNestedCall,
PNestedCall & nestedCall,
DicoValue * dico,
const PString & varIdentifier )

Update variables with nested calls.

Parameters
[out]mapReadyVar: map of variables with values only
[out]mapNestedVar: map of variables with nested calls
nestedCall: nested call which is the split value of the dico
mapVarWithNestedCall: map of the variables which use nested calls
dico: DicoValue to be used
varIdentifier: String used to detect the variable names (example: '$' for ${varName}, § for §{varName})

Definition at line 156 of file dico_replace_var.cpp.

158{
159 //For each call, we check if it comes from the mapReadyVar, from the mapNestedVar or from nowhere
160 PString outputValue("");
161 std::vector<PNestedStr> & vecNestedStr = nestedCall.getVecNestedStr();
162 for(std::vector<PNestedStr>::iterator it(vecNestedStr.begin()); it != vecNestedStr.end(); ++it){
163 if(!it->getIsVarCall()){ //If it is not a var call, we skip it
164 outputValue += it->getValue();
165 continue;
166 }
167 PString varName(it->getValue());
168 //If the var comes from mapNestedVar, we have to resolve this one first
169 MapVarWithNestedCall::iterator itNested = mapVarWithNestedCall.find(varName);
170 if(itNested != mapVarWithNestedCall.end()){
171 dico_update_all_nestedCall(mapReadyVar, mapNestedVar, mapVarWithNestedCall, itNested->second.first, itNested->second.second, varIdentifier);
172 mapVarWithNestedCall.erase(varName); //We solve varName, so we erase it from the mapNestedVar
173 //But now, the varName entry does exist in the mapReadyVar
174 }
175 //If the var comes from mapReadyVar, we replace it
176 PMapKnownVar::iterator itReady = mapReadyVar.find(varName);
177 if(itReady != mapReadyVar.end()){
178 outputValue += itReady->second;
179 }else{
180 //If we cannot find it, let's keep it unsolved (it can we a global variable)
181 outputValue += varIdentifier + "{" + varName + "}";
182 }
183 }
184 //Finally, we can update the dico value, but we deal with the quotation
185 PString firstQuote(""), prevValue(dico->getValue());
186 if(prevValue.size() != 0lu){
187 if(prevValue.front() == '\''){firstQuote = "'";}
188 else if(prevValue.front() == '"'){firstQuote = "\"";}
189 }
190 dico->setValue(firstQuote + outputValue + firstQuote);
191
192 //And put its value into the mapReadyVar, because we solve it
193 mapReadyVar[nestedCall.getName()] = outputValue;
194}
void setValue(const PString &value)
Sets the value of the DicoValue.
T getValue() const
Convert the value of the current DicoValue into a type.
const PString & getName() const
Gets the name of the PNestedCall.
void dico_update_all_nestedCall(PMapKnownVar &mapReadyVar, PVecReplaceVar &mapNestedVar, MapVarWithNestedCall &mapVarWithNestedCall, PNestedCall &nestedCall, DicoValue *dico, const PString &varIdentifier)
Update variables with nested calls.

References dico_update_all_nestedCall(), PNestedCall::getName(), DicoValue::getValue(), PNestedCall::getVecNestedStr(), and DicoValue::setValue().

Referenced by dico_update_all_nestedCall(), and dico_update_all_var().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dico_update_all_var()

void dico_update_all_var ( PMapKnownVar & mapReadyVar,
PVecReplaceVar & mapNestedVar,
MapVarWithNestedCall & mapVarWithNestedCall,
const PString & varIdentifier )

Update the variable which contains nested calls.

Parameters
[out]mapReadyVar: map of variables with values only
[out]mapNestedVar: map of variables with nested calls
mapVarWithNestedCall: map of variable with nested call
varIdentifier: String used to detect the variable names (example: '$' for ${varName}, § for §{varName})

Definition at line 202 of file dico_replace_var.cpp.

204{
205 //Let's try to complete the nested call
206 for(PVecReplaceVar::iterator itNested(mapNestedVar.begin()); itNested != mapNestedVar.end(); ++itNested){
207 PNestedCall & nestedCall = itNested->first;
208 DicoValue * dico = itNested->second;
209
210 dico_update_all_nestedCall(mapReadyVar, mapNestedVar, mapVarWithNestedCall, nestedCall, dico, varIdentifier);
211 }
212}
Dictionnary of values.
Definition DicoValue.h:17

References dico_update_all_nestedCall().

Referenced by dico_replace_var().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: