1
0

svc_filereadI.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #ifndef __WASABI_SVC_FILEREADI_H
  2. #define __WASABI_SVC_FILEREADI_H
  3. #include <api/service/svcs/svc_fileread.h>
  4. #include <api/filereader/api_readercallback.h>
  5. // derive from this one
  6. class NOVTABLE svc_fileReaderI : public svc_fileReader
  7. {
  8. public:
  9. virtual int isMine(const wchar_t *filename, int mode = SvcFileReader::READ) { return -1; }
  10. virtual int open(const wchar_t *filename, int mode = SvcFileReader::READ) = 0; // return 1 on success
  11. virtual size_t read(int8_t *buffer, size_t length) = 0; // return number of bytes read (if < length then eof)
  12. virtual size_t write(const int8_t *buffer, size_t length) = 0; // return number of bytes written
  13. virtual void close() = 0; //must be safe to call even when not open
  14. virtual int canSetEOF() { return 0; }
  15. virtual int setEOF(uint64_t newlen) { return -1; }
  16. virtual void abort() { } // tells the reader to abort its current prebuffering/reader
  17. virtual int getLength() { return -1; } // return -1 on unknown/infinite
  18. virtual int getPos() = 0;
  19. virtual int canSeek() { return 0; }
  20. virtual int seek(uint64_t position) { return 0; }
  21. virtual uint64_t bytesAvailable(uint64_t requested) { return requested; }
  22. virtual int hasHeaders() { return 0; }
  23. virtual const char *getHeader(const char *header) { return (const char *)NULL; }
  24. virtual int exists(const wchar_t *filename) { return 0; } // return 1 if true, 0 if not, -1 if unknown
  25. virtual int remove(const wchar_t *filename) { return 0; } // return 1 on success, 0 on error
  26. virtual int removeUndoable(const wchar_t *filename) { return -1; }
  27. virtual int move(const wchar_t *filename, const wchar_t *destfilename) { return 0; } // return 1 on success, 0 on error
  28. virtual void setMetaDataCallback(api_readercallback *cb) { }
  29. virtual int canPrefetch() { return 1; } // return 1 if your reader should prefetch infos about the file in pledit
  30. // (HTTP reader will return 0 here for instance)
  31. protected:
  32. RECVS_DISPATCH;
  33. };
  34. // derive from this one
  35. class NOVTABLE MetaDataReaderCallbackI : public api_readercallback {
  36. public:
  37. virtual void metaDataReader_onData(const char *data, int size)=0;
  38. protected:
  39. #undef CBCLASS
  40. #define CBCLASS MetaDataReaderCallbackI
  41. START_DISPATCH_INLINE;
  42. VCB(METADATAREADERONDATA, metaDataReader_onData);
  43. END_DISPATCH;
  44. #undef CBCLASS
  45. };
  46. #include <api/service/svcs/svc_redir.h>
  47. #include <bfc/std_file.h> // for WA_MAX_PATH but this needs to be in a better place
  48. #define MAX_FILEREADER_REDIRECT 256
  49. // note: this class handles both redirection and calling open()
  50. class FileReaderEnum : public SvcEnumT<svc_fileReader> {
  51. public:
  52. FileReaderEnum(const wchar_t *filename, int mode=SvcFileReader::READ, int allow_redirect=FALSE) :
  53. fn(filename), m(mode)
  54. {
  55. if (allow_redirect) {
  56. for (int done = 0, c = 0; !done && c < MAX_FILEREADER_REDIRECT; done = 1, c++) {
  57. RedirectEnum re(fn);
  58. svc_redirect *svc;
  59. while ((svc = re.getNext(FALSE)) != NULL) {
  60. wchar_t buf[WA_MAX_PATH]=L"";
  61. if (svc->redirect(fn, L"Filename", buf, WA_MAX_PATH)) {
  62. fn = buf;
  63. done = 0;
  64. }
  65. re.getLastFactory()->releaseInterface(svc);
  66. }
  67. }
  68. }
  69. }
  70. virtual int testService(svc_fileReader *svc) {
  71. if (!svc->isMine(fn)) return 0;
  72. return !!svc->open(fn, m);
  73. }
  74. private:
  75. const wchar_t *fn;
  76. int m;
  77. };
  78. #endif