cmSystemTools.cxx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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] == ' ' || className[i] == '\t')
  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. pos = className.find('\t');
  46. if(pos != std::string::npos)
  47. {
  48. className = className.substr(0, pos);
  49. }
  50. return className;
  51. }
  52. bool cmSystemTools::MakeDirectory(const char* path)
  53. {
  54. std::string dir = path;
  55. // replace all of the \ with /
  56. size_t pos = 0;
  57. while((pos = dir.find('\\', pos)) != std::string::npos)
  58. {
  59. dir[pos] = '/';
  60. pos++;
  61. }
  62. pos = dir.find(':');
  63. if(pos == std::string::npos)
  64. {
  65. pos = 0;
  66. }
  67. while((pos = dir.find('/', pos)) != std::string::npos)
  68. {
  69. std::string topdir = dir.substr(0, pos);
  70. Mkdir(topdir.c_str());
  71. pos++;
  72. }
  73. if(Mkdir(path) != 0)
  74. {
  75. // if it is some other error besides directory exists
  76. // then return false
  77. if(errno != EEXIST)
  78. {
  79. return false;
  80. }
  81. }
  82. return true;
  83. }
  84. // replace replace with with as many times as it shows up in source.
  85. // write the result into source.
  86. void cmSystemTools::ReplaceString(std::string& source,
  87. const char* replace,
  88. const char* with)
  89. {
  90. int lengthReplace = strlen(replace);
  91. std::string rest;
  92. size_t start = source.find(replace);
  93. while(start != std::string::npos)
  94. {
  95. rest = source.substr(start+lengthReplace);
  96. source = source.substr(0, start);
  97. source += with;
  98. source += rest;
  99. start = source.find(replace, start + lengthReplace );
  100. }
  101. }
  102. // return true if the file exists
  103. bool cmSystemTools::FileExists(const char* filename)
  104. {
  105. struct stat fs;
  106. if (stat(filename, &fs) != 0)
  107. {
  108. return false;
  109. }
  110. else
  111. {
  112. return true;
  113. }
  114. }
  115. // Read a list from a CMakeLists.txt file open stream.
  116. // assume the stream has just read "VAR = \"
  117. // read until there is not a "\" at the end of the line.
  118. void cmSystemTools::ReadList(std::vector<std::string>& stringList,
  119. std::ifstream& fin)
  120. {
  121. char inbuffer[2048];
  122. bool done = false;
  123. while ( !done )
  124. {
  125. fin.getline(inbuffer, sizeof(inbuffer) );
  126. std::string inname = inbuffer;
  127. if(inname.find('\\') == std::string::npos)
  128. {
  129. done = true;
  130. }
  131. if(inname.size())
  132. {
  133. stringList.push_back(cmSystemTools::CleanUpName(inname.c_str()));
  134. }
  135. }
  136. }
  137. // convert windows slashes to unix slashes \ with /
  138. void cmSystemTools::ConvertToUnixSlashes(std::string& path)
  139. {
  140. std::string::size_type pos = path.find('\\');
  141. while(pos != std::string::npos)
  142. {
  143. path[pos] = '/';
  144. pos = path.find('\\');
  145. }
  146. // remove any trailing slash
  147. if(path[path.size()-1] == '/')
  148. {
  149. path = path.substr(0, path.size()-1);
  150. }
  151. }
  152. int cmSystemTools::Grep(const char* dir, const char* file, const char* expression)
  153. {
  154. std::string path = dir;
  155. path += "/";
  156. path += file;
  157. std::ifstream fin(path.c_str());
  158. char buffer[2056];
  159. int count = 0;
  160. cmRegularExpression reg(expression);
  161. while(fin)
  162. {
  163. fin.getline(buffer, sizeof(buffer));
  164. count += reg.find(buffer);
  165. }
  166. return count;
  167. }
  168. std::string cmSystemTools::ExtractVariable(const char* variable,
  169. const char* l)
  170. {
  171. std::string line = l;
  172. size_t varstart = line.find(variable);
  173. size_t start = line.find("=");
  174. if(start != std::string::npos && start > varstart )
  175. {
  176. start++;
  177. while(line[start] == ' ' && start < line.size())
  178. {
  179. start++;
  180. }
  181. size_t end = line.size()-1;
  182. while(line[end] == ' ' && end > start)
  183. {
  184. end--;
  185. }
  186. return line.substr(start, end).c_str();
  187. }
  188. return std::string("");
  189. }