Components/IFS/src/include/IFS/FileSystem.h
Go to the documentation of this file.
1 
27 #pragma once
28 
29 #include "IFileSystem.h"
30 #include <Delegate.h>
31 
32 namespace IFS
33 {
39 class FileSystem : public IFileSystem
40 {
41 public:
42  static constexpr FileSystem& cast(IFileSystem& fs)
43  {
44  return static_cast<FileSystem&>(fs);
45  }
46 
47  static constexpr FileSystem* cast(IFileSystem* fs)
48  {
49  return static_cast<FileSystem*>(fs);
50  }
51 
56  int opendir(const String& path, DirHandle& dir)
57  {
58  return opendir(path.c_str(), dir);
59  }
60 
66  int makedirs(const char* path);
67 
68  int makedirs(const String& path)
69  {
70  return makedirs(path.c_str());
71  }
72 
73  using IFileSystem::stat;
77  int stat(const String& path, Stat* s)
78  {
79  return stat(path.c_str(), s);
80  }
81  int stat(const String& path, Stat& s)
82  {
83  return stat(path.c_str(), &s);
84  }
85 
86  using IFileSystem::fstat;
90  int fstat(FileHandle file, Stat& stat)
91  {
92  return fstat(file, &stat);
93  }
94 
95  using IFileSystem::open;
103  FileHandle open(const String& path, OpenFlags flags)
104  {
105  return open(path.c_str(), flags);
106  }
107 
113  {
114  int pos = tell(file);
115  return (pos < 0) ? pos : ftruncate(file, pos);
116  }
117 
123  int truncate(const char* fileName, size_t newSize);
124 
125  int truncate(const String& fileName, size_t newSize)
126  {
127  return truncate(fileName.c_str(), newSize);
128  }
129 
130  using IFileSystem::rename;
138  int rename(const String& oldpath, const String& newpath)
139  {
140  return rename(oldpath.c_str(), newpath.c_str());
141  }
142 
143  using IFileSystem::remove;
149  int remove(const String& path)
150  {
151  return remove(path.c_str());
152  }
153 
158  uint32_t getSize(FileHandle file);
159 
164  uint32_t getSize(const char* fileName);
165 
166  uint32_t getSize(const String& fileName)
167  {
168  return getSize(fileName.c_str());
169  }
170 
179 
187  int readContent(FileHandle file, size_t size, ReadContentCallback callback);
188 
195  int readContent(FileHandle file, ReadContentCallback callback);
196 
203  int readContent(const String& filename, ReadContentCallback callback);
204 
221  size_t getContent(const char* fileName, char* buffer, size_t bufSize);
222 
223  size_t getContent(const String& fileName, char* buffer, size_t bufSize)
224  {
225  return getContent(fileName.c_str(), buffer, bufSize);
226  }
227 
238  String getContent(const String& fileName);
239 
252  int setContent(const char* fileName, const void* content, size_t length);
253 
254  int setContent(const char* fileName, const char* content)
255  {
256  return setContent(fileName, content, (content == nullptr) ? 0 : strlen(content));
257  }
258 
259  int setContent(const String& fileName, const char* content)
260  {
261  return setContent(fileName.c_str(), content);
262  }
263 
264  int setContent(const String& fileName, const void* content, size_t length)
265  {
266  return setContent(fileName.c_str(), content, length);
267  }
268 
269  int setContent(const String& fileName, const String& content)
270  {
271  return setContent(fileName.c_str(), content.c_str(), content.length());
272  }
273 
275 };
276 
277 #ifdef ARCH_HOST
278 namespace Host
279 {
281 }
282 #endif
283 
284 } // namespace IFS
285 
uint32_t getSize(const String &fileName)
Definition: Components/IFS/src/include/IFS/FileSystem.h:166
virtual int remove(const char *path)=0
remove (delete) a file by path
int rename(const String &oldpath, const String &newpath)
Definition: Components/IFS/src/include/IFS/FileSystem.h:138
struct FileDir * DirHandle
Definition: IFileSystem.h:65
int setContent(const String &fileName, const String &content)
Definition: Components/IFS/src/include/IFS/FileSystem.h:269
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:641
int stat(const String &path, Stat &s)
Definition: Components/IFS/src/include/IFS/FileSystem.h:81
virtual int rename(const char *oldpath, const char *newpath)=0
rename a file
Installable File System base class.
Definition: IFileSystem.h:100
virtual FileHandle open(const char *path, OpenFlags flags)=0
open a file by name/path
static constexpr FileSystem & cast(IFileSystem &fs)
Definition: Components/IFS/src/include/IFS/FileSystem.h:42
virtual int opendir(const char *path, DirHandle &dir)=0
open a directory for reading
The String class.
Definition: WString.h:136
int stat(const String &path, Stat *s)
get file information
Definition: Components/IFS/src/include/IFS/FileSystem.h:77
Installable File System base class.
Definition: Components/IFS/src/include/IFS/FileSystem.h:39
Definition: Delegate.h:20
int setContent(const String &fileName, const char *content)
Definition: Components/IFS/src/include/IFS/FileSystem.h:259
int16_t FileHandle
File handle.
Definition: Stat.h:39
Definition: DirectoryTemplate.h:36
int opendir(const String &path, DirHandle &dir)
open a directory for reading
Definition: Components/IFS/src/include/IFS/FileSystem.h:56
virtual int ftruncate(FileHandle file, size_t new_size)=0
Truncate (reduce) the size of an open file.
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:237
int readContent(FileHandle file, size_t size, ReadContentCallback callback)
Read from current file position and invoke callback for each block read.
int setContent(const char *fileName, const void *content, size_t length)
int truncate(const char *fileName, size_t newSize)
Truncate a file to a specific size.
IFS::FileSystem * getFileSystem()
Get the currently active file system, if any.
Definition: Core/FileSystem.h:97
virtual int stat(const char *path, Stat *stat)=0
get file information
int makedirs(const char *path)
Create a directory and any intermediate directories if they do not already exist. ...
File Status structure.
Definition: Stat.h:51
virtual int32_t tell(FileHandle file)=0
get current file position
Manage a set of bit values using enumeration.
Definition: BitSet.h:43
uint32_t getSize(FileHandle file)
Get size of file.
int setContent(const String &fileName, const void *content, size_t length)
Definition: Components/IFS/src/include/IFS/FileSystem.h:264
size_t getContent(const String &fileName, char *buffer, size_t bufSize)
Definition: Components/IFS/src/include/IFS/FileSystem.h:223
size_t getContent(const char *fileName, char *buffer, size_t bufSize)
int setContent(const char *fileName, const char *content)
Definition: Components/IFS/src/include/IFS/FileSystem.h:254
virtual int fstat(FileHandle file, Stat *stat)=0
get file information
int ftruncate(FileHandle file)
Truncate an open file at the current cursor position.
Definition: Components/IFS/src/include/IFS/FileSystem.h:112
int truncate(const String &fileName, size_t newSize)
Definition: Components/IFS/src/include/IFS/FileSystem.h:125
int makedirs(const String &path)
Definition: Components/IFS/src/include/IFS/FileSystem.h:68
int fstat(FileHandle file, Stat &stat)
get file information
Definition: Components/IFS/src/include/IFS/FileSystem.h:90
FileHandle open(const String &path, OpenFlags flags)
open a file by name/path
Definition: Components/IFS/src/include/IFS/FileSystem.h:103
static constexpr FileSystem * cast(IFileSystem *fs)
Definition: Components/IFS/src/include/IFS/FileSystem.h:47