]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/WorkspaceIO.cxx
More on graph editor
[cpPlugins.git] / lib / cpPlugins / Interface / WorkspaceIO.cxx
1 #include <cpPlugins/Interface/Workspace.h>
2 #include <third_party/tinyxml/tinyxml.h>
3
4 // -------------------------------------------------------------------------
5 std::string cpPlugins::Interface::Workspace::
6 LoadWorkspace( const std::string& fname )
7 {
8   TiXmlDocument* doc = new TiXmlDocument( fname.c_str( ) );
9   doc->LoadFile( );
10   TiXmlElement* root = doc->RootElement( );
11   if( root == NULL )
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;
16
17   // Read plugins files
18   TiXmlElement* plugins = root->FirstChildElement( "plugins" );
19   while( plugins != NULL )
20   {
21     const char* value = plugins->Attribute( "filename" );
22     if( value != NULL )
23     {
24       if( !( this->LoadPlugins( value ) ) )
25         err << "no valid plugins file \"" << value << "\"" << std::endl;
26
27     } // fi
28     plugins = plugins->NextSiblingElement( "plugins" );
29
30   } // elihw
31
32   // Read plugins paths
33   plugins = root->FirstChildElement( "plugins_dir" );
34   while( plugins != NULL )
35   {
36     const char* value = plugins->Attribute( "path" );
37     if( value != NULL )
38     {
39       int recursive;
40       if( plugins->QueryIntAttribute( "recursive", &recursive ) != TIXML_SUCCESS )
41         recursive = 0;
42       if( !( this->LoadPluginsPath( value, recursive == 1 ) ) )
43         err << "No valid plugins path \"" << value << "\"" << std::endl;
44
45     } // fi
46     plugins = plugins->NextSiblingElement( "plugins_dir" );
47
48   } // elihw
49
50   // Read filters
51   TiXmlElement* filter = root->FirstChildElement( "filter" );
52   while( filter != NULL )
53   {
54     const char* class_value = filter->Attribute( "class" );
55     const char* name_value = filter->Attribute( "name" );
56     if( class_value != NULL && name_value != NULL )
57     {
58       if( this->CreateFilter( class_value, name_value ) )
59       {
60         // Read parameters
61         TParameters* parameters = this->GetParameters( name_value );
62         TiXmlElement* param = filter->FirstChildElement( "parameter" );
63         while( param != NULL )
64         {
65           const char* param_name = param->Attribute( "name" );
66           const char* param_type = param->Attribute( "type" );
67           if( param_name != NULL && param_type != NULL )
68           {
69             std::string param_type_str( param_type );
70             const char* value = param->Attribute( "value" );
71             if( value != NULL )
72             {
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" )
79               {
80                 TParameters::TInt v;
81                 value_str >> v;
82                 parameters->SetInt( param_name, v );
83               }
84               else if( param_type_str == "Uint" )
85               {
86                 TParameters::TUint v;
87                 value_str >> v;
88                 parameters->SetUint( param_name, v );
89               }
90               else if( param_type_str == "Real" )
91               {
92                 TParameters::TReal v;
93                 value_str >> v;
94                 parameters->SetReal( param_name, v );
95               }
96               /* TODO
97                  else if( param_type_str == "Index" )
98                  else if( param_type_str == "Point" )
99                  else if( param_type_str == "Vector" )
100               */
101             }
102             else
103             {
104               if( param_type_str == "StringList" )
105               {
106                 TiXmlElement* item = param->FirstChildElement( "item" );
107                 while( item != NULL )
108                 {
109                   value = item->Attribute( "value" );
110                   if( value != NULL )
111                     parameters->AddToStringList( param_name, value );
112                   item = item->NextSiblingElement( "item" );
113
114                 } // elihw
115               }
116               else if( param_type_str == "BoolList" )
117               {
118                 TiXmlElement* item = param->FirstChildElement( "item" );
119                 while( item != NULL )
120                 {
121                   value = item->Attribute( "value" );
122                   if( value != NULL )
123                     parameters->AddToBoolList( param_name, value[ 0 ] != '0' );
124                   item = item->NextSiblingElement( "item" );
125
126                 } // elihw
127               }
128               else if( param_type_str == "IntList" )
129               {
130                 TiXmlElement* item = param->FirstChildElement( "item" );
131                 while( item != NULL )
132                 {
133                   value = item->Attribute( "value" );
134                   if( value != NULL )
135                   {
136                     std::istringstream value_str( value );
137                     TParameters::TInt v;
138                     value_str >> v;
139                     parameters->AddToIntList( param_name, v );
140
141                   } // fi
142                   item = item->NextSiblingElement( "item" );
143
144                 } // elihw
145               }
146               else if( param_type_str == "UintList" )
147               {
148                 TiXmlElement* item = param->FirstChildElement( "item" );
149                 while( item != NULL )
150                 {
151                   value = item->Attribute( "value" );
152                   if( value != NULL )
153                   {
154                     std::istringstream value_str( value );
155                     TParameters::TUint v;
156                     value_str >> v;
157                     parameters->AddToUintList( param_name, v );
158
159                   } // fi
160                   item = item->NextSiblingElement( "item" );
161
162                 } // elihw
163               }
164               else if( param_type_str == "RealList" )
165               {
166                 TiXmlElement* item = param->FirstChildElement( "item" );
167                 while( item != NULL )
168                 {
169                   value = item->Attribute( "value" );
170                   if( value != NULL )
171                   {
172                     std::istringstream value_str( value );
173                     TParameters::TReal v;
174                     value_str >> v;
175                     parameters->AddToRealList( param_name, v );
176
177                   } // fi
178                   item = item->NextSiblingElement( "item" );
179
180                 } // elihw
181               }
182               /* TODO
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" );
187               */
188             } // fi
189
190           } // fi
191           param = param->NextSiblingElement( "parameter" );
192
193         } // elihw
194       }
195       else
196         err
197           << "No valid class \"" << class_value << "\" with name \""
198           << name_value << "\"" << std::endl;
199     }
200     else
201       err << "Incomplete data." << std::endl;
202     filter = filter->NextSiblingElement( "filter" );
203
204   } // elihw
205
206   // Read filters
207   TiXmlElement* connection = root->FirstChildElement( "connection" );
208   while( connection != NULL )
209   {
210     TiXmlElement* orig = connection->FirstChildElement( "origin" );
211     TiXmlElement* dest = connection->FirstChildElement( "destination" );
212     if( orig != NULL && dest != NULL )
213     {
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" );
218       if(
219         orig_filter != NULL && dest_filter != NULL &&
220         orig_name != NULL && dest_name != NULL
221         )
222         this->Connect( orig_filter, dest_filter, orig_name, dest_name );
223
224     } // fi
225     connection = connection->NextSiblingElement( "connection" );
226
227   } // elihw
228
229   // Finish and return
230   delete doc;
231   return( err.str( ) );
232 }
233
234 // -------------------------------------------------------------------------
235 std::string cpPlugins::Interface::Workspace::
236 SaveWorkspace( const std::string& fname ) const
237 {
238   std::stringstream err;
239   TiXmlDocument* doc = new TiXmlDocument( );
240   TiXmlElement* root = new TiXmlElement( "cpPlugins_Workspace" );
241
242   // Save plugins
243   for(
244     auto plugIt = this->m_LoadedPlugins.begin( );
245     plugIt != this->m_LoadedPlugins.end( );
246     ++plugIt
247     )
248   {
249     TiXmlElement* plugin = new TiXmlElement( "plugins" );
250     plugin->SetAttribute( "filename", plugIt->c_str( ) );
251     root->LinkEndChild( plugin );
252
253   } // rof
254
255   // Save vertices
256   auto vIt = this->m_Graph->BeginVertices( );
257   for( ; vIt != this->m_Graph->EndVertices( ); ++vIt )
258   {
259     auto filter = dynamic_cast< TFilter* >( vIt->second.GetPointer( ) );
260     auto data = dynamic_cast< TData* >( vIt->second.GetPointer( ) );
261     if( filter != NULL )
262     {
263       TiXmlElement* e = new TiXmlElement( "filter" );
264       e->SetAttribute( "class", filter->GetClassName( ).c_str( ) );
265       e->SetAttribute( "name", filter->GetName( ) );
266
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 )
271       {
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 );
311
312       } // rof
313       root->LinkEndChild( e );
314     }
315     else if( data != NULL )
316     {
317     } // fi
318
319   } // rof
320
321   // Physical write and return
322   doc->LinkEndChild( root );
323   doc->SaveFile( fname.c_str( ) );
324   delete doc;
325   return( err.str( ) );
326 }
327
328 // eof - $RCSfile$