0.3.3 XMLWriter

This commit is contained in:
rob tillaart 2022-11-27 12:54:57 +01:00
parent 685c941af1
commit 9cbf8c5591
8 changed files with 191 additions and 102 deletions

View File

@ -1,3 +1,18 @@
platforms:
rpipico:
board: rp2040:rp2040:rpipico
package: rp2040:rp2040
gcc:
features:
defines:
- ARDUINO_ARCH_RP2040
warnings:
flags:
packages:
rp2040:rp2040:
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
@ -7,8 +22,10 @@ compile:
- leonardo
- m4
- esp32
# - esp8266
# - mega2560
# - esp8266 # causes analog pins to fail
- mega2560
- rpipico
libraries:
- "Ethernet"
- "SD"

View File

@ -0,0 +1,89 @@
# Change Log XMLWriter
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.3.3] - 2022-11-27
- Add RP2040 support to build-CI.
- Add CHANGELOG.md
- update readme.md
## [0.3.2] - 2021-12-29
- update library.json
- update readme.md
- update license
- update unit test
- minor edits
## [0.3.1] - 2021-11-11
- refactor naming to improve readability
- update build-CI,
- update readme.md, Badges.
## [0.3.0] - 2021-01-09
- Arduino-ci + unit tests
- add getIndentSize()
- add version()
- add debug()
----
## [0.2.4] - 2020-07-07
- fix #6 Print interface made public
## [0.2.3] - 2020-06-19
- fix library.json
## [0.2.2] - 2020-04-29
= dynamic buffer size in constructor
## [0.2.1] - 2020-04-26
- performance optimized
- add setconfig()
- add newLine()
## [0.2.0] - 2020-04-24
- refactor
- add examples
- add #pragma
- add print as base class
----
## [0.1.9] - 2017-12-09
- add PROGMEM support for escape() strings
## [0.1.8] - 2017-12-09
- fix casting issue #83 (long -> int32_t);
## [0.1.7] - 2017-07-26
- add const where possible
## [0.1.6] - 2016-03-16
- added incrIndent(), decrIndent()
- add indent()
- add raw()
## [0.1.05] - 2015-05-23
- added XMLWRITER_MAXTAGSIZE 15 (to support KML coordinates tag)
## [0.1.04] - 2015-05-21
- refactored - reduce RAM -> used F() macro etc.
## [0.1.03] - 2015-03-07
- refactored - footprint + interface
## [0.1.02] - 2013-11-07
- add setIndentSize()
= corrected history
- add escape support
## [0.1.01] - 2013-11-07
- rework interfaces
## [0.1.00] - 2013-11-06
- initial version

View File

@ -28,11 +28,11 @@ and choose one that is appropriate.
Indicative sizes based upon the examples.
Run your tests to find your application optimum.
| STREAM | SIZE |
|:---------|:-----------|
| Ethernet | 20-30 |
| Serial | 5 |
| SD File | 10-16 |
| STREAM | SIZE |
|:-----------|:-----------|
| Ethernet | 20-30 |
| Serial | 5 |
| SD File | 10-16 |
**IMPORTANT:** When using buffering you should always call **XML.flush()**
at the end of the XML generation. This will flush the last bytes in the internal buffer into the output stream.
@ -130,12 +130,12 @@ can inject strings.
## Configuration flags
| Flag | Value | Description |
|:-----------------|:------|:------------------|
|XMLWRITER_NONE | 0x00 | minimize output, smaller & faster |
|XMLWRITER_COMMENT | 0x01 | allow comments |
|XMLWRITER_INDENT | 0x02 | allow indentation |
|XMLWRITER_NEWLINE | 0x04 | allow newlines |
| Flag | Value | Description |
|:--------------------|:--------|:--------------------|
| XMLWRITER_NONE | 0x00 | minimize output, smaller & faster |
| XMLWRITER_COMMENT | 0x01 | allow comments |
| XMLWRITER_INDENT | 0x02 | allow indentation |
| XMLWRITER_NEWLINE | 0x04 | allow newlines |
- **setConfig(XMLWRITER_NONE);** to minimize the output in bytes.
@ -151,7 +151,12 @@ See examples
## Future
#### must
- update documentation
-
#### should
#### could
- move code to .cpp

View File

@ -1,32 +1,9 @@
//
// FILE: XMLWriter.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.3.2
// VERSION: 0.3.3
// DATE: 2013-11-06
// PURPOSE: Arduino library for creating XML
//
// HISTORY:
// 0.1.00 2013-11-06 initial version
// 0.1.01 2013-11-07 rework interfaces
// 0.1.02 2013-11-07 +setIndentSize(), corrected history, +escape support
// 0.1.03 2015-03-07 refactored - footprint + interface
// 0.1.04 2015-05-21 refactored - reduce RAM -> used F() macro etc.
// 0.1.05 2015-05-23 added XMLWRITER_MAXTAGSIZE 15 (to support KML coordinates tag)
// 0.1.6 2016-03-16 added incrIndent(), decrIndent(), indent(), raw();
// 0.1.7 2017-07-26 added const where possible
// 0.1.8 2017-12-09 fix casting issue #83 (long -> int32_t);
// 0.1.9 2017-12-09 add PROGMEM support for escape() strings
// 0.2.0 2020-04-24 refactor, added examples, #pragma, print as base class
// 0.2.1 2020-04-26 performance optimized, setconfig() + newLine() added
// 0.2.2 2020-04-29 dynamic buffer size in constructor
// 0.2.3 2020-06-19 fix library.json
// 0.2.4 2020-07-07 fix #6 Print interface made public
// 0.3.0 2021-01-09 Arduino-ci + unit tests
// add getIndentSize(); version(); debug();
// 0.3.1 2021-11-11 refactor naming to improve readability
// update build-CI,
// update readme.md, Badges.
// 0.3.2 2021-12-29 update library.json, readme, license, unit test, minor edits
// PURPOSE: Arduino library for creating XML
#include "XMLWriter.h"
@ -202,7 +179,7 @@ void XMLWriter::writeNode(const char* tag, const char* str)
///////////////////////////////////////////////////////////////
//
// TAGFIELD
// TAGFIELD
//
void XMLWriter::tagField(const char *field, const uint8_t value, const uint8_t base)
{
@ -258,7 +235,7 @@ void XMLWriter::tagField(const char *field, const bool value)
{
print(' ');
print(field);
// F() is slower & uses less RAM but 15 bytes saved
// F() is slower & uses less RAM but 15 bytes saved
print(value ? F("=\"true\"") : F("=\"false\""));
}
@ -275,7 +252,7 @@ void XMLWriter::tagField(const char *field, const double value, const uint8_t de
///////////////////////////////////////////////////////////////
//
// WRITENODE
// WRITENODE
//
void XMLWriter::writeNode(const char* tag, const uint8_t value, const uint8_t base)
{
@ -326,7 +303,7 @@ void XMLWriter::writeNode(const char* tag, const int32_t value, const uint8_t ba
void XMLWriter::writeNode(const char* tag, const bool value)
{
tagOpen(tag, "", NONEWLINE);
// F() is slower & uses less RAM but saves 9 bytes
// F() is slower & uses less RAM but saves 9 bytes
print(value ? F("true") : F("false"));
tagClose(NOINDENT);
}
@ -344,8 +321,8 @@ void XMLWriter::indent()
{
if (_config & XMLWRITER_INDENT)
{
// as indentation is a multiple of 2
// this is nice balance between speed and RAM.
// as indentation is a multiple of 2
// this is nice balance between speed and RAM.
for (uint8_t i = _indent; i > 0; i-= 2) print(" ");
}
}
@ -365,8 +342,8 @@ void XMLWriter::flush()
if (_bufferIndex > 0)
{
_buffer[_bufferIndex] = 0;
_stream->write(_buffer, _bufferIndex); // saves ~40 bytes on UNO.
// _stream->print(_buffer);
_stream->write(_buffer, _bufferIndex); // saves ~40 bytes on UNO.
// _stream->print(_buffer);
_bufferIndex = 0;
}
};
@ -374,7 +351,7 @@ void XMLWriter::flush()
////////////////////////////////////////////////////////////////////
//
// ESCAPE
// ESCAPE
//
#ifdef XMLWRITER_ESCAPE_SUPPORT
@ -393,7 +370,7 @@ PROGMEM const char* const expanded[] =
};
#else
// NOTE: & and ; are handled in code. // 25 bytes RAM
// NOTE: & and ; are handled in code. // 25 bytes RAM
static char expanded[][5] = { "quot", "apos","lt","gt","amp"};
#endif
@ -406,7 +383,7 @@ void XMLWriter::escape(const char* str)
char* q = strchr(c, *p);
if (q == NULL) print(*p);
#ifdef __PROGMEM__
else
else
{
char buf[8];
strcpy_P(buf, (char*)pgm_read_word(&(expanded[q - c])));
@ -416,7 +393,7 @@ void XMLWriter::escape(const char* str)
else
{
print('&');
print(expanded[q - c]); // uint8_t idx = q-c;
print(expanded[q - c]); // uint8_t idx = q-c;
print(';');
}
#endif
@ -426,5 +403,5 @@ void XMLWriter::escape(const char* str)
#endif
// -- END OF FILE --
// -- END OF FILE --

View File

@ -2,111 +2,110 @@
//
// FILE: XMLWriter.h
// AUTHOR: Rob Tillaart
// VERSION: 0.3.2
// VERSION: 0.3.3
// DATE: 2013-11-06
// PURPOSE: Arduino library for creating XML
//
// PURPOSE: Arduino library for creating XML
#include "Arduino.h"
#define XMLWRITER_VERSION (F("0.3.2"))
#define XMLWRITER_VERSION (F("0.3.3"))
// for comment()
// for comment()
#define NOMULTILINE false
#define MULTILINE true
// for tagOpen(), tagEnd()
// for tagOpen(), tagEnd()
#define NEWLINE true
#define NONEWLINE false
#define NOINDENT false // for tagClose()
#define NOINDENT false // for tagClose()
// for tagEnd()
// for tagEnd()
#define SLASH true
#define NOSLASH false
// deepness of XML tree 5..10
// needed for stack of tagStack
// deepness of XML tree 5..10
// needed for stack of tagStack
#ifndef XMLWRITER_MAXLEVEL
#define XMLWRITER_MAXLEVEL 5 // adjust for deeper nested structures
#define XMLWRITER_MAXLEVEL 5 // adjust for deeper nested structures
#endif
#ifndef XMLWRITER_MAXTAGSIZE
#define XMLWRITER_MAXTAGSIZE 15 // adjust for longer fields - !! eats memory !!
#define XMLWRITER_MAXTAGSIZE 15 // adjust for longer fields - !! eats memory !!
#endif
// reduce footprint by commenting next line
// reduce footprint by commenting next line
#define XMLWRITER_ESCAPE_SUPPORT
// configuration - setConfig
// configuration - setConfig
#define XMLWRITER_NONE 0x00
#define XMLWRITER_COMMENT 0x01
#define XMLWRITER_INDENT 0x02
#define XMLWRITER_NEWLINE 0x04
// uncomment next line to reduce ~30bytes RAM in escape() (AVR oonly)
// #define __PROGMEM__
// uncomment next line to reduce ~30bytes RAM in escape() (AVR oonly)
// #define __PROGMEM__
class XMLWriter : public Print
{
public:
// default = Serial
// default = Serial
XMLWriter(Print* stream = &Serial, uint8_t bufferSize = 10);
~XMLWriter();
void reset();
// to show/strip comment, indent, newLine
// to minimize the output setConfig(0);
// to show/strip comment, indent, newLine
// to minimize the output setConfig(0);
void setConfig(uint8_t config) { _config = config; };
// standard XML header
// standard XML header
void header();
// prints XMLWRITER_VERSION as comment
// prints XMLWRITER_VERSION as comment
void version();
// prints debug information into the XML as comment
// prints debug information into the XML as comment
void debug();
// if multiline == true it does not indent to allow bigger text blocks
// <!-- text -->
// if multiline == true it does not indent to allow bigger text blocks
// <!-- text -->
void comment(const char* text, const bool multiLine = false);
// add a number of newlines to the output, default = 1.
// add a number of newlines to the output, default = 1.
void newLine(uint8_t n = 1);
// TAG
// TAG
//
// <tag>
// <tag>
void tagOpen(const char* tag, const bool newline = true);
// <tag name="name">
// <tag name="name">
void tagOpen(const char* tag, const char* name, const bool newline = true);
// </tag>
// </tag>
void tagClose(const bool ind = true);
// <tag
// <tag
void tagStart(const char* tag);
// field="value"
// field="value"
void tagField(const char* field, const char* value);
// />
// />
void tagEnd(const bool newline = true, const bool addSlash = true);
// <tag>value</tag>
// <tag>value</tag>
void writeNode(const char* tag, const char* value);
// INDENT
// INDENT
//
// typically 0,2,4; default == 2;
// multiple of 2;
// typically 0,2,4; default == 2;
// multiple of 2;
void setIndentSize(const uint8_t size = 2) { _indentStep = size; };
uint8_t getIndentSize() { return _indentStep; };
// for manual layout control
// for manual layout control
void incrIndent() { _indent += _indentStep; };
void decrIndent() { _indent -= _indentStep; };
void indent();
@ -140,39 +139,39 @@ public:
#ifdef XMLWRITER_ESCAPE_SUPPORT
// expands the special xml chars
// expands the special xml chars
void escape(const char* str);
#endif
// One need to call flush() at the end of writing to empty the internal buffer.
// Note: this is overridden of the Print interface
// One need to call flush() at the end of writing to empty the internal buffer.
// Note: this is overridden of the Print interface
void flush();
// metrics
// metrics
uint8_t bufferIndex() { return _bufferIndex; };
uint32_t bytesWritten() { return _bytesOut; };
private:
// output stream, Print Class
// output stream, Print Class
Print* _stream;
size_t write(uint8_t c);
// for indentation
// for indentation
uint8_t _indent;
uint8_t _indentStep;
// configuration
// configuration
uint8_t _config;
// stack - used to remember the current tagname to create
// automatic the right close tag.
// stack - used to remember the current tagname to create
// automatic the right close tag.
uint8_t _tagIndex;
char _tagStack[XMLWRITER_MAXLEVEL][XMLWRITER_MAXTAGSIZE + 1];
// output admin
// output admin
char * _buffer;
uint8_t _bufferSize;
uint8_t _bufferIndex;
@ -180,5 +179,5 @@ private:
};
// -- END OF FILE --
// -- END OF FILE --

View File

@ -46,8 +46,10 @@ MULTILINE LITERAL1
NEWLINE LITERAL1
NONEWLINE LITERAL1
NOINDENT LITERAL1
SLASH LITERAL1
NOSLASH LITERAL1
XMLWRITER_NONE LITERAL1
XMLWRITER_COMMENT LITERAL1
XMLWRITER_INDENT LITERAL1

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/XMLWriter"
},
"version": "0.3.2",
"version": "0.3.3",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",

View File

@ -1,5 +1,5 @@
name=XMLWriter
version=0.3.2
version=0.3.3
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for creating XML