CmPath.cpp 10 KB

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