cmSystemTools.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2000 National Library of Medicine
  8. All rights reserved.
  9. See COPYRIGHT.txt for copyright details.
  10. =========================================================================*/
  11. #include "cmSystemTools.h"
  12. #include "errno.h"
  13. #include "stdio.h"
  14. #include <sys/stat.h>
  15. #include "cmRegularExpression.h"
  16. #if defined(_MSC_VER) || defined(__BORLANDC__)
  17. #include <windows.h>
  18. #include <direct.h>
  19. #define _unlink unlink
  20. inline int Mkdir(const char* dir)
  21. {
  22. return _mkdir(dir);
  23. }
  24. #else
  25. #include <sys/types.h>
  26. #include <fcntl.h>
  27. #include <unistd.h>
  28. inline int Mkdir(const char* dir)
  29. {
  30. return mkdir(dir, 00777);
  31. }
  32. #endif
  33. bool cmSystemTools::s_ErrorOccured = false;
  34. // adds the elements of the env variable path to the arg passed in
  35. void cmSystemTools::GetPath(std::vector<std::string>& path)
  36. {
  37. #if defined(_WIN32) && !defined(__CYGWIN__)
  38. char* pathSep = ";";
  39. #else
  40. char* pathSep = ":";
  41. #endif
  42. std::string pathEnv = getenv("PATH");
  43. std::string::size_type start =0;
  44. bool done = false;
  45. while(!done)
  46. {
  47. std::string::size_type endpos = pathEnv.find(pathSep, start);
  48. if(endpos != std::string::npos)
  49. {
  50. path.push_back(pathEnv.substr(start, endpos-start));
  51. start = endpos+1;
  52. }
  53. else
  54. {
  55. done = true;
  56. }
  57. }
  58. }
  59. const char* cmSystemTools::GetExecutableExtension()
  60. {
  61. #if defined(_WIN32)
  62. return ".exe";
  63. #else
  64. return "";
  65. #endif
  66. }
  67. bool cmSystemTools::MakeDirectory(const char* path)
  68. {
  69. std::string dir = path;
  70. // replace all of the \ with /
  71. size_t pos = 0;
  72. while((pos = dir.find('\\', pos)) != std::string::npos)
  73. {
  74. dir[pos] = '/';
  75. pos++;
  76. }
  77. pos = dir.find(':');
  78. if(pos == std::string::npos)
  79. {
  80. pos = 0;
  81. }
  82. while((pos = dir.find('/', pos)) != std::string::npos)
  83. {
  84. std::string topdir = dir.substr(0, pos);
  85. Mkdir(topdir.c_str());
  86. pos++;
  87. }
  88. if(Mkdir(path) != 0)
  89. {
  90. // if it is some other error besides directory exists
  91. // then return false
  92. if(errno != EEXIST)
  93. {
  94. return false;
  95. }
  96. }
  97. return true;
  98. }
  99. // replace replace with with as many times as it shows up in source.
  100. // write the result into source.
  101. void cmSystemTools::ReplaceString(std::string& source,
  102. const char* replace,
  103. const char* with)
  104. {
  105. int lengthReplace = strlen(replace);
  106. std::string rest;
  107. size_t start = source.find(replace);
  108. while(start != std::string::npos)
  109. {
  110. rest = source.substr(start+lengthReplace);
  111. source = source.substr(0, start);
  112. source += with;
  113. source += rest;
  114. start = source.find(replace, start + lengthReplace );
  115. }
  116. }
  117. // return true if the file exists
  118. bool cmSystemTools::FileExists(const char* filename)
  119. {
  120. struct stat fs;
  121. if (stat(filename, &fs) != 0)
  122. {
  123. return false;
  124. }
  125. else
  126. {
  127. return true;
  128. }
  129. }
  130. // convert windows slashes to unix slashes \ with /
  131. void cmSystemTools::ConvertToUnixSlashes(std::string& path)
  132. {
  133. std::string::size_type pos = path.find('\\');
  134. while(pos != std::string::npos)
  135. {
  136. path[pos] = '/';
  137. pos = path.find('\\');
  138. }
  139. // remove any trailing slash
  140. if(path[path.size()-1] == '/')
  141. {
  142. path = path.substr(0, path.size()-1);
  143. }
  144. }
  145. int cmSystemTools::Grep(const char* dir, const char* file,
  146. const char* expression)
  147. {
  148. std::string path = dir;
  149. path += "/";
  150. path += file;
  151. std::ifstream fin(path.c_str());
  152. char buffer[2056];
  153. int count = 0;
  154. cmRegularExpression reg(expression);
  155. while(fin)
  156. {
  157. fin.getline(buffer, sizeof(buffer));
  158. count += reg.find(buffer);
  159. }
  160. return count;
  161. }
  162. void cmSystemTools::ConvertCygwinPath(std::string& pathname)
  163. {
  164. if(pathname.find("/cygdrive/") != std::string::npos)
  165. {
  166. std::string cygStuff = pathname.substr(0, 11);
  167. std::string replace;
  168. replace += cygStuff.at(10);
  169. replace += ":";
  170. cmSystemTools::ReplaceString(pathname, cygStuff.c_str(), replace.c_str());
  171. }
  172. }
  173. bool cmSystemTools::ParseFunction(std::ifstream& fin,
  174. std::string& name,
  175. std::vector<std::string>& arguments)
  176. {
  177. name = "";
  178. arguments = std::vector<std::string>();
  179. const int BUFFER_SIZE = 4096;
  180. char inbuffer[BUFFER_SIZE];
  181. if(!fin)
  182. {
  183. return false;
  184. }
  185. if(fin.getline(inbuffer, BUFFER_SIZE ) )
  186. {
  187. cmRegularExpression blankLine("^$");
  188. cmRegularExpression comment("^#.*$");
  189. cmRegularExpression oneLiner("^[ \t]*([A-Za-z_0-9]*)[ \t]*\\((.*)\\)[ \t]*$");
  190. cmRegularExpression multiLine("^[ \t]*([A-Za-z_0-9]*)[ \t]*\\((.*)$");
  191. cmRegularExpression lastLine("^(.*)\\)[ \t]*$");
  192. // BEGIN VERBATIM JUNK SHOULD BE REMOVED
  193. cmRegularExpression verbatim("BEGIN MAKE VERBATIM");
  194. if(verbatim.find(inbuffer))
  195. {
  196. cmRegularExpression endVerbatim("END MAKE VERBATIM");
  197. name = "VERBATIM";
  198. bool done = false;
  199. while(!done)
  200. {
  201. if(fin.getline(inbuffer, BUFFER_SIZE))
  202. {
  203. if(endVerbatim.find(inbuffer))
  204. {
  205. done = true;
  206. }
  207. else
  208. {
  209. arguments.push_back(inbuffer);
  210. }
  211. }
  212. else
  213. {
  214. done = true;
  215. }
  216. }
  217. return true;
  218. }
  219. // END VERBATIM JUNK SHOULD BE REMOVED
  220. // check for black line or comment
  221. if(blankLine.find(inbuffer) || comment.find(inbuffer))
  222. {
  223. return false;
  224. }
  225. // look for a oneline fun(arg arg2)
  226. else if(oneLiner.find(inbuffer))
  227. {
  228. // the arguments are the second match
  229. std::string args = oneLiner.match(2);
  230. name = oneLiner.match(1);
  231. // break up the arguments
  232. cmSystemTools::GetArguments(args, arguments);
  233. return true;
  234. }
  235. // look for a start of a multiline with no trailing ")" fun(arg arg2
  236. else if(multiLine.find(inbuffer))
  237. {
  238. name = multiLine.match(1);
  239. std::string args = multiLine.match(2);
  240. cmSystemTools::GetArguments(args, arguments);
  241. // Read lines until the closing paren is hit
  242. bool done = false;
  243. while(!done)
  244. {
  245. // read lines until the end paren is found
  246. if(fin.getline(inbuffer, BUFFER_SIZE ) )
  247. {
  248. if(lastLine.find(inbuffer))
  249. {
  250. done = true;
  251. std::string args = lastLine.match(1);
  252. cmSystemTools::GetArguments(args, arguments);
  253. }
  254. else
  255. {
  256. std::string line = inbuffer;
  257. cmSystemTools::GetArguments(line, arguments);
  258. }
  259. }
  260. else
  261. {
  262. cmSystemTools::Error("Parse error in read function missing end )",
  263. inbuffer);
  264. return false;
  265. }
  266. }
  267. return true;
  268. }
  269. else
  270. {
  271. cmSystemTools::Error("Parse error in read function ", inbuffer);
  272. return false;
  273. }
  274. }
  275. return false;
  276. }
  277. void cmSystemTools::GetArguments(std::string& line,
  278. std::vector<std::string>& arguments)
  279. {
  280. // Match a normal argument (not quoted, no spaces).
  281. cmRegularExpression normalArgument("[\t ]*([^\" \t]+)[\t ]*");
  282. // Match a quoted argument (surrounded by double quotes, spaces allowed).
  283. cmRegularExpression quotedArgument("[\t ]*(\"[^\"]*\")[\t ]*");
  284. bool done = false;
  285. while(!done)
  286. {
  287. std::string arg;
  288. long endpos;
  289. bool foundQuoted = quotedArgument.find(line.c_str());
  290. bool foundNormal = normalArgument.find(line.c_str());
  291. if(foundQuoted && foundNormal)
  292. {
  293. // Both matches were found. Take the earlier one.
  294. if(normalArgument.start(1) < quotedArgument.start(1))
  295. {
  296. arg = normalArgument.match(1);
  297. endpos = normalArgument.end(1);
  298. }
  299. else
  300. {
  301. arg = quotedArgument.match(1);
  302. endpos = quotedArgument.end(1);
  303. // Strip off the double quotes on the ends.
  304. arg = arg.substr(1, arg.length()-2);
  305. }
  306. }
  307. else if (foundQuoted)
  308. {
  309. arg = quotedArgument.match(1);
  310. endpos = quotedArgument.end(1);
  311. // Strip off the double quotes on the ends.
  312. arg = arg.substr(1, arg.length()-2);
  313. }
  314. else if(foundNormal)
  315. {
  316. arg = normalArgument.match(1);
  317. endpos = normalArgument.end(1);
  318. }
  319. else
  320. {
  321. done = true;
  322. }
  323. if(!done)
  324. {
  325. arguments.push_back(arg);
  326. line = line.substr(endpos, line.length() - endpos);
  327. }
  328. }
  329. }
  330. void cmSystemTools::Error(const char* m1, const char* m2)
  331. {
  332. std::string message = "CMake Error: ";
  333. if(m1)
  334. {
  335. message += m1;
  336. }
  337. if(m2)
  338. {
  339. message += m2;
  340. }
  341. cmSystemTools::s_ErrorOccured = true;
  342. #if defined(_WIN32) && !defined(__CYGWIN__)
  343. ::MessageBox(0, message.c_str(), 0, MB_OK);
  344. std::cerr << message.c_str() << std::endl;
  345. #else
  346. std::cerr << message.c_str() << std::endl;
  347. #endif
  348. }
  349. void cmSystemTools::CopyFileIfDifferent(const char* source,
  350. const char* destination)
  351. {
  352. if(cmSystemTools::FilesDiffer(source, destination))
  353. {
  354. cmSystemTools::cmCopyFile(source, destination);
  355. }
  356. }
  357. bool cmSystemTools::FilesDiffer(const char* source,
  358. const char* destination)
  359. {
  360. struct stat statSource;
  361. if (stat(source, &statSource) != 0)
  362. {
  363. return true;
  364. }
  365. struct stat statDestination;
  366. if (stat(destination, &statDestination) != 0)
  367. {
  368. return true;
  369. }
  370. if(statSource.st_size != statDestination.st_size)
  371. {
  372. return true;
  373. }
  374. std::ifstream finSource(source);
  375. std::ifstream finDestination(destination);
  376. if(!finSource || !finDestination)
  377. {
  378. return true;
  379. }
  380. while(finSource && finDestination)
  381. {
  382. char s, d;
  383. finSource >> s;
  384. finDestination >> d;
  385. if(s != d)
  386. {
  387. return true;
  388. }
  389. }
  390. return false;
  391. }
  392. void cmSystemTools::cmCopyFile(const char* source,
  393. const char* destination)
  394. {
  395. std::ifstream fin(source);
  396. char buff[4096];
  397. std::ofstream fout(destination);
  398. if(!fout )
  399. {
  400. cmSystemTools::Error("CopyFile failed to open input file", source);
  401. }
  402. if(!fin)
  403. {
  404. cmSystemTools::Error("CopyFile failed to open output file", destination);
  405. }
  406. while(fin)
  407. {
  408. fin.getline(buff, 4096);
  409. if(fin)
  410. {
  411. fout << buff << "\n";
  412. }
  413. }
  414. }
  415. // return true if the file exists
  416. long int cmSystemTools::ModifiedTime(const char* filename)
  417. {
  418. struct stat fs;
  419. if (stat(filename, &fs) != 0)
  420. {
  421. return 0;
  422. }
  423. else
  424. {
  425. return (long int)fs.st_mtime;
  426. }
  427. }
  428. void cmSystemTools::RemoveFile(const char* source)
  429. {
  430. unlink(source);
  431. }