Sming Framework API
Sming - Open Source framework for high efficiency WiFi SoC ESP8266 native development with C++ language.
DataSourceStream.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 
8 #ifndef _SMING_CORE_DATASTREAM_H_
9 #define _SMING_CORE_DATASTREAM_H_
10 
11 #include <user_config.h>
12 #include "../SmingCore/FileSystem.h"
13 #include "../Services/ArduinoJson/include/ArduinoJson.h"
14 #include "../Wiring/WString.h"
15 #include "../Wiring/WHashMap.h"
16 
17 #define TEMPLATE_MAX_VAR_NAME_LEN 16
18 
19 class HttpRequest;
20 
26 {
33 };
44 class TemplateVariables : public HashMap<String, String>
45 {
46 };
47 
50 {
51 public:
52  virtual ~IDataSourceStream() {}
53 
58  virtual StreamType getStreamType() = 0;
59 
66  virtual uint16_t readMemoryBlock(char* data, int bufSize) = 0;
67 
72  virtual bool seek(int len) = 0;
73 
77  virtual bool isFinished() = 0;
78 };
79 
82 {
83 public:
87  virtual ~MemoryDataStream();
88 
89  //Use base class documentation
90  virtual StreamType getStreamType() { return eSST_Memory; }
91 
95  const char* getStreamPointer() { return pos; }
96 
100  int getStreamLength() { return size; }
101 
106  virtual size_t write(uint8_t charToWrite);
107 
113  virtual size_t write(const uint8_t *buffer, size_t size);
114 
115  //Use base class documentation
116  virtual uint16_t readMemoryBlock(char* data, int bufSize);
117 
118  //Use base class documentation
119  virtual bool seek(int len);
120 
121  //Use base class documentation
122  virtual bool isFinished();
123 
124 private:
125  char* buf;
126  char* pos;
127  int size;
128  int capacity;
129 };
130 
133 {
134 public:
135 
139  FileStream();
140  FileStream(String fileName);
141  virtual ~FileStream();
142 
143  virtual bool attach(String fileName, FileOpenFlags openFlags);
144  //Use base class documentation
145  virtual StreamType getStreamType() { return eSST_File; }
146 
147  virtual size_t write(uint8_t charToWrite);
148  virtual size_t write(const uint8_t *buffer, size_t size);
149 
150  //Use base class documentation
151  virtual uint16_t readMemoryBlock(char* data, int bufSize);
152 
153  //Use base class documentation
154  virtual bool seek(int len);
155 
156  //Use base class documentation
157  virtual bool isFinished();
158 
159  String fileName();
160  bool fileExist();
161 
165  inline int getPos() { return pos; }
166 
167 private:
168  file_t handle;
169  int pos;
170  int size;
171 };
179 {
184 };
191 class TemplateFileStream : public FileStream
193 {
194 public:
198  TemplateFileStream(String templateFileName);
199  virtual ~TemplateFileStream();
200 
201  //Use base class documentation
203 
204  //Use base class documentation
205  virtual uint16_t readMemoryBlock(char* data, int bufSize);
206 
207  //Use base class documentation
208  virtual bool seek(int len);
209 
215  void setVar(String name, String value);
216 
220  void setVarsFromRequest(const HttpRequest& request);
221 
225  inline TemplateVariables& variables() { return templateData; }
226 
227 private:
228  TemplateVariables templateData;
229  TemplateExpandState state;
230  String varName;
231  int skipBlockSize;
232  int varDataPos;
233  int varWaitSize;
234 };
235 
238 {
239 public:
243  virtual ~JsonObjectStream();
244 
245  //Use base class documentation
247 
251  JsonObject& getRoot();
252 
253  //Use base class documentation
254  virtual uint16_t readMemoryBlock(char* data, int bufSize);
255 
256 private:
257  DynamicJsonBuffer buffer;
258  JsonObject &rootNode;
259  bool send;
260 };
261 
263 #endif /* _SMING_CORE_DATASTREAM_H_ */
bool fileExist(const String name)
Check if a file exists on file system.
Template expand state sending variable.
Definition: DataSourceStream.h:183
Template file stream class.
Definition: DataSourceStream.h:192
Template expand state found.
Definition: DataSourceStream.h:181
Definition: WHashMap.h:24
Template file data stream.
Definition: DataSourceStream.h:29
Memory data stream.
Definition: DataSourceStream.h:27
Base class for data source stream.
Definition: DataSourceStream.h:49
virtual StreamType getStreamType()
Get the stream type.
Definition: DataSourceStream.h:202
Template expand state start variable.
Definition: DataSourceStream.h:182
JSON object stream class.
Definition: DataSourceStream.h:237
Definition: WString.h:42
JSON object data stream.
Definition: DataSourceStream.h:30
Provides formatted output to stream.
Definition: Print.h:31
Template expand state wait.
Definition: DataSourceStream.h:180
File stream class.
Definition: DataSourceStream.h:132
const char * getStreamPointer()
Get a pointer to the current position.
Definition: DataSourceStream.h:95
virtual StreamType getStreamType()
Get the stream type.
Definition: DataSourceStream.h:90
TemplateExpandState
Template file stream expand state.
Definition: DataSourceStream.h:178
Unknown data stream type.
Definition: DataSourceStream.h:32
StreamType
Data stream type.
Definition: DataSourceStream.h:25
File data stream.
Definition: DataSourceStream.h:28
Template variable (hash map) class.
Definition: DataSourceStream.h:44
virtual StreamType getStreamType()
Get the stream type.
Definition: DataSourceStream.h:246
Definition: HttpRequest.h:27
TemplateVariables & variables()
Get the template variables.
Definition: DataSourceStream.h:225
virtual StreamType getStreamType()
Get the stream type.
Definition: DataSourceStream.h:145
User defined data stream.
Definition: DataSourceStream.h:31
int getStreamLength()
Get size of stream.
Definition: DataSourceStream.h:100
int getPos()
Get the offset of cursor from beginning of data.
Definition: DataSourceStream.h:165
FileOpenFlags
File open flags.
Definition: FileSystem.h:22
Memory data stream class.
Definition: DataSourceStream.h:81