FileSystem.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  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 deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // 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 FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "ArrayPtr.h"
  24. #include "Context.h"
  25. #include "File.h"
  26. #include "FileSystem.h"
  27. #include "Log.h"
  28. #include <cstdio>
  29. #include <cstring>
  30. #ifdef WIN32
  31. #ifndef _MSC_VER
  32. #define _WIN32_IE 0x501
  33. #endif
  34. #include <windows.h>
  35. #include <shellapi.h>
  36. #include <direct.h>
  37. #include <shlobj.h>
  38. #else
  39. #include <dirent.h>
  40. #include <errno.h>
  41. #include <unistd.h>
  42. #include <sys/stat.h>
  43. #include <sys/wait.h>
  44. #define MAX_PATH 256
  45. #endif
  46. #if defined(__APPLE__)
  47. #include <mach-o/dyld.h>
  48. #endif
  49. #ifdef ANDROID
  50. extern "C" const char* SDL_Android_GetFilesDir();
  51. #endif
  52. #ifdef IOS
  53. extern "C" const char* SDL_IOS_GetResourceDir();
  54. #endif
  55. #include "DebugNew.h"
  56. namespace Urho3D
  57. {
  58. FileSystem::FileSystem(Context* context) :
  59. Object(context)
  60. {
  61. }
  62. FileSystem::~FileSystem()
  63. {
  64. }
  65. bool FileSystem::SetCurrentDir(const String& pathName)
  66. {
  67. if (!CheckAccess(pathName))
  68. {
  69. LOGERROR("Access denied to " + pathName);
  70. return false;
  71. }
  72. #ifdef WIN32
  73. if (SetCurrentDirectoryW(GetWideNativePath(pathName).CString()) == FALSE)
  74. {
  75. LOGERROR("Failed to change directory to " + pathName);
  76. return false;
  77. }
  78. #else
  79. if (chdir(GetNativePath(pathName).CString()) != 0)
  80. {
  81. LOGERROR("Failed to change directory to " + pathName);
  82. return false;
  83. }
  84. #endif
  85. return true;
  86. }
  87. bool FileSystem::CreateDir(const String& pathName)
  88. {
  89. if (!CheckAccess(pathName))
  90. {
  91. LOGERROR("Access denied to " + pathName);
  92. return false;
  93. }
  94. #ifdef WIN32
  95. bool success = (CreateDirectoryW(GetWideNativePath(RemoveTrailingSlash(pathName)).CString(), 0) == TRUE) ||
  96. (GetLastError() == ERROR_ALREADY_EXISTS);
  97. #else
  98. bool success = mkdir(GetNativePath(RemoveTrailingSlash(pathName)).CString(), S_IRWXU) == 0 || errno == EEXIST;
  99. #endif
  100. if (success)
  101. LOGDEBUG("Created directory " + pathName);
  102. else
  103. LOGERROR("Failed to create directory " + pathName);
  104. return success;
  105. }
  106. int FileSystem::SystemCommand(const String& commandLine)
  107. {
  108. if (allowedPaths_.Empty())
  109. return system(commandLine.CString());
  110. else
  111. {
  112. LOGERROR("Executing an external command is not allowed");
  113. return -1;
  114. }
  115. }
  116. int FileSystem::SystemRun(const String& fileName, const Vector<String>& arguments)
  117. {
  118. if (allowedPaths_.Empty())
  119. {
  120. String fixedFileName = GetNativePath(fileName);
  121. #ifdef WIN32
  122. // Add .exe extension if no extension defined
  123. if (GetExtension(fixedFileName).Empty())
  124. fixedFileName += ".exe";
  125. String commandLine = "\"" + fixedFileName + "\"";
  126. for (unsigned i = 0; i < arguments.Size(); ++i)
  127. commandLine += " " + arguments[i];
  128. STARTUPINFOW startupInfo;
  129. PROCESS_INFORMATION processInfo;
  130. memset(&startupInfo, 0, sizeof startupInfo);
  131. memset(&processInfo, 0, sizeof processInfo);
  132. WString commandLineW(commandLine);
  133. if (!CreateProcessW(NULL, (wchar_t*)commandLineW.CString(), 0, 0, 0, CREATE_NO_WINDOW, 0, 0, &startupInfo, &processInfo))
  134. {
  135. LOGERROR("Failed to execute command " + commandLine);
  136. return -1;
  137. }
  138. WaitForSingleObject(processInfo.hProcess, INFINITE);
  139. DWORD exitCode;
  140. GetExitCodeProcess(processInfo.hProcess, &exitCode);
  141. CloseHandle(processInfo.hProcess);
  142. CloseHandle(processInfo.hThread);
  143. return exitCode;
  144. #else
  145. pid_t pid = fork();
  146. if (!pid)
  147. {
  148. PODVector<const char*> argPtrs;
  149. argPtrs.Push(fixedFileName.CString());
  150. for (unsigned i = 0; i < arguments.Size(); ++i)
  151. argPtrs.Push(arguments[i].CString());
  152. argPtrs.Push(0);
  153. execvp(argPtrs[0], (char**)&argPtrs[0]);
  154. return -1; // Return -1 if we could not spawn the process
  155. }
  156. else if (pid > 0)
  157. {
  158. int exitCode;
  159. wait(&exitCode);
  160. return exitCode;
  161. }
  162. else
  163. {
  164. LOGERROR("Failed to fork");
  165. return -1;
  166. }
  167. #endif
  168. }
  169. else
  170. {
  171. LOGERROR("Executing an external command is not allowed");
  172. return -1;
  173. }
  174. }
  175. bool FileSystem::SystemOpen(const String& fileName, const String& mode)
  176. {
  177. if (allowedPaths_.Empty())
  178. {
  179. if (!FileExists(fileName) && !DirExists(fileName))
  180. {
  181. LOGERROR("File or directory " + fileName + " not found");
  182. return false;
  183. }
  184. #ifdef WIN32
  185. bool success = (size_t)ShellExecuteW(0, !mode.Empty() ? WString(mode).CString() : 0,
  186. GetWideNativePath(fileName).CString(), 0, 0, SW_SHOW) > 32;
  187. #else
  188. Vector<String> arguments;
  189. arguments.Push(fileName);
  190. bool success = SystemRun(
  191. #if defined(__APPLE__)
  192. "/usr/bin/open",
  193. #else
  194. "/usr/bin/xdg-open",
  195. #endif
  196. arguments) == 0;
  197. #endif
  198. if (!success)
  199. LOGERROR("Failed to open " + fileName + " externally");
  200. return success;
  201. }
  202. else
  203. {
  204. LOGERROR("Opening a file externally is not allowed");
  205. return false;
  206. }
  207. }
  208. bool FileSystem::Copy(const String& srcFileName, const String& destFileName)
  209. {
  210. if (!CheckAccess(GetPath(srcFileName)))
  211. {
  212. LOGERROR("Access denied to " + srcFileName);
  213. return false;
  214. }
  215. if (!CheckAccess(GetPath(destFileName)))
  216. {
  217. LOGERROR("Access denied to " + destFileName);
  218. return false;
  219. }
  220. SharedPtr<File> srcFile(new File(context_, srcFileName, FILE_READ));
  221. if (!srcFile->IsOpen())
  222. return false;
  223. SharedPtr<File> destFile(new File(context_, destFileName, FILE_WRITE));
  224. if (!destFile->IsOpen())
  225. return false;
  226. unsigned fileSize = srcFile->GetSize();
  227. SharedArrayPtr<unsigned char> buffer(new unsigned char[fileSize]);
  228. unsigned bytesRead = srcFile->Read(buffer.Get(), fileSize);
  229. unsigned bytesWritten = destFile->Write(buffer.Get(), fileSize);
  230. return bytesRead == fileSize && bytesWritten == fileSize;
  231. }
  232. bool FileSystem::Rename(const String& srcFileName, const String& destFileName)
  233. {
  234. if (!CheckAccess(GetPath(srcFileName)))
  235. {
  236. LOGERROR("Access denied to " + srcFileName);
  237. return false;
  238. }
  239. if (!CheckAccess(GetPath(destFileName)))
  240. {
  241. LOGERROR("Access denied to " + destFileName);
  242. return false;
  243. }
  244. #ifdef WIN32
  245. return MoveFileW(GetWideNativePath(srcFileName).CString(), GetWideNativePath(destFileName).CString()) != 0;
  246. #else
  247. return rename(GetNativePath(srcFileName).CString(), GetNativePath(destFileName).CString()) == 0;
  248. #endif
  249. }
  250. bool FileSystem::Delete(const String& fileName)
  251. {
  252. if (!CheckAccess(GetPath(fileName)))
  253. {
  254. LOGERROR("Access denied to " + fileName);
  255. return false;
  256. }
  257. #ifdef WIN32
  258. return DeleteFileW(GetWideNativePath(fileName).CString()) != 0;
  259. #else
  260. return remove(GetNativePath(fileName).CString()) == 0;
  261. #endif
  262. }
  263. String FileSystem::GetCurrentDir() const
  264. {
  265. #ifdef WIN32
  266. wchar_t path[MAX_PATH];
  267. path[0] = 0;
  268. GetCurrentDirectoryW(MAX_PATH, path);
  269. return AddTrailingSlash(String(path));
  270. #else
  271. char path[MAX_PATH];
  272. path[0] = 0;
  273. getcwd(path, MAX_PATH);
  274. return AddTrailingSlash(String(path));
  275. #endif
  276. }
  277. bool FileSystem::CheckAccess(const String& pathName) const
  278. {
  279. String fixedPath = AddTrailingSlash(pathName);
  280. // If no allowed directories defined, succeed always
  281. if (allowedPaths_.Empty())
  282. return true;
  283. // If there is any attempt to go to a parent directory, disallow
  284. if (fixedPath.Contains(".."))
  285. return false;
  286. // Check if the path is a partial match of any of the allowed directories
  287. for (HashSet<String>::ConstIterator i = allowedPaths_.Begin(); i != allowedPaths_.End(); ++i)
  288. {
  289. if (fixedPath.Find(*i) == 0)
  290. return true;
  291. }
  292. // Not found, so disallow
  293. return false;
  294. }
  295. unsigned FileSystem::GetLastModifiedTime(const String& fileName) const
  296. {
  297. if (fileName.Empty() || !CheckAccess(fileName))
  298. return 0;
  299. #ifdef WIN32
  300. WIN32_FILE_ATTRIBUTE_DATA fileAttrData;
  301. memset(&fileAttrData, 0, sizeof fileAttrData);
  302. if (GetFileAttributesExW(WString(fileName).CString(), GetFileExInfoStandard, &fileAttrData))
  303. {
  304. ULARGE_INTEGER ull;
  305. ull.LowPart = fileAttrData.ftLastWriteTime.dwLowDateTime;
  306. ull.HighPart = fileAttrData.ftLastWriteTime.dwHighDateTime;
  307. return (unsigned)(ull.QuadPart / 10000000ULL - 11644473600ULL);
  308. }
  309. else
  310. return 0;
  311. #else
  312. struct stat st;
  313. if (!stat(fileName.CString(), &st))
  314. return (unsigned)st.st_mtime;
  315. else
  316. return 0;
  317. #endif
  318. }
  319. bool FileSystem::FileExists(const String& fileName) const
  320. {
  321. if (!CheckAccess(GetPath(fileName)))
  322. return false;
  323. String fixedName = GetNativePath(RemoveTrailingSlash(fileName));
  324. #ifdef ANDROID
  325. if (fixedName.StartsWith("/apk/"))
  326. {
  327. SDL_RWops* rwOps = SDL_RWFromFile(fileName.Substring(5).CString(), "rb");
  328. if (rwOps)
  329. {
  330. SDL_RWclose(rwOps);
  331. return true;
  332. }
  333. else
  334. return false;
  335. }
  336. #endif
  337. #ifdef WIN32
  338. DWORD attributes = GetFileAttributesW(WString(fixedName).CString());
  339. if (attributes == INVALID_FILE_ATTRIBUTES || attributes & FILE_ATTRIBUTE_DIRECTORY)
  340. return false;
  341. #else
  342. struct stat st;
  343. if (stat(fixedName.CString(), &st) || st.st_mode & S_IFDIR)
  344. return false;
  345. #endif
  346. return true;
  347. }
  348. bool FileSystem::DirExists(const String& pathName) const
  349. {
  350. if (!CheckAccess(pathName))
  351. return false;
  352. #ifndef WIN32
  353. // Always return true for the root directory
  354. if (pathName == "/")
  355. return true;
  356. #endif
  357. String fixedName = GetNativePath(RemoveTrailingSlash(pathName));
  358. #ifdef ANDROID
  359. /// \todo Actually check for existence, now true is always returned for directories within the APK
  360. if (fixedName.StartsWith("/apk/"))
  361. return true;
  362. #endif
  363. #ifdef WIN32
  364. DWORD attributes = GetFileAttributesW(WString(fixedName).CString());
  365. if (attributes == INVALID_FILE_ATTRIBUTES || !(attributes & FILE_ATTRIBUTE_DIRECTORY))
  366. return false;
  367. #else
  368. struct stat st;
  369. if (stat(fixedName.CString(), &st) || !(st.st_mode & S_IFDIR))
  370. return false;
  371. #endif
  372. return true;
  373. }
  374. void FileSystem::ScanDir(Vector<String>& result, const String& pathName, const String& filter, unsigned flags, bool recursive) const
  375. {
  376. result.Clear();
  377. if (CheckAccess(pathName))
  378. {
  379. String initialPath = AddTrailingSlash(pathName);
  380. ScanDirInternal(result, initialPath, initialPath, filter, flags, recursive);
  381. }
  382. }
  383. String FileSystem::GetProgramDir() const
  384. {
  385. // Return cached value if possible
  386. if (!programDir_.Empty())
  387. return programDir_;
  388. #if defined(ANDROID)
  389. // This is an internal directory specifier pointing to the assets in the .apk
  390. // Files from this directory will be opened using special handling
  391. programDir_ = "/apk/";
  392. return programDir_;
  393. #elif defined(IOS)
  394. programDir_ = AddTrailingSlash(SDL_IOS_GetResourceDir());
  395. return programDir_;
  396. #elif defined(WIN32)
  397. wchar_t exeName[MAX_PATH];
  398. exeName[0] = 0;
  399. GetModuleFileNameW(0, exeName, MAX_PATH);
  400. programDir_ = GetPath(String(exeName));
  401. #elif defined(__APPLE__)
  402. char exeName[MAX_PATH];
  403. memset(exeName, 0, MAX_PATH);
  404. unsigned size = MAX_PATH;
  405. _NSGetExecutablePath(exeName, &size);
  406. programDir_ = GetPath(String(exeName));
  407. #elif defined(__linux__)
  408. char exeName[MAX_PATH];
  409. memset(exeName, 0, MAX_PATH);
  410. pid_t pid = getpid();
  411. String link = "/proc/" + String(pid) + "/exe";
  412. readlink(link.CString(), exeName, MAX_PATH);
  413. programDir_ = GetPath(String(exeName));
  414. #endif
  415. // If the executable directory does not contain CoreData & Data directories, but the current working directory does, use the
  416. // current working directory instead
  417. /// \todo Should not rely on such fixed convention
  418. String currentDir = GetCurrentDir();
  419. if (!DirExists(programDir_ + "CoreData") && !DirExists(programDir_ + "Data") && (DirExists(currentDir + "CoreData") ||
  420. DirExists(currentDir + "Data")))
  421. programDir_ = currentDir;
  422. // Sanitate /./ construct away
  423. programDir_.Replace("/./", "/");
  424. return programDir_;
  425. }
  426. String FileSystem::GetUserDocumentsDir() const
  427. {
  428. #if defined(ANDROID)
  429. return AddTrailingSlash(SDL_Android_GetFilesDir());
  430. #elif defined(IOS)
  431. return AddTrailingSlash(SDL_IOS_GetResourceDir());
  432. #elif defined(WIN32)
  433. wchar_t pathName[MAX_PATH];
  434. pathName[0] = 0;
  435. SHGetSpecialFolderPathW(0, pathName, CSIDL_PERSONAL, 0);
  436. return AddTrailingSlash(String(pathName));
  437. #else
  438. char pathName[MAX_PATH];
  439. pathName[0] = 0;
  440. strcpy(pathName, getenv("HOME"));
  441. return AddTrailingSlash(String(pathName));
  442. #endif
  443. }
  444. void FileSystem::RegisterPath(const String& pathName)
  445. {
  446. if (pathName.Empty())
  447. return;
  448. allowedPaths_.Insert(AddTrailingSlash(pathName));
  449. }
  450. void FileSystem::ScanDirInternal(Vector<String>& result, String path, const String& startPath,
  451. const String& filter, unsigned flags, bool recursive) const
  452. {
  453. path = AddTrailingSlash(path);
  454. String deltaPath;
  455. if (path.Length() > startPath.Length())
  456. deltaPath = path.Substring(startPath.Length());
  457. String filterExtension = filter.Substring(filter.Find('.'));
  458. if (filterExtension.Contains('*'))
  459. filterExtension.Clear();
  460. #ifdef WIN32
  461. WIN32_FIND_DATAW info;
  462. HANDLE handle = FindFirstFileW(WString(path + "*").CString(), &info);
  463. if (handle != INVALID_HANDLE_VALUE)
  464. {
  465. do
  466. {
  467. String fileName(info.cFileName);
  468. if (!fileName.Empty())
  469. {
  470. if (info.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN && !(flags & SCAN_HIDDEN))
  471. continue;
  472. if (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  473. {
  474. if (flags & SCAN_DIRS)
  475. result.Push(deltaPath + fileName);
  476. if (recursive && fileName != "." && fileName != "..")
  477. ScanDirInternal(result, path + fileName, startPath, filter, flags, recursive);
  478. }
  479. else if (flags & SCAN_FILES)
  480. {
  481. if (filterExtension.Empty() || fileName.EndsWith(filterExtension))
  482. result.Push(deltaPath + fileName);
  483. }
  484. }
  485. }
  486. while (FindNextFileW(handle, &info));
  487. FindClose(handle);
  488. }
  489. #else
  490. DIR *dir;
  491. struct dirent *de;
  492. struct stat st;
  493. dir = opendir(GetNativePath(path).CString());
  494. if (dir)
  495. {
  496. while ((de = readdir(dir)))
  497. {
  498. /// \todo Filename may be unnormalized Unicode on Mac OS X. Re-normalize as necessary
  499. String fileName(de->d_name);
  500. bool normalEntry = fileName != "." && fileName != "..";
  501. if (normalEntry && !(flags & SCAN_HIDDEN) && fileName.StartsWith("."))
  502. continue;
  503. String pathAndName = path + fileName;
  504. if (!stat(pathAndName.CString(), &st))
  505. {
  506. if (st.st_mode & S_IFDIR)
  507. {
  508. if (flags & SCAN_DIRS)
  509. result.Push(deltaPath + fileName);
  510. if (recursive && normalEntry)
  511. ScanDirInternal(result, path + fileName, startPath, filter, flags, recursive);
  512. }
  513. else if (flags & SCAN_FILES)
  514. {
  515. if (filterExtension.Empty() || fileName.EndsWith(filterExtension))
  516. result.Push(deltaPath + fileName);
  517. }
  518. }
  519. }
  520. closedir(dir);
  521. }
  522. #endif
  523. }
  524. void SplitPath(const String& fullPath, String& pathName, String& fileName, String& extension, bool lowercaseExtension)
  525. {
  526. String fullPathCopy = GetInternalPath(fullPath);
  527. unsigned extPos = fullPathCopy.FindLast('.');
  528. unsigned pathPos = fullPathCopy.FindLast('/');
  529. if (extPos != String::NPOS && (pathPos == String::NPOS || extPos > pathPos))
  530. {
  531. extension = fullPathCopy.Substring(extPos);
  532. if (lowercaseExtension)
  533. extension = extension.ToLower();
  534. fullPathCopy = fullPathCopy.Substring(0, extPos);
  535. }
  536. else
  537. extension.Clear();
  538. pathPos = fullPathCopy.FindLast('/');
  539. if (pathPos != String::NPOS)
  540. {
  541. fileName = fullPathCopy.Substring(pathPos + 1);
  542. pathName = fullPathCopy.Substring(0, pathPos + 1);
  543. }
  544. else
  545. {
  546. fileName = fullPathCopy;
  547. pathName.Clear();
  548. }
  549. }
  550. String GetPath(const String& fullPath)
  551. {
  552. String path, file, extension;
  553. SplitPath(fullPath, path, file, extension);
  554. return path;
  555. }
  556. String GetFileName(const String& fullPath)
  557. {
  558. String path, file, extension;
  559. SplitPath(fullPath, path, file, extension);
  560. return file;
  561. }
  562. String GetExtension(const String& fullPath, bool lowercaseExtension)
  563. {
  564. String path, file, extension;
  565. SplitPath(fullPath, path, file, extension, lowercaseExtension);
  566. return extension;
  567. }
  568. String GetFileNameAndExtension(const String& fileName, bool lowercaseExtension)
  569. {
  570. String path, file, extension;
  571. SplitPath(fileName, path, file, extension, lowercaseExtension);
  572. return file + extension;
  573. }
  574. String ReplaceExtension(const String& fullPath, const String& newExtension)
  575. {
  576. String path, file, extension;
  577. SplitPath(fullPath, path, file, extension);
  578. return path + file + newExtension;
  579. }
  580. String AddTrailingSlash(const String& pathName)
  581. {
  582. String ret = pathName.Trimmed();
  583. ret.Replace('\\', '/');
  584. if (!ret.Empty() && ret.Back() != '/')
  585. ret += '/';
  586. return ret;
  587. }
  588. String RemoveTrailingSlash(const String& pathName)
  589. {
  590. String ret = pathName.Trimmed();
  591. ret.Replace('\\', '/');
  592. if (!ret.Empty() && ret.Back() == '/')
  593. ret.Resize(ret.Length() - 1);
  594. return ret;
  595. }
  596. String GetParentPath(const String& path)
  597. {
  598. unsigned pos = RemoveTrailingSlash(path).FindLast('/');
  599. if (pos != String::NPOS)
  600. return path.Substring(0, pos + 1);
  601. else
  602. return String();
  603. }
  604. String GetInternalPath(const String& pathName)
  605. {
  606. return pathName.Replaced('\\', '/');
  607. }
  608. String GetNativePath(const String& pathName)
  609. {
  610. #ifdef WIN32
  611. return pathName.Replaced('/', '\\');
  612. #else
  613. return pathName;
  614. #endif
  615. }
  616. WString GetWideNativePath(const String& pathName)
  617. {
  618. #ifdef WIN32
  619. return WString(pathName.Replaced('/', '\\'));
  620. #else
  621. return WString(pathName);
  622. #endif
  623. }
  624. bool IsAbsolutePath(const String& pathName)
  625. {
  626. if (pathName.Empty())
  627. return false;
  628. String path = GetInternalPath(pathName);
  629. if (path[0] == '/')
  630. return true;
  631. #ifdef WIN32
  632. if (path.Length() > 1 && IsAlpha(path[0]) && path[1] == ':')
  633. return true;
  634. #endif
  635. return false;
  636. }
  637. }