1 #include <cpPlugins/Interface/Workspace.h>
2 #include <third_party/tinyxml/tinyxml.h>
4 // -------------------------------------------------------------------------
5 std::string cpPlugins::Interface::Workspace::
6 LoadWorkspace( const std::string& fname )
8 TiXmlDocument* doc = new TiXmlDocument( fname.c_str( ) );
10 TiXmlElement* root = doc->RootElement( );
12 return( "cpPlugins::Interface::Workspace: No valid file" );
13 if( std::string( root->Value( ) ) != "cpPlugins_Workspace" )
14 return( "cpPlugins::Interface::Workspace: No valid workspace" );
15 std::stringstream err;
18 TiXmlElement* plugins = root->FirstChildElement( "plugins" );
19 while( plugins != NULL )
21 const char* value = plugins->Attribute( "filename" );
24 if( !( this->LoadPlugins( value ) ) )
25 err << "no valid plugins file \"" << value << "\"" << std::endl;
28 plugins = plugins->NextSiblingElement( "plugins" );
33 plugins = root->FirstChildElement( "plugins_dir" );
34 while( plugins != NULL )
36 const char* value = plugins->Attribute( "path" );
40 if( plugins->QueryIntAttribute( "recursive", &recursive ) != TIXML_SUCCESS )
42 if( !( this->LoadPluginsPath( value, recursive == 1 ) ) )
43 err << "No valid plugins path \"" << value << "\"" << std::endl;
46 plugins = plugins->NextSiblingElement( "plugins_dir" );
51 TiXmlElement* filter = root->FirstChildElement( "filter" );
52 while( filter != NULL )
54 const char* class_value = filter->Attribute( "class" );
55 const char* name_value = filter->Attribute( "name" );
56 if( class_value != NULL && name_value != NULL )
58 if( this->CreateFilter( class_value, name_value ) )
61 TParameters* parameters = this->GetParameters( name_value );
62 TiXmlElement* param = filter->FirstChildElement( "parameter" );
63 while( param != NULL )
65 const char* param_name = param->Attribute( "name" );
66 const char* param_type = param->Attribute( "type" );
67 if( param_name != NULL && param_type != NULL )
69 std::string param_type_str( param_type );
70 const char* value = param->Attribute( "value" );
73 std::istringstream value_str( value );
74 if( param_type_str == "String" )
75 parameters->SetString( param_name, value );
76 else if( param_type_str == "Bool" )
77 parameters->SetBool( param_name, value[ 0 ] != '0' );
78 else if( param_type_str == "Int" )
82 parameters->SetInt( param_name, v );
84 else if( param_type_str == "Uint" )
88 parameters->SetUint( param_name, v );
90 else if( param_type_str == "Real" )
94 parameters->SetReal( param_name, v );
97 else if( param_type_str == "Index" )
98 else if( param_type_str == "Point" )
99 else if( param_type_str == "Vector" )
104 if( param_type_str == "StringList" )
106 TiXmlElement* item = param->FirstChildElement( "item" );
107 while( item != NULL )
109 value = item->Attribute( "value" );
111 parameters->AddToStringList( param_name, value );
112 item = item->NextSiblingElement( "item" );
116 else if( param_type_str == "BoolList" )
118 TiXmlElement* item = param->FirstChildElement( "item" );
119 while( item != NULL )
121 value = item->Attribute( "value" );
123 parameters->AddToBoolList( param_name, value[ 0 ] != '0' );
124 item = item->NextSiblingElement( "item" );
128 else if( param_type_str == "IntList" )
130 TiXmlElement* item = param->FirstChildElement( "item" );
131 while( item != NULL )
133 value = item->Attribute( "value" );
136 std::istringstream value_str( value );
139 parameters->AddToIntList( param_name, v );
142 item = item->NextSiblingElement( "item" );
146 else if( param_type_str == "UintList" )
148 TiXmlElement* item = param->FirstChildElement( "item" );
149 while( item != NULL )
151 value = item->Attribute( "value" );
154 std::istringstream value_str( value );
155 TParameters::TUint v;
157 parameters->AddToUintList( param_name, v );
160 item = item->NextSiblingElement( "item" );
164 else if( param_type_str == "RealList" )
166 TiXmlElement* item = param->FirstChildElement( "item" );
167 while( item != NULL )
169 value = item->Attribute( "value" );
172 std::istringstream value_str( value );
173 TParameters::TReal v;
175 parameters->AddToRealList( param_name, v );
178 item = item->NextSiblingElement( "item" );
183 else if( param_type_str == "IndexList" )
184 else if( param_type_str == "PointList" )
185 else if( param_type_str == "VectorList" )
186 else if( param_type_str == "Choices" );
191 param = param->NextSiblingElement( "parameter" );
197 << "No valid class \"" << class_value << "\" with name \""
198 << name_value << "\"" << std::endl;
201 err << "Incomplete data." << std::endl;
202 filter = filter->NextSiblingElement( "filter" );
207 TiXmlElement* connection = root->FirstChildElement( "connection" );
208 while( connection != NULL )
210 TiXmlElement* orig = connection->FirstChildElement( "origin" );
211 TiXmlElement* dest = connection->FirstChildElement( "destination" );
212 if( orig != NULL && dest != NULL )
214 const char* orig_filter = orig->Attribute( "filter" );
215 const char* dest_filter = dest->Attribute( "filter" );
216 const char* orig_name = orig->Attribute( "name" );
217 const char* dest_name = dest->Attribute( "name" );
219 orig_filter != NULL && dest_filter != NULL &&
220 orig_name != NULL && dest_name != NULL
222 this->Connect( orig_filter, dest_filter, orig_name, dest_name );
225 connection = connection->NextSiblingElement( "connection" );
231 return( err.str( ) );
234 // -------------------------------------------------------------------------
235 std::string cpPlugins::Interface::Workspace::
236 SaveWorkspace( const std::string& fname ) const
238 std::stringstream err;
239 TiXmlDocument* doc = new TiXmlDocument( );
240 TiXmlElement* root = new TiXmlElement( "cpPlugins_Workspace" );
244 auto plugIt = this->m_LoadedPlugins.begin( );
245 plugIt != this->m_LoadedPlugins.end( );
249 TiXmlElement* plugin = new TiXmlElement( "plugins" );
250 plugin->SetAttribute( "filename", plugIt->c_str( ) );
251 root->LinkEndChild( plugin );
256 auto vIt = this->m_Graph->BeginVertices( );
257 for( ; vIt != this->m_Graph->EndVertices( ); ++vIt )
259 auto filter = dynamic_cast< TFilter* >( vIt->second.GetPointer( ) );
260 auto data = dynamic_cast< TData* >( vIt->second.GetPointer( ) );
263 TiXmlElement* e = new TiXmlElement( "filter" );
264 e->SetAttribute( "class", filter->GetClassName( ).c_str( ) );
265 e->SetAttribute( "name", filter->GetName( ) );
267 const TParameters* params = filter->GetParameters( );
268 std::vector< std::string > names;
269 params->GetNames( names );
270 for( auto nIt = names.begin( ); nIt != names.end( ); ++nIt )
272 TiXmlElement* p = new TiXmlElement( "parameter" );
273 p->SetAttribute( "name", nIt->c_str( ) );
274 //const char* param_type = param->Attribute( "type" );
275 if( params->HasString( *nIt ) )
276 p->SetAttribute( "type", "String" );
277 else if( params->HasBool( *nIt ) )
278 p->SetAttribute( "type", "Bool" );
279 else if( params->HasInt( *nIt ) )
280 p->SetAttribute( "type", "Int" );
281 else if( params->HasUint( *nIt ) )
282 p->SetAttribute( "type", "Uint" );
283 else if( params->HasReal( *nIt ) )
284 p->SetAttribute( "type", "Real" );
285 else if( params->HasIndex( *nIt ) )
286 p->SetAttribute( "type", "Index" );
287 else if( params->HasPoint( *nIt ) )
288 p->SetAttribute( "type", "Point" );
289 else if( params->HasVector( *nIt ) )
290 p->SetAttribute( "type", "Vector" );
291 else if( params->HasStringList( *nIt ) )
292 p->SetAttribute( "type", "StringList" );
293 else if( params->HasBoolList( *nIt ) )
294 p->SetAttribute( "type", "BoolList" );
295 else if( params->HasIntList( *nIt ) )
296 p->SetAttribute( "type", "IntList" );
297 else if( params->HasUintList( *nIt ) )
298 p->SetAttribute( "type", "UintList" );
299 else if( params->HasRealList( *nIt ) )
300 p->SetAttribute( "type", "RealList" );
301 else if( params->HasIndexList( *nIt ) )
302 p->SetAttribute( "type", "IndexList" );
303 else if( params->HasPointList( *nIt ) )
304 p->SetAttribute( "type", "PointList" );
305 else if( params->HasVectorList( *nIt ) )
306 p->SetAttribute( "type", "VectorList" );
307 else if( params->HasChoices( *nIt ) )
308 p->SetAttribute( "type", "Choices" );
309 p->SetAttribute( "value", params->GetString( *nIt, false ).c_str( ) );
310 e->LinkEndChild( p );
313 root->LinkEndChild( e );
315 else if( data != NULL )
321 // Physical write and return
322 doc->LinkEndChild( root );
323 doc->SaveFile( fname.c_str( ) );
325 return( err.str( ) );