~ubuntu-branches/ubuntu/intrepid/cmake/intrepid-backports

« back to all changes in this revision

Viewing changes to Source/cmUseMangledMesaCommand.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Maitland Bottoms
  • Date: 2002-02-14 18:36:25 UTC
  • Revision ID: james.westby@ubuntu.com-20020214183625-8m44isdas2k4l0f7
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*=========================================================================
 
2
 
 
3
  Program:   Insight Segmentation & Registration Toolkit
 
4
  Module:    $RCSfile: cmUseMangledMesaCommand.cxx,v $
 
5
  Language:  C++
 
6
  Date:      $Date: 2001/09/20 19:08:29 $
 
7
  Version:   $Revision: 1.3 $
 
8
 
 
9
Copyright (c) 2001 Insight Consortium
 
10
All rights reserved.
 
11
 
 
12
Redistribution and use in source and binary forms, with or without
 
13
modification, are permitted provided that the following conditions are met:
 
14
 
 
15
 * Redistributions of source code must retain the above copyright notice,
 
16
   this list of conditions and the following disclaimer.
 
17
 
 
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.
 
21
 
 
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.
 
25
 
 
26
  * Modified source versions must be plainly marked as such, and must not be
 
27
    misrepresented as being the original software.
 
28
 
 
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.
 
39
 
 
40
=========================================================================*/
 
41
#include "cmUseMangledMesaCommand.h"
 
42
#include "cmSystemTools.h"
 
43
 
 
44
// cmUseMangledMesaCommand
 
45
bool cmUseMangledMesaCommand::InitialPass(std::vector<std::string> const& argsIn)
 
46
 
47
  // expected two arguments:
 
48
  // arguement one: the full path to gl_mangle.h
 
49
  // arguement two : directory for output of edited headers
 
50
  if(argsIn.size() < 2)
 
51
    {
 
52
    this->SetError("called with incorrect number of arguments");
 
53
    return false;
 
54
    }
 
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);
 
62
  if(files.size() == 0)
 
63
    {
 
64
    cmSystemTools::Error("Could not open Mesa Directory ", inputDir);
 
65
    return false;
 
66
    }
 
67
  cmSystemTools::MakeDirectory(destDir);
 
68
  for(std::vector<std::string>::iterator i = files.begin();
 
69
      i != files.end(); ++i)
 
70
    {
 
71
    std::string path = inputDir;
 
72
    path += "/";
 
73
    path += *i;
 
74
    this->CopyAndFullPathMesaHeader(path.c_str(), destDir);
 
75
    }
 
76
  
 
77
  return true;
 
78
}
 
79
 
 
80
void 
 
81
cmUseMangledMesaCommand::
 
82
CopyAndFullPathMesaHeader(const char* source,
 
83
                          const char* outdir)
 
84
{
 
85
  std::string dir, file;
 
86
  cmSystemTools::SplitProgramPath(source, dir, file);
 
87
  std::string outFile = outdir;
 
88
  outFile += "/";
 
89
  outFile += file;
 
90
  std::string tempOutputFile = outFile;
 
91
  tempOutputFile += ".tmp";
 
92
  std::ofstream fout(tempOutputFile.c_str());
 
93
  if(!fout)
 
94
    {
 
95
    cmSystemTools::Error("Could not open file for write in copy operatation: ", 
 
96
                         tempOutputFile.c_str(), outdir);
 
97
    return;
 
98
    }
 
99
  std::ifstream fin(source);
 
100
  if(!fin)
 
101
    {
 
102
    cmSystemTools::Error("Could not open file for read in copy operatation",
 
103
                         source);
 
104
    return;
 
105
    }
 
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];
 
110
  std::string inLine;  
 
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)");
 
117
  while(fin)
 
118
    {
 
119
    fin.getline(buffer, bufSize);
 
120
    if(fin)
 
121
      {
 
122
      inLine = buffer;
 
123
      if(includeLine.find(inLine.c_str()))
 
124
        {
 
125
        std::string includeFile = includeLine.match(1);
 
126
        if(glDirLine.find(includeFile.c_str()))
 
127
          {
 
128
          std::string file = glDirLine.match(3);
 
129
          fout << "#include \"" << outdir << "/" << file.c_str() << "\"\n";
 
130
          }
 
131
        else if(glLine.find(includeFile.c_str()))
 
132
          {
 
133
          fout << "#include \"" << outdir << "/" << includeLine.match(1).c_str() << "\"\n";
 
134
          }
 
135
        else
 
136
          {
 
137
          fout << inLine << "\n";
 
138
          }
 
139
        }
 
140
      else
 
141
        {
 
142
        fout << inLine << "\n";
 
143
        }
 
144
      }
 
145
    }
 
146
  // close the files before attempting to copy
 
147
  fin.close();
 
148
  fout.close();
 
149
  cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
 
150
                                     outFile.c_str());
 
151
  cmSystemTools::RemoveFile(tempOutputFile.c_str());
 
152
}
 
153