From 49aa1ca861d49c5c25d1896482d15e7a962e92b8 Mon Sep 17 00:00:00 2001 From: Tristan Roux Date: Fri, 6 Dec 2019 12:02:27 +0100 Subject: [PATCH] Adding a formater (I think) --- README | 4 +++ src/xmlparser.c | 57 ++++++++++++++++++++++++++++++++++++++----- tests/tests_realxml.c | 0 tests/xmltest.txt | 1 + 4 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 README create mode 100644 tests/tests_realxml.c diff --git a/README b/README new file mode 100644 index 0000000..b479dfe --- /dev/null +++ b/README @@ -0,0 +1,4 @@ +Unsupported: + namespaces + attributes with semi quotes (') instead of quoetes (") + comments \ No newline at end of file diff --git a/src/xmlparser.c b/src/xmlparser.c index cac1be5..25d8743 100644 --- a/src/xmlparser.c +++ b/src/xmlparser.c @@ -5,11 +5,60 @@ ** xmlparser */ +#include "my.h" #include "xml.h" #include #include #include #include +#include + +bool is_space_usefull(char *nodestr, int i) +{ + if (i == 0 || !is_alphanum(nodestr[i - 1])) + return (false); + if (i + 1 == my_strlen(nodestr) || !is_alphanum(i + 1)) + return (false); + return (true); +} + +int xml_handle_prolog(char **nodestr) +{ + char *strconst = *nodestr; + + if (my_strstr(*nodestr, ""); + if (!*nodestr) { + free(strconst); + return (-1); + } + return (0); +} + +node *xml_parsestr(char *nodestr) +{ + node *n; + char *strconst = nodestr; + + if (xml_handle_prolog(&nodestr) < 0) + return (NULL); + for (int i = 0; nodestr[i]; i++) { + if (nodestr[i] == ' ') { + if (!is_space_usefull(nodestr, i)) + nodestr[i] = '\t'; + } + } + for (int i = 0; nodestr[i]; i++) { + if (nodestr[i] == '\t' || nodestr[i] == '\n') { + nodestr[i] = nodestr[i + 1]; + nodestr[i + 1] = '\t'; + i = 0; + } + } + n = xml_parsenode(&nodestr); + free(strconst); + return (n); +} node *xmlparse(char *path) { @@ -17,7 +66,6 @@ node *xmlparse(char *path) int fd = open(path, O_RDONLY); struct stat stats; char *nodestr; - char *strconst; int count; if (fd < 0) @@ -26,11 +74,8 @@ node *xmlparse(char *path) nodestr = malloc(stats.st_size); if (nodestr) { count = read(fd, nodestr, stats.st_size); - if (count == stats.st_size) { - strconst = nodestr; - n = xml_parsenode(&nodestr); - } - free(strconst); + if (count == stats.st_size) + n = xml_parsestr(nodestr); } close(fd); return (n); diff --git a/tests/tests_realxml.c b/tests/tests_realxml.c new file mode 100644 index 0000000..e69de29 diff --git a/tests/xmltest.txt b/tests/xmltest.txt index ef102e7..a62ab77 100644 --- a/tests/xmltest.txt +++ b/tests/xmltest.txt @@ -1 +1,2 @@ + \ No newline at end of file