Sming Framework API
Sming - Open Source framework for high efficiency WiFi SoC ESP8266 native development with C++ language.
FileStream.h
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/anakod/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * FileStream.h
8  *
9  ****/
10 
11 #ifndef _SMING_CORE_DATA_FILE_STREAM_H_
12 #define _SMING_CORE_DATA_FILE_STREAM_H_
13 
14 #include "ReadWriteStream.h"
15 #include "FileSystem.h"
16 
25 {
26 public:
27  FileStream()
28  {
29  }
30 
35  {
36  open(fileName, openFlags);
37  }
38 
39  ~FileStream()
40  {
41  close();
42  }
43 
48  void attach(file_t file, size_t size);
49 
51  bool attach(const String& fileName, FileOpenFlags openFlags = eFO_ReadOnly) SMING_DEPRECATED
52  {
53  return open(fileName, openFlags);
54  }
55 
62  bool open(const String& fileName, FileOpenFlags openFlags = eFO_ReadOnly);
63 
66  void close();
67 
68  //Use base class documentation
69  StreamType getStreamType() const override
70  {
71  return eSST_File;
72  }
73 
74  size_t write(const uint8_t* buffer, size_t size) override;
75 
76  //Use base class documentation
77  uint16_t readMemoryBlock(char* data, int bufSize) override;
78 
79  //Use base class documentation
80  bool seek(int len) override;
81 
82  //Use base class documentation
83  bool isFinished() override
84  {
85  return fileIsEOF(handle);
86  }
87 
91  String fileName() const;
92 
96  bool fileExist() const
97  {
98  return handle >= 0;
99  }
100 
101  String getName() const override
102  {
103  return fileName();
104  }
105 
106  bool isValid() const override
107  {
108  return fileExist();
109  }
110 
114  size_t getPos() const
115  {
116  return pos;
117  }
118 
122  int available() override
123  {
124  return size - pos;
125  }
126 
127  String id() const override;
128 
133  {
134  return lastError;
135  }
136 
137 private:
142  bool check(int res)
143  {
144  if(res >= 0) {
145  return true;
146  }
147 
148  if(lastError >= 0) {
149  lastError = res;
150  }
151  return false;
152  }
153 
154 private:
155  file_t handle = -1;
156  size_t pos = 0;
157  size_t size = 0;
158  int lastError = SPIFFS_OK;
159 };
160 
163 #endif /* _SMING_CORE_DATA_FILE_STREAM_H_ */
int available() override
Return the total length of the stream.
Definition: FileStream.h:122
void attach(file_t file, size_t size)
Attach this stream object to an open file handle.
bool attach(const String &fileName, FileOpenFlags openFlags=eFO_ReadOnly) SMING_DEPRECATED
Definition: FileStream.h:51
size_t getPos() const
Get the offset of cursor from beginning of data.
Definition: FileStream.h:114
String fileName() const
Filename of file stream is attached to.
bool open(const String &fileName, FileOpenFlags openFlags=eFO_ReadOnly)
Open a file and attach this stream object to it.
bool isValid() const override
Determine if the stream object contains valid data.
Definition: FileStream.h:106
int getLastError()
determine if an error occurred during operation
Definition: FileStream.h:132
The string class.
Definition: WString.h:104
size_t write(const uint8_t *buffer, size_t size) override
Write chars to stream.
void close()
Close file.
String getName() const override
Returns name of the resource.
Definition: FileStream.h:101
File stream class.
Definition: FileStream.h:24
FileStream(const String &fileName, FileOpenFlags openFlags=eFO_ReadOnly)
Create a file stream.
Definition: FileStream.h:34
bool fileIsEOF(file_t file)
Check if at end of file.
StreamType getStreamType() const override
Get the stream type.
Definition: FileStream.h:69
bool isFinished() override
Check if all data has been read.
Definition: FileStream.h:83
Read only file.
Definition: FileSystem.h:26
StreamType
Data stream type.
Definition: DataSourceStream.h:22
File data stream.
Definition: DataSourceStream.h:25
bool seek(int len) override
Move read cursor.
uint16_t readMemoryBlock(char *data, int bufSize) override
Read a block of memory.
Base class for read/write stream.
Definition: ReadWriteStream.h:23
String id() const override
Returns unique id of the resource.
FileOpenFlags
File open flags.
Definition: FileSystem.h:25
bool fileExist() const
Determine if file exists.
Definition: FileStream.h:96