PhoenixInkscape  2.0.0
Generate multiple png files with svg inkscape files
DicoValue.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 #include <fstream>
8 #include "DicoValue.h"
9 
12 
13 }
14 
16 
19  copyDicoValue(other);
20 }
21 
24 
25 }
26 
28 
32  copyDicoValue(other);
33  return *this;
34 }
35 
37 
40 bool DicoValue::load(const PPath & fileName){
41  PFileParser parser;
42  if(!parser.open(fileName)){return false;}
43  return loadParser(parser);
44 }
45 
47 
53 bool DicoValue::save(const PPath & fileName, const PString & valueDecorator, PString baseIndentation, PString baseNewLine) const{
54  PString out(toString(valueDecorator, baseIndentation, baseNewLine));
55  return fileName.saveFileContent(out);
56 }
57 
59 
62 bool DicoValue::fromString(const PString & content){
63  PFileParser parser;
64  parser.setFileContent(content);
65  return loadParser(parser);
66 }
67 
69 
74 PString DicoValue::toString(const PString & valueDecorator, PString baseIndentation, PString baseNewLine) const{
75  PString out(baseNewLine+"{");
76  out += saveRecurse(baseIndentation, valueDecorator, baseIndentation, baseNewLine);
77  out += baseNewLine+"}"+baseNewLine;
78  return out;
79 }
80 
82 void DicoValue::print() const{
83  std::cout << "{" << std::endl;
84  std::cout << saveRecurse("\t", "\"", "\t", "\n") << std::endl;
85  std::cout << "{" << std::endl;
86 }
87 
89 
91 bool DicoValue::hasKey() const{return p_key != "";}
92 
94 
96 bool DicoValue::hasMap() const{return p_mapChild.size() != 0lu;}
97 
99 
101 bool DicoValue::hasVec() const{return p_vecChild.size() != 0lu;}
102 
104 
107 bool DicoValue::isKeyExist(const PString & key) const{
108  std::map<PString, DicoValue>::const_iterator it(p_mapChild.find(key));
109  return it != p_mapChild.end();
110 }
111 
113 
116 const DicoValue * DicoValue::getMap(const PString & key) const{
117  std::map<PString, DicoValue>::const_iterator it(p_mapChild.find(key));
118  if(it != p_mapChild.end()){
119  return &(it->second);
120  }else{
121  return NULL;
122  }
123 }
124 
126 
130  std::map<PString, DicoValue>::const_iterator it(p_mapChild.find(key));
131  if(it != p_mapChild.end()){
132  return (DicoValue *)&(it->second);
133  }else{
134  return NULL;
135  }
136 }
137 
139 
141 void DicoValue::setValue(const PString & value){
142  p_value = value;
143 }
144 
146 
148 void DicoValue::setKey(const PString & key){
149  p_key = key;
150 }
151 
153 
155 void DicoValue::setVecChild(const std::vector<DicoValue> & vecChild){
156  p_vecChild = vecChild;
157 }
158 
160 
162 void DicoValue::setMapChild(const std::map<PString, DicoValue> & mapChild){
163  p_mapChild = mapChild;
164 }
165 
167 
169 const PString & DicoValue::getValue() const{
170  return p_value;
171 }
172 
174 
177  return p_value;
178 }
179 
181 
184  return p_value.eraseFirstLastChar("\"\'");
185 }
186 
188 
190 const PString & DicoValue::getKey() const{
191  return p_key;
192 }
193 
195 
198  return p_key;
199 }
200 
202 
204 const std::vector<DicoValue> & DicoValue::getVecChild() const{
205  return p_vecChild;
206 }
207 
209 
211 std::vector<DicoValue> & DicoValue::getVecChild(){
212  return p_vecChild;
213 }
214 
216 
218 const std::map<PString, DicoValue> & DicoValue::getMapChild() const{
219  return p_mapChild;
220 }
221 
223 
225 std::map<PString, DicoValue> & DicoValue::getMapChild(){
226  return p_mapChild;
227 }
228 
230 
233  p_value = other.p_value;
234  p_key = other.p_key;
235  p_vecChild = other.p_vecChild;
236  p_mapChild = other.p_mapChild;
237 }
238 
240 
244  parser.setEscapeChar('\\');
245  parser.setWhiteSpace(" \t\n");
246  parser.setSeparator(",:{}\"");
247  bool isRunning(true);
248  while(!parser.isEndOfFile() && isRunning){
249  if(parseDicoValue(parser, isRunning)){
250  parser.skipWhiteSpace();
251  }else{
252  errorAt(parser, isRunning, "Cannot parse dico value");
253  }
254  }
255  return isRunning;
256 }
257 
259 
263 bool DicoValue::parseDicoValue(PFileParser & parser, bool & isRunning){
264  if(parseListOrMap(parser, isRunning)){return true;}
265  else{
266  PString nextKeyOrValue(parseString(parser));
267  if(nextKeyOrValue == ""){
268  nextKeyOrValue = parser.getStrComposedOf("abcdefghijklmnopqsrtuvwxyzABCDEFGHIJKLMNOPQSRTUVWXYZ0123456789._-+");
269  if(nextKeyOrValue == ""){
270  return errorAt(parser, isRunning,
271  "Expecting a string or a keywork composed of letters, number, underscore, slash or minus");
272  }
273  }
274  if(parser.isMatch(":")){ //It was a key for a dictionnary
275 // std::cerr << "DicoValue::parseDicoValue : find key '"<<nextKeyOrValue<<"'" << std::endl;
276  p_key = nextKeyOrValue;
277  if(!parseDicoValue(parser, isRunning)){
278  return errorAt(parser, isRunning, "Cannot parse value");
279  }
280  }else{ //It was a value
281  p_value = nextKeyOrValue;
282  }
283  parser.skipWhiteSpace();
284  }
285  return true;
286 }
287 
289 
293 bool DicoValue::parseListOrMap(PFileParser & parser, bool & isRunning){
294  if(!parser.isMatch("{")){return false;} //If this is not a {, then it is not a list or a map
295 
296  while(!parser.isEndOfFile() && !parser.isMatch("}") && isRunning){
297  DicoValue dv;
298  if(dv.parseDicoValue(parser, isRunning)){
299  if(dv.p_key != ""){ //It is a dico entry
300 // std::cerr << "DicoValue::parseListOrMap : loadParser add DicoValue with key '"<<dv.p_key<<"'" << std::endl;
301  p_mapChild[dv.p_key] = dv;
302  }else{ //It is a value
303  p_vecChild.push_back(dv);
304  }
305  }else{
306  errorAt(parser, isRunning, "Cannot parse dico value");
307  }
308  if(parser.isMatch(",")){}
309  else if(parser.isMatchRewind("}")){}
310  else{
311  return errorAt(parser, isRunning, "Expect ',' or '}' after value");
312  }
313  }
314 
315  return true;
316 }
317 
319 
323  if(parser.isMatch("\"")){
324  return parser.getUntilKeyWithoutPatern("\"");
325  }else if(parser.isMatch("'")){
326  return parser.getUntilKeyWithoutPatern("'");
327  }
328  return "";
329 }
330 
332 
337 bool DicoValue::errorAt(PFileParser & parser, bool & isRunning, const PString & errorMsg){
338  isRunning = false;
339  std::cerr << "DicoValue::errorAt : " << parser.getLocation() << std::endl;
340  std::cerr << "\t" << errorMsg << std::endl;
341  return true;
342 }
343 
345 
351 PString DicoValue::saveRecurse(const PString & indentation, const PString & valueDecorator, PString baseIndentation, PString baseNewLine) const{
352  PString out(""), newIndentation(indentation);
353  if(p_key != ""){
354  newIndentation = indentation + baseIndentation;
355  }
356  if(p_mapChild.size() != 0lu){
357  if(p_key != ""){out += baseNewLine + indentation +valueDecorator + p_key + valueDecorator + ": {";}
358  PString comma("");
359  for(MapDicoValue::const_iterator it(p_mapChild.begin()); it != p_mapChild.end(); ++it){
360  out += comma;
361  out += it->second.saveRecurse(newIndentation, valueDecorator, baseIndentation, baseNewLine);
362  comma = ",";
363  }
364  if(p_key != ""){out += baseNewLine+indentation+"}";}
365  }else if(p_vecChild.size() != 0lu){
366  if(p_key != ""){out += baseNewLine + indentation + valueDecorator + p_key + valueDecorator + ": {";}
367  PString comma("");
368  for(VecDicoValue::const_iterator it(p_vecChild.begin()); it != p_vecChild.end(); ++it){
369  out += comma;
370  out += it->saveRecurse(newIndentation, valueDecorator, baseIndentation, baseNewLine);
371  comma = ", ";
372  }
373  if(p_key != ""){out += "}";}
374  }else{
375  PString valueToSave(p_value);
376  if(valueDecorator != ""){
377  valueToSave = valueDecorator + p_value + valueDecorator;
378  }else if(p_value.find(" \t\n':/")){
379  valueToSave = "\"" + p_value + "\"";
380  }
381  if(p_key != ""){out += baseNewLine + indentation + valueDecorator + p_key + valueDecorator + ": "+valueToSave;}
382  else{out += valueToSave;}
383  }
384  return out;
385 }
386 
387 
Dictionnary of values.
Definition: DicoValue.h:17
void setVecChild(const std::vector< DicoValue > &vecChild)
Sets the vecChild of the DicoValue.
Definition: DicoValue.cpp:155
PString toString(const PString &valueDecorator="", PString baseIndentation="\t", PString baseNewLine="\n") const
Convert the DicoValue into a string.
Definition: DicoValue.cpp:74
const PString & getKey() const
Gets the key of the DicoValue.
Definition: DicoValue.cpp:190
bool isKeyExist(const PString &key) const
Say if the given key exists in the map of children.
Definition: DicoValue.cpp:107
PString p_value
Value of the current entry.
Definition: DicoValue.h:67
void copyDicoValue(const DicoValue &other)
Copy Function of class DicoValue.
Definition: DicoValue.cpp:232
void setKey(const PString &key)
Sets the key of the DicoValue.
Definition: DicoValue.cpp:148
bool parseListOrMap(PFileParser &parser, bool &isRunning)
Parse a list or a map.
Definition: DicoValue.cpp:293
bool fromString(const PString &content)
Create a DicoValue from a PString.
Definition: DicoValue.cpp:62
bool hasKey() const
Say if the DicoValue has a key.
Definition: DicoValue.cpp:91
const std::vector< DicoValue > & getVecChild() const
Gets the vecChild of the DicoValue.
Definition: DicoValue.cpp:204
PString saveRecurse(const PString &indentation, const PString &valueDecorator, PString baseIndentation, PString baseNewLine) const
Save the DicoValue with a text file.
Definition: DicoValue.cpp:351
DicoValue & operator=(const DicoValue &other)
Operator = of class DicoValue.
Definition: DicoValue.cpp:31
virtual ~DicoValue()
Destructor of class DicoValue.
Definition: DicoValue.cpp:23
const DicoValue * getMap(const PString &key) const
Get a DicoValue in the map of the current one.
Definition: DicoValue.cpp:116
bool save(const PPath &fileName, const PString &valueDecorator="", PString baseIndentation="\t", PString baseNewLine="\n") const
Save the DicoValue with a text file.
Definition: DicoValue.cpp:53
void setValue(const PString &value)
Sets the value of the DicoValue.
Definition: DicoValue.cpp:141
bool hasMap() const
Say if the DicoValue has a map of children.
Definition: DicoValue.cpp:96
void print() const
Print the DicoValue.
Definition: DicoValue.cpp:82
PString p_key
Key of the current entry.
Definition: DicoValue.h:69
DicoValue()
Constructor of class DicoValue.
Definition: DicoValue.cpp:11
std::map< PString, DicoValue > p_mapChild
Map of sub DicoValue.
Definition: DicoValue.h:73
const std::map< PString, DicoValue > & getMapChild() const
Gets the mapChild of the DicoValue.
Definition: DicoValue.cpp:218
PString parseString(PFileParser &parser)
Parse a string.
Definition: DicoValue.cpp:322
std::vector< DicoValue > p_vecChild
Vector of sub DicoValue.
Definition: DicoValue.h:71
T getValue() const
Convert the value of the current DicoValue into a type.
bool parseDicoValue(PFileParser &parser, bool &isRunning)
Parse a DicoValue with a text file.
Definition: DicoValue.cpp:263
bool loadParser(PFileParser &parser)
Load the DicoValue with a parser.
Definition: DicoValue.cpp:243
bool errorAt(PFileParser &parser, bool &isRunning, const PString &errorMsg)
Print the parsing error.
Definition: DicoValue.cpp:337
PString getString() const
Get a string value without the first and/or last quote or double quote in there are some.
Definition: DicoValue.cpp:183
bool load(const PPath &fileName)
Load the DicoValue with a text file.
Definition: DicoValue.cpp:40
void setMapChild(const std::map< PString, DicoValue > &mapChild)
Sets the mapChild of the DicoValue.
Definition: DicoValue.cpp:162
bool hasVec() const
Say if the DicoValue has a vector of children.
Definition: DicoValue.cpp:101
classe qui permet de parser des fichiers texte en renvoyant les tokens les uns après les autres
Definition: PFileParser.h:20
void setSeparator(const PString &separator)
Initialise la liste des caractères séparateurs.
Definition: PFileParser.cpp:43
bool open(const PPath &fileName)
Fonction qui ouvre le fichier que l'on va parser.
Definition: PFileParser.cpp:24
PString getUntilKeyWithoutPatern(const PString &patern)
Renvoie la chaine de caractère du caractère courant jusqu'à patern exclu.
void setEscapeChar(char escapeChar)
Sets the escape character of the PFileParser.
Definition: PFileParser.cpp:58
PString getStrComposedOf(const PString &charset)
Get string composed of the characters in the string charset.
void setWhiteSpace(const PString &whiteSpace)
Initialise la liste des caractères blancs.
Definition: PFileParser.cpp:35
bool isMatchRewind(const PString &patern)
Do a isMatch and then go back at the previous position.
bool isMatch(const PString &patern)
Says if the patern match with the current caracters of the PFileParser.
PLocation getLocation() const
Fonction qui renvoie la PLocation du PFileParser.
void skipWhiteSpace()
Skip the white space if there is at the current caracter position.
void setFileContent(const PString &fileContent)
Set the file content.
Definition: PFileParser.cpp:50
bool isEndOfFile() const
Dit si on est à la fin du fichier.
Definition: PFileParser.cpp:88
Path of a directory or a file.
Definition: PPath.h:17
bool saveFileContent(const PString &content) const
Save a PString in a file.
Definition: PPath.cpp:395
Extends the std::string.
Definition: PString.h:16
bool find(char ch) const
Find a char in a string.
Definition: PString.cpp:371
PString eraseFirstLastChar(const PString &vecChar) const
Erase first and last char in a string.
Definition: PString.cpp:545