BsPath.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. #include "BsPrerequisitesUtil.h"
  2. #include "BsException.h"
  3. namespace BansheeEngine
  4. {
  5. const Path Path::BLANK = Path();
  6. Path::Path()
  7. :mIsAbsolute(false)
  8. { }
  9. Path::Path(const WString& pathStr, PathType type)
  10. {
  11. assign(pathStr, type);
  12. }
  13. Path::Path(const String& pathStr, PathType type)
  14. {
  15. assign(pathStr, type);
  16. }
  17. Path::Path(wchar_t* pathStr, PathType type)
  18. {
  19. assign(pathStr);
  20. }
  21. Path::Path(const char* pathStr, PathType type)
  22. {
  23. assign(pathStr);
  24. }
  25. Path::Path(const Path& other)
  26. {
  27. assign(other);
  28. }
  29. Path& Path::operator= (const Path& path)
  30. {
  31. assign(path);
  32. return *this;
  33. }
  34. Path& Path::operator= (const WString& pathStr)
  35. {
  36. assign(pathStr);
  37. return *this;
  38. }
  39. Path& Path::operator= (const String& pathStr)
  40. {
  41. assign(pathStr);
  42. return *this;
  43. }
  44. Path& Path::operator= (const wchar_t* pathStr)
  45. {
  46. assign(pathStr);
  47. return *this;
  48. }
  49. Path& Path::operator= (const char* pathStr)
  50. {
  51. assign(pathStr);
  52. return *this;
  53. }
  54. void Path::swap(Path& path)
  55. {
  56. std::swap(mDirectories, path.mDirectories);
  57. std::swap(mFilename, path.mFilename);
  58. std::swap(mDevice, path.mDevice);
  59. std::swap(mNode, path.mNode);
  60. std::swap(mIsAbsolute, path.mIsAbsolute);
  61. }
  62. void Path::assign(const Path& path)
  63. {
  64. mDirectories = path.mDirectories;
  65. mFilename = path.mFilename;
  66. mDevice = path.mDevice;
  67. mNode = path.mNode;
  68. mIsAbsolute = path.mIsAbsolute;
  69. }
  70. void Path::assign(const WString& pathStr, PathType type)
  71. {
  72. assign(pathStr.data(), (UINT32)pathStr.length(), type);
  73. }
  74. void Path::assign(const String& pathStr, PathType type)
  75. {
  76. assign(pathStr.data(), (UINT32)pathStr.length(), type);
  77. }
  78. void Path::assign(const wchar_t* pathStr, PathType type)
  79. {
  80. assign(pathStr, (UINT32)wcslen(pathStr), type);
  81. }
  82. void Path::assign(const char* pathStr, PathType type)
  83. {
  84. assign(pathStr, (UINT32)strlen(pathStr), type);
  85. }
  86. void Path::assign(const wchar_t* pathStr, UINT32 numChars, PathType type)
  87. {
  88. switch (type)
  89. {
  90. case PathType::Windows:
  91. parseWindows(pathStr, numChars);
  92. break;
  93. case PathType::Unix:
  94. parseUnix(pathStr, numChars);
  95. break;
  96. default:
  97. #if BS_PLATFORM == BS_PLATFORM_WIN32
  98. parseWindows(pathStr, numChars);
  99. #elif BS_PLATFORM == BS_PLATFORM_APPLE || BS_PLATFORM == BS_PLATFORM_LINUX
  100. parseUnix(pathStr, numChars);
  101. #else
  102. static_assert(false, "Unsupported platform for path.");
  103. #endif
  104. break;
  105. }
  106. }
  107. void Path::assign(const char* pathStr, UINT32 numChars, PathType type)
  108. {
  109. switch (type)
  110. {
  111. case PathType::Windows:
  112. parseWindows(pathStr, numChars);
  113. break;
  114. case PathType::Unix:
  115. parseUnix(pathStr, numChars);
  116. break;
  117. default:
  118. #if BS_PLATFORM == BS_PLATFORM_WIN32
  119. parseWindows(pathStr, numChars);
  120. #elif BS_PLATFORM == BS_PLATFORM_APPLE || BS_PLATFORM == BS_PLATFORM_LINUX
  121. parseUnix(pathStr, numChars);
  122. #else
  123. static_assert(false, "Unsupported platform for path.");
  124. #endif
  125. break;
  126. }
  127. }
  128. WString Path::toWString(PathType type) const
  129. {
  130. switch (type)
  131. {
  132. case PathType::Windows:
  133. return buildWindows();
  134. case PathType::Unix:
  135. return buildUnix();
  136. default:
  137. #if BS_PLATFORM == BS_PLATFORM_WIN32
  138. return buildWindows();
  139. #elif BS_PLATFORM == BS_PLATFORM_APPLE || BS_PLATFORM == BS_PLATFORM_LINUX
  140. return buildUnix();
  141. #else
  142. static_assert(false, "Unsupported platform for path.");
  143. #endif
  144. break;
  145. }
  146. }
  147. String Path::toString(PathType type) const
  148. {
  149. switch (type)
  150. {
  151. case PathType::Windows:
  152. return BansheeEngine::toString(buildWindows());
  153. case PathType::Unix:
  154. return BansheeEngine::toString(buildUnix());
  155. default:
  156. #if BS_PLATFORM == BS_PLATFORM_WIN32
  157. return BansheeEngine::toString(buildWindows());
  158. #elif BS_PLATFORM == BS_PLATFORM_APPLE || BS_PLATFORM == BS_PLATFORM_LINUX
  159. return BansheeEngine::toString(buildUnix());
  160. #else
  161. static_assert(false, "Unsupported platform for path.");
  162. #endif
  163. break;
  164. }
  165. }
  166. Path Path::getParent() const
  167. {
  168. Path copy = *this;
  169. copy.makeParent();
  170. return copy;
  171. }
  172. Path Path::getAbsolute(const Path& base) const
  173. {
  174. Path copy = *this;
  175. copy.makeAbsolute(base);
  176. return copy;
  177. }
  178. Path Path::getRelative(const Path& base) const
  179. {
  180. Path copy = *this;
  181. copy.makeRelative(base);
  182. return copy;
  183. }
  184. Path Path::getDirectory() const
  185. {
  186. Path copy = *this;
  187. copy.mFilename.clear();
  188. return copy;
  189. }
  190. Path& Path::makeParent()
  191. {
  192. if (mFilename.empty())
  193. {
  194. if (mDirectories.empty())
  195. {
  196. if (!mIsAbsolute)
  197. mDirectories.push_back(L"..");
  198. }
  199. else
  200. {
  201. if (mDirectories.back() == L"..")
  202. mDirectories.push_back(L"..");
  203. else
  204. mDirectories.pop_back();
  205. }
  206. }
  207. else
  208. {
  209. mFilename.clear();
  210. }
  211. return *this;
  212. }
  213. Path& Path::makeAbsolute(const Path& base)
  214. {
  215. if (mIsAbsolute)
  216. return *this;
  217. Path absDir = base.getDirectory();
  218. if (base.isFile())
  219. absDir.pushDirectory(base.mFilename);
  220. for (auto& dir : mDirectories)
  221. absDir.pushDirectory(dir);
  222. absDir.setFilename(mFilename);
  223. *this = absDir;
  224. return *this;
  225. }
  226. Path& Path::makeRelative(const Path& base)
  227. {
  228. if (!base.includes(*this))
  229. return *this;
  230. mDirectories.erase(mDirectories.begin(), mDirectories.begin() + base.mDirectories.size());
  231. // Sometimes a directory name can be interpreted as a file and we're okay with that. Check for that
  232. // special case.
  233. if (base.isFile())
  234. {
  235. if (mDirectories.size() > 0)
  236. mDirectories.erase(mDirectories.begin());
  237. else
  238. mFilename = L"";
  239. }
  240. mDevice = L"";
  241. mNode = L"";
  242. mIsAbsolute = false;
  243. return *this;
  244. }
  245. bool Path::includes(const Path& child) const
  246. {
  247. if (mDevice != child.mDevice)
  248. return false;
  249. if (mNode != child.mNode)
  250. return false;
  251. auto iterParent = mDirectories.begin();
  252. auto iterChild = child.mDirectories.begin();
  253. for (; iterParent != mDirectories.end(); ++iterChild, ++iterParent)
  254. {
  255. if (iterChild == child.mDirectories.end())
  256. return false;
  257. if (!comparePathElem(*iterChild, *iterParent))
  258. return false;
  259. }
  260. if (!mFilename.empty())
  261. {
  262. if (iterChild == child.mDirectories.end())
  263. {
  264. if (child.mFilename.empty())
  265. return false;
  266. if (!comparePathElem(child.mFilename, mFilename))
  267. return false;
  268. }
  269. else
  270. {
  271. if (!comparePathElem(*iterChild, mFilename))
  272. return false;
  273. }
  274. }
  275. return true;
  276. }
  277. bool Path::equals(const Path& other) const
  278. {
  279. if (mIsAbsolute != other.mIsAbsolute)
  280. return false;
  281. if (mIsAbsolute)
  282. {
  283. if (!comparePathElem(mDevice, other.mDevice))
  284. return false;
  285. }
  286. if (mDirectories.size() != other.mDirectories.size())
  287. return false;
  288. if (!comparePathElem(mFilename, other.mFilename))
  289. return false;
  290. if (!comparePathElem(mNode, other.mNode))
  291. return false;
  292. auto iterMe = mDirectories.begin();
  293. auto iterOther = other.mDirectories.begin();
  294. for (; iterMe != mDirectories.end(); ++iterMe, ++iterOther)
  295. {
  296. if (!comparePathElem(*iterMe, *iterOther))
  297. return false;
  298. }
  299. return true;
  300. }
  301. Path& Path::append(const Path& path)
  302. {
  303. if (!mFilename.empty())
  304. pushDirectory(mFilename);
  305. for (auto& dir : path.mDirectories)
  306. pushDirectory(dir);
  307. mFilename = path.mFilename;
  308. return *this;
  309. }
  310. void Path::setBasename(const WString& basename)
  311. {
  312. mFilename = basename + getWExtension();
  313. }
  314. void Path::setBasename(const String& basename)
  315. {
  316. mFilename = BansheeEngine::toWString(basename) + getWExtension();
  317. }
  318. void Path::setExtension(const WString& extension)
  319. {
  320. WStringStream stream;
  321. stream << getWFilename(false);
  322. stream << extension;
  323. mFilename = stream.str();
  324. }
  325. void Path::setExtension(const String& extension)
  326. {
  327. setExtension(BansheeEngine::toWString(extension));
  328. }
  329. WString Path::getWFilename(bool extension) const
  330. {
  331. if (extension)
  332. return mFilename;
  333. else
  334. {
  335. WString::size_type pos = mFilename.rfind(L'.');
  336. if (pos != WString::npos)
  337. return mFilename.substr(0, pos);
  338. else
  339. return mFilename;
  340. }
  341. }
  342. String Path::getFilename(bool extension) const
  343. {
  344. return BansheeEngine::toString(getWFilename(extension));
  345. }
  346. WString Path::getWExtension() const
  347. {
  348. WString::size_type pos = mFilename.rfind(L'.');
  349. if (pos != WString::npos)
  350. return mFilename.substr(pos);
  351. else
  352. return WString();
  353. }
  354. String Path::getExtension() const
  355. {
  356. return BansheeEngine::toString(getWExtension());
  357. }
  358. const WString& Path::getWDirectory(UINT32 idx) const
  359. {
  360. if (idx >= (UINT32)mDirectories.size())
  361. {
  362. BS_EXCEPT(InvalidParametersException, "Index out of range: " + BansheeEngine::toString(idx) +
  363. ". Valid range: [0, " + BansheeEngine::toString((UINT32)mDirectories.size() - 1) + "]");
  364. }
  365. return mDirectories[idx];
  366. }
  367. String Path::getDirectory(UINT32 idx) const
  368. {
  369. return BansheeEngine::toString(getWDirectory(idx));
  370. }
  371. WString Path::getWTail(PathType type) const
  372. {
  373. if (isFile())
  374. return mFilename;
  375. else if (mDirectories.size() > 0)
  376. return mDirectories.back();
  377. else
  378. return StringUtil::WBLANK;
  379. }
  380. String Path::getTail(PathType type) const
  381. {
  382. return BansheeEngine::toString(getWTail(type));
  383. }
  384. void Path::clear()
  385. {
  386. mDirectories.clear();
  387. mDevice.clear();
  388. mFilename.clear();
  389. mNode.clear();
  390. mIsAbsolute = false;
  391. }
  392. void Path::throwInvalidPathException(const WString& path) const
  393. {
  394. BS_EXCEPT(InvalidParametersException, "Incorrectly formatted path provided: " + BansheeEngine::toString(path));
  395. }
  396. void Path::throwInvalidPathException(const String& path) const
  397. {
  398. BS_EXCEPT(InvalidParametersException, "Incorrectly formatted path provided: " + path);
  399. }
  400. WString Path::buildWindows() const
  401. {
  402. WStringStream result;
  403. if (!mNode.empty())
  404. {
  405. result << L"\\\\";
  406. result << mNode;
  407. result << L"\\";
  408. }
  409. else if (!mDevice.empty())
  410. {
  411. result << mDevice;
  412. result << L":\\";
  413. }
  414. else if (mIsAbsolute)
  415. {
  416. result << L"\\";
  417. }
  418. for (auto& dir : mDirectories)
  419. {
  420. result << dir;
  421. result << L"\\";
  422. }
  423. result << mFilename;
  424. return result.str();
  425. }
  426. WString Path::buildUnix() const
  427. {
  428. WStringStream result;
  429. auto dirIter = mDirectories.begin();
  430. if (!mDevice.empty())
  431. {
  432. result << L"/";
  433. result << mDevice;
  434. result << L":/";
  435. }
  436. else if (mIsAbsolute)
  437. {
  438. if (dirIter != mDirectories.end() && *dirIter == L"~")
  439. {
  440. result << L"~";
  441. dirIter++;
  442. }
  443. result << L"/";
  444. }
  445. for (; dirIter != mDirectories.end(); ++dirIter)
  446. {
  447. result << *dirIter;
  448. result << L"/";
  449. }
  450. result << mFilename;
  451. return result.str();
  452. }
  453. Path Path::operator+ (const Path& rhs) const
  454. {
  455. return Path::combine(*this, rhs);
  456. }
  457. Path& Path::operator+= (const Path& rhs)
  458. {
  459. return append(rhs);
  460. }
  461. bool Path::comparePathElem(const WString& left, const WString& right)
  462. {
  463. if (left.size() != right.size())
  464. return false;
  465. // TODO: Case sensitive/insensitive file path actually depends on used file-system but I'm not gonna check that
  466. for (UINT32 i = 0; i < (UINT32)left.size(); i++)
  467. {
  468. if (tolower(left[i]) != tolower(right[i]))
  469. return false;
  470. }
  471. return true;
  472. }
  473. Path Path::combine(const Path& left, const Path& right)
  474. {
  475. Path output = left;
  476. return output.append(right);
  477. }
  478. void Path::pushDirectory(const WString& dir)
  479. {
  480. if (!dir.empty() && dir != L".")
  481. {
  482. if (dir == L"..")
  483. {
  484. if (!mDirectories.empty() && mDirectories.back() != L"..")
  485. mDirectories.pop_back();
  486. else
  487. mDirectories.push_back(dir);
  488. }
  489. else
  490. mDirectories.push_back(dir);
  491. }
  492. }
  493. void Path::pushDirectory(const String& dir)
  494. {
  495. pushDirectory(BansheeEngine::toWString(dir));
  496. }
  497. }