1
/*=========================================================================
3
Program: Insight Segmentation & Registration Toolkit
4
Module: $RCSfile: cmUseMangledMesaCommand.cxx,v $
6
Date: $Date: 2001/09/20 19:08:29 $
7
Version: $Revision: 1.3 $
9
Copyright (c) 2001 Insight Consortium
12
Redistribution and use in source and binary forms, with or without
13
modification, are permitted provided that the following conditions are met:
15
* Redistributions of source code must retain the above copyright notice,
16
this list of conditions and the following disclaimer.
18
* Redistributions in binary form must reproduce the above copyright notice,
19
this list of conditions and the following disclaimer in the documentation
20
and/or other materials provided with the distribution.
22
* The name of the Insight Consortium, nor the names of any consortium members,
23
nor of any contributors, may be used to endorse or promote products derived
24
from this software without specific prior written permission.
26
* Modified source versions must be plainly marked as such, and must not be
27
misrepresented as being the original software.
29
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
30
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
33
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
36
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
37
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40
=========================================================================*/
41
#include "cmUseMangledMesaCommand.h"
42
#include "cmSystemTools.h"
44
// cmUseMangledMesaCommand
45
bool cmUseMangledMesaCommand::InitialPass(std::vector<std::string> const& argsIn)
47
// expected two arguments:
48
// arguement one: the full path to gl_mangle.h
49
// arguement two : directory for output of edited headers
52
this->SetError("called with incorrect number of arguments");
55
std::vector<std::string> args = argsIn;
56
m_Makefile->ExpandVariablesInString(args[0]);
57
m_Makefile->ExpandVariablesInString(args[1]);
58
const char* inputDir = args[0].c_str();
59
const char* destDir = args[1].c_str();
60
std::vector<std::string> files;
61
cmSystemTools::Glob(inputDir, "\\.h$", files);
64
cmSystemTools::Error("Could not open Mesa Directory ", inputDir);
67
cmSystemTools::MakeDirectory(destDir);
68
for(std::vector<std::string>::iterator i = files.begin();
69
i != files.end(); ++i)
71
std::string path = inputDir;
74
this->CopyAndFullPathMesaHeader(path.c_str(), destDir);
81
cmUseMangledMesaCommand::
82
CopyAndFullPathMesaHeader(const char* source,
85
std::string dir, file;
86
cmSystemTools::SplitProgramPath(source, dir, file);
87
std::string outFile = outdir;
90
std::string tempOutputFile = outFile;
91
tempOutputFile += ".tmp";
92
std::ofstream fout(tempOutputFile.c_str());
95
cmSystemTools::Error("Could not open file for write in copy operatation: ",
96
tempOutputFile.c_str(), outdir);
99
std::ifstream fin(source);
102
cmSystemTools::Error("Could not open file for read in copy operatation",
106
// now copy input to output and expand varibles in the
107
// input file at the same time
108
const int bufSize = 4096;
109
char buffer[bufSize];
111
// regular expression for any #include line
112
cmRegularExpression includeLine("^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)[\">]");
113
// regular expression for gl/ or GL/ in a file (match(1) of above)
114
cmRegularExpression glDirLine("(gl|GL)(/|\\\\)([^<\"]+)");
115
// regular expression for gl GL or xmesa in a file (match(1) of above)
116
cmRegularExpression glLine("(gl|GL|xmesa)");
119
fin.getline(buffer, bufSize);
123
if(includeLine.find(inLine.c_str()))
125
std::string includeFile = includeLine.match(1);
126
if(glDirLine.find(includeFile.c_str()))
128
std::string file = glDirLine.match(3);
129
fout << "#include \"" << outdir << "/" << file.c_str() << "\"\n";
131
else if(glLine.find(includeFile.c_str()))
133
fout << "#include \"" << outdir << "/" << includeLine.match(1).c_str() << "\"\n";
137
fout << inLine << "\n";
142
fout << inLine << "\n";
146
// close the files before attempting to copy
149
cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
151
cmSystemTools::RemoveFile(tempOutputFile.c_str());