cmFileCommand.cxx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 "cmFileCommand.h"
  14. // cmLibraryCommand
  15. bool cmFileCommand::InitialPass(std::vector<std::string> const& args)
  16. {
  17. if(args.size() < 2 )
  18. {
  19. this->SetError("must be called with at least two arguments.");
  20. return false;
  21. }
  22. std::string subCommand = args[0];
  23. if ( subCommand == "WRITE" )
  24. {
  25. return this->HandleWriteCommand(args, false);
  26. }
  27. else if ( subCommand == "APPEND" )
  28. {
  29. return this->HandleWriteCommand(args, true);
  30. }
  31. else if ( subCommand == "READ" )
  32. {
  33. return this->HandleReadCommand(args);
  34. }
  35. std::string e = "does not recognize sub-command "+subCommand;
  36. this->SetError(e.c_str());
  37. return true;
  38. }
  39. //----------------------------------------------------------------------------
  40. bool cmFileCommand::HandleWriteCommand(std::vector<std::string> const& args,
  41. bool append)
  42. {
  43. std::string message;
  44. std::vector<std::string>::const_iterator i = args.begin();
  45. i++; // Get rid of subcommand
  46. std::string fileName = *i;
  47. i++;
  48. for(;i != args.end(); ++i)
  49. {
  50. message += *i;
  51. }
  52. std::string dir = cmSystemTools::GetFilenamePath(fileName);
  53. cmSystemTools::MakeDirectory(dir.c_str());
  54. std::ofstream file(fileName.c_str(), append?std::ios::app: std::ios::out);
  55. if ( !file )
  56. {
  57. std::string error = "Internal CMake error when trying to open file: ";
  58. error += fileName.c_str();
  59. error += " for writting.";
  60. this->SetError(error.c_str());
  61. return false;
  62. }
  63. file << message << std::endl;
  64. file.close();
  65. return true;
  66. }
  67. //----------------------------------------------------------------------------
  68. bool cmFileCommand::HandleReadCommand(std::vector<std::string> const& args)
  69. {
  70. if ( args.size() != 3 )
  71. {
  72. this->SetError("READ must be called with two additional arguments");
  73. }
  74. std::string fileName = args[1];
  75. std::string variable = args[2];
  76. std::ifstream file(fileName.c_str(), std::ios::in);
  77. if ( !file )
  78. {
  79. std::string error = "Internal CMake error when trying to open file: ";
  80. error += fileName.c_str();
  81. error += " for reading.";
  82. this->SetError(error.c_str());
  83. return false;
  84. }
  85. std::string output;
  86. std::string line;
  87. bool has_newline = false;
  88. while ( cmSystemTools::GetLineFromStream(file, line, &has_newline) )
  89. {
  90. output += line;
  91. if ( has_newline )
  92. {
  93. output += "\n";
  94. }
  95. }
  96. m_Makefile->AddDefinition(variable.c_str(), output.c_str());
  97. return true;
  98. }