Filesystem.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * Copyright (c) 2006-2015 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. // LOVE
  21. #include "Filesystem.h"
  22. #include "common/utf8.h"
  23. // Assume POSIX or Visual Studio.
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #if defined(LOVE_MACOSX)
  27. #include "common/OSX.h"
  28. #elif defined(LOVE_IOS)
  29. #include "common/iOS.h"
  30. #elif defined(LOVE_WINDOWS)
  31. #include <windows.h>
  32. #include "common/utf8.h"
  33. #elif defined(LOVE_LINUX)
  34. #include <unistd.h>
  35. #endif
  36. namespace love
  37. {
  38. namespace filesystem
  39. {
  40. Filesystem::Filesystem()
  41. {
  42. }
  43. Filesystem::~Filesystem()
  44. {
  45. }
  46. bool Filesystem::isRealDirectory(const std::string &path) const
  47. {
  48. #ifdef LOVE_WINDOWS
  49. // make sure non-ASCII paths work.
  50. std::wstring wpath = to_widestr(path);
  51. struct _stat buf;
  52. if (_wstat(wpath.c_str(), &buf) != 0)
  53. return false;
  54. return (buf.st_mode & _S_IFDIR) == _S_IFDIR;
  55. #else
  56. // Assume POSIX support...
  57. struct stat buf;
  58. if (stat(path.c_str(), &buf) != 0)
  59. return false;
  60. return S_ISDIR(buf.st_mode) != 0;
  61. #endif
  62. }
  63. std::string Filesystem::getExecutablePath() const
  64. {
  65. #if defined(LOVE_MACOSX)
  66. return love::osx::getExecutablePath();
  67. #elif defined(LOVE_IOS)
  68. return love::ios::getExecutablePath();
  69. #elif defined(LOVE_WINDOWS)
  70. wchar_t buffer[MAX_PATH + 1] = {0};
  71. if (GetModuleFileNameW(nullptr, buffer, MAX_PATH) == 0)
  72. return "";
  73. return to_utf8(buffer);
  74. #elif defined(LOVE_LINUX)
  75. char buffer[2048] = {0};
  76. ssize_t len = readlink("/proc/self/exe", buffer, 2048);
  77. if (len <= 0)
  78. return "";
  79. return std::string(buffer, len);
  80. #else
  81. #error Missing implementation for Filesystem::getExecutablePath!
  82. #endif
  83. }
  84. } // filesystem
  85. } // love