Skip to content

Commit

Permalink
Add conditional parsing of PDO strings
Browse files Browse the repository at this point in the history
If the resulting SII file does not contain the PDO mapping we also don't
need to read the PDO strings.

Fixes: Clean up string section https://www.wrike.com/open.htm?id=949291074
  • Loading branch information
fjes committed Mar 31, 2023
1 parent 94c2b01 commit ac38c8c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
21 changes: 13 additions & 8 deletions esi.c
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ static int parse_pdo_get_data_type(char *xmldatatype)
return -1; /* unrecognized */
}

static struct _pdo_entry *parse_pdo_entry(xmlNode *val, SiiInfo *sii)
static struct _pdo_entry *parse_pdo_entry(xmlNode *val, SiiInfo *sii, int include_pdo_strings)
{
struct _pdo_entry *entry = calloc(1, sizeof(struct _pdo_entry));

Expand All @@ -777,7 +777,10 @@ static struct _pdo_entry *parse_pdo_entry(xmlNode *val, SiiInfo *sii)
fprintf(stderr, "[WARNING] Reading child content of size 0 (line: %d)\n", child->line);
tmp = -1;
} else {
tmp = sii_strings_add(sii, (char *)child->children->content);
if (include_pdo_strings)
tmp = sii_strings_add(sii, (char *)child->children->content);
else
tmp = 0;
}

if (tmp < 0) {
Expand Down Expand Up @@ -808,7 +811,7 @@ static struct _pdo_entry *parse_pdo_entry(xmlNode *val, SiiInfo *sii)
return entry;
}

static void parse_pdo(xmlNode *current, SiiInfo *sii)
static void parse_pdo(xmlNode *current, SiiInfo *sii, int include_pdo_strings)
{
enum eSection type;
if (xmlStrcmp(current->name, Char2xmlChar("RxPdo")) == 0)
Expand Down Expand Up @@ -857,7 +860,9 @@ static void parse_pdo(xmlNode *current, SiiInfo *sii)
/* then parse the pdo list - all <Entry> children */
for (xmlNode *val = current->children; val; val = val->next) {
if (xmlStrncmp(val->name, Char2xmlChar("Name"), xmlStrlen(val->name)) == 0) {
int tmp = sii_strings_add(sii, (char *)val->children->content);
int tmp = 0;
if (include_pdo_strings)
tmp = sii_strings_add(sii, (char *)val->children->content);
if (tmp < 0) {
fprintf(stderr, "Error creating input string!\n");
pdo->name_index = 0;
Expand All @@ -871,7 +876,7 @@ static void parse_pdo(xmlNode *current, SiiInfo *sii)
} else if (xmlStrncmp(val->name, Char2xmlChar("Entry"), xmlStrlen(val->name)) == 0) {
/* add new pdo entry */
pdo->entries += 1;
struct _pdo_entry *entry = parse_pdo_entry(val, sii);
struct _pdo_entry *entry = parse_pdo_entry(val, sii, include_pdo_strings);
pdo_entry_add(pdo, entry);
pdosize += 8; /* size of every pdo entry */
}
Expand Down Expand Up @@ -996,7 +1001,7 @@ void esi_release(struct _esi_data *esi)
free(esi);
}

int esi_parse(EsiData *esi, int device_number)
int esi_parse(EsiData *esi, int device_number, int include_pdo_strings)
{
xmlNode *root = xmlDocGetRootElement(esi->doc);

Expand Down Expand Up @@ -1038,9 +1043,9 @@ int esi_parse(EsiData *esi, int device_number)
parse_dclock(current, esi->sii);
#endif
} else if (xmlStrncmp(current->name, Char2xmlChar("RxPdo"), xmlStrlen(current->name)) == 0) {
parse_pdo(current, esi->sii);
parse_pdo(current, esi->sii, include_pdo_strings);
} else if (xmlStrncmp(current->name, Char2xmlChar("TxPdo"), xmlStrlen(current->name)) == 0) {
parse_pdo(current, esi->sii);
parse_pdo(current, esi->sii, include_pdo_strings);
}
}

Expand Down
2 changes: 1 addition & 1 deletion esi.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void esi_release(EsiData *esi);
void esi_print_xml(EsiData *esi);
void esi_print_sii(EsiData *esi);

int esi_parse(EsiData *esi, int device_number);
int esi_parse(EsiData *esi, int device_number, int include_pdo_strings);

SiiInfo *esi_get_sii(EsiData *esi);
#endif /* ESI_H */
3 changes: 2 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ static int parse_xml_input(const unsigned char *buffer, size_t length, unsigned
EsiData *esi = esi_init_string(buffer, length);
//esi_print_xml(esi);

if (esi_parse(esi, device)) {
int include_pdo_strings = g_add_pdo_mapping || g_print_content;
if (esi_parse(esi, device, include_pdo_strings)) {
fprintf(stderr, "Error something went wrong in XML parsing\n");
esi_release(esi);
return -1;
Expand Down

0 comments on commit ac38c8c

Please sign in to comment.