Use double quotes around switches to avoid escaping with backslash

Escaping with backslash arbitrary characters seems to be an issue with most YAML parsers.
This commit is contained in:
Raphael Coeffic
2021-12-31 10:52:20 +01:00
parent 2244e369ed
commit 20961e460b
2 changed files with 30 additions and 11 deletions
@@ -458,11 +458,12 @@ extern const struct YamlIdStr enum_SwitchSources[];
#define r_swtchSrc nullptr
bool w_swtchSrc(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque)
static bool w_swtchSrc_unquoted(const YamlNode* node, uint32_t val,
yaml_writer_func wf, void* opaque)
{
int32_t sval = yaml_to_signed(val, node->size);
if (sval < 0) {
wf(opaque, "\\!", 2);
wf(opaque, "!", 1);
sval = abs(sval);
}
@@ -509,6 +510,15 @@ bool w_swtchSrc(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* o
return wf(opaque, str, strlen(str));
}
static bool w_swtchSrc(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque)
{
if (!wf(opaque,"\"",1)
|| !w_swtchSrc_unquoted(node, val, wf, opaque)
|| !wf(opaque,"\"",1))
return false;
return true;
}
bool cfn_is_active(void* user, uint8_t* data, uint32_t bitoffs)
{
data += bitoffs >> 3UL;
@@ -996,13 +1006,13 @@ bool w_logicSw(void* user, uint8_t* data, uint32_t bitoffs,
case LS_FAMILY_BOOL:
case LS_FAMILY_STICKY:
if (!w_swtchSrc(&_ls_node_v1, ls->v1, wf, opaque)) return false;
if (!w_swtchSrc_unquoted(&_ls_node_v1, ls->v1, wf, opaque)) return false;
if (!wf(opaque,",",1)) return false;
if (!w_swtchSrc(&_ls_node_v2, ls->v2, wf, opaque)) return false;
if (!w_swtchSrc_unquoted(&_ls_node_v2, ls->v2, wf, opaque)) return false;
break;
case LS_FAMILY_EDGE:
if (!w_swtchSrc(&_ls_node_v1, ls->v1, wf, opaque)) return false;
if (!w_swtchSrc_unquoted(&_ls_node_v1, ls->v1, wf, opaque)) return false;
if (!wf(opaque,",",1)) return false;
str = yaml_unsigned2str(lswTimerValue(ls->v2));
if (!wf(opaque,str,strlen(str))) return false;
@@ -44,7 +44,6 @@ namespace yaml_conv_220 {
uint8_t select_sensor_cfg(void* user, uint8_t* data, uint32_t bitoffs);
extern const struct YamlIdStr enum_SwitchConfig[];
bool w_swtchSrc(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque);
bool cfn_is_active(void* user, uint8_t* data, uint32_t bitoffs);
bool gvar_is_active(void* user, uint8_t* data, uint32_t bitoffs);
@@ -757,11 +756,12 @@ static uint32_t r_swtchSrc(const YamlNode* node, const char* val, uint8_t val_le
return neg ? -ival : ival;
}
static bool w_swtchSrc(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque)
static bool w_swtchSrc_unquoted(const YamlNode* node, uint32_t val,
yaml_writer_func wf, void* opaque)
{
int32_t sval = yaml_to_signed(val, node->size);
if (sval < 0) {
wf(opaque, "\\!", 2);
wf(opaque, "!", 1);
sval = abs(sval);
}
@@ -808,6 +808,15 @@ static bool w_swtchSrc(const YamlNode* node, uint32_t val, yaml_writer_func wf,
return wf(opaque, str, strlen(str));
}
bool w_swtchSrc(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque)
{
if (!wf(opaque,"\"",1)
|| !w_swtchSrc_unquoted(node, val, wf, opaque)
|| !wf(opaque,"\"",1))
return false;
return true;
}
bool cfn_is_active(void* user, uint8_t* data, uint32_t bitoffs)
{
return yaml_conv_220::cfn_is_active(user, data, bitoffs);
@@ -1541,13 +1550,13 @@ static bool w_logicSw(void* user, uint8_t* data, uint32_t bitoffs,
case LS_FAMILY_BOOL:
case LS_FAMILY_STICKY:
if (!w_swtchSrc(&_ls_node_v1, ls->v1, wf, opaque)) return false;
if (!w_swtchSrc_unquoted(&_ls_node_v1, ls->v1, wf, opaque)) return false;
if (!wf(opaque,",",1)) return false;
if (!w_swtchSrc(&_ls_node_v2, ls->v2, wf, opaque)) return false;
if (!w_swtchSrc_unquoted(&_ls_node_v2, ls->v2, wf, opaque)) return false;
break;
case LS_FAMILY_EDGE:
if (!w_swtchSrc(&_ls_node_v1, ls->v1, wf, opaque)) return false;
if (!w_swtchSrc_unquoted(&_ls_node_v1, ls->v1, wf, opaque)) return false;
if (!wf(opaque,",",1)) return false;
str = yaml_unsigned2str(lswTimerValue(ls->v2));
if (!wf(opaque,str,strlen(str))) return false;