cmWriteFileCommand.cxx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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& argsIn)
  16. {
  17. if(argsIn.size() < 2 )
  18. {
  19. this->SetError("called with incorrect number of arguments");
  20. return false;
  21. }
  22. std::vector<std::string> args;
  23. cmSystemTools::ExpandListArguments(argsIn, args);
  24. std::string message;
  25. std::vector<std::string>::const_iterator i = args.begin();
  26. std::string fileName = *i;
  27. bool overwrite = true;
  28. i++;
  29. for(;i != args.end(); ++i)
  30. {
  31. if ( *i == "APPEND" )
  32. {
  33. overwrite = false;
  34. }
  35. else
  36. {
  37. message += *i;
  38. }
  39. }
  40. std::string dir = cmSystemTools::GetFilenamePath(fileName);
  41. cmSystemTools::MakeDirectory(dir.c_str());
  42. std::ofstream file(fileName.c_str(), overwrite?std::ios::out : std::ios::app);
  43. if ( !file )
  44. {
  45. std::string error = "Internal CMake error when trying to open file: ";
  46. error += fileName.c_str();
  47. this->SetError(error.c_str());
  48. return false;
  49. }
  50. file << message << std::endl;
  51. file.close();
  52. return true;
  53. }