cmSourceFile.cxx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 "cmSourceFile.h"
  14. #include "cmStandardIncludes.h"
  15. #include "cmSystemTools.h"
  16. // Set the name of the class and the full path to the file.
  17. // The class must be found in dir and end in name.cxx, name.txx,
  18. // name.c or it will be considered a header file only class
  19. // and not included in the build process
  20. void cmSourceFile::SetName(const char* name, const char* dir,
  21. const std::vector<std::string>& sourceExts,
  22. const std::vector<std::string>& headerExts)
  23. {
  24. m_HeaderFileOnly = true;
  25. m_SourceName = name;
  26. std::string pathname = dir;
  27. // the name might include the full path already, so
  28. // check for this case
  29. if (name && (name[0] == '/' ||
  30. (name[0] != '\0' && name[1] == ':')))
  31. {
  32. pathname = "";
  33. }
  34. if(pathname != "")
  35. {
  36. pathname += "/";
  37. }
  38. // First try and see whether the listed file can be found
  39. // as is without extensions added on.
  40. pathname += m_SourceName;
  41. std::string hname = pathname;
  42. if(cmSystemTools::FileExists(hname.c_str()))
  43. {
  44. std::string::size_type pos = hname.rfind('.');
  45. if(pos != std::string::npos)
  46. {
  47. m_SourceExtension = hname.substr(pos+1, hname.size()-pos);
  48. std::string::size_type pos2 = hname.rfind('/');
  49. if(pos2 != std::string::npos)
  50. {
  51. m_SourceName = hname.substr(pos2+1, pos - pos2-1);
  52. }
  53. else
  54. {
  55. m_SourceName = hname.substr(0, pos);
  56. }
  57. }
  58. // See if the file is a header file
  59. if(std::find( headerExts.begin(), headerExts.end(), m_SourceExtension ) == headerExts.end())
  60. m_HeaderFileOnly = false;
  61. else
  62. m_HeaderFileOnly = true;
  63. m_FullPath = hname;
  64. return;
  65. }
  66. // Next, try the various source extensions
  67. for( std::vector<std::string>::const_iterator ext = sourceExts.begin();
  68. ext != sourceExts.end(); ++ext )
  69. {
  70. hname = pathname;
  71. hname += ".";
  72. hname += *ext;
  73. if(cmSystemTools::FileExists(hname.c_str()))
  74. {
  75. m_SourceExtension = *ext;
  76. m_HeaderFileOnly = false;
  77. m_FullPath = hname;
  78. return;
  79. }
  80. }
  81. // Finally, try the various header extensions
  82. for( std::vector<std::string>::const_iterator ext = headerExts.begin();
  83. ext != headerExts.end(); ++ext )
  84. {
  85. hname = pathname;
  86. hname += ".";
  87. hname += *ext;
  88. if(cmSystemTools::FileExists(hname.c_str()))
  89. {
  90. m_SourceExtension = *ext;
  91. m_FullPath = hname;
  92. return;
  93. }
  94. }
  95. std::string errorMsg = "\n\nTried";
  96. for( std::vector<std::string>::const_iterator ext = sourceExts.begin();
  97. ext != sourceExts.end(); ++ext )
  98. {
  99. errorMsg += " .";
  100. errorMsg += *ext;
  101. }
  102. for( std::vector<std::string>::const_iterator ext = headerExts.begin();
  103. ext != headerExts.end(); ++ext )
  104. {
  105. errorMsg += " .";
  106. errorMsg += *ext;
  107. }
  108. cmSystemTools::Error("can not find file ", pathname.c_str(),
  109. errorMsg.c_str());
  110. }
  111. void cmSourceFile::SetName(const char* name, const char* dir, const char *ext,
  112. bool hfo)
  113. {
  114. m_HeaderFileOnly = hfo;
  115. m_SourceName = name;
  116. std::string pathname = dir;
  117. if(pathname != "")
  118. {
  119. pathname += "/";
  120. }
  121. pathname += m_SourceName;
  122. if(ext && strlen(ext))
  123. {
  124. pathname += ".";
  125. pathname += ext;
  126. }
  127. m_FullPath = pathname;
  128. m_SourceExtension = ext;
  129. return;
  130. }
  131. void cmSourceFile::Print() const
  132. {
  133. if(m_AbstractClass)
  134. {
  135. std::cout << "Abstract ";
  136. }
  137. else
  138. {
  139. std::cout << "Concrete ";
  140. }
  141. if(m_HeaderFileOnly)
  142. {
  143. std::cout << "Header file ";
  144. }
  145. else
  146. {
  147. std::cout << "CXX file ";
  148. }
  149. std::cout << m_SourceName << std::endl;
  150. }