HostFileStream.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  * HostFileStream.h
8  *
9  ****/
10 
11 #pragma once
12 
14 #include <FileSystem.h>
15 
24 {
25 public:
27  {
28  }
29 
34  HostFileStream(const String& fileName, FileOpenFlags openFlags = eFO_ReadOnly)
35  {
36  open(fileName, openFlags);
37  }
38 
40  {
41  close();
42  }
43 
50  bool open(const String& fileName, FileOpenFlags openFlags = eFO_ReadOnly);
51 
54  void close();
55 
56  StreamType getStreamType() const override
57  {
58  return eSST_File;
59  }
60 
61  size_t write(const uint8_t* buffer, size_t size) override;
62 
63  uint16_t readMemoryBlock(char* data, int bufSize) override;
64 
65  int seekFrom(int offset, unsigned origin) override;
66 
67  bool isFinished() override;
68 
69  String getName() const override
70  {
71  return filename;
72  }
73 
74  bool isValid() const override
75  {
76  return handle >= 0;
77  }
78 
82  size_t getPos() const
83  {
84  return pos;
85  }
86 
90  size_t getSize() const
91  {
92  return size;
93  }
94 
98  int available() override
99  {
100  return size - pos;
101  }
102 
107  {
108  return lastError;
109  }
110 
115  bool truncate(size_t newSize);
116 
120  bool truncate()
121  {
122  return truncate(pos);
123  }
124 
125 private:
126  bool check(int res, const char* func);
127 
128 private:
129  int handle = -1;
130  String filename;
131  size_t pos = 0;
132  size_t size = 0;
133  int lastError = 0;
134 };
135 
bool truncate()
Truncate file at current position.
Definition: HostFileStream.h:120
size_t getPos() const
Get the offset of cursor from beginning of data.
Definition: HostFileStream.h:82
HostFileStream()
Definition: HostFileStream.h:26
int seekFrom(int offset, unsigned origin) override
Change position in stream.
int getLastError()
determine if an error occurred during operation
Definition: HostFileStream.h:106
StreamType getStreamType() const override
Get the stream type.
Definition: HostFileStream.h:56
The String class.
Definition: WString.h:136
HostFileStream(const String &fileName, FileOpenFlags openFlags=eFO_ReadOnly)
Create a file stream.
Definition: HostFileStream.h:34
bool isValid() const override
Determine if the stream object contains valid data.
Definition: HostFileStream.h:74
uint16_t readMemoryBlock(char *data, int bufSize) override
Read a block of memory.
int available() override
Return the maximum bytes available to read, from current position.
Definition: HostFileStream.h:98
bool open(const String &fileName, FileOpenFlags openFlags=eFO_ReadOnly)
Open a file and attach this stream object to it.
void close()
Close file.
Read only file.
Definition: FileSystem.h:26
String getName() const override
Returns name of the resource.
Definition: HostFileStream.h:69
StreamType
Data stream type.
Definition: DataSourceStream.h:22
File data stream.
Definition: DataSourceStream.h:25
Host File stream class.
Definition: HostFileStream.h:23
size_t write(const uint8_t *buffer, size_t size) override
Write chars to stream.
~HostFileStream()
Definition: HostFileStream.h:39
size_t getSize() const
Get the total file size.
Definition: HostFileStream.h:90
bool isFinished() override
Check if all data has been read.
Base class for read/write stream.
Definition: ReadWriteStream.h:22
FileOpenFlags
File open flags.
Definition: FileSystem.h:25