Arduino DLNA Server
Loading...
Searching...
No Matches
XMLAttributeParser.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstring>
4#include "basic/Str.h"
5
6namespace tiny_dlna {
7
18 public:
41 static bool extractAttribute(const char* xml, const char* tagName,
42 const char* attrName, char* outBuf,
43 size_t bufSize) {
44 if (!xml || !tagName || !attrName || !outBuf || bufSize == 0) return false;
45 const char* p = strstr(xml, tagName);
46 while (p) {
47 const char* tagEnd = strchr(p, '>');
48 if (!tagEnd) return false;
49 const char* attrPos = strstr(p, attrName);
50 if (attrPos && attrPos < tagEnd) {
51 const char* q = strchr(attrPos, '"');
52 if (!q || q > tagEnd) return false;
53 q++;
54 const char* qend = strchr(q, '"');
55 if (!qend || qend > tagEnd) return false;
56 size_t vlen = (size_t)(qend - q);
57 size_t copyLen = vlen < (bufSize - 1) ? vlen : (bufSize - 1);
58 if (copyLen > 0) memcpy(outBuf, q, copyLen);
59 outBuf[copyLen] = '\0';
60 return true;
61 }
62 p = strstr(p + 1, tagName);
63 }
64 return false;
65 }
66
94 static bool extractAttributeToken(const char* xml, const char* tagName,
95 const char* attrName, int n,
96 char* outBuf, size_t bufSize) {
97 if (!xml || !tagName || !attrName || n <= 0 || !outBuf || bufSize == 0) return false;
98 char proto[256] = {0};
99 bool ok = extractAttribute(xml, tagName, attrName, proto, sizeof(proto));
100 if (!ok || proto[0] == '\0') return false;
101 // proto format: token1:token2:token3:token4
102 int col = 1;
103 const char* tokenStart = proto;
104 if (n > 1) {
105 for (size_t i = 0; proto[i] != '\0'; ++i) {
106 if (proto[i] == ':') {
107 ++col;
108 if (col == n) {
109 tokenStart = proto + i + 1;
110 break;
111 }
112 }
113 }
114 }
115 if (!tokenStart || *tokenStart == '\0') return false;
116 // copy token into outBuf until ':' or end
117 size_t j = 0;
118 for (; tokenStart[j] != '\0' && tokenStart[j] != ':' && j + 1 < bufSize; ++j) outBuf[j] = tokenStart[j];
119 outBuf[j] = '\0';
120 return outBuf[0] != '\0';
121 }
122};
123
124} // namespace tiny_dlna
Small utility to extract attributes from a start-tag in an XML fragment.
Definition: XMLAttributeParser.h:17
static bool extractAttributeToken(const char *xml, const char *tagName, const char *attrName, int n, char *outBuf, size_t bufSize)
Extract the nth (1-based) colon-separated token from an attribute value located on the first occurren...
Definition: XMLAttributeParser.h:94
static bool extractAttribute(const char *xml, const char *tagName, const char *attrName, char *outBuf, size_t bufSize)
Find the first occurrence of a start-tag and extract an attribute value.
Definition: XMLAttributeParser.h:41
Definition: Allocator.h:6