00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <fstream>
00026 #include <string>
00027 #include <vector>
00028
00029 #include <boost/regex.hpp>
00030
00031 #include "WKernel.h"
00032 #include "combiner/WModuleProjectFileCombiner.h"
00033 #include "WRoiProjectFileIO.h"
00034 #include "../graphicsEngine/WGEProjectFileIO.h"
00035 #include "../common/exceptions/WFileNotFound.h"
00036 #include "../common/exceptions/WFileOpenFailed.h"
00037
00038 #include "WProjectFile.h"
00039
00040 WProjectFile::WProjectFile( boost::filesystem::path project ):
00041 WThreadedRunner(),
00042 boost::enable_shared_from_this< WProjectFile >(),
00043 m_project( project )
00044 {
00045
00046
00047
00048 m_parsers.push_back( boost::shared_ptr< WProjectFileIO >( new WModuleProjectFileCombiner() ) );
00049
00050
00051 m_parsers.push_back( boost::shared_ptr< WProjectFileIO >( new WRoiProjectFileIO() ) );
00052
00053
00054 m_parsers.push_back( boost::shared_ptr< WProjectFileIO >( new WGEProjectFileIO() ) );
00055 }
00056
00057 WProjectFile::~WProjectFile()
00058 {
00059
00060 m_parsers.clear();
00061 }
00062
00063 boost::shared_ptr< WProjectFileIO > WProjectFile::getCameraWriter()
00064 {
00065 return boost::shared_ptr< WProjectFileIO >( new WGEProjectFileIO() );
00066 }
00067
00068 boost::shared_ptr< WProjectFileIO > WProjectFile::getModuleWriter()
00069 {
00070 return boost::shared_ptr< WProjectFileIO >( new WModuleProjectFileCombiner() );
00071 }
00072
00073 boost::shared_ptr< WProjectFileIO > WProjectFile::getROIWriter()
00074 {
00075 return boost::shared_ptr< WProjectFileIO >( new WRoiProjectFileIO() );
00076 }
00077
00078 void WProjectFile::load()
00079 {
00080
00081 WKernel::getRunningKernel()->getRootContainer()->addPendingThread( shared_from_this() );
00082
00083
00084 run();
00085 }
00086
00087 void WProjectFile::save( const std::vector< boost::shared_ptr< WProjectFileIO > >& writer )
00088 {
00089 wlog::info( "Project File" ) << "Saving project file \"" << m_project.file_string() << "\".";
00090
00091
00092 std::ofstream output( m_project.file_string().c_str() );
00093 if( !output.is_open() )
00094 {
00095 throw WFileOpenFailed( std::string( "The project file \"" ) + m_project.file_string() +
00096 std::string( "\" could not be opened for write access." ) );
00097 }
00098
00099
00100 for( std::vector< boost::shared_ptr< WProjectFileIO > >::const_iterator iter = writer.begin(); iter != writer.end(); ++iter )
00101 {
00102 ( *iter )->save( output );
00103 output << std::endl;
00104 }
00105
00106 output.close();
00107 }
00108
00109 void WProjectFile::save()
00110 {
00111 save( m_parsers );
00112 }
00113
00114 void WProjectFile::threadMain()
00115 {
00116 try
00117 {
00118
00119 wlog::info( "Project File" ) << "Loading project file \"" << m_project.file_string() << "\".";
00120
00121
00122 std::ifstream input( m_project.file_string().c_str() );
00123 if( !input.is_open() )
00124 {
00125 throw WFileNotFound( std::string( "The project file \"" ) + m_project.file_string() +
00126 std::string( "\" does not exist." ) );
00127 }
00128
00129
00130 static const boost::regex commentRe( "^ *//.*$" );
00131
00132
00133 std::string line;
00134 int i = 0;
00135 bool match = false;
00136 boost::smatch matches;
00137
00138 while( std::getline( input, line ) )
00139 {
00140 ++i;
00141 match = false;
00142
00143
00144 for( std::vector< boost::shared_ptr< WProjectFileIO > >::const_iterator iter = m_parsers.begin(); iter != m_parsers.end(); ++iter )
00145 {
00146 try
00147 {
00148 if( ( *iter )->parse( line, i ) )
00149 {
00150 match = true;
00151
00152 break;
00153 }
00154 }
00155 catch( const std::exception& e )
00156 {
00157 wlog::error( "Project Loader" ) << "Line " << i << ": Parsing caused an exception. Line Malformed? Skipping.";
00158 }
00159 }
00160
00161
00162 if( !match && !line.empty() && !boost::regex_match( line, matches, commentRe ) )
00163 {
00164
00165 wlog::warn( "Project Loader" ) << "Line " << i << ": Malformed. Skipping.";
00166 }
00167 }
00168
00169 input.close();
00170
00171
00172 for( std::vector< boost::shared_ptr< WProjectFileIO > >::const_iterator iter = m_parsers.begin(); iter != m_parsers.end(); ++iter )
00173 {
00174 ( *iter )->done();
00175 }
00176 }
00177 catch( const std::exception& e )
00178 {
00179
00180 WKernel::getRunningKernel()->getRootContainer()->finishedPendingThread( shared_from_this() );
00181
00182
00183 throw e;
00184 }
00185
00186
00187 WKernel::getRunningKernel()->getRootContainer()->finishedPendingThread( shared_from_this() );
00188 }
00189