fileread.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include <precomp.h>
  2. #include "fileread.h"
  3. #include <api/service/svc_enum.h>
  4. void *FileReaders::open(const wchar_t *filename, const wchar_t *mode) {
  5. int m=0;
  6. const wchar_t *p=mode;
  7. wchar_t c;
  8. while(c=*(p++))
  9. {
  10. switch(c)
  11. {
  12. case 'r': m=SvcFileReader::READ; break;
  13. case 'w': m=SvcFileReader::WRITE; break;
  14. case 'a': m=SvcFileReader::APPEND; break;
  15. case '+': m|=SvcFileReader::PLUS; break;
  16. case 'b': m|=SvcFileReader::BINARY; break;
  17. case 't': m|=SvcFileReader::TEXT; break;
  18. }
  19. }
  20. return FileReaderEnum(filename, m).getFirst();
  21. }
  22. void FileReaders::close(void *handle)
  23. {
  24. svc_fileReader *fr=(svc_fileReader *)handle;
  25. fr->close();
  26. SvcEnum::release(fr);
  27. }
  28. size_t FileReaders::read(void *buffer, size_t size, void *handle)
  29. {
  30. svc_fileReader *fr=(svc_fileReader *)handle;
  31. return fr->read((int8_t *)buffer,size);
  32. }
  33. size_t FileReaders::write(const void *buffer, size_t size, void *handle)
  34. {
  35. svc_fileReader *fr=(svc_fileReader *)handle;
  36. return fr->write((int8_t *)buffer,size);
  37. }
  38. int FileReaders::seek(int64_t offset, int origin, void *handle)
  39. {
  40. svc_fileReader *fr=(svc_fileReader *)handle;
  41. if(!fr->canSeek()) return -1;
  42. if(origin==SEEK_SET) return fr->seek(offset);
  43. if(origin==SEEK_CUR) return fr->seek(fr->getPos()+offset);
  44. return fr->seek(fr->getLength()-offset);
  45. }
  46. uint64_t FileReaders::tell(void *handle)
  47. {
  48. svc_fileReader *fr=(svc_fileReader *)handle;
  49. return fr->getPos();
  50. }
  51. uint64_t FileReaders::getFileSize(void *handle)
  52. {
  53. svc_fileReader *fr=(svc_fileReader *)handle;
  54. return fr->getLength();
  55. }
  56. int FileReaders::exists(const wchar_t *filename) {
  57. // Note that we do not do a system lock on the service since we can just
  58. // release it directly via the factory
  59. waService *s;
  60. for(int i=0;s=WASABI_API_SVC->service_enumService(WaSvc::FILEREADER,i);i++) {
  61. svc_fileReader *fr=(svc_fileReader *)s->getInterface(FALSE);
  62. if(fr->isMine(filename) == 1 || fr->open(filename)) {
  63. int ret=fr->exists(filename);
  64. fr->close();
  65. s->releaseInterface(fr); fr = NULL;
  66. return ret;
  67. }
  68. s->releaseInterface(fr); fr = NULL;
  69. }
  70. return 0;
  71. }
  72. int FileReaders::remove(const wchar_t *filename) {
  73. // Note that we do not do a system lock on the service since we can just
  74. // release it directly via the factory
  75. waService *s;
  76. for(int i=0;s=WASABI_API_SVC->service_enumService(WaSvc::FILEREADER,i);i++) {
  77. svc_fileReader *fr=(svc_fileReader *)s->getInterface(FALSE);
  78. if (fr->isMine(filename) == 1 || fr->open(filename)) {
  79. fr->close();
  80. int ret = fr->remove(filename);
  81. s->releaseInterface(fr); fr = NULL;
  82. return ret;
  83. }
  84. s->releaseInterface(fr); fr = NULL;
  85. }
  86. return 0;
  87. }
  88. int FileReaders::removeUndoable(const wchar_t *filename) {
  89. // Note that we do not do a system lock on the service since we can just
  90. // release it directly via the factory
  91. waService *s;
  92. for(int i=0;s=WASABI_API_SVC->service_enumService(WaSvc::FILEREADER,i);i++) {
  93. svc_fileReader *fr=(svc_fileReader *)s->getInterface(FALSE);
  94. if(fr->isMine(filename) == 1 || fr->open(filename)) {
  95. fr->close();
  96. int ret = fr->removeUndoable(filename);
  97. s->releaseInterface(fr); fr = NULL;
  98. return ret;
  99. }
  100. s->releaseInterface(fr); fr = NULL;
  101. }
  102. return 0;
  103. }
  104. int FileReaders::move(const wchar_t *filename, const wchar_t *destfilename) {
  105. // Note that we do not do a system lock on the service since we can just
  106. // release it directly via the factory
  107. waService *s;
  108. for(int i=0;s=WASABI_API_SVC->service_enumService(WaSvc::FILEREADER,i);i++) {
  109. svc_fileReader *fr=(svc_fileReader *)s->getInterface(FALSE);
  110. if(fr->isMine(filename) == 1 || fr->open(filename)) {
  111. fr->close();
  112. int ret = fr->move(filename,destfilename);
  113. s->releaseInterface(fr); fr = NULL;
  114. return ret;
  115. }
  116. s->releaseInterface(fr); fr = NULL;
  117. }
  118. return 0;
  119. }