FileSystem.cpp 21 KB

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