1 // Copyright 2014 PDFium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 6 7 #ifndef CORE_FXCRT_FX_STREAM_H_ 8 #define CORE_FXCRT_FX_STREAM_H_ 9 10 #include "core/fxcrt/fx_string.h" 11 #include "core/fxcrt/fx_system.h" 12 #include "core/fxcrt/retain_ptr.h" 13 14 #if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ 15 #include <direct.h> 16 17 class CFindFileDataA; 18 typedef CFindFileDataA FX_FileHandle; 19 20 #else // _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ 21 22 #include <dirent.h> 23 #include <fcntl.h> 24 #include <sys/stat.h> 25 #include <sys/types.h> 26 #include <unistd.h> 27 28 typedef DIR FX_FileHandle; 29 #endif // _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ 30 31 FX_FileHandle* FX_OpenFolder(const char* path); 32 bool FX_GetNextFile(FX_FileHandle* handle, ByteString* filename, bool* bFolder); 33 void FX_CloseFolder(FX_FileHandle* handle); 34 35 #define FX_FILEMODE_ReadOnly 1 36 #define FX_FILEMODE_Truncate 2 37 38 class IFX_WriteStream : virtual public Retainable { 39 public: 40 virtual bool WriteBlock(const void* pData, size_t size) = 0; 41 virtual bool WriteString(const ByteStringView& str) = 0; 42 }; 43 44 class IFX_ArchiveStream : public IFX_WriteStream { 45 public: 46 virtual bool WriteByte(uint8_t byte) = 0; 47 virtual bool WriteDWord(uint32_t i) = 0; 48 virtual FX_FILESIZE CurrentOffset() const = 0; 49 }; 50 51 class IFX_ReadStream : virtual public Retainable { 52 public: 53 virtual bool IsEOF() = 0; 54 virtual FX_FILESIZE GetPosition() = 0; 55 virtual size_t ReadBlock(void* buffer, size_t size) = 0; 56 }; 57 58 class IFX_SeekableWriteStream : public IFX_WriteStream { 59 public: 60 // IFX_WriteStream: 61 bool WriteBlock(const void* pData, size_t size) override; 62 63 virtual FX_FILESIZE GetSize() = 0; 64 virtual bool Flush() = 0; 65 virtual bool WriteBlock(const void* pData, 66 FX_FILESIZE offset, 67 size_t size) = 0; 68 }; 69 70 class IFX_SeekableReadStream : public IFX_ReadStream { 71 public: 72 static RetainPtr<IFX_SeekableReadStream> CreateFromFilename( 73 const char* filename); 74 75 // IFX_ReadStream: 76 bool IsEOF() override; 77 FX_FILESIZE GetPosition() override; 78 size_t ReadBlock(void* buffer, size_t size) override; 79 80 virtual bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) = 0; 81 virtual FX_FILESIZE GetSize() = 0; 82 }; 83 84 class IFX_SeekableStream : public IFX_SeekableReadStream, 85 public IFX_SeekableWriteStream { 86 public: 87 static RetainPtr<IFX_SeekableStream> CreateFromFilename(const char* filename, 88 uint32_t dwModes); 89 90 static RetainPtr<IFX_SeekableStream> CreateFromFilename( 91 const wchar_t* filename, 92 uint32_t dwModes); 93 94 // IFX_SeekableReadStream: 95 bool IsEOF() override = 0; 96 FX_FILESIZE GetPosition() override = 0; 97 size_t ReadBlock(void* buffer, size_t size) override = 0; 98 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override = 0; 99 FX_FILESIZE GetSize() override = 0; 100 101 // IFX_SeekableWriteStream: 102 bool WriteBlock(const void* buffer, 103 FX_FILESIZE offset, 104 size_t size) override = 0; 105 bool WriteBlock(const void* buffer, size_t size) override; 106 bool WriteString(const ByteStringView& str) override; 107 108 bool Flush() override = 0; 109 }; 110 111 #if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ 112 class CFindFileData { 113 public: ~CFindFileData()114 virtual ~CFindFileData() {} 115 HANDLE m_Handle; 116 bool m_bEnd; 117 }; 118 119 class CFindFileDataA : public CFindFileData { 120 public: ~CFindFileDataA()121 ~CFindFileDataA() override {} 122 WIN32_FIND_DATAA m_FindData; 123 }; 124 #endif 125 126 #endif // CORE_FXCRT_FX_STREAM_H_ 127