cmFileCommand.cxx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. #include "cmGlob.h"
  15. // cmLibraryCommand
  16. bool cmFileCommand::InitialPass(std::vector<std::string> const& args)
  17. {
  18. if(args.size() < 2 )
  19. {
  20. this->SetError("must be called with at least two arguments.");
  21. return false;
  22. }
  23. std::string subCommand = args[0];
  24. if ( subCommand == "WRITE" )
  25. {
  26. return this->HandleWriteCommand(args, false);
  27. }
  28. else if ( subCommand == "APPEND" )
  29. {
  30. return this->HandleWriteCommand(args, true);
  31. }
  32. else if ( subCommand == "READ" )
  33. {
  34. return this->HandleReadCommand(args);
  35. }
  36. else if ( subCommand == "GLOB" )
  37. {
  38. return this->HandleGlobCommand(args);
  39. }
  40. std::string e = "does not recognize sub-command "+subCommand;
  41. this->SetError(e.c_str());
  42. return true;
  43. }
  44. //----------------------------------------------------------------------------
  45. bool cmFileCommand::HandleWriteCommand(std::vector<std::string> const& args,
  46. bool append)
  47. {
  48. std::string message;
  49. std::vector<std::string>::const_iterator i = args.begin();
  50. i++; // Get rid of subcommand
  51. std::string fileName = *i;
  52. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  53. {
  54. fileName = m_Makefile->GetCurrentDirectory();
  55. fileName += "/" + *i;
  56. }
  57. i++;
  58. for(;i != args.end(); ++i)
  59. {
  60. message += *i;
  61. }
  62. std::string dir = cmSystemTools::GetFilenamePath(fileName);
  63. cmSystemTools::MakeDirectory(dir.c_str());
  64. std::ofstream file(fileName.c_str(), append?std::ios::app: std::ios::out);
  65. if ( !file )
  66. {
  67. std::string error = "Internal CMake error when trying to open file: ";
  68. error += fileName.c_str();
  69. error += " for writting.";
  70. this->SetError(error.c_str());
  71. return false;
  72. }
  73. file << message << std::endl;
  74. file.close();
  75. return true;
  76. }
  77. //----------------------------------------------------------------------------
  78. bool cmFileCommand::HandleReadCommand(std::vector<std::string> const& args)
  79. {
  80. if ( args.size() != 3 )
  81. {
  82. this->SetError("READ must be called with two additional arguments");
  83. return false;
  84. }
  85. std::string fileName = args[1];
  86. if ( !cmsys::SystemTools::FileIsFullPath(args[1].c_str()) )
  87. {
  88. fileName = m_Makefile->GetCurrentDirectory();
  89. fileName += "/" + args[1];
  90. }
  91. std::string variable = args[2];
  92. std::ifstream file(fileName.c_str(), std::ios::in);
  93. if ( !file )
  94. {
  95. std::string error = "Internal CMake error when trying to open file: ";
  96. error += fileName.c_str();
  97. error += " for reading.";
  98. this->SetError(error.c_str());
  99. return false;
  100. }
  101. std::string output;
  102. std::string line;
  103. bool has_newline = false;
  104. while ( cmSystemTools::GetLineFromStream(file, line, &has_newline) )
  105. {
  106. output += line;
  107. if ( has_newline )
  108. {
  109. output += "\n";
  110. }
  111. }
  112. m_Makefile->AddDefinition(variable.c_str(), output.c_str());
  113. return true;
  114. }
  115. //----------------------------------------------------------------------------
  116. bool cmFileCommand::HandleGlobCommand(std::vector<std::string> const& args)
  117. {
  118. if ( args.size() < 2 )
  119. {
  120. this->SetError("GLOB requires at least a variable name");
  121. return false;
  122. }
  123. std::vector<std::string>::const_iterator i = args.begin();
  124. i++; // Get rid of subcommand
  125. std::string variable = *i;
  126. i++;
  127. cmGlob g;
  128. std::string output = "";
  129. bool first = true;
  130. for ( ; i != args.end(); ++i )
  131. {
  132. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  133. {
  134. std::string expr = m_Makefile->GetCurrentDirectory();
  135. expr += "/" + *i;
  136. g.FindFiles(expr);
  137. }
  138. else
  139. {
  140. g.FindFiles(*i);
  141. }
  142. std::vector<std::string>::size_type cc;
  143. std::vector<std::string>& files = g.GetFiles();
  144. for ( cc = 0; cc < files.size(); cc ++ )
  145. {
  146. if ( !first )
  147. {
  148. output += ";";
  149. }
  150. output += files[cc];
  151. first = false;
  152. }
  153. }
  154. m_Makefile->AddDefinition(variable.c_str(), output.c_str());
  155. return true;
  156. }