File.h
Go to the documentation of this file.
1 
20 #pragma once
21 
22 #include "FsBase.h"
23 
24 namespace IFS
25 {
26 #define GET_FS() \
27  auto fs = getFileSystem(); \
28  if(fs == nullptr) { \
29  return lastError; \
30  }
31 
32 #define GET_FS_BOOL() \
33  auto fs = getFileSystem(); \
34  if(fs == nullptr) { \
35  return false; \
36  }
37 
41 class File : public FsBase
42 {
43 public:
44  using FsBase::FsBase;
45 
46  // Common flag combinations
47  static constexpr OpenFlags ReadOnly{OpenFlag::Read};
48  static constexpr OpenFlags WriteOnly{OpenFlag::Write};
49  static constexpr OpenFlags ReadWrite{OpenFlag::Read | OpenFlag::Write};
50  static constexpr OpenFlags Create{OpenFlag::Create};
51  static constexpr OpenFlags Append{OpenFlag::Append};
52  static constexpr OpenFlags Truncate{OpenFlag::Truncate};
53  static constexpr OpenFlags CreateNewAlways{OpenFlag::Create | OpenFlag::Truncate};
54 
56  {
57  close();
58  }
59 
60  explicit operator bool() const
61  {
62  return handle >= 0;
63  }
64 
70  bool stat(Stat& stat)
71  {
72  GET_FS_BOOL();
73  return check(fs->fstat(handle, &stat));
74  }
75 
87  int control(ControlCode code, void* buffer, size_t bufSize)
88  {
89  GET_FS();
90  int res = fs->fcontrol(handle, code, buffer, bufSize);
91  check(res);
92  return res;
93  }
94 
101  bool open(const String& path, OpenFlags flags = OpenFlag::Read)
102  {
103  GET_FS_BOOL();
104  fs->close(handle);
105  handle = fs->open(path.c_str(), flags);
106  return check(handle);
107  }
108 
115  bool open(const Stat& stat, OpenFlags flags = OpenFlag::Read)
116  {
117  GET_FS_BOOL();
118  fs->close(handle);
119  handle = fs->fopen(stat, flags);
120  return check(handle);
121  }
122 
127  bool close()
128  {
129  if(handle < 0) {
130  return true;
131  }
132  GET_FS_BOOL();
133  int res = fs->close(handle);
134  handle = -1;
135  return check(res);
136  }
137 
144  int read(void* data, size_t size)
145  {
146  GET_FS();
147  int res = fs->read(handle, data, size);
148  check(res);
149  return res;
150  }
151 
158  int write(const void* data, size_t size)
159  {
160  GET_FS();
161  int res = fs->write(handle, data, size);
162  check(res);
163  return res;
164  }
165 
166  bool write(const String& s)
167  {
168  size_t len = s.length();
169  return write(s.c_str(), len) == int(len);
170  }
171 
178  int seek(int offset, SeekOrigin origin)
179  {
180  GET_FS();
181  int res = fs->lseek(handle, offset, origin);
182  check(res);
183  return res;
184  }
185 
190  bool eof()
191  {
192  auto fs = getFileSystem();
193  if(fs == nullptr) {
194  return true;
195  }
196  int res = fs->eof(handle);
197  check(res);
198  return res != FS_OK;
199  }
200 
205  int32_t tell()
206  {
207  GET_FS();
208  int res = fs->tell(handle);
209  check(res);
210  return res;
211  }
212 
218  bool truncate(size_t new_size)
219  {
220  GET_FS();
221  int res = fs->ftruncate(handle, new_size);
222  check(res);
223  return res;
224  }
225 
230  bool truncate()
231  {
232  GET_FS();
233  int res = fs->ftruncate(handle);
234  check(res);
235  return res;
236  }
237 
242  bool flush()
243  {
244  GET_FS_BOOL();
245  return check(fs->flush(handle));
246  }
247 
253  bool setacl(const ACL& acl)
254  {
255  GET_FS_BOOL();
256  return check(fs->setacl(handle, acl));
257  }
258 
264  bool settime(time_t mtime)
265  {
266  GET_FS_BOOL();
267  return check(fs->settime(handle, mtime));
268  }
269 
275  bool setcompression(const Compression& compression)
276  {
277  GET_FS_BOOL();
278  return check(fs->setcompression(handle, compression));
279  }
280 
285  bool remove()
286  {
287  GET_FS_BOOL();
288  int res = fs->fremove(handle);
289  if(!check(res)) {
290  return false;
291  }
292  fs->close(handle);
293  handle = -1;
294  return true;
295  }
296 
301  uint32_t getSize()
302  {
303  GET_FS();
304  return fs->getSize(handle);
305  }
306 
308 
315  int readContent(size_t size, ReadContentCallback callback)
316  {
317  GET_FS();
318  return fs->readContent(handle, size, callback);
319  }
320 
327  {
328  GET_FS();
329  return fs->readContent(handle, callback);
330  }
331 
341  {
342  String s;
343  auto len = getSize();
344  if(!s.setLength(len)) {
345  return nullptr;
346  }
347 
348  if(read(s.begin(), len) != int(len)) {
349  return nullptr;
350  }
351 
352  return s;
353  }
354 
355 private:
356  FileHandle handle{-1};
357 };
358 
359 }; // namespace IFS
bool eof()
determine if current file position is at end of file
Definition: File.h:190
ControlCode
See IFS::IFileSystem::fcontrol
Definition: Control.h:31
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:641
int write(const void *data, size_t size)
write content to a file at current position and advance cursor
Definition: File.h:158
bool flush()
flush any buffered data to physical media
Definition: File.h:242
FsBase(IFileSystem *filesys)
Definition: FsBase.h:30
int seek(int offset, SeekOrigin origin)
change file read/write position
Definition: File.h:178
static constexpr OpenFlags Append
Definition: File.h:51
Delegate< int(const char *buffer, size_t size)> ReadContentCallback
Callback for readContent method.
Definition: Components/IFS/src/include/IFS/FileSystem.h:178
bool setacl(const ACL &acl)
Set access control information for file.
Definition: File.h:253
bool write(const String &s)
Definition: File.h:166
static constexpr OpenFlags ReadWrite
Definition: File.h:49
static constexpr OpenFlags ReadOnly
Definition: File.h:47
bool check(int res)
Check file operation result and note error code.
Definition: FsBase.h:65
Wraps up all file access methods.
Definition: File.h:41
int read(void *data, size_t size)
read content from a file and advance cursor
Definition: File.h:144
The String class.
Definition: WString.h:136
bool truncate()
Truncate an open file at the current cursor position.
Definition: File.h:230
bool close()
close an open file
Definition: File.h:127
uint32_t getSize()
Get size of file.
Definition: File.h:301
Definition: Delegate.h:20
char * begin()
Get a modifiable pointer to String content.
Definition: WString.h:650
SeekOrigin
Stream/file seek origins.
Definition: SeekOrigin.h:18
static constexpr OpenFlags WriteOnly
Definition: File.h:48
#define GET_FS_BOOL()
Definition: File.h:32
bool setcompression(const Compression &compression)
Set file compression information.
Definition: File.h:275
bool open(const Stat &stat, OpenFlags flags=OpenFlag::Read)
open a file from it&#39;s stat structure
Definition: File.h:115
int readContent(ReadContentCallback callback)
Read from current file position to end of file and invoke callback for each block read...
Definition: File.h:326
int16_t FileHandle
File handle.
Definition: Stat.h:39
Definition: DirectoryTemplate.h:36
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:237
~File()
Definition: File.h:55
bool setLength(size_t length)
set the string length accordingly, expanding if necessary
#define GET_FS()
Definition: File.h:26
bool stat(Stat &stat)
get file information
Definition: File.h:70
String getContent()
Read content of a file.
Definition: File.h:340
File Status structure.
Definition: Stat.h:51
Manage a set of bit values using enumeration.
Definition: BitSet.h:43
int32_t tell()
get current file position
Definition: File.h:205
bool settime(time_t mtime)
Set modification time for file.
Definition: File.h:264
Definition: Access.h:34
bool truncate(size_t new_size)
Truncate (reduce) the size of an open file.
Definition: File.h:218
static constexpr OpenFlags CreateNewAlways
Definition: File.h:53
bool open(const String &path, OpenFlags flags=OpenFlag::Read)
open a file by name/path
Definition: File.h:101
int readContent(size_t size, ReadContentCallback callback)
Read from current file position and invoke callback for each block read.
Definition: File.h:315
int control(ControlCode code, void *buffer, size_t bufSize)
Low-level and non-standard file control operations.
Definition: File.h:87
Definition: FsBase.h:27
FileSystem * getFileSystem() const
Definition: FsBase.h:52
constexpr ErrorCode FS_OK
Definition: Error.h:130
static constexpr OpenFlags Create
Definition: File.h:50
static constexpr OpenFlags Truncate
Definition: File.h:52
A compression descriptor.
Definition: Compression.h:38