Tulip  4.6.0
Better Visualization Through Research
library/tulip-core/include/tulip/YajlFacade.h
00001 /*
00002  *
00003  * This file is part of Tulip (www.tulip-software.org)
00004  *
00005  * Authors: David Auber and the Tulip development Team
00006  * from LaBRI, University of Bordeaux
00007  *
00008  * Tulip is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU Lesser General Public License
00010  * as published by the Free Software Foundation, either version 3
00011  * of the License, or (at your option) any later version.
00012  *
00013  * Tulip is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016  * See the GNU General Public License for more details.
00017  *
00018  */
00019 ///@cond DOXYGEN_HIDDEN
00020 
00021 
00022 #include <string>
00023 
00024 #include <tulip/tulipconf.h>
00025 
00026 namespace tlp {
00027 class PluginProgress;
00028 }
00029 
00030 struct yajl_gen_t;
00031 
00032 /**
00033  * @brief A Simple C++ wrapper around the C library 'yajl' parsing capabilities.
00034  *
00035  * It uses callbacks to signal what is being parsed (map start and end, strings, ...), and this class is intended to be subclassed,
00036  * with the subclass overriding the callbacks to process the events.
00037  **/
00038 class TLP_SCOPE YajlParseFacade {
00039 public:
00040   YajlParseFacade(tlp::PluginProgress* progress=NULL);
00041 
00042   virtual ~YajlParseFacade() {
00043   }
00044   /**
00045    * @brief Parses a JSON file.
00046    * Once this function is called, the callbacks (all the parse* functions) will get called when the corresponding event happens.
00047    *
00048    * @param filename The file to parse.
00049    * @return void
00050    **/
00051   void parse(std::string filename);
00052   void parse(const unsigned char* data, int length);
00053 
00054   virtual void parseNull();
00055   virtual void parseBoolean(bool boolVal);
00056   virtual void parseInteger(long long integerVal);
00057   virtual void parseDouble(double doubleVal);
00058   virtual void parseNumber(const char * numberVal, size_t numberLen);
00059   virtual void parseString(const std::string& value);
00060   virtual void parseMapKey(const std::string& value);
00061   virtual void parseStartMap();
00062   virtual void parseEndMap();
00063   virtual void parseStartArray();
00064   virtual void parseEndArray();
00065 
00066   bool parsingSucceeded() const;
00067   std::string errorMessage() const;
00068 
00069 protected:
00070   tlp::PluginProgress* _progress;
00071   bool _parsingSucceeded;
00072   std::string _errorMessage;
00073 };
00074 
00075 class YajlWriteFacade {
00076 public:
00077   YajlWriteFacade();
00078   ~YajlWriteFacade();
00079 
00080   std::string generatedString();
00081 
00082   void writeInteger(long long int number);
00083   void writeDouble(double number);
00084   void writeNumber(const char* str, size_t len);
00085   void writeString(const std::string& text);
00086   void writeNull();
00087   void writeBool(bool value);
00088   void writeMapOpen();
00089   void writeMapClose();
00090   void writeArrayOpen();
00091   void writeArrayClose();
00092 protected:
00093   yajl_gen_t* _generator;
00094 };
00095 ///@endcond
 All Classes Files Functions Variables Enumerations Enumerator Properties