Files
edgetx-gx12/radio/src/storage/yaml/yaml_parser.h
T
philmozandGitHub 92651429a2 feat(color): Use generic YAML parser to save/load Theme yaml files (#3346)
* Change ThemeFile class to use generic YAML parser instead of hard wired code.
Fix handling of empty input lines in YAML parser.
Increase size of YAML parser variables to handle theme 'info' property length.

* Update comment.

* Fix error.

* Proper fix for blank lines in YAML file.

* Fix YAML blank line handling (attempt 3).

* Ensure theme YAML file is backward compatible.

* Store selected theme in radio settings instead of selectedtheme.txt file.
On start, if radio settings theme is empty, try and load from selectedtheme.txt file.
In Companion, save/load the selected theme in the radio settings YAML file; but no UI to select theme.

* Update selected theme name in radio.yml if the theme name was edited.

* Only save 'selectedTheme' in Companion on color LCD radios.
2023-04-09 13:32:30 +10:00

107 lines
2.3 KiB
C++

/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#ifndef _yaml_parser_h_
#define _yaml_parser_h_
#include <stdint.h>
#define MAX_STR 256
#define MAX_DEPTH 16 // 12 real + 4 virtual
struct YamlParserCalls
{
bool (*to_parent) (void* ctx);
bool (*to_child) (void* ctx);
bool (*to_next_elmt) (void* ctx);
bool (*find_node) (void* ctx, char* buf, uint8_t len);
void (*set_attr) (void* ctx, char* buf, uint16_t len);
};
class YamlParser
{
enum ParserState {
ps_Indent=0,
ps_Dash,
ps_Attr,
ps_AttrQuo,
ps_AttrSP,
ps_Sep,
ps_Val,
ps_ValEsc,
ps_ValQuo,
ps_ValEsc1,
ps_ValEsc2,
ps_ValEsc3,
ps_CRLF
};
// last indents for each level
uint8_t indents[MAX_DEPTH];
// current indent
uint8_t indent;
// current level
uint8_t level;
// parser state
uint8_t state;
uint8_t saved_state;
// scratch buffer w/ 16 bytes
// used for attribute and values
char scratch_buf[MAX_STR];
uint16_t scratch_len;
bool node_found;
bool eof;
// tree iterator state
const YamlParserCalls* calls;
void* ctx;
// Reset parser state for next line
void reset();
bool toChild();
bool toParent();
uint8_t getLastIndent();
public:
enum YamlResult {
DONE_PARSING,
CONTINUE_PARSING,
STRING_OVERFLOW
};
YamlParser();
void init(const YamlParserCalls* parser_calls, void* parser_ctx);
YamlResult parse(const char* buffer, unsigned int size);
void set_eof() { eof = true; }
};
#endif