From 3f752ea4cdc91b1abc956583e1309070a71463b3 Mon Sep 17 00:00:00 2001 From: Andy Cedilnik Date: Mon, 8 Mar 2004 19:05:04 -0500 Subject: [PATCH] ENH: Move implementation of configure_file to cmMakefile, so that other classes can use it --- Source/cmConfigureFileCommand.cxx | 58 +++----------------------- Source/cmMakefile.cxx | 68 +++++++++++++++++++++++++++++++ Source/cmMakefile.h | 6 +++ 3 files changed, 79 insertions(+), 53 deletions(-) diff --git a/Source/cmConfigureFileCommand.cxx b/Source/cmConfigureFileCommand.cxx index b63e86333..bd3b4d36c 100644 --- a/Source/cmConfigureFileCommand.cxx +++ b/Source/cmConfigureFileCommand.cxx @@ -72,59 +72,11 @@ void cmConfigureFileCommand::FinalPass() void cmConfigureFileCommand::ConfigureFile() { - m_Makefile->AddCMakeDependFile(m_InputFile.c_str()); - cmSystemTools::ConvertToUnixSlashes(m_OuputFile); - mode_t perm = 0; - cmSystemTools::GetPermissions(m_InputFile.c_str(), perm); - std::string::size_type pos = m_OuputFile.rfind('/'); - if(pos != std::string::npos) - { - std::string path = m_OuputFile.substr(0, pos); - cmSystemTools::MakeDirectory(path.c_str()); - } - - if(m_CopyOnly) - { - cmSystemTools::CopyFileIfDifferent(m_InputFile.c_str(), - m_OuputFile.c_str()); - } - else - { - std::string tempOutputFile = m_OuputFile; - tempOutputFile += ".tmp"; - std::ofstream fout(tempOutputFile.c_str()); - if(!fout) - { - cmSystemTools::Error("Could not open file for write in copy operatation ", - tempOutputFile.c_str()); - return; - } - std::ifstream fin(m_InputFile.c_str()); - if(!fin) - { - cmSystemTools::Error("Could not open file for read in copy operatation ", - m_InputFile.c_str()); - return; - } - - // now copy input to output and expand variables in the - // input file at the same time - std::string inLine; - std::string outLine; - while( cmSystemTools::GetLineFromStream(fin, inLine) ) - { - outLine = ""; - m_Makefile->ConfigureString(inLine, outLine, m_AtOnly, m_EscapeQuotes); - fout << outLine.c_str() << "\n"; - } - // close the files before attempting to copy - fin.close(); - fout.close(); - cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(), - m_OuputFile.c_str()); - cmSystemTools::RemoveFile(tempOutputFile.c_str()); - cmSystemTools::SetPermissions(m_OuputFile.c_str(), perm); - } + m_Makefile->ConfigureFile(m_InputFile.c_str(), + m_OuputFile.c_str(), + m_CopyOnly, + m_AtOnly, + m_EscapeQuotes); } diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx index 02ae60f48..51a07b81c 100644 --- a/Source/cmMakefile.cxx +++ b/Source/cmMakefile.cxx @@ -2269,3 +2269,71 @@ void cmMakefile::ConfigureString(const std::string& input, this->ExpandVariablesInString(output, escapeQuotes, atOnly); this->RemoveVariablesInString(output, atOnly); } + +int cmMakefile::ConfigureFile(const char* infile, const char* outfile, + int copyonly, int atOnly, int escapeQuotes) +{ + std::string soutfile = outfile; + std::string sinfile = infile; + this->AddCMakeDependFile(infile); + cmSystemTools::ConvertToUnixSlashes(soutfile); + mode_t perm = 0; + cmSystemTools::GetPermissions(sinfile.c_str(), perm); + std::string::size_type pos = soutfile.rfind('/'); + if(pos != std::string::npos) + { + std::string path = soutfile.substr(0, pos); + cmSystemTools::MakeDirectory(path.c_str()); + } + + if(copyonly) + { + if ( !cmSystemTools::CopyFileIfDifferent(sinfile.c_str(), + soutfile.c_str())) + { + return 0; + } + } + else + { + std::string tempOutputFile = soutfile; + tempOutputFile += ".tmp"; + std::ofstream fout(tempOutputFile.c_str()); + if(!fout) + { + cmSystemTools::Error( + "Could not open file for write in copy operatation ", + tempOutputFile.c_str()); + return 0; + } + std::ifstream fin(sinfile.c_str()); + if(!fin) + { + cmSystemTools::Error("Could not open file for read in copy operatation ", + sinfile.c_str()); + return 0; + } + + // now copy input to output and expand variables in the + // input file at the same time + std::string inLine; + std::string outLine; + while( cmSystemTools::GetLineFromStream(fin, inLine) ) + { + outLine = ""; + this->ConfigureString(inLine, outLine, atOnly, escapeQuotes); + fout << outLine.c_str() << "\n"; + } + // close the files before attempting to copy + fin.close(); + fout.close(); + cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(), + soutfile.c_str()); + cmSystemTools::RemoveFile(tempOutputFile.c_str()); + cmSystemTools::SetPermissions(soutfile.c_str(), perm); + } + return 1; +} + + + diff --git a/Source/cmMakefile.h b/Source/cmMakefile.h index ffba58d45..3c26c1692 100644 --- a/Source/cmMakefile.h +++ b/Source/cmMakefile.h @@ -629,6 +629,12 @@ public: * Return a location of a file in cmake or custom modules directory */ std::string GetModulesFile(const char* name); + + /** + * Copy file but change lines acording to ConfigureString + */ + int ConfigureFile(const char* infile, const char* outfile, + int copyonly, int atOnly, int escapeQuotes); protected: // add link libraries and directories to the target