From e060848bef2a5781e09c3bd108c9e27e9d01b498 Mon Sep 17 00:00:00 2001 From: Anonymus Raccoon Date: Sat, 11 Jan 2020 17:53:14 +0100 Subject: [PATCH] Helping the user to free --- include/xml.h | 5 +++++ src/strangeget.c | 11 +++++++++-- src/xmlget.c | 19 +++++++++++++++---- src/xmlparser.c | 17 +++++++++++++++-- 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/include/xml.h b/include/xml.h index c0f3a39..389ddb3 100644 --- a/include/xml.h +++ b/include/xml.h @@ -10,6 +10,9 @@ typedef struct dictionary dictionary; #pragma once +#include +#include + struct dictionary { char *key; @@ -29,7 +32,9 @@ struct node node *xml_parse(const char *path); node *xml_getnode(node *parent, const char *name); +bool xml_hasproperty(node *n, const char *key); char *xml_getproperty(node *n, const char *key); +char *xml_gettempprop(node *n, const char *key); int xml_getintprop(node *n, const char *key); int xml_getbinaprop(node *n, const char *key); int xml_gethexaprop(node *n, const char *key); diff --git a/src/strangeget.c b/src/strangeget.c index 7014a6a..0e02eb6 100644 --- a/src/strangeget.c +++ b/src/strangeget.c @@ -6,11 +6,13 @@ */ #include "xml.h" +#include "xml_internal.h" #include "my.h" +#include int xml_gethexaprop(node *n, const char *key) { - char *prop = xml_getproperty(n, key); + char *prop = xml_gettempprop(n, key); if (!prop || my_strlen(prop) == 0) return (0); @@ -24,11 +26,16 @@ int xml_gethexaprop(node *n, const char *key) int xml_getbinaprop(node *n, const char *key) { - char *prop = xml_getproperty(n, key); + char *prop = xml_gettempprop(n, key); if (!prop || my_strlen(prop) == 0) return (0); if (prop[0] == '0' && prop[1] == 'b') prop += 2; return (my_getnbr_base(prop, "01")); +} + +bool xml_hasproperty(node *n, const char *key) +{ + return (xml_gettempprop(n, key) != NULL); } \ No newline at end of file diff --git a/src/xmlget.c b/src/xmlget.c index 381ef59..62b7bd5 100644 --- a/src/xmlget.c +++ b/src/xmlget.c @@ -11,18 +11,29 @@ #include "math.h" #include -char *xml_getproperty(node *n, const char *key) +char *xml_gettempprop(node *n, const char *key) { + if (!n) + return (NULL); for (dictionary *prop = n->properties; prop; prop = prop->next) { if (!my_strcmp(key, prop->key)) - return (my_strdup(prop->value)); + return (prop->value); } return (NULL); } +char *xml_getproperty(node *n, const char *key) +{ + char *prop = xml_gettempprop(n, key); + + if (prop) + return (my_strdup(prop)); + return (NULL); +} + int xml_getintprop(node *n, const char *key) { - char *prop = xml_getproperty(n, key); + char *prop = xml_gettempprop(n, key); if (!prop || my_strlen(prop) == 0) return (0); @@ -31,7 +42,7 @@ int xml_getintprop(node *n, const char *key) float xml_getfloatprop(node *n, const char *key) { - char *prop = xml_getproperty(n, key); + char *prop = xml_gettempprop(n, key); float nbr; int deci; diff --git a/src/xmlparser.c b/src/xmlparser.c index d1af3f8..5bf3c62 100644 --- a/src/xmlparser.c +++ b/src/xmlparser.c @@ -29,11 +29,24 @@ int xml_handle_prolog(char **nodestr) return (0); } +bool is_charvalid(char c) +{ + if (c == ' ' || c == '\t' || c == '\n' || c == '\r') + return (false); + if (c == '"' || c == '\'') + return (false); + if (c == '=') + return (false); + if (c == '>' || c == '<' || c == '/') + return (false); + return (true); +} + bool is_space_usefull(char *nodestr, int i) { - if (i == 0 || (!is_alphanum(nodestr[i - 1]) && nodestr[i - 1] != '"')) + if (i == 0 || (!is_charvalid(nodestr[i - 1]) && nodestr[i - 1] != '"')) return (false); - if (i + 1 == my_strlen(nodestr) || !is_alphanum(nodestr[i + 1])) + if (i + 1 == my_strlen(nodestr) || !is_charvalid(nodestr[i + 1])) return (false); return (true); }