OpenWalnut  1.5.0dev
WGEShaderCodeInjector.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <algorithm>
26 #include <ostream>
27 #include <sstream>
28 #include <string>
29 
30 #include <boost/regex.hpp>
31 
32 #include "../../common/WLogger.h"
33 #include "WGEShaderCodeInjector.h"
34 
36  m_keyword( std::string( "%" ) + keyword + std::string( "%" ) )
37 {
38  // initialize members
39 }
40 
42 {
43  // cleanup
44 }
45 
46 std::string WGEShaderCodeInjector::process( const std::string& /*file*/, const std::string& code ) const
47 {
48  if( !getActive() )
49  {
50  return code;
51  }
52 
53  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
54  // replace keyword
55  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
56 
57  // go through each line again
58  std::string line;
59  std::stringstream completeCode( code );
60  std::ostringstream newCode;
61  while( std::getline( completeCode, line ) )
62  {
63  size_t found = line.find( m_keyword );
64  if( found != std::string::npos )
65  {
66  line.replace( found, m_keyword.length(), m_code );
67  }
68  newCode << line << std::endl;
69  }
70 
71  return newCode.str();
72 }
73 
74 void WGEShaderCodeInjector::setCode( std::string code )
75 {
76  m_code = code;
77  updated();
78 }
std::string m_code
the custom code.
virtual ~WGEShaderCodeInjector()
Destructor.
virtual std::string process(const std::string &file, const std::string &code) const
Process the whole code.
void setCode(std::string code)
Define the code that replaces the keyword.
std::string m_keyword
What to replace.
WGEShaderCodeInjector(std::string keyword)
Default constructor.
virtual void updated()
Fires m_updateCondition which should denote an update in the preprocessor filter.
bool getActive() const
If the preprocessor is active, this returns true.