Filesystem.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * Copyright (c) 2006-2016 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/macosx.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. love::Type Filesystem::type("filesystem", &Module::type);
  41. Filesystem::Filesystem()
  42. {
  43. }
  44. Filesystem::~Filesystem()
  45. {
  46. }
  47. void Filesystem::setAndroidSaveExternal(bool useExternal)
  48. {
  49. this->useExternal = useExternal;
  50. }
  51. bool Filesystem::isAndroidSaveExternal() const
  52. {
  53. return useExternal;
  54. }
  55. FileData *Filesystem::newFileData(const void *data, size_t size, const char *filename) const
  56. {
  57. FileData *fd = new FileData(size, std::string(filename));
  58. memcpy(fd->getData(), data, size);
  59. return fd;
  60. }
  61. bool Filesystem::isRealDirectory(const std::string &path) const
  62. {
  63. #ifdef LOVE_WINDOWS
  64. // make sure non-ASCII paths work.
  65. std::wstring wpath = to_widestr(path);
  66. struct _stat buf;
  67. if (_wstat(wpath.c_str(), &buf) != 0)
  68. return false;
  69. return (buf.st_mode & _S_IFDIR) == _S_IFDIR;
  70. #else
  71. // Assume POSIX support...
  72. struct stat buf;
  73. if (stat(path.c_str(), &buf) != 0)
  74. return false;
  75. return S_ISDIR(buf.st_mode) != 0;
  76. #endif
  77. }
  78. std::string Filesystem::getExecutablePath() const
  79. {
  80. #if defined(LOVE_MACOSX)
  81. return love::macosx::getExecutablePath();
  82. #elif defined(LOVE_IOS)
  83. return love::ios::getExecutablePath();
  84. #elif defined(LOVE_WINDOWS)
  85. wchar_t buffer[MAX_PATH + 1] = {0};
  86. if (GetModuleFileNameW(nullptr, buffer, MAX_PATH) == 0)
  87. return "";
  88. return to_utf8(buffer);
  89. #elif defined(LOVE_LINUX)
  90. char buffer[2048] = {0};
  91. ssize_t len = readlink("/proc/self/exe", buffer, 2048);
  92. if (len <= 0)
  93. return "";
  94. return std::string(buffer, len);
  95. #else
  96. #error Missing implementation for Filesystem::getExecutablePath!
  97. #endif
  98. }
  99. } // filesystem
  100. } // love