cmSystemTools.cxx 4.3 KB

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