cmWriteFileCommand.cxx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmWriteFileCommand.h"
  14. // cmLibraryCommand
  15. bool cmWriteFileCommand::InitialPass(std::vector<std::string> const& args)
  16. {
  17. if(args.size() < 2 )
  18. {
  19. this->SetError("called with incorrect number of arguments");
  20. return false;
  21. }
  22. std::string message;
  23. std::vector<std::string>::const_iterator i = args.begin();
  24. std::string fileName = *i;
  25. bool overwrite = true;
  26. i++;
  27. for(;i != args.end(); ++i)
  28. {
  29. if ( *i == "APPEND" )
  30. {
  31. overwrite = false;
  32. }
  33. else
  34. {
  35. message += *i;
  36. }
  37. }
  38. std::string dir = cmSystemTools::GetFilenamePath(fileName);
  39. cmSystemTools::MakeDirectory(dir.c_str());
  40. std::ofstream file(fileName.c_str(), overwrite?std::ios::out : std::ios::app);
  41. if ( !file )
  42. {
  43. std::string error = "Internal CMake error when trying to open file: ";
  44. error += fileName.c_str();
  45. this->SetError(error.c_str());
  46. return false;
  47. }
  48. file << message << std::endl;
  49. file.close();
  50. return true;
  51. }