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