cmSystemTools.cxx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #include "cmSystemTools.h"
  2. #include "errno.h"
  3. #include <sys/stat.h>
  4. #include "cmRegularExpression.h"
  5. #if defined(_MSC_VER) || defined(__BORLANDC__)
  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. bool cmSystemTools::MakeDirectory(const char* path)
  22. {
  23. std::string dir = path;
  24. // replace all of the \ with /
  25. size_t pos = 0;
  26. while((pos = dir.find('\\', pos)) != std::string::npos)
  27. {
  28. dir[pos] = '/';
  29. pos++;
  30. }
  31. pos = dir.find(':');
  32. if(pos == std::string::npos)
  33. {
  34. pos = 0;
  35. }
  36. while((pos = dir.find('/', pos)) != std::string::npos)
  37. {
  38. std::string topdir = dir.substr(0, pos);
  39. Mkdir(topdir.c_str());
  40. pos++;
  41. }
  42. if(Mkdir(path) != 0)
  43. {
  44. // if it is some other error besides directory exists
  45. // then return false
  46. if(errno != EEXIST)
  47. {
  48. return false;
  49. }
  50. }
  51. return true;
  52. }
  53. // replace replace with with as many times as it shows up in source.
  54. // write the result into source.
  55. void cmSystemTools::ReplaceString(std::string& source,
  56. const char* replace,
  57. const char* with)
  58. {
  59. int lengthReplace = strlen(replace);
  60. std::string rest;
  61. size_t start = source.find(replace);
  62. while(start != std::string::npos)
  63. {
  64. rest = source.substr(start+lengthReplace);
  65. source = source.substr(0, start);
  66. source += with;
  67. source += rest;
  68. start = source.find(replace, start + lengthReplace );
  69. }
  70. }
  71. // return true if the file exists
  72. bool cmSystemTools::FileExists(const char* filename)
  73. {
  74. struct stat fs;
  75. if (stat(filename, &fs) != 0)
  76. {
  77. return false;
  78. }
  79. else
  80. {
  81. return true;
  82. }
  83. }
  84. // convert windows slashes to unix slashes \ with /
  85. void cmSystemTools::ConvertToUnixSlashes(std::string& path)
  86. {
  87. std::string::size_type pos = path.find('\\');
  88. while(pos != std::string::npos)
  89. {
  90. path[pos] = '/';
  91. pos = path.find('\\');
  92. }
  93. // remove any trailing slash
  94. if(path[path.size()-1] == '/')
  95. {
  96. path = path.substr(0, path.size()-1);
  97. }
  98. }
  99. int cmSystemTools::Grep(const char* dir, const char* file, const char* expression)
  100. {
  101. std::string path = dir;
  102. path += "/";
  103. path += file;
  104. std::ifstream fin(path.c_str());
  105. char buffer[2056];
  106. int count = 0;
  107. cmRegularExpression reg(expression);
  108. while(fin)
  109. {
  110. fin.getline(buffer, sizeof(buffer));
  111. count += reg.find(buffer);
  112. }
  113. return count;
  114. }
  115. void cmSystemTools::ConvertCygwinPath(std::string& pathname)
  116. {
  117. if(pathname.find("/cygdrive/") != std::string::npos)
  118. {
  119. std::string cygStuff = pathname.substr(0, 11);
  120. std::string replace;
  121. replace += cygStuff.at(10);
  122. replace += ":";
  123. cmSystemTools::ReplaceString(pathname, cygStuff.c_str(), replace.c_str());
  124. }
  125. }
  126. bool cmSystemTools::ParseFunction(std::ifstream& fin,
  127. std::string& name,
  128. std::vector<std::string>& arguments)
  129. {
  130. name = "";
  131. arguments = std::vector<std::string>();
  132. const int BUFFER_SIZE = 4096;
  133. char inbuffer[BUFFER_SIZE];
  134. if(!fin)
  135. {
  136. return false;
  137. }
  138. if(fin.getline(inbuffer, BUFFER_SIZE ) )
  139. {
  140. cmRegularExpression blankLine("^$");
  141. cmRegularExpression comment("^#.*");
  142. cmRegularExpression oneLiner("[ \t]*([A-Za-z_0-9]*).*\\((.*)\\)");
  143. cmRegularExpression multiLine("[ \t]*([A-Za-z_0-9]*).*\\((.*)");
  144. cmRegularExpression lastLine("(.*)\\)");
  145. // BEGIN VERBATIM JUNK SHOULD BE REMOVED
  146. cmRegularExpression verbatim("BEGIN MAKE VERBATIM");
  147. if(verbatim.find(inbuffer))
  148. {
  149. cmRegularExpression endVerbatim("END MAKE VERBATIM");
  150. name = "VERBATIM";
  151. bool done = false;
  152. while(!done)
  153. {
  154. if(fin.getline(inbuffer, BUFFER_SIZE))
  155. {
  156. if(endVerbatim.find(inbuffer))
  157. {
  158. done = true;
  159. }
  160. else
  161. {
  162. arguments.push_back(inbuffer);
  163. }
  164. }
  165. else
  166. {
  167. done = true;
  168. }
  169. }
  170. return true;
  171. }
  172. // END VERBATIM JUNK SHOULD BE REMOVED
  173. // check for black line or comment
  174. if(blankLine.find(inbuffer) || comment.find(inbuffer))
  175. {
  176. return false;
  177. }
  178. // look for a oneline fun(arg arg2)
  179. else if(oneLiner.find(inbuffer))
  180. {
  181. // the arguments are the second match
  182. std::string args = oneLiner.match(2);
  183. name = oneLiner.match(1);
  184. // break up the arguments
  185. cmSystemTools::GetArguments(args, arguments);
  186. return true;
  187. }
  188. // look for a start of a multiline with no trailing ")" fun(arg arg2
  189. else if(multiLine.find(inbuffer))
  190. {
  191. name = multiLine.match(1);
  192. std::string args = multiLine.match(2);
  193. cmSystemTools::GetArguments(args, arguments);
  194. // Read lines until the closing paren is hit
  195. bool done = false;
  196. while(!done)
  197. {
  198. // read lines until the end paren is found
  199. if(fin.getline(inbuffer, BUFFER_SIZE ) )
  200. {
  201. if(lastLine.find(inbuffer))
  202. {
  203. done = true;
  204. std::string args = lastLine.match(1);
  205. cmSystemTools::GetArguments(args, arguments);
  206. }
  207. else
  208. {
  209. std::string line = inbuffer;
  210. cmSystemTools::GetArguments(line, arguments);
  211. }
  212. }
  213. }
  214. return true;
  215. }
  216. else
  217. {
  218. cmSystemTools::Error("Parse error in read function ", inbuffer);
  219. return false;
  220. }
  221. }
  222. return false;
  223. }
  224. void cmSystemTools::GetArguments(std::string& line,
  225. std::vector<std::string>& arguments)
  226. {
  227. cmRegularExpression argument("[\t ]*([-/\\\\{}\\$A-Za-z_0-9]+)[\t ]*");
  228. cmRegularExpression argumentWithSpaces("[\t ]*\"([- /\\\\{}\\$A-Za-z_0-9]+)\"[\t ]*");
  229. std::string arg(" ");
  230. while(arg.length() )
  231. {
  232. arg = "";
  233. long endpos;
  234. if (argumentWithSpaces.find(line.c_str()))
  235. {
  236. arg = argumentWithSpaces.match(1);
  237. endpos = argumentWithSpaces.end(1);
  238. }
  239. else if(argument.find(line.c_str()))
  240. {
  241. arg = argument.match(1);
  242. endpos = argument.end(1);
  243. }
  244. if(arg.length())
  245. {
  246. arguments.push_back(arg);
  247. line = line.substr(endpos, line.length() - endpos);
  248. }
  249. }
  250. }
  251. void cmSystemTools::Error(const char* m1, const char* m2)
  252. {
  253. std::string message = "CMake Error: ";
  254. if(m1)
  255. {
  256. message += m1;
  257. }
  258. if(m2)
  259. {
  260. message += m2;
  261. }
  262. #ifdef _WIN32
  263. // MessageBox(0, message.c_str(), 0, MB_OK);
  264. std::cerr << message.c_str() << std::endl;
  265. #else
  266. std::cerr << message.c_str() << std::endl;
  267. #endif
  268. }