FsBase.h
Go to the documentation of this file.
1 /****
2  * FsBase.h
3  * Common base for file system classes
4  *
5  * Copyright 2019 mikee47 <mike@sillyhouse.net>
6  *
7  * This file is part of the IFS Library
8  *
9  * This library is free software: you can redistribute it and/or modify it under the terms of the
10  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along with this library.
17  * If not, see <https://www.gnu.org/licenses/>.
18  *
19  ****/
20 
21 #pragma once
22 
23 #include "FileSystem.h"
24 
25 namespace IFS
26 {
27 #define GET_FS(failure) \
28  auto fs = getFileSystem(); \
29  if(fs == nullptr) { \
30  return failure; \
31  }
32 
33 class FsBase
34 {
35 public:
36  FsBase(IFileSystem* filesys = nullptr) : fileSystem(FileSystem::cast(filesys))
37  {
38  }
39 
40  bool isValid() const
41  {
42  return fileSystem != nullptr;
43  }
44 
49  {
50  return lastError;
51  }
52 
53  String getErrorString(int err) const
54  {
55  return fileSystem == nullptr ? Error::toString(err) : fileSystem->getErrorString(err);
56  }
57 
59  {
60  return getErrorString(lastError);
61  }
62 
64  {
65  if(fileSystem != nullptr) {
66  return fileSystem;
67  }
68 
69  auto res = getDefaultFileSystem();
70  if(res != nullptr) {
71  return res;
72  }
73 
74  lastError = Error::NoFileSystem;
75  return nullptr;
76  }
77 
78 protected:
83  bool check(int64_t res)
84  {
85  if(res >= 0) {
86  return true;
87  }
88 
89  lastError = int(res);
90  return false;
91  }
92 
93 protected:
94  mutable int lastError{FS_OK};
95 
96 private:
97  FileSystem* fileSystem;
98 };
99 
100 } // namespace IFS
Installable File System base class.
Definition: Components/IFS/src/include/IFS/FileSystem.h:40
Definition: FsBase.h:34
FsBase(IFileSystem *filesys=nullptr)
Definition: FsBase.h:36
bool check(int64_t res)
Check file operation result and note error code.
Definition: FsBase.h:83
String getLastErrorString() const
Definition: FsBase.h:58
String getErrorString(int err) const
Definition: FsBase.h:53
int getLastError()
determine if an error occurred during operation
Definition: FsBase.h:48
int lastError
Definition: FsBase.h:94
bool isValid() const
Definition: FsBase.h:40
FileSystem * getFileSystem() const
Definition: FsBase.h:63
Installable File System base class.
Definition: IFileSystem.h:100
virtual String getErrorString(int err)
get the text for a returned error code
Definition: IFileSystem.h:214
The String class.
Definition: WString.h:137
String toString(int err)
get text for an error code
constexpr ErrorCode FS_OK
Definition: Components/IFS/src/include/IFS/Error.h:130
Definition: DirectoryTemplate.h:37
FileSystem * getDefaultFileSystem()
Framework should implement this method.