Sming Framework API
Sming - Open Source framework for high efficiency WiFi SoC ESP8266 native development with C++ language.
TemplateStream.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  * TemplateStream.h
8  *
9  ****/
10 
11 #ifndef _SMING_CORE_DATA_STREAM_TEMPLATE_STREAM_H_
12 #define _SMING_CORE_DATA_STREAM_TEMPLATE_STREAM_H_
13 
14 #include "DataSourceStream.h"
15 #include "WHashMap.h"
16 #include "WString.h"
17 
18 #define TEMPLATE_MAX_VAR_NAME_LEN 16
19 
23 class TemplateVariables : public HashMap<String, String>
24 {
25 };
26 
36 };
44 {
45 public:
49  TemplateStream(IDataSourceStream* stream) : stream(stream)
50  {
51  // Pre-allocate string to maximum length
52  varName.reserve(TEMPLATE_MAX_VAR_NAME_LEN);
53  }
54 
56  {
57  delete stream;
58  }
59 
60  //Use base class documentation
61  StreamType getStreamType() const override
62  {
63  return stream ? eSST_Template : eSST_Invalid;
64  }
65 
66  //Use base class documentation
67  uint16_t readMemoryBlock(char* data, int bufSize) override;
68 
69  //Use base class documentation
70  bool seek(int len) override;
71 
72  bool isFinished() override
73  {
74  return stream ? stream->isFinished() : true;
75  }
76 
82  void setVar(const String& name, const String& value)
83  {
84  templateData[name] = value;
85  }
86 
90  void setVars(const TemplateVariables& vars)
91  {
92  templateData.setMultiple(vars);
93  }
94 
99  {
100  return templateData;
101  }
102 
103  String getName() const override
104  {
105  return stream ? stream->getName() : nullptr;
106  }
107 
117 private:
118  IDataSourceStream* stream = nullptr;
119  TemplateVariables templateData;
121  String varName;
122  size_t skipBlockSize = 0;
123  size_t varDataPos = 0;
124  size_t varWaitSize = 0;
125 };
126 
129 #endif /* _SMING_CORE_DATA_STREAM_TEMPLATE_STREAM_H_ */
Template expand state sending variable.
Definition: TemplateStream.h:35
Template expand state found.
Definition: TemplateStream.h:33
Definition: WHashMap.h:35
StreamType getStreamType() const override
Get the stream type.
Definition: TemplateStream.h:61
Base class for data source stream.
Definition: DataSourceStream.h:39
String getName() const override
Returns name of the resource.
Definition: TemplateStream.h:103
Template expand state start variable.
Definition: TemplateStream.h:34
bool isFinished() override
Check if all data has been read.
Definition: TemplateStream.h:72
The string class.
Definition: WString.h:104
Template expand state wait.
Definition: TemplateStream.h:32
TemplateStream(IDataSourceStream *stream)
Create a template stream.
Definition: TemplateStream.h:49
Definition: TemplateStream.h:43
TemplateExpandState
Template file stream expand state.
Definition: TemplateStream.h:31
TemplateVariables & variables()
Get the template variables.
Definition: TemplateStream.h:98
StreamType
Data stream type.
Definition: DataSourceStream.h:22
Template data stream.
Definition: DataSourceStream.h:26
Template variable (hash map) class.
Definition: TemplateStream.h:23
Stream content not valid.
Definition: DataSourceStream.h:23
void setVars(const TemplateVariables &vars)
Set multiple variables in the template file.
Definition: TemplateStream.h:90
void setVar(const String &name, const String &value)
Set value of a variable in the template file.
Definition: TemplateStream.h:82