FileStream.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * FileStream.h
8  *
9  ****/
10 
11 #pragma once
12 
13 #include "ReadWriteStream.h"
14 #include "FileSystem.h"
15 
24 {
25 public:
27  {
28  }
29 
35  {
36  open(fileName, openFlags);
37  }
38 
40  {
41  close();
42  }
43 
48  void attach(file_t file, size_t size);
49 
52  {
53  return open(fileName, openFlags);
54  }
55 
62  bool open(const String& fileName, FileOpenFlags openFlags = eFO_ReadOnly);
63 
66  void close();
67 
68  StreamType getStreamType() const override
69  {
70  return eSST_File;
71  }
72 
73  size_t write(const uint8_t* buffer, size_t size) override;
74 
75  uint16_t readMemoryBlock(char* data, int bufSize) override;
76 
77  int seekFrom(int offset, unsigned origin) override;
78 
79  bool isFinished() override
80  {
81  return fileIsEOF(handle);
82  }
83 
87  String fileName() const;
88 
92  bool fileExist() const
93  {
94  return handle >= 0;
95  }
96 
97  String getName() const override
98  {
99  return fileName();
100  }
101 
102  bool isValid() const override
103  {
104  return fileExist();
105  }
106 
110  size_t getPos() const
111  {
112  return pos;
113  }
114 
118  size_t getSize() const
119  {
120  return size;
121  }
122 
126  int available() override
127  {
128  return size - pos;
129  }
130 
131  String id() const override;
132 
137  {
138  return lastError;
139  }
140 
145  bool truncate(size_t newSize);
146 
150  bool truncate()
151  {
152  return truncate(pos);
153  }
154 
155 private:
160  bool check(int res)
161  {
162  if(res >= 0) {
163  return true;
164  }
165 
166  if(lastError >= 0) {
167  lastError = res;
168  }
169  return false;
170  }
171 
172 private:
173  file_t handle = -1;
174  size_t pos = 0;
175  size_t size = 0;
176  int lastError = SPIFFS_OK;
177 };
178 
int available() override
Return the maximum bytes available to read, from current position.
Definition: FileStream.h:126
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)
Definition: FileStream.h:51
bool fileExist() const
Determine if file exists.
Definition: FileStream.h:92
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:102
int getLastError()
determine if an error occurred during operation
Definition: FileStream.h:136
The String class.
Definition: WString.h:136
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:97
File stream class.
Definition: FileStream.h:23
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.
#define SMING_DEPRECATED
Definition: sming_attr.h:30
int seekFrom(int offset, unsigned origin) override
Change position in stream.
StreamType getStreamType() const override
Get the stream type.
Definition: FileStream.h:68
bool isFinished() override
Check if all data has been read.
Definition: FileStream.h:79
Read only file.
Definition: FileSystem.h:26
size_t getPos() const
Get the offset of cursor from beginning of data.
Definition: FileStream.h:110
#define SPIFFS_OK
Definition: spiffs.h:16
StreamType
Data stream type.
Definition: DataSourceStream.h:22
File data stream.
Definition: DataSourceStream.h:25
~FileStream()
Definition: FileStream.h:39
signed short file_t
File handle.
Definition: FileSystem.h:22
uint16_t readMemoryBlock(char *data, int bufSize) override
Read a block of memory.
bool truncate()
Truncate file at current position.
Definition: FileStream.h:150
size_t getSize() const
Get the total file size.
Definition: FileStream.h:118
Base class for read/write stream.
Definition: ReadWriteStream.h:22
String id() const override
Returns unique id of the resource.
String fileName() const
Filename of file stream is attached to.
FileOpenFlags
File open flags.
Definition: FileSystem.h:25
FileStream()
Definition: FileStream.h:26