cmConfigureFileNoAutoconf.cxx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2000 National Library of Medicine
  8. All rights reserved.
  9. See COPYRIGHT.txt for copyright details.
  10. =========================================================================*/
  11. #include "cmConfigureFileNoAutoconf.h"
  12. // cmConfigureFileNoAutoconf
  13. bool cmConfigureFileNoAutoconf::Invoke(std::vector<std::string>& args)
  14. {
  15. if(args.size() != 2 )
  16. {
  17. this->SetError("called with incorrect number of arguments, expected 2");
  18. return false;
  19. }
  20. m_InputFile = args[0];
  21. m_OuputFile = args[1];
  22. return true;
  23. }
  24. void cmConfigureFileNoAutoconf::FinalPass()
  25. {
  26. #ifdef CMAKE_HAS_AUTOCONF
  27. return;
  28. #else
  29. m_Makefile->ExpandVariablesInString(m_InputFile);
  30. m_Makefile->ExpandVariablesInString(m_OuputFile);
  31. std::ifstream fin(m_InputFile.c_str());
  32. if(!fin)
  33. {
  34. cmSystemTools::Error("Could not open file for read in copy operatation",
  35. m_InputFile.c_str());
  36. return;
  37. }
  38. cmSystemTools::ConvertToUnixSlashes(m_OuputFile);
  39. std::string::size_type pos = m_OuputFile.rfind('/');
  40. if(pos != std::string::npos)
  41. {
  42. std::string path = m_OuputFile.substr(0, pos);
  43. cmSystemTools::MakeDirectory(path.c_str());
  44. }
  45. std::string tempOutputFile = m_OuputFile;
  46. tempOutputFile += ".tmp";
  47. std::ofstream fout(tempOutputFile.c_str());
  48. if(!fout)
  49. {
  50. cmSystemTools::Error("Could not open file for write in copy operatation",
  51. tempOutputFile.c_str());
  52. return;
  53. }
  54. // now copy input to output and expand varibles in the
  55. // input file at the same time
  56. const int bufSize = 4096;
  57. char buffer[bufSize];
  58. std::string inLine;
  59. while(fin)
  60. {
  61. fin.getline(buffer, bufSize);
  62. if(fin)
  63. {
  64. inLine = buffer;
  65. m_Makefile->ExpandVariablesInString(inLine);
  66. fout << inLine << "\n";
  67. }
  68. }
  69. // close the files before attempting to copy
  70. fin.close();
  71. fout.close();
  72. cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
  73. m_OuputFile.c_str());
  74. cmSystemTools::RemoveFile(tempOutputFile.c_str());
  75. #endif
  76. }