diff options
| author | Tim Redfern <tim@eclectronics.org> | 2014-01-20 15:39:23 +0000 |
|---|---|---|
| committer | Tim Redfern <tim@eclectronics.org> | 2014-01-20 15:39:23 +0000 |
| commit | 6ae7506f595d45e752ffd0cebb6392c707db2d3c (patch) | |
| tree | 761d0cf5f42a1c14bb4ce9d2023ab65d59b77520 /NT/src/graph.cpp | |
| parent | 344e9b1a484b361cac7b07500cb7d2699cc93c29 (diff) | |
making json loader
Diffstat (limited to 'NT/src/graph.cpp')
| -rw-r--r-- | NT/src/graph.cpp | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/NT/src/graph.cpp b/NT/src/graph.cpp index 50aaa0c..6c8ccf2 100644 --- a/NT/src/graph.cpp +++ b/NT/src/graph.cpp @@ -1,4 +1,76 @@ #include "graph.h" +using namespace Rotor; +using namespace std; +bool Graph::check_audio(string audio,string path){ + if (audio!="") { + audio_filename=path+audio; + Poco::File f=Poco::File(audio_filename); + if (f.exists()) { + //here we should check the audio is actually valid + audio_loaded=true; + cerr<<"Rotor: loading "<<audio_filename<<" from graph"<<endl; + return true; + } + cerr<<"Rotor: audio file "<<audio_filename<<" not found"<<endl; + } + return false; +} +bool Graph::load_file(std::string filename,std::string media_path){ + Poco::File f=Poco::File(filename); + if (f.exists()) { + Poco::FileInputStream fis(filename); + Poco::CountingInputStream countingIstr(fis); + std::string str; + Poco::StreamCopier::copyToString(countingIstr, str); + return parse_json(str,media_path); + } + cerr<<"Rotor: graph "<<filename<<" not found"<<endl; + return false; +} +bool Graph::parse_json(string &data,string &media_path){ + Json::Value root; // will contain the root value after parsing. + Json::Reader reader; + bool parsingSuccessful = reader.parse( data, root ); + if ( !parsingSuccessful ) + { + std::cout << "Failed to parse configuration\n" + << reader.getFormattedErrorMessages(); + return false; + } + //The json validates, now we should attempt to retain nodes which haven't changed + //for now just clear the existing graph + clear(); + Node_factory factory; + check_audio(root["audio"].asString(),media_path); + init(root["id"].asString(),root["description"].asString()); + Json::Value jnodes = root["nodes"]; + for ( uint32_t i = 0; i < jnodes.size(); ++i ) { + string node_id=jnodes[i]["id"].asString(); + Node* node=factory.create(jnodes[i]); + if (node) { + if (nodes.find(node_id)==nodes.end()){ + cerr << "Rotor: creating node '"<<node_id<<"': '"<<jnodes[i]["type"].asString()<< "'" << endl; + //kind of have to do the links from outside thus + //how do deal with variable arrays? + // + //on our left.. an array of pointers to variables.. + //some of which contain arrays of pointers to variables + // + //on our right.. an unordered map of nodes to be linked to + for (auto var: node->vars){ + + } + nodes[node_id]=node; + } + else cerr << "ERROR: duplicate node '"<<node_id<<"' "<< endl; + } + else { + cerr << "ERROR: graph loader cannot find node '" <<jnodes[i]["type"].asString()<< "'" << endl; + return false; + } + } + return true; +} |
