Arduino DLNA Server
Loading...
Searching...
No Matches
StrPrint.h
Go to the documentation of this file.
1#pragma once
2#include "Print.h"
3#include "Str.h"
4#include "dlna_config.h"
5
6namespace tiny_dlna {
7
13class StrPrint : public Print {
14 public:
15 StrPrint(int incSize = STR_PRINT_INC_SIZE) { inc_size = incSize; }
16 size_t write(uint8_t ch) override {
17 if (str.length() >= str.capacity() - 1) {
19 }
20 str.add((const char)ch);
21 return 1;
22 }
23
24 size_t write(const uint8_t* buffer, size_t size) override {
25 size_t result = 0;
26 for (int j = 0; j < size; j++) {
27 result += write(buffer[j]);
28 }
29 // expand encoded entities if desired
30 if (expand_encoded) {
31 str.replaceAll("&amp;", "&");
32 str.replaceAll("&lt;", "<");
33 str.replaceAll("&gt;", ">");
34 }
35
36 return result;
37 }
38
39 const char* c_str() { return str.c_str(); }
40
41 size_t length() { return str.length(); }
42
43 void reset() { str.reset(); }
44
45 void consume(int n) { str.remove(n); }
46
47 void setExpandEncoded(bool flag) { expand_encoded = flag; }
48
49 size_t printf(const char* fmt, ...) {
50 char buf[MAX_PRINTF_SIZE];
51 va_list args;
52 va_start(args, fmt);
53 int n = vsnprintf(buf, sizeof(buf), fmt, args);
54 va_end(args);
55 if (n > 0) {
56 write((const uint8_t*)buf, n);
57 return n;
58 }
59 return 0;
60 }
61
62 protected:
65 bool expand_encoded = false;
66};
67
68} // namespace tiny_dlna
Print to a dynamic string.
Definition: StrPrint.h:13
const char * c_str()
Definition: StrPrint.h:39
StrPrint(int incSize=STR_PRINT_INC_SIZE)
Definition: StrPrint.h:15
size_t length()
Definition: StrPrint.h:41
size_t write(uint8_t ch) override
Definition: StrPrint.h:16
void reset()
Definition: StrPrint.h:43
size_t write(const uint8_t *buffer, size_t size) override
Definition: StrPrint.h:24
bool expand_encoded
Definition: StrPrint.h:65
void consume(int n)
Definition: StrPrint.h:45
void setExpandEncoded(bool flag)
Definition: StrPrint.h:47
int inc_size
Definition: StrPrint.h:64
size_t printf(const char *fmt,...)
Definition: StrPrint.h:49
Str str
Definition: StrPrint.h:63
Heap-backed string utility used throughout tiny_dlna.
Definition: Str.h:27
int length() const
Current length (int)
Definition: Str.h:57
int replaceAll(const char *toReplace, const char *replaced)
Replace all occurrences of toReplace with replaced; returns count.
Definition: Str.h:279
void reset()
Clear contents (alias of clear)
Definition: Str.h:157
void add(const char *append)
Append C-string (ignored if nullptr)
Definition: Str.h:96
void setCapacity(size_t newLen)
Reserve capacity.
Definition: Str.h:323
const char * c_str() const
C-string pointer to internal buffer.
Definition: Str.h:88
void remove(int n)
removes the first n characters
Definition: Str.h:226
size_t capacity() const
Current capacity.
Definition: Str.h:325
#define MAX_PRINTF_SIZE
Max printf buffer size.
Definition: dlna_config.h:50
#define STR_PRINT_INITIAL_SIZE
Define initial size for StrPrint.
Definition: dlna_config.h:30
#define STR_PRINT_INC_SIZE
Define increment size for StrPrint.
Definition: dlna_config.h:35
Definition: Allocator.h:13