Kill FX_HFILE
[pdfium.git] / core / include / fxcrt / fx_stream.h
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_INCLUDE_FXCRT_FX_STREAM_H_
8 #define CORE_INCLUDE_FXCRT_FX_STREAM_H_
9
10 #include "fx_string.h"
11
12 void* FX_OpenFolder(const FX_CHAR* path);
13 void* FX_OpenFolder(const FX_WCHAR* path);
14 FX_BOOL FX_GetNextFile(void* handle, CFX_ByteString& filename, FX_BOOL& bFolder);
15 FX_BOOL FX_GetNextFile(void* handle, CFX_WideString& filename, FX_BOOL& bFolder);
16 void FX_CloseFolder(void* handle);
17 FX_WCHAR FX_GetFolderSeparator();
18 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
19 #define FX_FILESIZE                     int32_t
20 #else
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <sys/stat.h>
24 #ifndef O_BINARY
25 #define O_BINARY                0
26 #endif
27 #ifndef O_LARGEFILE
28 #define O_LARGEFILE             0
29 #endif
30 #define FX_FILESIZE                     off_t
31 #endif
32 #define FX_GETBYTEOFFSET32(a)   0
33 #define FX_GETBYTEOFFSET40(a)   0
34 #define FX_GETBYTEOFFSET48(a)   0
35 #define FX_GETBYTEOFFSET56(a)   0
36 #define FX_GETBYTEOFFSET24(a)  ((uint8_t)(a>>24))
37 #define FX_GETBYTEOFFSET16(a)  ((uint8_t)(a>>16))
38 #define FX_GETBYTEOFFSET8(a)   ((uint8_t)(a>>8))
39 #define FX_GETBYTEOFFSET0(a)   ((uint8_t)(a))
40 #define FX_FILEMODE_Write               0
41 #define FX_FILEMODE_ReadOnly    1
42 #define FX_FILEMODE_Truncate    2
43 FX_BOOL         FX_File_Exist(const CFX_ByteStringC& fileName);
44 FX_BOOL         FX_File_Exist(const CFX_WideStringC& fileName);
45 FX_BOOL         FX_File_Delete(const CFX_ByteStringC& fileName);
46 FX_BOOL         FX_File_Delete(const CFX_WideStringC& fileName);
47 FX_BOOL         FX_File_Copy(const CFX_ByteStringC& fileNameSrc, const CFX_ByteStringC& fileNameDst);
48 FX_BOOL         FX_File_Copy(const CFX_WideStringC& fileNameSrc, const CFX_WideStringC& fileNameDst);
49 FX_BOOL         FX_File_Move(const CFX_ByteStringC& fileNameSrc, const CFX_ByteStringC& fileNameDst);
50 FX_BOOL         FX_File_Move(const CFX_WideStringC& fileNameSrc, const CFX_WideStringC& fileNameDst);
51 class IFX_StreamWrite
52 {
53 public:
54     virtual ~IFX_StreamWrite() { }
55     virtual void                Release() = 0;
56
57     virtual     FX_BOOL         WriteBlock(const void* pData, size_t size) = 0;
58 };
59 class IFX_FileWrite : public IFX_StreamWrite
60 {
61 public:
62
63     virtual void                        Release() = 0;
64
65     virtual FX_FILESIZE         GetSize() = 0;
66
67     virtual FX_BOOL                     Flush() = 0;
68
69     virtual     FX_BOOL                 WriteBlock(const void* pData, FX_FILESIZE offset, size_t size) = 0;
70     virtual     FX_BOOL                 WriteBlock(const void* pData, size_t size)
71     {
72         return WriteBlock(pData, GetSize(), size);
73     }
74 };
75 IFX_FileWrite* FX_CreateFileWrite(const FX_CHAR* filename);
76 IFX_FileWrite* FX_CreateFileWrite(const FX_WCHAR* filename);
77 class IFX_StreamRead
78 {
79 public:
80     virtual ~IFX_StreamRead() { }
81
82     virtual void                        Release() = 0;
83
84     virtual FX_BOOL                     IsEOF() = 0;
85
86     virtual FX_FILESIZE         GetPosition() = 0;
87
88     virtual size_t                      ReadBlock(void* buffer, size_t size) = 0;
89 };
90 class IFX_FileRead : IFX_StreamRead
91 {
92 public:
93     virtual void                        Release() = 0;
94
95     virtual FX_FILESIZE         GetSize() = 0;
96
97     virtual FX_BOOL                     IsEOF()
98     {
99         return FALSE;
100     }
101
102     virtual FX_FILESIZE         GetPosition()
103     {
104         return 0;
105     }
106
107     virtual FX_BOOL                     SetRange(FX_FILESIZE offset, FX_FILESIZE size)
108     {
109         return FALSE;
110     }
111
112     virtual void                        ClearRange() {}
113
114     virtual FX_BOOL                     ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) = 0;
115
116     virtual size_t                      ReadBlock(void* buffer, size_t size)
117     {
118         return 0;
119     }
120 };
121 IFX_FileRead* FX_CreateFileRead(const FX_CHAR* filename);
122 IFX_FileRead* FX_CreateFileRead(const FX_WCHAR* filename);
123 class IFX_FileStream : public IFX_FileRead, public IFX_FileWrite
124 {
125 public:
126
127     virtual IFX_FileStream*             Retain() = 0;
128
129     virtual void                                Release() = 0;
130
131     virtual FX_FILESIZE                 GetSize() = 0;
132
133     virtual FX_BOOL                             IsEOF() = 0;
134
135     virtual FX_FILESIZE                 GetPosition() = 0;
136
137     virtual FX_BOOL                             ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) = 0;
138
139     virtual size_t                              ReadBlock(void* buffer, size_t size) = 0;
140
141     virtual     FX_BOOL                         WriteBlock(const void* buffer, FX_FILESIZE offset, size_t size) = 0;
142     virtual     FX_BOOL                         WriteBlock(const void* buffer, size_t size)
143     {
144         return WriteBlock(buffer, GetSize(), size);
145     }
146
147     virtual FX_BOOL                             Flush() = 0;
148 };
149 IFX_FileStream*         FX_CreateFileStream(const FX_CHAR* filename, FX_DWORD dwModes);
150 IFX_FileStream*         FX_CreateFileStream(const FX_WCHAR* filename, FX_DWORD dwModes);
151 class IFX_MemoryStream : public IFX_FileStream
152 {
153 public:
154
155     virtual FX_BOOL                     IsConsecutive() const = 0;
156
157     virtual void                        EstimateSize(size_t nInitSize, size_t nGrowSize) = 0;
158
159     virtual uint8_t*            GetBuffer() const = 0;
160
161     virtual void                        AttachBuffer(uint8_t* pBuffer, size_t nSize, FX_BOOL bTakeOver = FALSE) = 0;
162
163     virtual void                        DetachBuffer() = 0;
164 };
165 IFX_MemoryStream*       FX_CreateMemoryStream(uint8_t* pBuffer, size_t nSize, FX_BOOL bTakeOver = FALSE);
166 IFX_MemoryStream*       FX_CreateMemoryStream(FX_BOOL bConsecutive = FALSE);
167 class IFX_BufferRead : public IFX_StreamRead
168 {
169 public:
170
171     virtual void                        Release() = 0;
172
173     virtual FX_BOOL                     IsEOF() = 0;
174
175     virtual FX_FILESIZE         GetPosition() = 0;
176
177     virtual size_t                      ReadBlock(void* buffer, size_t size) = 0;
178
179     virtual FX_BOOL                     ReadNextBlock(FX_BOOL bRestart = FALSE) = 0;
180
181     virtual const uint8_t*              GetBlockBuffer() = 0;
182
183     virtual size_t                      GetBlockSize() = 0;
184
185     virtual FX_FILESIZE         GetBlockOffset() = 0;
186 };
187
188 #endif  // CORE_INCLUDE_FXCRT_FX_STREAM_H_