cmSystemTools.cxx 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmSystemTools.h"
  14. #include "errno.h"
  15. #include "stdio.h"
  16. #include <sys/stat.h>
  17. #include "cmRegularExpression.h"
  18. #include <ctype.h>
  19. #include "cmDirectory.h"
  20. // support for realpath call
  21. #ifndef _WIN32
  22. #include <limits.h>
  23. #include <stdlib.h>
  24. #include <sys/param.h>
  25. #include <sys/wait.h>
  26. #endif
  27. #if defined(_MSC_VER) || defined(__BORLANDC__)
  28. #include <windows.h>
  29. #include <direct.h>
  30. #define _unlink unlink
  31. inline int Mkdir(const char* dir)
  32. {
  33. return _mkdir(dir);
  34. }
  35. inline const char* Getcwd(char* buf, unsigned int len)
  36. {
  37. return _getcwd(buf, len);
  38. }
  39. inline int Chdir(const char* dir)
  40. {
  41. #if defined(__BORLANDC__)
  42. return chdir(dir);
  43. #else
  44. return _chdir(dir);
  45. #endif
  46. }
  47. #else
  48. #include <sys/types.h>
  49. #include <fcntl.h>
  50. #include <unistd.h>
  51. inline int Mkdir(const char* dir)
  52. {
  53. return mkdir(dir, 00777);
  54. }
  55. inline const char* Getcwd(char* buf, unsigned int len)
  56. {
  57. return getcwd(buf, len);
  58. }
  59. inline int Chdir(const char* dir)
  60. {
  61. return chdir(dir);
  62. }
  63. #endif
  64. bool cmSystemTools::s_DisableRunCommandOutput = false;
  65. bool cmSystemTools::s_ErrorOccured = false;
  66. bool cmSystemTools::s_DisableMessages = false;
  67. void (*cmSystemTools::s_ErrorCallback)(const char*, const char*, bool&);
  68. // adds the elements of the env variable path to the arg passed in
  69. void cmSystemTools::GetPath(std::vector<std::string>& path)
  70. {
  71. #if defined(_WIN32) && !defined(__CYGWIN__)
  72. const char* pathSep = ";";
  73. #else
  74. const char* pathSep = ":";
  75. #endif
  76. std::string pathEnv = getenv("PATH");
  77. // A hack to make the below algorithm work.
  78. if(pathEnv[pathEnv.length()-1] != ':')
  79. {
  80. pathEnv += pathSep;
  81. }
  82. std::string::size_type start =0;
  83. bool done = false;
  84. while(!done)
  85. {
  86. std::string::size_type endpos = pathEnv.find(pathSep, start);
  87. if(endpos != std::string::npos)
  88. {
  89. path.push_back(pathEnv.substr(start, endpos-start));
  90. start = endpos+1;
  91. }
  92. else
  93. {
  94. done = true;
  95. }
  96. }
  97. for(std::vector<std::string>::iterator i = path.begin();
  98. i != path.end(); ++i)
  99. {
  100. cmSystemTools::ConvertToUnixSlashes(*i);
  101. }
  102. }
  103. const char* cmSystemTools::GetExecutableExtension()
  104. {
  105. #if defined(_WIN32) || defined(__CYGWIN__)
  106. return ".exe";
  107. #else
  108. return "";
  109. #endif
  110. }
  111. bool cmSystemTools::MakeDirectory(const char* path)
  112. {
  113. if(cmSystemTools::FileExists(path))
  114. {
  115. return true;
  116. }
  117. std::string dir = path;
  118. if(dir.size() == 0)
  119. {
  120. return false;
  121. }
  122. cmSystemTools::ConvertToUnixSlashes(dir);
  123. std::string::size_type pos = dir.find(':');
  124. if(pos == std::string::npos)
  125. {
  126. pos = 0;
  127. }
  128. std::string topdir;
  129. while((pos = dir.find('/', pos)) != std::string::npos)
  130. {
  131. topdir = dir.substr(0, pos);
  132. Mkdir(topdir.c_str());
  133. pos++;
  134. }
  135. if(dir[dir.size()-1] == '/')
  136. {
  137. topdir = dir.substr(0, dir.size());
  138. }
  139. else
  140. {
  141. topdir = dir;
  142. }
  143. if(Mkdir(topdir.c_str()) != 0)
  144. {
  145. // There is a bug in the Borland Run time library which makes MKDIR
  146. // return EACCES when it should return EEXISTS
  147. // if it is some other error besides directory exists
  148. // then return false
  149. if( (errno != EEXIST)
  150. #ifdef __BORLANDC__
  151. && (errno != EACCES)
  152. #endif
  153. )
  154. {
  155. cmSystemTools::Error("Faild to create directory:", path);
  156. return false;
  157. }
  158. }
  159. return true;
  160. }
  161. // replace replace with with as many times as it shows up in source.
  162. // write the result into source.
  163. void cmSystemTools::ReplaceString(std::string& source,
  164. const char* replace,
  165. const char* with)
  166. {
  167. std::string::size_type lengthReplace = strlen(replace);
  168. std::string rest;
  169. std::string::size_type start = source.find(replace);
  170. while(start != std::string::npos)
  171. {
  172. rest = source.substr(start+lengthReplace);
  173. source = source.substr(0, start);
  174. source += with;
  175. source += rest;
  176. start = source.find(replace, start + lengthReplace );
  177. }
  178. }
  179. #if defined(_WIN32) && !defined(__CYGWIN__)
  180. // Get the data of key value.
  181. // Example :
  182. // HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.1\InstallPath
  183. // => will return the data of the "default" value of the key
  184. // HKEY_LOCAL_MACHINE\SOFTWARE\Scriptics\Tcl\8.4;Root
  185. // => will return the data of the "Root" value of the key
  186. bool ReadAValue(std::string &res, const char *key)
  187. {
  188. // find the primary key
  189. std::string primary = key;
  190. std::string second;
  191. std::string valuename;
  192. size_t start = primary.find("\\");
  193. if (start == std::string::npos)
  194. {
  195. return false;
  196. }
  197. size_t valuenamepos = primary.find(";");
  198. if (valuenamepos != std::string::npos)
  199. {
  200. valuename = primary.substr(valuenamepos+1);
  201. }
  202. second = primary.substr(start+1, valuenamepos-start-1);
  203. primary = primary.substr(0, start);
  204. HKEY primaryKey;
  205. if (primary == "HKEY_CURRENT_USER")
  206. {
  207. primaryKey = HKEY_CURRENT_USER;
  208. }
  209. if (primary == "HKEY_CURRENT_CONFIG")
  210. {
  211. primaryKey = HKEY_CURRENT_CONFIG;
  212. }
  213. if (primary == "HKEY_CLASSES_ROOT")
  214. {
  215. primaryKey = HKEY_CLASSES_ROOT;
  216. }
  217. if (primary == "HKEY_LOCAL_MACHINE")
  218. {
  219. primaryKey = HKEY_LOCAL_MACHINE;
  220. }
  221. if (primary == "HKEY_USERS")
  222. {
  223. primaryKey = HKEY_USERS;
  224. }
  225. HKEY hKey;
  226. if(RegOpenKeyEx(primaryKey, second.c_str(),
  227. 0, KEY_READ, &hKey) != ERROR_SUCCESS)
  228. {
  229. return false;
  230. }
  231. else
  232. {
  233. DWORD dwType, dwSize;
  234. dwSize = 1023;
  235. char data[1024];
  236. if(RegQueryValueEx(hKey, (LPTSTR)valuename.c_str(), NULL, &dwType,
  237. (BYTE *)data, &dwSize) == ERROR_SUCCESS)
  238. {
  239. if (dwType == REG_SZ)
  240. {
  241. res = data;
  242. return true;
  243. }
  244. }
  245. }
  246. return false;
  247. }
  248. #endif
  249. // replace replace with with as many times as it shows up in source.
  250. // write the result into source.
  251. void cmSystemTools::ExpandRegistryValues(std::string& source)
  252. {
  253. #if defined(_WIN32) && !defined(__CYGWIN__)
  254. // Regular expression to match anything inside [...] that begins in HKEY.
  255. // Note that there is a special rule for regular expressions to match a
  256. // close square-bracket inside a list delimited by square brackets.
  257. // The "[^]]" part of this expression will match any character except
  258. // a close square-bracket. The ']' character must be the first in the
  259. // list of characters inside the [^...] block of the expression.
  260. cmRegularExpression regEntry("\\[(HKEY[^]]*)\\]");
  261. // check for black line or comment
  262. while (regEntry.find(source))
  263. {
  264. // the arguments are the second match
  265. std::string key = regEntry.match(1);
  266. std::string val;
  267. if (ReadAValue(val,key.c_str()))
  268. {
  269. std::string reg = "[";
  270. reg += key + "]";
  271. cmSystemTools::ReplaceString(source, reg.c_str(), val.c_str());
  272. }
  273. else
  274. {
  275. std::string reg = "[";
  276. reg += key + "]";
  277. cmSystemTools::ReplaceString(source, reg.c_str(), "/registry");
  278. }
  279. }
  280. #endif
  281. }
  282. std::string cmSystemTools::EscapeQuotes(const char* str)
  283. {
  284. std::string result = "";
  285. for(const char* ch = str; *ch != '\0'; ++ch)
  286. {
  287. if(*ch == '"')
  288. {
  289. result += '\\';
  290. }
  291. result += *ch;
  292. }
  293. return result;
  294. }
  295. bool cmSystemTools::SameFile(const char* file1, const char* file2)
  296. {
  297. struct stat fileStat1, fileStat2;
  298. if (stat(file1, &fileStat1) == 0 && stat(file2, &fileStat2) == 0)
  299. {
  300. // see if the files are the same file
  301. // check the device inode and size
  302. if(fileStat2.st_dev == fileStat1.st_dev &&
  303. fileStat2.st_ino == fileStat1.st_ino &&
  304. fileStat2.st_size == fileStat1.st_size
  305. )
  306. {
  307. return true;
  308. }
  309. }
  310. return false;
  311. }
  312. // return true if the file exists
  313. bool cmSystemTools::FileExists(const char* filename)
  314. {
  315. struct stat fs;
  316. if (stat(filename, &fs) != 0)
  317. {
  318. return false;
  319. }
  320. else
  321. {
  322. return true;
  323. }
  324. }
  325. // Return a capitalized string (i.e the first letter is uppercased, all other
  326. // are lowercased)
  327. std::string cmSystemTools::Capitalized(const std::string& s)
  328. {
  329. std::string n;
  330. n.resize(s.size());
  331. n[0] = toupper(s[0]);
  332. for (size_t i = 1; i < s.size(); i++)
  333. {
  334. n[i] = tolower(s[i]);
  335. }
  336. return n;
  337. }
  338. // Return a lower case string
  339. std::string cmSystemTools::LowerCase(const std::string& s)
  340. {
  341. std::string n;
  342. n.resize(s.size());
  343. for (size_t i = 0; i < s.size(); i++)
  344. {
  345. n[i] = tolower(s[i]);
  346. }
  347. return n;
  348. }
  349. // Return a lower case string
  350. std::string cmSystemTools::UpperCase(const std::string& s)
  351. {
  352. std::string n;
  353. n.resize(s.size());
  354. for (size_t i = 0; i < s.size(); i++)
  355. {
  356. n[i] = toupper(s[i]);
  357. }
  358. return n;
  359. }
  360. // convert windows slashes to unix slashes
  361. void cmSystemTools::ConvertToUnixSlashes(std::string& path)
  362. {
  363. std::string::size_type pos = 0;
  364. while((pos = path.find('\\', pos)) != std::string::npos)
  365. {
  366. path[pos] = '/';
  367. pos++;
  368. }
  369. // remove any trailing slash
  370. if(path.size() && path[path.size()-1] == '/')
  371. {
  372. path = path.substr(0, path.size()-1);
  373. }
  374. // if there is a tilda ~ then replace it with HOME
  375. if(path.find("~") == 0)
  376. {
  377. if (getenv("HOME"))
  378. {
  379. path = std::string(getenv("HOME")) + path.substr(1);
  380. }
  381. }
  382. // if there is a /tmp_mnt in a path get rid of it!
  383. // stupid sgi's
  384. if(path.find("/tmp_mnt") == 0)
  385. {
  386. path = path.substr(8);
  387. }
  388. }
  389. // change // to /, and escape any spaces in the path
  390. std::string cmSystemTools::ConvertToUnixOutputPath(const char* path)
  391. {
  392. std::string ret = path;
  393. // remove // except at the beginning might be a cygwin drive
  394. std::string::size_type pos = 1;
  395. while((pos = ret.find("//", pos)) != std::string::npos)
  396. {
  397. ret.erase(pos, 1);
  398. }
  399. // now escape spaces if there is a space in the path
  400. if(ret.find(" ") != std::string::npos)
  401. {
  402. std::string result = "";
  403. char lastch = 1;
  404. for(const char* ch = ret.c_str(); *ch != '\0'; ++ch)
  405. {
  406. // if it is already escaped then don't try to escape it again
  407. if(*ch == ' ' && lastch != '\\')
  408. {
  409. result += '\\';
  410. }
  411. result += *ch;
  412. lastch = *ch;
  413. }
  414. ret = result;
  415. }
  416. return ret;
  417. }
  418. std::string cmSystemTools::EscapeSpaces(const char* str)
  419. {
  420. #if defined(_WIN32) && !defined(__CYGWIN__)
  421. std::string result;
  422. // if there are spaces
  423. std::string temp = str;
  424. if (temp.find(" ") != std::string::npos &&
  425. temp.find("\"")==std::string::npos)
  426. {
  427. result = "\"";
  428. result += str;
  429. result += "\"";
  430. return result;
  431. }
  432. return str;
  433. #else
  434. std::string result = "";
  435. for(const char* ch = str; *ch != '\0'; ++ch)
  436. {
  437. if(*ch == ' ')
  438. {
  439. result += '\\';
  440. }
  441. result += *ch;
  442. }
  443. return result;
  444. #endif
  445. }
  446. std::string cmSystemTools::ConvertToOutputPath(const char* path)
  447. {
  448. #if defined(_WIN32) && !defined(__CYGWIN__)
  449. return cmSystemTools::ConvertToWindowsOutputPath(path);
  450. #else
  451. return cmSystemTools::ConvertToUnixOutputPath(path);
  452. #endif
  453. }
  454. // remove double slashes not at the start
  455. std::string cmSystemTools::ConvertToWindowsOutputPath(const char* path)
  456. {
  457. std::string ret = path;
  458. std::string::size_type pos = 0;
  459. // first convert all of the slashes
  460. while((pos = ret.find('/', pos)) != std::string::npos)
  461. {
  462. ret[pos] = '\\';
  463. pos++;
  464. }
  465. // check for really small paths
  466. if(ret.size() < 2)
  467. {
  468. return ret;
  469. }
  470. // now clean up a bit and remove double slashes
  471. // Only if it is not the first position in the path which is a network
  472. // path on windows
  473. pos = 1; // start at position 1
  474. while((pos = ret.find("\\\\", pos)) != std::string::npos)
  475. {
  476. ret.erase(pos, 1);
  477. }
  478. // now double quote the path if it has spaces in it
  479. // and is not already double quoted
  480. if(ret.find(" ") != std::string::npos
  481. && ret[0] != '\"')
  482. {
  483. std::string result;
  484. result = "\"" + ret + "\"";
  485. ret = result;
  486. }
  487. return ret;
  488. }
  489. bool cmSystemTools::ParseFunction(std::ifstream& fin,
  490. std::string& name,
  491. std::vector<std::string>& arguments,
  492. const char* filename,
  493. bool& parseError)
  494. {
  495. parseError = false;
  496. name = "";
  497. arguments = std::vector<std::string>();
  498. const int BUFFER_SIZE = 4096;
  499. char inbuffer[BUFFER_SIZE];
  500. if(!fin)
  501. {
  502. return false;
  503. }
  504. if(fin.getline(inbuffer, BUFFER_SIZE ) )
  505. {
  506. cmRegularExpression blankLine("^[ \t\r]*$");
  507. cmRegularExpression comment("^[ \t]*#.*$");
  508. cmRegularExpression oneLiner("^[ \t]*([A-Za-z_0-9]*)[ \t]*\\((.*)\\)[ \t\r]*$");
  509. cmRegularExpression multiLine("^[ \t]*([A-Za-z_0-9]*)[ \t]*\\((.*)$");
  510. cmRegularExpression lastLine("^(.*)\\)[ \t\r]*$");
  511. // check for black line or comment
  512. if(blankLine.find(inbuffer) || comment.find(inbuffer))
  513. {
  514. return false;
  515. }
  516. // look for a oneline fun(arg arg2)
  517. else if(oneLiner.find(inbuffer))
  518. {
  519. // the arguments are the second match
  520. std::string args = oneLiner.match(2);
  521. name = oneLiner.match(1);
  522. // break up the arguments
  523. cmSystemTools::GetArguments(args, arguments);
  524. return true;
  525. }
  526. // look for a start of a multiline with no trailing ")" fun(arg arg2
  527. else if(multiLine.find(inbuffer))
  528. {
  529. name = multiLine.match(1);
  530. std::string args = multiLine.match(2);
  531. cmSystemTools::GetArguments(args, arguments);
  532. // Read lines until the closing paren is hit
  533. bool done = false;
  534. while(!done)
  535. {
  536. // read lines until the end paren is found
  537. if(fin.getline(inbuffer, BUFFER_SIZE ) )
  538. {
  539. // Check for comment lines and ignore them.
  540. if(blankLine.find(inbuffer) || comment.find(inbuffer))
  541. { continue; }
  542. // Is this the last line?
  543. if(lastLine.find(inbuffer))
  544. {
  545. done = true;
  546. std::string args = lastLine.match(1);
  547. cmSystemTools::GetArguments(args, arguments);
  548. }
  549. else
  550. {
  551. std::string line = inbuffer;
  552. cmSystemTools::GetArguments(line, arguments);
  553. }
  554. }
  555. else
  556. {
  557. parseError = true;
  558. cmSystemTools::Error("Parse error in read function missing end )\nIn File: ",
  559. filename, "\nCurrent line:", inbuffer);
  560. return false;
  561. }
  562. }
  563. return true;
  564. }
  565. else
  566. {
  567. parseError = true;
  568. cmSystemTools::Error("Parse error in read function\nIn file:",
  569. filename, "\nCurrent line:", inbuffer);
  570. return false;
  571. }
  572. }
  573. return false;
  574. }
  575. void cmSystemTools::GetArguments(std::string& line,
  576. std::vector<std::string>& arguments)
  577. {
  578. // Match a normal argument (not quoted, no spaces).
  579. cmRegularExpression normalArgument("[ \t]*(([^ \t\r\\]|[\\].)+)[ \t\r]*");
  580. // Match a quoted argument (surrounded by double quotes, spaces allowed).
  581. cmRegularExpression quotedArgument("[ \t]*(\"([^\"\\]|[\\].)*\")[ \t\r]*");
  582. bool done = false;
  583. while(!done)
  584. {
  585. std::string arg;
  586. std::string::size_type endpos;
  587. bool foundQuoted = quotedArgument.find(line.c_str());
  588. bool foundNormal = normalArgument.find(line.c_str());
  589. if(foundQuoted && foundNormal)
  590. {
  591. // Both matches were found. Take the earlier one.
  592. // Favor double-quoted version if there is a tie.
  593. if(normalArgument.start(1) < quotedArgument.start(1))
  594. {
  595. arg = normalArgument.match(1);
  596. endpos = normalArgument.end(1);
  597. }
  598. else
  599. {
  600. arg = quotedArgument.match(1);
  601. endpos = quotedArgument.end(1);
  602. // Strip off the double quotes on the ends.
  603. arg = arg.substr(1, arg.length()-2);
  604. }
  605. }
  606. else if (foundQuoted)
  607. {
  608. arg = quotedArgument.match(1);
  609. endpos = quotedArgument.end(1);
  610. // Strip off the double quotes on the ends.
  611. arg = arg.substr(1, arg.length()-2);
  612. }
  613. else if(foundNormal)
  614. {
  615. arg = normalArgument.match(1);
  616. endpos = normalArgument.end(1);
  617. }
  618. else
  619. {
  620. done = true;
  621. }
  622. if(!done)
  623. {
  624. arguments.push_back(cmSystemTools::RemoveEscapes(arg.c_str()));
  625. line = line.substr(endpos, line.length() - endpos);
  626. }
  627. }
  628. }
  629. std::string cmSystemTools::RemoveEscapes(const char* s)
  630. {
  631. std::string result = "";
  632. for(const char* ch = s; *ch; ++ch)
  633. {
  634. if(*ch == '\\')
  635. {
  636. ++ch;
  637. switch (*ch)
  638. {
  639. case '\\': result.insert(result.end(), '\\'); break;
  640. case '"': result.insert(result.end(), '"'); break;
  641. case ' ': result.insert(result.end(), ' '); break;
  642. case 't': result.insert(result.end(), '\t'); break;
  643. case 'n': result.insert(result.end(), '\n'); break;
  644. case 'r': result.insert(result.end(), '\r'); break;
  645. case '0': result.insert(result.end(), '\0'); break;
  646. case '\0':
  647. {
  648. cmSystemTools::Error("Trailing backslash in argument:\n", s);
  649. return result;
  650. }
  651. default:
  652. {
  653. std::string chStr(1, *ch);
  654. cmSystemTools::Error("Invalid escape sequence \\", chStr.c_str(),
  655. "\nin argument ", s);
  656. }
  657. }
  658. }
  659. else
  660. {
  661. result.insert(result.end(), *ch);
  662. }
  663. }
  664. return result;
  665. }
  666. void cmSystemTools::Error(const char* m1, const char* m2,
  667. const char* m3, const char* m4)
  668. {
  669. std::string message = "CMake Error: ";
  670. if(m1)
  671. {
  672. message += m1;
  673. }
  674. if(m2)
  675. {
  676. message += m2;
  677. }
  678. if(m3)
  679. {
  680. message += m3;
  681. }
  682. if(m4)
  683. {
  684. message += m4;
  685. }
  686. cmSystemTools::s_ErrorOccured = true;
  687. cmSystemTools::Message(message.c_str(),"Error");
  688. }
  689. void cmSystemTools::SetErrorCallback(ErrorCallback f)
  690. {
  691. s_ErrorCallback = f;
  692. }
  693. void cmSystemTools::Message(const char* m1, const char *title)
  694. {
  695. if(s_DisableMessages)
  696. {
  697. return;
  698. }
  699. if(s_ErrorCallback)
  700. {
  701. (*s_ErrorCallback)(m1, title, s_DisableMessages);
  702. return;
  703. }
  704. else
  705. {
  706. std::cerr << m1 << std::endl;
  707. }
  708. }
  709. bool cmSystemTools::CopyFileIfDifferent(const char* source,
  710. const char* destination)
  711. {
  712. if(cmSystemTools::FilesDiffer(source, destination))
  713. {
  714. cmSystemTools::cmCopyFile(source, destination);
  715. return true;
  716. }
  717. return false;
  718. }
  719. bool cmSystemTools::FilesDiffer(const char* source,
  720. const char* destination)
  721. {
  722. struct stat statSource;
  723. if (stat(source, &statSource) != 0)
  724. {
  725. return true;
  726. }
  727. struct stat statDestination;
  728. if (stat(destination, &statDestination) != 0)
  729. {
  730. return true;
  731. }
  732. if(statSource.st_size != statDestination.st_size)
  733. {
  734. return true;
  735. }
  736. std::ifstream finSource(source);
  737. std::ifstream finDestination(destination);
  738. if(!finSource || !finDestination)
  739. {
  740. return true;
  741. }
  742. const int buffer_length = 4096;
  743. char bufferSource[buffer_length];
  744. char bufferDest[buffer_length];
  745. while(finSource && finDestination)
  746. {
  747. if(finSource.getline(bufferSource, buffer_length, '\n')
  748. || finSource.gcount())
  749. {
  750. if(finDestination.getline(bufferDest, buffer_length, '\n')
  751. || finDestination.gcount())
  752. {
  753. // both if statements passed
  754. if(finSource.eof())
  755. {
  756. if(!finDestination.eof())
  757. {
  758. return true;
  759. }
  760. if(finSource.gcount() != finDestination.gcount())
  761. {
  762. return true;
  763. }
  764. if(strncmp(bufferSource, bufferDest, finSource.gcount()) != 0)
  765. {
  766. return true;
  767. }
  768. }
  769. else if(finSource.fail())
  770. {
  771. if(!finDestination.fail())
  772. {
  773. return true;
  774. }
  775. if(strcmp(bufferSource, bufferDest) != 0)
  776. {
  777. return true;
  778. }
  779. finSource.clear(finSource.rdstate() & ~std::ios::failbit);
  780. finDestination.clear(finDestination.rdstate() & ~std::ios::failbit);
  781. }
  782. else
  783. {
  784. if(strcmp(bufferSource, bufferDest) != 0)
  785. {
  786. return true;
  787. }
  788. }
  789. }
  790. else
  791. {
  792. return true;
  793. }
  794. }
  795. }
  796. return false;
  797. }
  798. /**
  799. * Copy a file named by "source" to the file named by "destination".
  800. */
  801. void cmSystemTools::cmCopyFile(const char* source,
  802. const char* destination)
  803. {
  804. const int bufferSize = 4096;
  805. char buffer[bufferSize];
  806. std::ifstream fin(source,
  807. #ifdef _WIN32
  808. std::ios::binary |
  809. #endif
  810. std::ios::in);
  811. if(!fin)
  812. {
  813. cmSystemTools::Error("CopyFile failed to open input file \"",
  814. source, "\"");
  815. return;
  816. }
  817. std::ofstream fout(destination,
  818. #ifdef _WIN32
  819. std::ios::binary |
  820. #endif
  821. std::ios::out | std::ios::trunc);
  822. if(!fout)
  823. {
  824. cmSystemTools::Error("CopyFile failed to open output file \"",
  825. destination, "\"");
  826. return;
  827. }
  828. // This copy loop is very sensitive on certain platforms with
  829. // slightly broken stream libraries (like HPUX). Normally, it is
  830. // incorrect to not check the error condition on the fin.read()
  831. // before using the data, but the fin.gcount() will be zero if an
  832. // error occurred. Therefore, the loop should be safe everywhere.
  833. while(fin)
  834. {
  835. fin.read(buffer, bufferSize);
  836. if(fin.gcount())
  837. {
  838. fout.write(buffer, fin.gcount());
  839. }
  840. }
  841. }
  842. // return true if the file exists
  843. long int cmSystemTools::ModifiedTime(const char* filename)
  844. {
  845. struct stat fs;
  846. if (stat(filename, &fs) != 0)
  847. {
  848. return 0;
  849. }
  850. else
  851. {
  852. return (long int)fs.st_mtime;
  853. }
  854. }
  855. bool cmSystemTools::RemoveFile(const char* source)
  856. {
  857. return unlink(source) != 0 ? false : true;
  858. }
  859. bool cmSystemTools::IsOn(const char* val)
  860. {
  861. if (!val)
  862. {
  863. return false;
  864. }
  865. std::basic_string<char> v = val;
  866. for(std::basic_string<char>::iterator c = v.begin();
  867. c != v.end(); c++)
  868. {
  869. *c = toupper(*c);
  870. }
  871. return (v == "ON" || v == "1" || v == "YES" || v == "TRUE" || v == "Y");
  872. }
  873. bool cmSystemTools::IsOff(const char* val)
  874. {
  875. if (!val || strlen(val) == 0)
  876. {
  877. return true;
  878. }
  879. std::basic_string<char> v = val;
  880. for(std::basic_string<char>::iterator c = v.begin();
  881. c != v.end(); c++)
  882. {
  883. *c = toupper(*c);
  884. }
  885. return (v == "OFF" || v == "0" || v == "NO" || v == "FALSE" ||
  886. v == "N" || v == "NOTFOUND" || v == "IGNORE");
  887. }
  888. bool cmSystemTools::RunCommand(const char* command,
  889. std::string& output,
  890. bool verbose)
  891. {
  892. int foo;
  893. return cmSystemTools::RunCommand(command, output, foo, verbose);
  894. }
  895. bool cmSystemTools::RunCommand(const char* command,
  896. std::string& output,
  897. int &retVal, bool verbose)
  898. {
  899. const int BUFFER_SIZE = 4096;
  900. char buffer[BUFFER_SIZE];
  901. if(s_DisableRunCommandOutput)
  902. {
  903. verbose = false;
  904. }
  905. #if defined(WIN32) && !defined(__CYGWIN__)
  906. std::string commandToFile = command;
  907. commandToFile += " > ";
  908. std::string tempFile;
  909. tempFile += _tempnam(0, "cmake");
  910. commandToFile += tempFile;
  911. retVal = system(commandToFile.c_str());
  912. std::ifstream fin(tempFile.c_str());
  913. if(!fin)
  914. {
  915. if(verbose)
  916. {
  917. std::string errormsg = "RunCommand produced no output: command: \"";
  918. errormsg += command;
  919. errormsg += "\"";
  920. errormsg += "\nOutput file: ";
  921. errormsg += tempFile;
  922. cmSystemTools::Error(errormsg.c_str());
  923. }
  924. fin.close();
  925. cmSystemTools::RemoveFile(tempFile.c_str());
  926. return false;
  927. }
  928. while(fin)
  929. {
  930. fin.getline(buffer, BUFFER_SIZE);
  931. output += buffer;
  932. }
  933. fin.close();
  934. cmSystemTools::RemoveFile(tempFile.c_str());
  935. return true;
  936. #else
  937. if(verbose)
  938. {
  939. std::cout << "running " << command << std::endl;
  940. }
  941. fflush(stdout);
  942. fflush(stderr);
  943. FILE* cpipe = popen(command, "r");
  944. if(!cpipe)
  945. {
  946. return false;
  947. }
  948. fgets(buffer, BUFFER_SIZE, cpipe);
  949. while(!feof(cpipe))
  950. {
  951. if(verbose)
  952. {
  953. std::cout << buffer << std::flush;
  954. }
  955. output += buffer;
  956. fgets(buffer, BUFFER_SIZE, cpipe);
  957. }
  958. retVal = pclose(cpipe);
  959. retVal = WEXITSTATUS(retVal);
  960. return true;
  961. #endif
  962. }
  963. /**
  964. * Find the file the given name. Searches the given path and then
  965. * the system search path. Returns the full path to the file if it is
  966. * found. Otherwise, the empty string is returned.
  967. */
  968. std::string cmSystemTools::FindFile(const char* name,
  969. const std::vector<std::string>& userPaths)
  970. {
  971. // Add the system search path to our path.
  972. std::vector<std::string> path = userPaths;
  973. cmSystemTools::GetPath(path);
  974. std::string tryPath;
  975. for(std::vector<std::string>::const_iterator p = path.begin();
  976. p != path.end(); ++p)
  977. {
  978. tryPath = *p;
  979. tryPath += "/";
  980. tryPath += name;
  981. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  982. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  983. {
  984. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  985. }
  986. }
  987. // Couldn't find the file.
  988. return "";
  989. }
  990. /**
  991. * Find the executable with the given name. Searches the given path and then
  992. * the system search path. Returns the full path to the executable if it is
  993. * found. Otherwise, the empty string is returned.
  994. */
  995. std::string cmSystemTools::FindProgram(const char* name,
  996. const std::vector<std::string>& userPaths)
  997. {
  998. // See if the executable exists as written.
  999. if(cmSystemTools::FileExists(name) &&
  1000. !cmSystemTools::FileIsDirectory(name))
  1001. {
  1002. return cmSystemTools::CollapseFullPath(name);
  1003. }
  1004. std::string tryPath = name;
  1005. tryPath += cmSystemTools::GetExecutableExtension();
  1006. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1007. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1008. {
  1009. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1010. }
  1011. // Add the system search path to our path.
  1012. std::vector<std::string> path = userPaths;
  1013. cmSystemTools::GetPath(path);
  1014. for(std::vector<std::string>::const_iterator p = path.begin();
  1015. p != path.end(); ++p)
  1016. {
  1017. tryPath = *p;
  1018. tryPath += "/";
  1019. tryPath += name;
  1020. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1021. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1022. {
  1023. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1024. }
  1025. #ifdef _WIN32
  1026. tryPath += ".com";
  1027. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1028. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1029. {
  1030. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1031. }
  1032. tryPath = *p;
  1033. tryPath += "/";
  1034. tryPath += name;
  1035. #endif
  1036. tryPath += cmSystemTools::GetExecutableExtension();
  1037. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1038. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1039. {
  1040. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1041. }
  1042. }
  1043. // Couldn't find the program.
  1044. return "";
  1045. }
  1046. /**
  1047. * Find the library with the given name. Searches the given path and then
  1048. * the system search path. Returns the full path to the library if it is
  1049. * found. Otherwise, the empty string is returned.
  1050. */
  1051. std::string cmSystemTools::FindLibrary(const char* name,
  1052. const std::vector<std::string>& userPaths)
  1053. {
  1054. // See if the executable exists as written.
  1055. if(cmSystemTools::FileExists(name))
  1056. {
  1057. return cmSystemTools::CollapseFullPath(name);
  1058. }
  1059. // Add the system search path to our path.
  1060. std::vector<std::string> path = userPaths;
  1061. cmSystemTools::GetPath(path);
  1062. std::string tryPath;
  1063. for(std::vector<std::string>::const_iterator p = path.begin();
  1064. p != path.end(); ++p)
  1065. {
  1066. #if defined(_WIN32) && !defined(__CYGWIN__)
  1067. tryPath = *p;
  1068. tryPath += "/";
  1069. tryPath += name;
  1070. tryPath += ".lib";
  1071. if(cmSystemTools::FileExists(tryPath.c_str()))
  1072. {
  1073. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1074. }
  1075. #else
  1076. tryPath = *p;
  1077. tryPath += "/lib";
  1078. tryPath += name;
  1079. tryPath += ".so";
  1080. if(cmSystemTools::FileExists(tryPath.c_str()))
  1081. {
  1082. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1083. }
  1084. tryPath = *p;
  1085. tryPath += "/lib";
  1086. tryPath += name;
  1087. tryPath += ".a";
  1088. if(cmSystemTools::FileExists(tryPath.c_str()))
  1089. {
  1090. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1091. }
  1092. tryPath = *p;
  1093. tryPath += "/lib";
  1094. tryPath += name;
  1095. tryPath += ".sl";
  1096. if(cmSystemTools::FileExists(tryPath.c_str()))
  1097. {
  1098. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1099. }
  1100. #endif
  1101. }
  1102. // Couldn't find the library.
  1103. return "";
  1104. }
  1105. bool cmSystemTools::FileIsDirectory(const char* name)
  1106. {
  1107. struct stat fs;
  1108. if(stat(name, &fs) == 0)
  1109. {
  1110. #if _WIN32
  1111. return ((fs.st_mode & _S_IFDIR) != 0);
  1112. #else
  1113. return S_ISDIR(fs.st_mode);
  1114. #endif
  1115. }
  1116. else
  1117. {
  1118. return false;
  1119. }
  1120. }
  1121. int cmSystemTools::ChangeDirectory(const char *dir)
  1122. {
  1123. return Chdir(dir);
  1124. }
  1125. std::string cmSystemTools::GetCurrentWorkingDirectory()
  1126. {
  1127. char buf[2048];
  1128. std::string path = Getcwd(buf, 2048);
  1129. return path;
  1130. }
  1131. /**
  1132. * Given the path to a program executable, get the directory part of the path with the
  1133. * file stripped off. If there is no directory part, the empty string is returned.
  1134. */
  1135. std::string cmSystemTools::GetProgramPath(const char* in_name)
  1136. {
  1137. std::string dir, file;
  1138. cmSystemTools::SplitProgramPath(in_name, dir, file);
  1139. return dir;
  1140. }
  1141. /**
  1142. * Given the path to a program executable, get the directory part of the path
  1143. * with the file stripped off. If there is no directory part, the empty
  1144. * string is returned.
  1145. */
  1146. void cmSystemTools::SplitProgramPath(const char* in_name,
  1147. std::string& dir,
  1148. std::string& file)
  1149. {
  1150. dir = in_name;
  1151. file = "";
  1152. cmSystemTools::ConvertToUnixSlashes(dir);
  1153. if(!cmSystemTools::FileIsDirectory(dir.c_str()))
  1154. {
  1155. std::string::size_type slashPos = dir.rfind("/");
  1156. if(slashPos != std::string::npos)
  1157. {
  1158. file = dir.substr(slashPos+1);
  1159. dir = dir.substr(0, slashPos);
  1160. }
  1161. else
  1162. {
  1163. file = dir;
  1164. dir = "";
  1165. }
  1166. }
  1167. if((dir != "") && !cmSystemTools::FileIsDirectory(dir.c_str()))
  1168. {
  1169. std::string oldDir = in_name;
  1170. cmSystemTools::ConvertToUnixSlashes(oldDir);
  1171. cmSystemTools::Error("Error splitting file name off end of path:\n",
  1172. oldDir.c_str(), "\nDirectory not found: ",
  1173. dir.c_str());
  1174. dir = in_name;
  1175. return;
  1176. }
  1177. }
  1178. /**
  1179. * Given a path to a file or directory, convert it to a full path.
  1180. * This collapses away relative paths. The full path is returned.
  1181. */
  1182. std::string cmSystemTools::CollapseFullPath(const char* in_name)
  1183. {
  1184. std::string dir, file;
  1185. cmSystemTools::SplitProgramPath(in_name, dir, file);
  1186. #ifdef _WIN32
  1187. // Ultra-hack warning:
  1188. // This changes to the target directory, saves the working directory,
  1189. // and then changes back to the original working directory.
  1190. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  1191. if(dir != "") { Chdir(dir.c_str()); }
  1192. std::string newDir = cmSystemTools::GetCurrentWorkingDirectory();
  1193. Chdir(cwd.c_str());
  1194. cmSystemTools::ConvertToUnixSlashes(newDir);
  1195. std::string newPath = newDir+"/"+file;
  1196. return newPath;
  1197. #else
  1198. # ifdef MAXPATHLEN
  1199. char resolved_name[MAXPATHLEN];
  1200. # else
  1201. # ifdef PATH_MAX
  1202. char resolved_name[PATH_MAX];
  1203. # else
  1204. char resolved_name[5024];
  1205. # endif
  1206. # endif
  1207. if(dir != "")
  1208. {
  1209. realpath(dir.c_str(), resolved_name);
  1210. dir = resolved_name;
  1211. }
  1212. else
  1213. {
  1214. dir = cmSystemTools::GetCurrentWorkingDirectory();
  1215. }
  1216. if(file == "")
  1217. {
  1218. return dir;
  1219. }
  1220. return dir + "/" + file;
  1221. #endif
  1222. }
  1223. /**
  1224. * Return path of a full filename (no trailing slashes).
  1225. * Warning: returned path is converted to Unix slashes format.
  1226. */
  1227. std::string cmSystemTools::GetFilenamePath(const std::string& filename)
  1228. {
  1229. std::string fn = filename;
  1230. cmSystemTools::ConvertToUnixSlashes(fn);
  1231. std::string::size_type slash_pos = fn.rfind("/");
  1232. if(slash_pos != std::string::npos)
  1233. {
  1234. return fn.substr(0, slash_pos);
  1235. }
  1236. else
  1237. {
  1238. return "";
  1239. }
  1240. }
  1241. /**
  1242. * Return file name of a full filename (i.e. file name without path).
  1243. */
  1244. std::string cmSystemTools::GetFilenameName(const std::string& filename)
  1245. {
  1246. std::string fn = filename;
  1247. cmSystemTools::ConvertToUnixSlashes(fn);
  1248. std::string::size_type slash_pos = fn.rfind("/");
  1249. if(slash_pos != std::string::npos)
  1250. {
  1251. return fn.substr(slash_pos + 1);
  1252. }
  1253. else
  1254. {
  1255. return filename;
  1256. }
  1257. }
  1258. /**
  1259. * Return file extension of a full filename (dot included).
  1260. * Warning: this is the longest extension (for example: .tar.gz)
  1261. */
  1262. std::string cmSystemTools::GetFilenameExtension(const std::string& filename)
  1263. {
  1264. std::string name = cmSystemTools::GetFilenameName(filename);
  1265. std::string::size_type dot_pos = name.find(".");
  1266. if(dot_pos != std::string::npos)
  1267. {
  1268. return name.substr(dot_pos);
  1269. }
  1270. else
  1271. {
  1272. return "";
  1273. }
  1274. }
  1275. /**
  1276. * Return file extension of a full filename (dot included).
  1277. */
  1278. std::string cmSystemTools::GetFilenameShortestExtension(const std::string& filename)
  1279. {
  1280. std::string name = cmSystemTools::GetFilenameName(filename);
  1281. std::string::size_type dot_pos = name.rfind(".");
  1282. if(dot_pos != std::string::npos)
  1283. {
  1284. return name.substr(dot_pos);
  1285. }
  1286. else
  1287. {
  1288. return "";
  1289. }
  1290. }
  1291. /**
  1292. * Return file name without extension of a full filename (i.e. without path).
  1293. * Warning: it considers the longest extension (for example: .tar.gz)
  1294. */
  1295. std::string cmSystemTools::GetFilenameNameWithoutExtension(const std::string& filename)
  1296. {
  1297. std::string name = cmSystemTools::GetFilenameName(filename);
  1298. std::string::size_type dot_pos = name.find(".");
  1299. if(dot_pos != std::string::npos)
  1300. {
  1301. return name.substr(0, dot_pos);
  1302. }
  1303. else
  1304. {
  1305. return name;
  1306. }
  1307. }
  1308. void cmSystemTools::Glob(const char *directory, const char *regexp,
  1309. std::vector<std::string>& files)
  1310. {
  1311. cmDirectory d;
  1312. cmRegularExpression reg(regexp);
  1313. if (d.Load(directory))
  1314. {
  1315. size_t numf;
  1316. unsigned int i;
  1317. numf = d.GetNumberOfFiles();
  1318. for (i = 0; i < numf; i++)
  1319. {
  1320. std::string fname = d.GetFile(i);
  1321. if (reg.find(fname))
  1322. {
  1323. files.push_back(fname);
  1324. }
  1325. }
  1326. }
  1327. }
  1328. void cmSystemTools::GlobDirs(const char *fullPath,
  1329. std::vector<std::string>& files)
  1330. {
  1331. std::string path = fullPath;
  1332. std::string::size_type pos = path.find("/*");
  1333. if(pos == std::string::npos)
  1334. {
  1335. files.push_back(fullPath);
  1336. return;
  1337. }
  1338. std::string startPath = path.substr(0, pos);
  1339. std::string finishPath = path.substr(pos+2);
  1340. cmDirectory d;
  1341. if (d.Load(startPath.c_str()))
  1342. {
  1343. for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
  1344. {
  1345. if((std::string(d.GetFile(i)) != ".")
  1346. && (std::string(d.GetFile(i)) != ".."))
  1347. {
  1348. std::string fname = startPath;
  1349. fname +="/";
  1350. fname += d.GetFile(i);
  1351. if(cmSystemTools::FileIsDirectory(fname.c_str()))
  1352. {
  1353. fname += finishPath;
  1354. cmSystemTools::GlobDirs(fname.c_str(), files);
  1355. }
  1356. }
  1357. }
  1358. }
  1359. }
  1360. void cmSystemTools::ExpandListArguments(std::vector<std::string> const& arguments,
  1361. std::vector<std::string>& newargs)
  1362. {
  1363. std::vector<std::string>::const_iterator i;
  1364. for(i = arguments.begin();i != arguments.end(); ++i)
  1365. {
  1366. if(i->find(';') != std::string::npos)
  1367. {
  1368. std::string::size_type start = 0;
  1369. std::string::size_type endpos = 0;
  1370. while(endpos != std::string::npos)
  1371. {
  1372. endpos = i->find(';', start);
  1373. std::string::size_type len;
  1374. if(endpos != std::string::npos)
  1375. {
  1376. len = endpos - start;
  1377. }
  1378. else
  1379. {
  1380. len = i->size()-start;
  1381. }
  1382. newargs.push_back(i->substr(start, len));
  1383. start = endpos+1;
  1384. }
  1385. }
  1386. else
  1387. {
  1388. newargs.push_back(*i);
  1389. }
  1390. }
  1391. }