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