FIX: stupid pb fixed (close to being medieval'ed by The Ken)
[cmake.git] / Source / cmConfigureFileCommand.cxx
blob3df9c92542195d3bc3de48b98f52fd27461314fa
1 /*=========================================================================
3 Program: Insight Segmentation & Registration Toolkit
4 Module: $RCSfile: cmConfigureFileCommand.cxx,v $
5 Language: C++
6 Date: $Date: 2002-03-05 23:41:20 $
7 Version: $Revision: 1.15 $
9 Copyright (c) 2002 Insight Consortium. All rights reserved.
10 See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmConfigureFileCommand.h"
19 // cmConfigureFileCommand
20 bool cmConfigureFileCommand::InitialPass(std::vector<std::string> const& args)
22 if(args.size() < 2 )
24 this->SetError("called with incorrect number of arguments, expected 2");
25 return false;
27 m_InputFile = args[0];
28 m_OuputFile = args[1];
29 m_CopyOnly = false;
30 m_EscapeQuotes = false;
31 m_Immediate = false;
32 m_AtOnly = false;
33 for(unsigned int i=2;i < args.size();++i)
35 if(args[i] == "COPYONLY")
37 m_CopyOnly = true;
39 else if(args[i] == "ESCAPE_QUOTES")
41 m_EscapeQuotes = true;
43 else if(args[i] == "@ONLY")
45 m_AtOnly = true;
47 else if(args[i] == "IMMEDIATE")
49 m_Immediate = true;
53 // If we were told to copy the file immediately, then do it on the
54 // first pass (now).
55 if(m_Immediate)
57 this->ConfigureFile();
60 return true;
63 void cmConfigureFileCommand::FinalPass()
65 if(!m_Immediate)
67 this->ConfigureFile();
71 void cmConfigureFileCommand::ConfigureFile()
73 m_Makefile->AddCMakeDependFile(m_InputFile.c_str());
74 cmSystemTools::ConvertToUnixSlashes(m_OuputFile);
75 std::string::size_type pos = m_OuputFile.rfind('/');
76 if(pos != std::string::npos)
78 std::string path = m_OuputFile.substr(0, pos);
79 cmSystemTools::MakeDirectory(path.c_str());
82 if(m_CopyOnly)
84 cmSystemTools::CopyFileIfDifferent(m_InputFile.c_str(),
85 m_OuputFile.c_str());
87 else
89 std::string tempOutputFile = m_OuputFile;
90 tempOutputFile += ".tmp";
91 std::ofstream fout(tempOutputFile.c_str());
92 if(!fout)
94 cmSystemTools::Error("Could not open file for write in copy operatation ",
95 tempOutputFile.c_str());
96 return;
98 std::ifstream fin(m_InputFile.c_str());
99 if(!fin)
101 cmSystemTools::Error("Could not open file for read in copy operatation ",
102 m_InputFile.c_str());
103 return;
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 cmRegularExpression cmdefine("#cmakedefine[ \t]*([A-Za-z_0-9]*)");
112 while(fin)
114 fin.getline(buffer, bufSize);
115 if(fin)
117 inLine = buffer;
118 m_Makefile->ExpandVariablesInString(inLine, m_EscapeQuotes, m_AtOnly);
119 m_Makefile->RemoveVariablesInString(inLine, m_AtOnly);
120 // look for special cmakedefine symbol and handle it
121 // is the symbol defined
122 if (cmdefine.find(inLine))
124 const char *def = m_Makefile->GetDefinition(cmdefine.match(1).c_str());
125 if(!cmSystemTools::IsOff(def))
127 cmSystemTools::ReplaceString(inLine,
128 "#cmakedefine", "#define");
129 fout << inLine << "\n";
131 else
133 cmSystemTools::ReplaceString(inLine,
134 "#cmakedefine", "#undef");
135 fout << "/* " << inLine << " */\n";
138 else
140 fout << inLine << "\n";
144 // close the files before attempting to copy
145 fin.close();
146 fout.close();
147 cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
148 m_OuputFile.c_str());
149 cmSystemTools::RemoveFile(tempOutputFile.c_str());