Tulip  4.2.0
Better Visualization Through Research
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Groups Pages
YajlFacade.h
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux 1 and Inria Bordeaux - Sud Ouest
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 ///@cond DOXYGEN_HIDDEN
20 
21 
22 #include <string>
23 
24 #include <tulip/tulipconf.h>
25 
26 namespace tlp {
27 class PluginProgress;
28 }
29 
30 class yajl_gen_t;
31 
32 class QIODevice;
33 
34 /**
35  * @brief A Simple C++ wrapper around the C library 'yajl' parsing capabilities.
36  *
37  * It uses callbacks to signal what is being parsed (map start and end, strings, ...), and this class is intended to be subclassed,
38  * with the subclass overriding the callbacks to process the events.
39  **/
40 class TLP_SCOPE YajlParseFacade {
41 public:
42  YajlParseFacade(tlp::PluginProgress* progress=NULL) : _progress(progress), _parsingSucceeded(true) {}
43 
44  virtual ~YajlParseFacade() {
45  }
46  /**
47  * @brief Parses a JSON file.
48  * Once this function is called, the callbacks (all the parse* functions) will get called when the corresponding event happens.
49  *
50  * @param filename The file to parse.
51  * @return void
52  **/
53  void parse(std::string filename);
54  void parse(const unsigned char* data, int length);
55 
56  virtual void parseNull();
57  virtual void parseBoolean(bool boolVal);
58  virtual void parseInteger(long long integerVal);
59  virtual void parseDouble(double doubleVal);
60  virtual void parseNumber(const char * numberVal, size_t numberLen);
61  virtual void parseString(const std::string& value);
62  virtual void parseMapKey(const std::string& value);
63  virtual void parseStartMap();
64  virtual void parseEndMap();
65  virtual void parseStartArray();
66  virtual void parseEndArray();
67 
68  bool parsingSucceeded() const;
69  std::string errorMessage() const;
70 
71 protected:
72  tlp::PluginProgress* _progress;
73  bool _parsingSucceeded;
74  std::string _errorMessage;
75 };
76 
77 class YajlWriteFacade {
78 public:
79  YajlWriteFacade();
80  ~YajlWriteFacade();
81 
82  std::string generatedString();
83 
84  void writeInteger(long long int number);
85  void writeDouble(double number);
86  void writeNumber(const char* str, size_t len);
87  void writeString(const std::string& text);
88  void writeNull();
89  void writeBool(bool value);
90  void writeMapOpen();
91  void writeMapClose();
92  void writeArrayOpen();
93  void writeArrayClose();
94 protected:
95  yajl_gen_t* _generator;
96 };
97 ///@endcond