FileSystem.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // This code is in the public domain -- [email protected]
  2. #include "FileSystem.h"
  3. #if NV_OS_WIN32
  4. #define _CRT_NONSTDC_NO_WARNINGS // _chdir is defined deprecated, but that's a bug, chdir is deprecated, _chdir is *not*.
  5. //#include <shlwapi.h> // PathFileExists
  6. #include <windows.h> // GetFileAttributes
  7. #include <direct.h> // _mkdir
  8. #elif NV_OS_XBOX
  9. #include <Xtl.h>
  10. #else
  11. #include <sys/stat.h>
  12. #include <sys/types.h>
  13. #include <unistd.h>
  14. #endif
  15. #include <stdio.h> // remove, unlink
  16. using namespace nv;
  17. bool FileSystem::exists(const char * path)
  18. {
  19. #if NV_OS_UNIX
  20. return access(path, F_OK|R_OK) == 0;
  21. //struct stat buf;
  22. //return stat(path, &buf) == 0;
  23. #elif NV_OS_WIN32 || NV_OS_XBOX
  24. // PathFileExists requires linking to shlwapi.lib
  25. //return PathFileExists(path) != 0;
  26. return GetFileAttributesA(path) != INVALID_FILE_ATTRIBUTES;
  27. #elif NV_OS_ORBIS
  28. // SBtodoORBIS use Fios2??
  29. const int BUFFER_SIZE = 2048;
  30. char file_fullpath[BUFFER_SIZE];
  31. snprintf(file_fullpath, BUFFER_SIZE, "/app0/%s", path);
  32. if (FILE * fp = fopen(file_fullpath, "r"))
  33. {
  34. fclose(fp);
  35. return true;
  36. }
  37. return false;
  38. #else
  39. if (FILE * fp = fopen(path, "r"))
  40. {
  41. fclose(fp);
  42. return true;
  43. }
  44. return false;
  45. #endif
  46. }
  47. bool FileSystem::createDirectory(const char * path)
  48. {
  49. #if NV_OS_WIN32 || NV_OS_XBOX
  50. return CreateDirectoryA(path, NULL) != 0;
  51. #else
  52. return mkdir(path, 0777) != -1;
  53. #endif
  54. }
  55. bool FileSystem::changeDirectory(const char * path)
  56. {
  57. #if NV_OS_WIN32
  58. return _chdir(path) != -1;
  59. #elif NV_OS_XBOX
  60. // Xbox doesn't support Current Working Directory!
  61. return false;
  62. #elif NV_OS_ORBIS
  63. // Orbis doesn't support Current Working Directory!
  64. return false;
  65. #else
  66. return chdir(path) != -1;
  67. #endif
  68. }
  69. bool FileSystem::removeFile(const char * path)
  70. {
  71. // @@ Use unlink or remove?
  72. return remove(path) == 0;
  73. }