cmGetFilenameComponentCommand.cxx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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 "cmGetFilenameComponentCommand.h"
  14. #include "cmSystemTools.h"
  15. // cmGetFilenameComponentCommand
  16. bool cmGetFilenameComponentCommand::InitialPass(std::vector<std::string> const& args)
  17. {
  18. if(args.size() < 3)
  19. {
  20. this->SetError("called with incorrect number of arguments");
  21. return false;
  22. }
  23. // Check and see if the value has been stored in the cache
  24. // already, if so use that value
  25. if(args.size() == 4 && args[3] == "CACHE")
  26. {
  27. const char* cacheValue = m_Makefile->GetDefinition(args[0].c_str());
  28. if(cacheValue && strcmp(cacheValue, "NOTFOUND"))
  29. {
  30. return true;
  31. }
  32. }
  33. std::string result;
  34. std::string filename = args[1];
  35. m_Makefile->ExpandVariablesInString(filename);
  36. if (args[2] == "PATH")
  37. {
  38. result = cmSystemTools::GetFilenamePath(filename);
  39. }
  40. else if (args[2] == "NAME")
  41. {
  42. result = cmSystemTools::GetFilenameName(filename);
  43. }
  44. else if (args[2] == "EXT")
  45. {
  46. result = cmSystemTools::GetFilenameExtension(filename);
  47. }
  48. else if (args[2] == "NAME_WE")
  49. {
  50. result = cmSystemTools::GetFilenameNameWithoutExtension(filename);
  51. }
  52. else
  53. {
  54. std::string err = "unknow component " + args[2];
  55. this->SetError(err.c_str());
  56. return false;
  57. }
  58. if(args.size() == 4 && args[3] == "CACHE")
  59. {
  60. m_Makefile->AddCacheDefinition(args[0].c_str(),
  61. result.c_str(),
  62. "",
  63. args[2] == "PATH" ? cmCacheManager::FILEPATH
  64. : cmCacheManager::STRING);
  65. }
  66. else
  67. {
  68. m_Makefile->AddDefinition(args[0].c_str(), result.c_str());
  69. }
  70. return true;
  71. }