PolyPhysFSFileProvider.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include "polycode/core/PolyPhysFSFileProvider.h"
  2. using namespace Polycode;
  3. PhysFSFileProvider::PhysFSFileProvider() {
  4. canListFiles = true;
  5. }
  6. bool PhysFSFileProvider::parseFolder(const Polycode::String& pathString, bool showHidden, std::vector<OSFileEntry> &targetVector) {
  7. if(pathString != "/") {
  8. if(pathString.size() < 128) {
  9. if(PHYSFS_exists(pathString.c_str())) {
  10. if(PHYSFS_isDirectory(pathString.c_str())) {
  11. char **rc = PHYSFS_enumerateFiles(pathString.c_str());
  12. char **i;
  13. String fullPath;
  14. String fname;
  15. for (i = rc; *i != NULL; i++) {
  16. fname = std::string(*i);
  17. fullPath = pathString + "/" + fname;
  18. if((fname.c_str()[0] != '.' || (fname.c_str()[0] == '.' && showHidden)) && fname != "..") {
  19. if(PHYSFS_isDirectory(fullPath.c_str())) {
  20. targetVector.push_back(OSFileEntry(pathString, fname, OSFileEntry::TYPE_FOLDER));
  21. } else {
  22. targetVector.push_back(OSFileEntry(pathString, fname, OSFileEntry::TYPE_FILE));
  23. }
  24. }
  25. }
  26. PHYSFS_freeList(rc);
  27. return true;;
  28. }
  29. }
  30. }
  31. }
  32. return false;
  33. }
  34. Polycode::CoreFile *PhysFSFileProvider::openFile(const String &fileName, const String &opts) {
  35. PhysFSFile *retFile = NULL;
  36. if(PHYSFS_exists(fileName.c_str())) {
  37. if(!PHYSFS_isDirectory(fileName.c_str())) {
  38. retFile = new PhysFSFile();
  39. if(opts.find("a") != -1) {
  40. retFile->physFSFile = PHYSFS_openAppend(fileName.c_str());
  41. } else if(opts.find("w") != -1) {
  42. retFile->physFSFile = PHYSFS_openWrite(fileName.c_str());
  43. } else {
  44. retFile->physFSFile = PHYSFS_openRead(fileName.c_str());
  45. }
  46. return retFile;
  47. }
  48. }
  49. return NULL;
  50. }
  51. void PhysFSFileProvider::closeFile(Polycode::CoreFile *file) {
  52. PhysFSFile *physFSFile = (PhysFSFile*) file;
  53. PHYSFS_close(physFSFile->physFSFile);
  54. delete physFSFile;
  55. }
  56. long PhysFSFile::read( void * ptr, size_t size, size_t count) {
  57. return PHYSFS_read(physFSFile, ptr, size, count);
  58. }
  59. long PhysFSFile::write( const void * ptr, size_t size, size_t count) {
  60. return PHYSFS_write(physFSFile, ptr, size, count);
  61. }
  62. int PhysFSFile::seek(long int offset, int origin) {
  63. switch(origin) {
  64. case SEEK_SET:
  65. return PHYSFS_seek(physFSFile, offset);
  66. break;
  67. case SEEK_CUR: {
  68. PHYSFS_sint64 curoffset = PHYSFS_tell(physFSFile);
  69. return PHYSFS_seek(physFSFile, curoffset+offset);
  70. }
  71. break;
  72. case SEEK_END: {
  73. PHYSFS_sint64 fileLength = PHYSFS_fileLength(physFSFile);
  74. return PHYSFS_seek(physFSFile, fileLength-offset);
  75. }
  76. break;
  77. }
  78. return 0;
  79. }
  80. long PhysFSFile::tell() {
  81. return PHYSFS_tell(physFSFile);
  82. }