DescriptionStream.h
Go to the documentation of this file.
1 /****
2  * DescriptionStream.h
3  *
4  * Copyright 2019 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the Sming UPnP Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  ****/
19 
20 #pragma once
21 
22 #include "Object.h"
24 
25 namespace UPnP
26 {
28 {
29 public:
34  DescriptionStream(Object& object, const String& descriptionUrl) : object_(object)
35  {
36  setName(descriptionUrl);
37  segments[0].item = &object;
38  getContent();
39  }
40 
42  {
43  freeMem();
44  }
45 
50  void reset();
51 
52  bool isValid() const override
53  {
54  return true;
55  }
56 
57  uint16_t readMemoryBlock(char* data, int bufSize) override;
58 
59  bool seek(int len) override;
60 
61  bool isFinished() override
62  {
63  return !content && (state == State::done);
64  }
65 
66  String getName() const override
67  {
68  return name;
69  }
70 
71  MimeType getMimeType() const override
72  {
73  return MimeType::XML;
74  }
75 
76 protected:
77  void freeMem();
78  void getContent();
79  void setName(const String& descriptionUrl);
80 
81 private:
82  const Object& object_;
83  String name;
84  // Nesting levels
85  struct Segment {
86  Item* item{nullptr};
87  String footer;
88  ItemEnumerator* list{nullptr}; // active list
89  String listName;
90  uint8_t listIndex{0};
91  };
92  Segment segments[4];
93  String content; // Buffer for current segment being output
94  uint16_t readPos{0};
95  enum class State {
96  header,
97  item,
98  nextList,
99  listItem,
100  nextListItem,
101  done,
102  } state = State::header;
103  uint8_t segIndex{0}; // nesting level
104 };
105 
106 } // namespace UPnP
Base class for read-only stream.
Definition: DataSourceStream.h:46
The String class.
Definition: WString.h:137
Definition: DescriptionStream.h:28
bool isValid() const override
Determine if the stream object contains valid data.
Definition: DescriptionStream.h:52
void setName(const String &descriptionUrl)
String getName() const override
Returns name of the resource.
Definition: DescriptionStream.h:66
~DescriptionStream()
Definition: DescriptionStream.h:41
bool seek(int len) override
Move read cursor.
void reset()
Reset back to start.
bool isFinished() override
Check if all data has been read.
Definition: DescriptionStream.h:61
MimeType getMimeType() const override
Get MIME type for stream content.
Definition: DescriptionStream.h:71
DescriptionStream(Object &object, const String &descriptionUrl)
Construct a description stream.
Definition: DescriptionStream.h:34
uint16_t readMemoryBlock(char *data, int bufSize) override
Read a block of memory.
Definition: ItemEnumerator.h:30
Definition: Item.h:39
Definition: Libraries/UPnP/src/include/Network/UPnP/Object.h:38
MimeType
Definition: WebConstants.h:53
Definition: ActionRequest.h:25
@ header
Description template with main document element.