WProjectFile.cpp 4.26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

#include <fstream>
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
26 27
#include <string>
#include <vector>
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

#include <boost/regex.hpp>

#include "../kernel/WKernel.h"
#include "../kernel/combiner/WModuleProjectFileCombiner.h"
#include "exceptions/WFileNotFound.h"

#include "WProjectFile.h"

WProjectFile::WProjectFile( boost::filesystem::path project ):
    WThreadedRunner(),
    boost::enable_shared_from_this< WProjectFile >(),
    m_project( project )
{
    // initialize members

    // The module graph parser
    m_parsers.push_back( new WModuleProjectFileCombiner() );

    // The ROI parser
    // m_parsers.push_back( new W???() );

    // The Camera parser
    // m_parsers.push_back( new W???() );
}

WProjectFile::~WProjectFile()
{
    // cleanup
}

59
void WProjectFile::load()
60 61 62 63 64
{
    // the instance needs to be added here, as it else could be freed before the thread finishes ( remember: it is a shared_ptr ).
    WKernel::getRunningKernel()->getRootContainer()->addPendingThread( shared_from_this() );

    // actually run
65
    run();
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
}

void WProjectFile::threadMain()
{
    try
    {
        // Parse the file
        wlog::info( "Project File" ) << "Loading project file \"" << m_project.file_string() << "\".";

        // read the file
        std::ifstream input( m_project.file_string().c_str() );
        if ( !input.is_open() )
        {
            throw WFileNotFound( "The project file \"" + m_project.file_string() + "\" does not exist." );
        }

        // the comment
        static const boost::regex commentRe( "^//.*$" );

        // read it line by line
        std::string line;       // the current line
        int i = 0;              // line counter
        bool match = false;     // true of a parser successfully parsed the line
        boost::smatch matches;  // the list of matches

        while ( std::getline( input, line ) )
        {
            ++i;    // line number
            match = false;

            // allow each parser to handle the line.
            for ( std::vector< WProjectFileParser* >::const_iterator iter = m_parsers.begin(); iter != m_parsers.end(); ++iter )
            {
                if ( ( *iter )->parse( line, i ) )
                {
                    match = true;
                    // the first parser matching this line -> next line
                    break;
                }
            }

            // did someone match this line? Or is it empty or a comment?
            if ( !match && !line.empty() && !boost::regex_match( line, matches, commentRe ) )
            {
                // no it is something else -> warning!
                wlog::warn( "Project Loader" ) << "Line " << i << ": Malformed. Skipping.";
            }
        }

        input.close();

        // finally, let every one know that we have finished
        for ( std::vector< WProjectFileParser* >::const_iterator iter = m_parsers.begin(); iter != m_parsers.end(); ++iter )
        {
            ( *iter )->done();
        }
    }
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
123
    catch( const std::exception& e )
124 125 126 127 128 129 130 131 132 133 134 135
    {
        // remove from thread list
        WKernel::getRunningKernel()->getRootContainer()->finishedPendingThread( shared_from_this() );

        // re-throw
        throw e;
    }

    // remove from thread list
    WKernel::getRunningKernel()->getRootContainer()->finishedPendingThread( shared_from_this() );
}