2
0

path.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "core/util/path.h"
  23. namespace Torque
  24. {
  25. //-----------------------------------------------------------------------------
  26. void Path::_split(String name)
  27. {
  28. S32 pos = 0;
  29. S32 idx = 0;
  30. // Make sure we have platform separators
  31. name = PathToPlatform(name);
  32. // root:
  33. idx = name.find(':');
  34. if (idx >= 0)
  35. {
  36. mRoot = name.substr(0,idx);
  37. pos = idx + 1;
  38. }
  39. else if( name[ 0 ] == '/' )
  40. {
  41. mRoot = "/";
  42. }
  43. else
  44. {
  45. mRoot = "";
  46. }
  47. // Extract path and strip trailing '/'
  48. idx = name.find('/', 0, String::Right);
  49. if (idx >= pos)
  50. {
  51. S32 len = idx - pos;
  52. mPath = name.substr(pos,len? len: 1);
  53. mPath = Path::CleanSeparators(mPath);
  54. pos = idx + 1;
  55. }
  56. else
  57. {
  58. mPath = "";
  59. }
  60. // file.ext
  61. idx = name.find('.', 0, String::Right);
  62. if (idx >= pos)
  63. {
  64. mFile = name.substr(pos,idx - pos);
  65. mExt = name.substr(idx + 1,name.length() - idx - 1);
  66. }
  67. else
  68. {
  69. mFile = name.substr(pos,name.length() - pos);
  70. mExt = "";
  71. }
  72. }
  73. String Path::_join() const
  74. {
  75. String name;
  76. if( mRoot != '/' )
  77. name = Path::Join(mRoot, ':', mPath);
  78. else
  79. name = mPath;
  80. name = Path::Join(name, '/', mFile);
  81. name = Path::Join(name, '.', mExt);
  82. return name;
  83. }
  84. String Path::CleanSeparators(String path)
  85. {
  86. return path.replace( '\\', '/' );
  87. }
  88. String Path::CompressPath(String path)
  89. {
  90. // Remove "./" and "../" references. A ".." will also result in the
  91. // removal of the proceeding directory.
  92. // Also cleans separators as in CleanSeparators().
  93. // Assumes there are no trailing "/"
  94. // Start by cleaning separators
  95. path = Path::CleanSeparators(path);
  96. U32 src = 0;
  97. U32 dst = 0;
  98. while (path[src])
  99. {
  100. if (path[src] == '/' && path[src + 1] == '/')
  101. {
  102. src += 1;
  103. continue;
  104. }
  105. else if (path[src] == '.')
  106. {
  107. if (path[src + 1] == 0)
  108. {
  109. if (dst && path[dst - 1] == '/')
  110. dst--;
  111. src++;
  112. break;
  113. }
  114. else if (path[src + 1] == '/')
  115. {
  116. src += 2;
  117. continue;
  118. }
  119. else if (path[src + 1] == '.')
  120. {
  121. if (path[src + 2] == 0)
  122. {
  123. if (dst && path[dst - 1] == '/')
  124. dst = path.find('/', dst - 1, String::Right);
  125. src += 2;
  126. break;
  127. }
  128. if (dst && path[dst - 1] == '/')
  129. dst = path.find('/', dst - 1, String::Right) + 1;
  130. else
  131. dst += 3;
  132. src += 3;
  133. continue;
  134. }
  135. }
  136. if (dst != src)
  137. {
  138. String end = path.substr(src, path.length() - src);
  139. if (dst > 0 && end[(String::SizeType)0] == '/' && path[dst-1] == '/')
  140. end = end.substr(1, end.length() - 1);
  141. path.replace(dst, path.length() - dst, end);
  142. src = dst;
  143. }
  144. else
  145. {
  146. src++;
  147. dst++;
  148. }
  149. }
  150. if (src - dst)
  151. path.erase(dst, src - dst);
  152. return path;
  153. }
  154. Torque::Path Path::MakeRelativePath( const Path &makeRelative, const Path &relativeTo, U32 mode )
  155. {
  156. // We need to find the part of makeRelative that starts to diverge from
  157. // relativeTo. We only need to check up to the end of makeRelative or realtiveTo
  158. // (whichever comes first).
  159. U32 minDirCount = getMin(makeRelative.getDirectoryCount(), relativeTo.getDirectoryCount());
  160. // Store the index of the directory where we diverge. If we never diverge this
  161. // will end up being the same as the number of directories in makeRelative
  162. U32 divergeDirIdx = 0;
  163. for (divergeDirIdx = 0; divergeDirIdx < minDirCount; divergeDirIdx++)
  164. {
  165. // If our directories don't match then this is the diverge directory
  166. if (!makeRelative.getDirectory(divergeDirIdx).equal(relativeTo.getDirectory(divergeDirIdx), mode))
  167. break;
  168. }
  169. // Get the part of makeRelative's path after it diverged from relativeTo's path
  170. String uniquePath;
  171. // If we never diverged then divergeDirIdx will be equal to the number of
  172. // directories in makeRelative and this loop will immediately exit
  173. for (U32 i = divergeDirIdx; i < makeRelative.getDirectoryCount(); i++)
  174. uniquePath += makeRelative.getDirectory(i) + "/";
  175. // Go ahead and add the full file name
  176. uniquePath += makeRelative.getFullFileName();
  177. // Now calculate the relative offset
  178. String offsetPath;
  179. U32 numOffset = relativeTo.getDirectoryCount() - divergeDirIdx;
  180. // Push back an "up" for each directory we are offset
  181. for (U32 i = 0; i < numOffset; i++)
  182. offsetPath += "../";
  183. return offsetPath + uniquePath;
  184. }
  185. String Path::Join(const String& a,String::ValueType s,const String& b)
  186. {
  187. switch (s)
  188. {
  189. case '/':
  190. {
  191. if (b.isEmpty() || (b.length() == 1 && (b.c_str()[0] == '/')))
  192. return a;
  193. if (a.isEmpty())
  194. return b;
  195. String::ValueType c = a[a.length()-1];
  196. if (c == ':' || ((c == '/') ^ (b.c_str()[0] == '/')))
  197. return a + b;
  198. if (c == '/' && b.c_str()[0] == '/')
  199. return a.substr(0,a.length() - 1) + b;
  200. break;
  201. }
  202. case ':':
  203. {
  204. if (a.isEmpty())
  205. return b;
  206. if (b.isEmpty())
  207. return a + ':';
  208. break;
  209. }
  210. case '.':
  211. {
  212. if (b.isEmpty())
  213. return a;
  214. if (a.isEmpty())
  215. return '.' + b;
  216. break;
  217. }
  218. default:
  219. break;
  220. }
  221. return a + s + b;
  222. }
  223. bool Path::appendPath( const Path &p )
  224. {
  225. mPath = CompressPath(Join(mPath,'/',p.getPath()));
  226. mIsDirtyPath = true;
  227. return true;
  228. }
  229. const String &Path::getFullFileName() const
  230. {
  231. if ( mIsDirtyFileName )
  232. {
  233. mFullFileName = mFile;
  234. if (mExt.isNotEmpty())
  235. mFullFileName += '.' + mExt;
  236. mIsDirtyFileName = false;
  237. }
  238. return mFullFileName;
  239. }
  240. const String& Path::getFullPath() const
  241. {
  242. if ( mIsDirtyPath )
  243. {
  244. mFullPath = _join();
  245. mIsDirtyPath = false;
  246. }
  247. return mFullPath;
  248. }
  249. String Path::getFullPathWithoutRoot() const
  250. {
  251. return Torque::Path::Join(getPath(), '/', getFullFileName());
  252. }
  253. String Path::getRootAndPath() const
  254. {
  255. if( mRoot != '/' )
  256. return Path::Join(mRoot, ':', mPath);
  257. else
  258. return mPath;
  259. }
  260. const String& Path::setRoot(const String &s)
  261. {
  262. if ( mRoot != s )
  263. {
  264. mIsDirtyPath = true;
  265. mRoot = s;
  266. }
  267. return mRoot;
  268. }
  269. const String& Path::setPath(const String &s)
  270. {
  271. String clean = CleanSeparators(s);
  272. if ( mPath != clean )
  273. {
  274. mIsDirtyPath = true;
  275. mPath = clean;
  276. }
  277. return mPath;
  278. }
  279. const String& Path::setFileName(const String &s)
  280. {
  281. if ( mFile != s )
  282. {
  283. mIsDirtyPath = true;
  284. mIsDirtyFileName = true;
  285. mFile = s;
  286. }
  287. return mFile;
  288. }
  289. const String& Path::setExtension(const String &s)
  290. {
  291. if ( mExt != s )
  292. {
  293. mIsDirtyPath = true;
  294. mIsDirtyFileName = true;
  295. mExt = s;
  296. }
  297. return mExt;
  298. }
  299. bool Path::isDirectory() const
  300. {
  301. return mFile.isEmpty() && mExt.isEmpty();
  302. }
  303. bool Path::isRelative() const
  304. {
  305. return (mPath.isEmpty() || mPath.c_str()[0] != '/');
  306. }
  307. bool Path::isAbsolute() const
  308. {
  309. return (!mPath.isEmpty() && mPath.c_str()[0] == '/');
  310. }
  311. U32 Path::getDirectoryCount() const
  312. {
  313. if (mPath.isEmpty())
  314. return 0;
  315. U32 count = 0;
  316. U32 offset = 0;
  317. if (mPath.c_str()[0] == '/')
  318. offset++;
  319. while (offset < mPath.length())
  320. {
  321. if (mPath[offset++] == '/')
  322. count++;
  323. }
  324. return count + 1;
  325. }
  326. String Path::getDirectory(U32 count) const
  327. {
  328. if (mPath.isEmpty())
  329. return String();
  330. U32 offset = 0;
  331. if (mPath.c_str()[0] == '/')
  332. offset++;
  333. while (count && offset < mPath.length())
  334. {
  335. if (mPath[offset++] == '/')
  336. count--;
  337. }
  338. U32 end = offset;
  339. while (end < mPath.length() && mPath[end] != '/')
  340. end++;
  341. return mPath.substr(offset,end - offset);
  342. }
  343. //-----------------------------------------------------------------------------
  344. String PathToPlatform(String file)
  345. {
  346. if (Path::OsSeparator != '/')
  347. file.replace( Path::OsSeparator, '/' );
  348. return file;
  349. }
  350. String PathToOS(String file)
  351. {
  352. if (Path::OsSeparator != '/')
  353. file.replace( '/', Path::OsSeparator );
  354. return file;
  355. }
  356. } // Namespace