cmSystemTools.cxx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "cmSystemTools.h"
  2. #include "errno.h"
  3. #include <sys/stat.h>
  4. #ifdef _MSC_VER
  5. #include <windows.h>
  6. #include <direct.h>
  7. inline int Mkdir(const char* dir)
  8. {
  9. return _mkdir(dir);
  10. }
  11. #else
  12. #include <sys/types.h>
  13. #include <fcntl.h>
  14. #include <unistd.h>
  15. inline int Mkdir(const char* dir)
  16. {
  17. return mkdir(dir, 00700);
  18. }
  19. #endif
  20. // remove extra spaces and the "\" character from the name
  21. // of the class as it is in the CMakeLists.txt
  22. std::string cmSystemTools::CleanUpName(const char* name)
  23. {
  24. std::string className = name;
  25. size_t i =0;
  26. while(className[i] == ' ')
  27. {
  28. i++;
  29. }
  30. if(i)
  31. {
  32. className = className.substr(i, className.size());
  33. }
  34. size_t pos = className.find('\\');
  35. if(pos != std::string::npos)
  36. {
  37. className = className.substr(0, pos);
  38. }
  39. pos = className.find(' ');
  40. if(pos != std::string::npos)
  41. {
  42. className = className.substr(0, pos);
  43. }
  44. return className;
  45. }
  46. bool cmSystemTools::MakeDirectory(const char* path)
  47. {
  48. std::string dir = path;
  49. // replace all of the \ with /
  50. size_t pos = 0;
  51. while((pos = dir.find('\\', pos)) != std::string::npos)
  52. {
  53. dir[pos] = '/';
  54. pos++;
  55. }
  56. pos = dir.find(':');
  57. if(pos == std::string::npos)
  58. {
  59. pos = 0;
  60. }
  61. while((pos = dir.find('/', pos)) != std::string::npos)
  62. {
  63. std::string topdir = dir.substr(0, pos);
  64. Mkdir(topdir.c_str());
  65. pos++;
  66. }
  67. if(Mkdir(path) != 0)
  68. {
  69. // if it is some other error besides directory exists
  70. // then return false
  71. if(errno != EEXIST)
  72. {
  73. return false;
  74. }
  75. }
  76. return true;
  77. }
  78. // replace replace with with as many times as it shows up in source.
  79. // write the result into source.
  80. void cmSystemTools::ReplaceString(std::string& source,
  81. const char* replace,
  82. const char* with)
  83. {
  84. int lengthReplace = strlen(replace);
  85. std::string rest;
  86. size_t start = source.find(replace);
  87. while(start != std::string::npos)
  88. {
  89. rest = source.substr(start+lengthReplace);
  90. source = source.substr(0, start);
  91. source += with;
  92. source += rest;
  93. start = source.find(replace, start + lengthReplace );
  94. }
  95. }
  96. // return true if the file exists
  97. bool cmSystemTools::FileExists(const char* filename)
  98. {
  99. struct stat fs;
  100. if (stat(filename, &fs) != 0)
  101. {
  102. return false;
  103. }
  104. else
  105. {
  106. return true;
  107. }
  108. }
  109. // Read a list from a CMakeLists.txt file open stream.
  110. // assume the stream has just read "VAR = \"
  111. // read until there is not a "\" at the end of the line.
  112. void cmSystemTools::ReadList(std::vector<std::string>& stringList,
  113. std::ifstream& fin)
  114. {
  115. char inbuffer[2048];
  116. bool done = false;
  117. while ( !done )
  118. {
  119. fin.getline(inbuffer, 2047 );
  120. std::string inname = inbuffer;
  121. if(inname.find('\\') == std::string::npos)
  122. {
  123. done = true;
  124. }
  125. if(inname.size())
  126. {
  127. stringList.push_back(cmSystemTools::CleanUpName(inname.c_str()));
  128. }
  129. }
  130. }
  131. // convert windows slashes to unix slashes \ with /
  132. void cmSystemTools::ConvertToUnixSlashes(std::string& path)
  133. {
  134. std::string::size_type pos = path.find('\\');
  135. while(pos != std::string::npos)
  136. {
  137. path[pos] = '/';
  138. pos = path.find('\\');
  139. }
  140. // remove any trailing slash
  141. if(path[path.size()-1] == '/')
  142. {
  143. path = path.substr(0, path.size()-1);
  144. }
  145. }