OpenWalnut 1.2.5
WProjectFile.cpp
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
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     // initialize members
00046 
00047     // The module graph parser
00048     m_parsers.push_back( boost::shared_ptr< WProjectFileIO >( new WModuleProjectFileCombiner() ) );
00049 
00050     // The ROI parser
00051     m_parsers.push_back( boost::shared_ptr< WProjectFileIO >( new WRoiProjectFileIO() ) );
00052 
00053     // The Camera parser
00054     m_parsers.push_back( boost::shared_ptr< WProjectFileIO >( new WGEProjectFileIO() ) );
00055 }
00056 
00057 WProjectFile::~WProjectFile()
00058 {
00059     // cleanup
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     // the instance needs to be added here, as it else could be freed before the thread finishes ( remember: it is a shared_ptr ).
00081     WKernel::getRunningKernel()->getRootContainer()->addPendingThread( shared_from_this() );
00082 
00083     // actually run
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     // open the file for write
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     // allow each parser to handle save request
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         // Parse the file
00119         wlog::info( "Project File" ) << "Loading project file \"" << m_project.file_string() << "\".";
00120 
00121         // read the file
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         // the comment
00130         static const boost::regex commentRe( "^ *//.*$" );
00131 
00132         // read it line by line
00133         std::string line;       // the current line
00134         int i = 0;              // line counter
00135         bool match = false;     // true of a parser successfully parsed the line
00136         boost::smatch matches;  // the list of matches
00137 
00138         while( std::getline( input, line ) )
00139         {
00140             ++i;    // line number
00141             match = false;
00142 
00143             // allow each parser to handle the line.
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                         // the first parser matching this line -> next line
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             // did someone match this line? Or is it empty or a comment?
00162             if( !match && !line.empty() && !boost::regex_match( line, matches, commentRe ) )
00163             {
00164                 // no it is something else -> warning!
00165                 wlog::warn( "Project Loader" ) << "Line " << i << ": Malformed. Skipping.";
00166             }
00167         }
00168 
00169         input.close();
00170 
00171         // finally, let every one know that we have finished
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         // remove from thread list
00180         WKernel::getRunningKernel()->getRootContainer()->finishedPendingThread( shared_from_this() );
00181 
00182         // re-throw
00183         throw e;
00184     }
00185 
00186     // remove from thread list
00187     WKernel::getRunningKernel()->getRootContainer()->finishedPendingThread( shared_from_this() );
00188 }
00189 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends