1 /*=========================================================================
3 Program: Insight Segmentation & Registration Toolkit
4 Module: $RCSfile: cmConfigureFileCommand.cxx,v $
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
)
24 this->SetError("called with incorrect number of arguments, expected 2");
27 m_InputFile
= args
[0];
28 m_OuputFile
= args
[1];
30 m_EscapeQuotes
= false;
33 for(unsigned int i
=2;i
< args
.size();++i
)
35 if(args
[i
] == "COPYONLY")
39 else if(args
[i
] == "ESCAPE_QUOTES")
41 m_EscapeQuotes
= true;
43 else if(args
[i
] == "@ONLY")
47 else if(args
[i
] == "IMMEDIATE")
53 // If we were told to copy the file immediately, then do it on the
57 this->ConfigureFile();
63 void cmConfigureFileCommand::FinalPass()
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());
84 cmSystemTools::CopyFileIfDifferent(m_InputFile
.c_str(),
89 std::string tempOutputFile
= m_OuputFile
;
90 tempOutputFile
+= ".tmp";
91 std::ofstream
fout(tempOutputFile
.c_str());
94 cmSystemTools::Error("Could not open file for write in copy operatation ",
95 tempOutputFile
.c_str());
98 std::ifstream
fin(m_InputFile
.c_str());
101 cmSystemTools::Error("Could not open file for read in copy operatation ",
102 m_InputFile
.c_str());
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 cmRegularExpression
cmdefine("#cmakedefine[ \t]*([A-Za-z_0-9]*)");
114 fin
.getline(buffer
, bufSize
);
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";
133 cmSystemTools::ReplaceString(inLine
,
134 "#cmakedefine", "#undef");
135 fout
<< "/* " << inLine
<< " */\n";
140 fout
<< inLine
<< "\n";
144 // close the files before attempting to copy
147 cmSystemTools::CopyFileIfDifferent(tempOutputFile
.c_str(),
148 m_OuputFile
.c_str());
149 cmSystemTools::RemoveFile(tempOutputFile
.c_str());