Arduino DLNA Server
Loading...
Searching...
No Matches
SdFatParser.h
Go to the documentation of this file.
1#pragma once
12#include <Print.h>
13
14#include <string>
15
16namespace tiny_dlna {
17
24 std::string name;
29 int level;
30};
31
45class SdFatParser : public Print {
46 public:
50 SdFatParser() { name.reserve(80); }
51
62 size_t write(uint8_t c) override {
63 switch (c) {
64 case '\n':
65 parse();
66 break;
67 case '\t':
68 break;
69 default:
70 name += c;
71 break;
72 }
73 return 1;
74 }
75
82 void setCallback(void (*cb)(SdFatFileInfo&, void* ref), void* ref = nullptr) {
83 this->ref = ref;
84 this->cb = cb;
85 }
86
87 protected:
89 std::string name;
91 void (*cb)(SdFatFileInfo&, void* ref) = nullptr;
93 void* ref = nullptr;
96
102 for (int j = 0; j < static_cast<int>(name.size()); j++) {
103 if (name[j] != ' ') return j;
104 }
105 return 0;
106 }
107
115 void parse() {
116 int spaces = spaceCount();
117 info.level = spaces / 2;
118 trim(name);
119 info.name = name;
120 // directories have a trailing /
122 !info.name.empty() && info.name[info.name.size() - 1] == '/';
123 // if it is a directory remove trailing /
124 if (info.is_directory) {
125 info.name.pop_back();
126 }
127 if (cb) cb(info, ref);
128 name.clear();
129 }
130
131 inline void rtrim(std::string& s) {
132 s.erase(std::find_if(s.rbegin(), s.rend(),
133 [](unsigned char ch) { return !std::isspace(ch); })
134 .base(),
135 s.end());
136 }
137
138 // Trim from the start (in place)
139 inline void ltrim(std::string& s) {
140 s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
141 return !std::isspace(ch);
142 }));
143 }
144
145 // Trim from both ends (in place)
146 inline void trim(std::string& s) {
147 rtrim(s);
148 ltrim(s);
149 }
150};
151
152} // namespace tiny_dlna
Simple character-by-character parser that emits SdFatFileInfo via a callback.
Definition: SdFatParser.h:45
SdFatParser()
Constructs the parser and reserves internal buffer capacity.
Definition: SdFatParser.h:50
int spaceCount()
Counts leading spaces in the current line buffer.
Definition: SdFatParser.h:101
void ltrim(std::string &s)
Definition: SdFatParser.h:139
std::string name
Definition: SdFatParser.h:89
void trim(std::string &s)
Definition: SdFatParser.h:146
void setCallback(void(*cb)(SdFatFileInfo &, void *ref), void *ref=nullptr)
Sets the callback that receives parsed entries.
Definition: SdFatParser.h:82
void parse()
Parses the accumulated line and emits a callback if set.
Definition: SdFatParser.h:115
SdFatFileInfo info
Definition: SdFatParser.h:95
void rtrim(std::string &s)
Definition: SdFatParser.h:131
void(* cb)(SdFatFileInfo &, void *ref)
Definition: SdFatParser.h:91
void * ref
Definition: SdFatParser.h:93
size_t write(uint8_t c) override
Consumes one character of input.
Definition: SdFatParser.h:62
Definition: Allocator.h:13
Describes an entry from a parsed SdFat directory listing.
Definition: SdFatParser.h:21
bool is_directory
Definition: SdFatParser.h:26
int level
Definition: SdFatParser.h:29
std::string name
Definition: SdFatParser.h:24