1 /* 2 3 Copyright (C) 2011 Grame 4 5 This library is free software; you can redistribute it and/or 6 modify it under the terms of the GNU Lesser General Public 7 License as published by the Free Software Foundation; either 8 version 2.1 of the License, or (at your option) any later version. 9 10 This library is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser General Public 16 License along with this library; if not, write to the Free Software 17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 19 Grame Research Laboratory, 9 rue du Garet, 69001 Lyon - France 20 research@grame.fr 21 22 */ 23 24 #ifndef __jsonroot__ 25 #define __jsonroot__ 26 27 #include <ostream> 28 #include <string> 29 #include <map> 30 #include <vector> 31 #include <sstream> 32 33 #include "smartpointer.h" 34 #include "jsonnode.h" 35 36 namespace httpdfaust 37 { 38 39 //-------------------------------------------------------------------------- 40 /*! 41 \brief a faust root is a terminal node and represents a faust parameter controler 42 */ 43 class jsonroot : public smartable 44 { 45 46 private: 47 48 std::string fName; 49 std::string fAddress; 50 int fPort; 51 int fInputs; 52 int fOutputs; 53 std::map<std::string, std::string> fMeta; 54 std::vector<Sjsonnode> fUi; 55 std::stringstream fJSON; 56 flatten(const std::string & src)57 inline std::string flatten(const std::string& src) 58 { 59 std::stringstream dst; 60 for (size_t i = 0; i < src.size(); i++) { 61 switch (src[i]) { 62 case '\n': 63 case '\t': 64 dst << ' '; 65 break; 66 default: 67 dst << src[i]; 68 break; 69 } 70 } 71 return dst.str(); 72 } 73 74 public: jsonroot(const char * name,const char * address,int port)75 jsonroot(const char *name, const char* address, int port) 76 :fName(name), fAddress(address), fPort(port), fInputs(0), fOutputs(0) {} ~jsonroot()77 virtual ~jsonroot() {} 78 79 void print(std::ostream& out) const; add(const Sjsonnode & node)80 void add(const Sjsonnode& node) { fUi.push_back(node); } setPort(int port)81 void setPort(int port) { fPort = port; } declare(const char * key,const char * val)82 void declare(const char* key, const char* val) { fMeta[key] = val; } setInputs(int inputs)83 void setInputs(int inputs) { fInputs = inputs;} setOutputs(int outputs)84 void setOutputs(int outputs) { fOutputs = outputs; } 85 std::string json(bool flat = false) { print(fJSON); return (flat) ? flatten(fJSON.str()) : fJSON.str(); } 86 }; 87 88 } // end namespoace 89 90 #endif 91