cmSystemTools.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2001 Insight Consortium
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. * The name of the Insight Consortium, nor the names of any consortium members,
  17. nor of any contributors, may be used to endorse or promote products derived
  18. from this software without specific prior written permission.
  19. * Modified source versions must be plainly marked as such, and must not be
  20. misrepresented as being the original software.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
  25. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. =========================================================================*/
  32. #include "cmSystemTools.h"
  33. #include "errno.h"
  34. #include "stdio.h"
  35. #include <sys/stat.h>
  36. #include "cmRegularExpression.h"
  37. #include <ctype.h>
  38. #if defined(_MSC_VER) || defined(__BORLANDC__)
  39. #include <windows.h>
  40. #include <direct.h>
  41. #define _unlink unlink
  42. inline int Mkdir(const char* dir)
  43. {
  44. return _mkdir(dir);
  45. }
  46. #else
  47. #include <sys/types.h>
  48. #include <fcntl.h>
  49. #include <unistd.h>
  50. inline int Mkdir(const char* dir)
  51. {
  52. return mkdir(dir, 00777);
  53. }
  54. #endif
  55. bool cmSystemTools::s_ErrorOccured = false;
  56. // adds the elements of the env variable path to the arg passed in
  57. void cmSystemTools::GetPath(std::vector<std::string>& path)
  58. {
  59. #if defined(_WIN32) && !defined(__CYGWIN__)
  60. const char* pathSep = ";";
  61. #else
  62. const char* pathSep = ":";
  63. #endif
  64. std::string pathEnv = getenv("PATH");
  65. std::string::size_type start =0;
  66. bool done = false;
  67. while(!done)
  68. {
  69. std::string::size_type endpos = pathEnv.find(pathSep, start);
  70. if(endpos != std::string::npos)
  71. {
  72. path.push_back(pathEnv.substr(start, endpos-start));
  73. start = endpos+1;
  74. }
  75. else
  76. {
  77. done = true;
  78. }
  79. }
  80. }
  81. const char* cmSystemTools::GetExecutableExtension()
  82. {
  83. #if defined(_WIN32)
  84. return ".exe";
  85. #else
  86. return "";
  87. #endif
  88. }
  89. bool cmSystemTools::MakeDirectory(const char* path)
  90. {
  91. std::string dir = path;
  92. // replace all of the \ with /
  93. size_t pos = 0;
  94. while((pos = dir.find('\\', pos)) != std::string::npos)
  95. {
  96. dir[pos] = '/';
  97. pos++;
  98. }
  99. pos = dir.find(':');
  100. if(pos == std::string::npos)
  101. {
  102. pos = 0;
  103. }
  104. while((pos = dir.find('/', pos)) != std::string::npos)
  105. {
  106. std::string topdir = dir.substr(0, pos);
  107. Mkdir(topdir.c_str());
  108. pos++;
  109. }
  110. if(Mkdir(path) != 0)
  111. {
  112. // if it is some other error besides directory exists
  113. // then return false
  114. if(errno != EEXIST)
  115. {
  116. return false;
  117. }
  118. }
  119. return true;
  120. }
  121. // replace replace with with as many times as it shows up in source.
  122. // write the result into source.
  123. void cmSystemTools::ReplaceString(std::string& source,
  124. const char* replace,
  125. const char* with)
  126. {
  127. int lengthReplace = strlen(replace);
  128. std::string rest;
  129. size_t start = source.find(replace);
  130. while(start != std::string::npos)
  131. {
  132. rest = source.substr(start+lengthReplace);
  133. source = source.substr(0, start);
  134. source += with;
  135. source += rest;
  136. start = source.find(replace, start + lengthReplace );
  137. }
  138. }
  139. std::string cmSystemTools::EscapeSpaces(const char* str)
  140. {
  141. std::string result = "";
  142. for(const char* ch = str; *ch != '\0'; ++ch)
  143. {
  144. if(*ch == ' ')
  145. {
  146. result += '\\';
  147. }
  148. result += *ch;
  149. }
  150. return result;
  151. }
  152. // return true if the file exists
  153. bool cmSystemTools::FileExists(const char* filename)
  154. {
  155. struct stat fs;
  156. if (stat(filename, &fs) != 0)
  157. {
  158. return false;
  159. }
  160. else
  161. {
  162. return true;
  163. }
  164. }
  165. // convert windows slashes to unix slashes \ with /
  166. void cmSystemTools::ConvertToUnixSlashes(std::string& path)
  167. {
  168. std::string::size_type pos = path.find('\\');
  169. while(pos != std::string::npos)
  170. {
  171. path[pos] = '/';
  172. pos = path.find('\\');
  173. }
  174. // remove any trailing slash
  175. if(path[path.size()-1] == '/')
  176. {
  177. path = path.substr(0, path.size()-1);
  178. }
  179. }
  180. int cmSystemTools::Grep(const char* dir, const char* file,
  181. const char* expression)
  182. {
  183. std::string path = dir;
  184. path += "/";
  185. path += file;
  186. std::ifstream fin(path.c_str());
  187. char buffer[2056];
  188. int count = 0;
  189. cmRegularExpression reg(expression);
  190. while(fin)
  191. {
  192. fin.getline(buffer, sizeof(buffer));
  193. count += reg.find(buffer);
  194. }
  195. return count;
  196. }
  197. void cmSystemTools::ConvertCygwinPath(std::string& pathname)
  198. {
  199. if(pathname.find("/cygdrive/") != std::string::npos)
  200. {
  201. std::string cygStuff = pathname.substr(0, 11);
  202. std::string replace;
  203. replace += cygStuff.at(10);
  204. replace += ":";
  205. cmSystemTools::ReplaceString(pathname, cygStuff.c_str(), replace.c_str());
  206. }
  207. }
  208. bool cmSystemTools::ParseFunction(std::ifstream& fin,
  209. std::string& name,
  210. std::vector<std::string>& arguments)
  211. {
  212. name = "";
  213. arguments = std::vector<std::string>();
  214. const int BUFFER_SIZE = 4096;
  215. char inbuffer[BUFFER_SIZE];
  216. if(!fin)
  217. {
  218. return false;
  219. }
  220. if(fin.getline(inbuffer, BUFFER_SIZE ) )
  221. {
  222. cmRegularExpression blankLine("^[ \t]*$");
  223. cmRegularExpression comment("^[ \t]*#.*$");
  224. cmRegularExpression oneLiner("^[ \t]*([A-Za-z_0-9]*)[ \t]*\\((.*)\\)[ \t]*$");
  225. cmRegularExpression multiLine("^[ \t]*([A-Za-z_0-9]*)[ \t]*\\((.*)$");
  226. cmRegularExpression lastLine("^(.*)\\)[ \t]*$");
  227. // BEGIN VERBATIM JUNK SHOULD BE REMOVED
  228. cmRegularExpression verbatim("BEGIN MAKE VERBATIM");
  229. if(verbatim.find(inbuffer))
  230. {
  231. cmRegularExpression endVerbatim("END MAKE VERBATIM");
  232. name = "VERBATIM";
  233. bool done = false;
  234. while(!done)
  235. {
  236. if(fin.getline(inbuffer, BUFFER_SIZE))
  237. {
  238. if(endVerbatim.find(inbuffer))
  239. {
  240. done = true;
  241. }
  242. else
  243. {
  244. arguments.push_back(inbuffer);
  245. }
  246. }
  247. else
  248. {
  249. done = true;
  250. }
  251. }
  252. return true;
  253. }
  254. // END VERBATIM JUNK SHOULD BE REMOVED
  255. // check for black line or comment
  256. if(blankLine.find(inbuffer) || comment.find(inbuffer))
  257. {
  258. return false;
  259. }
  260. // look for a oneline fun(arg arg2)
  261. else if(oneLiner.find(inbuffer))
  262. {
  263. // the arguments are the second match
  264. std::string args = oneLiner.match(2);
  265. name = oneLiner.match(1);
  266. // break up the arguments
  267. cmSystemTools::GetArguments(args, arguments);
  268. return true;
  269. }
  270. // look for a start of a multiline with no trailing ")" fun(arg arg2
  271. else if(multiLine.find(inbuffer))
  272. {
  273. name = multiLine.match(1);
  274. std::string args = multiLine.match(2);
  275. cmSystemTools::GetArguments(args, arguments);
  276. // Read lines until the closing paren is hit
  277. bool done = false;
  278. while(!done)
  279. {
  280. // read lines until the end paren is found
  281. if(fin.getline(inbuffer, BUFFER_SIZE ) )
  282. {
  283. // Check for comment lines and ignore them.
  284. if(blankLine.find(inbuffer) || comment.find(inbuffer))
  285. { continue; }
  286. // Is this the last line?
  287. if(lastLine.find(inbuffer))
  288. {
  289. done = true;
  290. std::string args = lastLine.match(1);
  291. cmSystemTools::GetArguments(args, arguments);
  292. }
  293. else
  294. {
  295. std::string line = inbuffer;
  296. cmSystemTools::GetArguments(line, arguments);
  297. }
  298. }
  299. else
  300. {
  301. cmSystemTools::Error("Parse error in read function missing end )",
  302. inbuffer);
  303. return false;
  304. }
  305. }
  306. return true;
  307. }
  308. else
  309. {
  310. cmSystemTools::Error("Parse error in read function ", inbuffer);
  311. return false;
  312. }
  313. }
  314. return false;
  315. }
  316. void cmSystemTools::GetArguments(std::string& line,
  317. std::vector<std::string>& arguments)
  318. {
  319. // Match a normal argument (not quoted, no spaces).
  320. cmRegularExpression normalArgument("[\t ]*([^\" \t]+)[\t ]*");
  321. // Match a quoted argument (surrounded by double quotes, spaces allowed).
  322. cmRegularExpression quotedArgument("[\t ]*(\"[^\"]*\")[\t ]*");
  323. bool done = false;
  324. while(!done)
  325. {
  326. std::string arg;
  327. long endpos;
  328. bool foundQuoted = quotedArgument.find(line.c_str());
  329. bool foundNormal = normalArgument.find(line.c_str());
  330. if(foundQuoted && foundNormal)
  331. {
  332. // Both matches were found. Take the earlier one.
  333. if(normalArgument.start(1) < quotedArgument.start(1))
  334. {
  335. arg = normalArgument.match(1);
  336. endpos = normalArgument.end(1);
  337. }
  338. else
  339. {
  340. arg = quotedArgument.match(1);
  341. endpos = quotedArgument.end(1);
  342. // Strip off the double quotes on the ends.
  343. arg = arg.substr(1, arg.length()-2);
  344. }
  345. }
  346. else if (foundQuoted)
  347. {
  348. arg = quotedArgument.match(1);
  349. endpos = quotedArgument.end(1);
  350. // Strip off the double quotes on the ends.
  351. arg = arg.substr(1, arg.length()-2);
  352. }
  353. else if(foundNormal)
  354. {
  355. arg = normalArgument.match(1);
  356. endpos = normalArgument.end(1);
  357. }
  358. else
  359. {
  360. done = true;
  361. }
  362. if(!done)
  363. {
  364. arguments.push_back(arg);
  365. line = line.substr(endpos, line.length() - endpos);
  366. }
  367. }
  368. }
  369. void cmSystemTools::Error(const char* m1, const char* m2,
  370. const char* m3, const char* m4)
  371. {
  372. std::string message = "CMake Error: ";
  373. if(m1)
  374. {
  375. message += m1;
  376. }
  377. if(m2)
  378. {
  379. message += m2;
  380. }
  381. if(m3)
  382. {
  383. message += m3;
  384. }
  385. if(m4)
  386. {
  387. message += m4;
  388. }
  389. cmSystemTools::s_ErrorOccured = true;
  390. #if defined(_WIN32) && !defined(__CYGWIN__)
  391. ::MessageBox(0, message.c_str(), 0, MB_OK);
  392. std::cerr << message.c_str() << std::endl;
  393. #else
  394. std::cerr << message.c_str() << std::endl;
  395. #endif
  396. }
  397. void cmSystemTools::CopyFileIfDifferent(const char* source,
  398. const char* destination)
  399. {
  400. if(cmSystemTools::FilesDiffer(source, destination))
  401. {
  402. cmSystemTools::cmCopyFile(source, destination);
  403. }
  404. }
  405. bool cmSystemTools::FilesDiffer(const char* source,
  406. const char* destination)
  407. {
  408. struct stat statSource;
  409. if (stat(source, &statSource) != 0)
  410. {
  411. return true;
  412. }
  413. struct stat statDestination;
  414. if (stat(destination, &statDestination) != 0)
  415. {
  416. return true;
  417. }
  418. if(statSource.st_size != statDestination.st_size)
  419. {
  420. return true;
  421. }
  422. std::ifstream finSource(source);
  423. std::ifstream finDestination(destination);
  424. if(!finSource || !finDestination)
  425. {
  426. return true;
  427. }
  428. while(finSource && finDestination)
  429. {
  430. char s, d;
  431. finSource >> s;
  432. finDestination >> d;
  433. if(s != d)
  434. {
  435. return true;
  436. }
  437. }
  438. return false;
  439. }
  440. void cmSystemTools::cmCopyFile(const char* source,
  441. const char* destination)
  442. {
  443. std::ifstream fin(source);
  444. char buff[4096];
  445. std::ofstream fout(destination);
  446. if(!fout )
  447. {
  448. cmSystemTools::Error("CopyFile failed to open input file", source);
  449. }
  450. if(!fin)
  451. {
  452. cmSystemTools::Error("CopyFile failed to open output file", destination);
  453. }
  454. while(fin)
  455. {
  456. fin.getline(buff, 4096);
  457. if(fin)
  458. {
  459. fout << buff << "\n";
  460. }
  461. }
  462. }
  463. // return true if the file exists
  464. long int cmSystemTools::ModifiedTime(const char* filename)
  465. {
  466. struct stat fs;
  467. if (stat(filename, &fs) != 0)
  468. {
  469. return 0;
  470. }
  471. else
  472. {
  473. return (long int)fs.st_mtime;
  474. }
  475. }
  476. void cmSystemTools::RemoveFile(const char* source)
  477. {
  478. unlink(source);
  479. }
  480. bool cmSystemTools::IsOn(const char* val)
  481. {
  482. if (!val)
  483. {
  484. return false;
  485. }
  486. std::basic_string<char> v = val;
  487. for(std::basic_string<char>::iterator c = v.begin();
  488. c != v.end(); c++)
  489. {
  490. *c = toupper(*c);
  491. }
  492. return (v == "ON" || v == "1" || v == "YES" || v == "TRUE" || v == "Y");
  493. }
  494. bool cmSystemTools::IsOff(const char* val)
  495. {
  496. if (!val)
  497. {
  498. return true;
  499. }
  500. std::basic_string<char> v = val;
  501. for(std::basic_string<char>::iterator c = v.begin();
  502. c != v.end(); c++)
  503. {
  504. *c = toupper(*c);
  505. }
  506. return (v == "OFF" || v == "0" || v == "NO" || v == "FALSE" ||
  507. v == "N" || v == "NOTFOUND");
  508. }