mirror of
https://github.com/zoriya/xmlParser.git
synced 2026-06-03 02:23:36 +00:00
Adding a formater (I think)
This commit is contained in:
@@ -0,0 +1,4 @@
|
|||||||
|
Unsupported:
|
||||||
|
namespaces
|
||||||
|
attributes with semi quotes (') instead of quoetes (")
|
||||||
|
comments
|
||||||
+51
-6
@@ -5,11 +5,60 @@
|
|||||||
** xmlparser
|
** xmlparser
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "my.h"
|
||||||
#include "xml.h"
|
#include "xml.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
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, "<?xml") == *nodestr)
|
||||||
|
*nodestr = 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)
|
node *xmlparse(char *path)
|
||||||
{
|
{
|
||||||
@@ -17,7 +66,6 @@ node *xmlparse(char *path)
|
|||||||
int fd = open(path, O_RDONLY);
|
int fd = open(path, O_RDONLY);
|
||||||
struct stat stats;
|
struct stat stats;
|
||||||
char *nodestr;
|
char *nodestr;
|
||||||
char *strconst;
|
|
||||||
int count;
|
int count;
|
||||||
|
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
@@ -26,11 +74,8 @@ node *xmlparse(char *path)
|
|||||||
nodestr = malloc(stats.st_size);
|
nodestr = malloc(stats.st_size);
|
||||||
if (nodestr) {
|
if (nodestr) {
|
||||||
count = read(fd, nodestr, stats.st_size);
|
count = read(fd, nodestr, stats.st_size);
|
||||||
if (count == stats.st_size) {
|
if (count == stats.st_size)
|
||||||
strconst = nodestr;
|
n = xml_parsestr(nodestr);
|
||||||
n = xml_parsenode(&nodestr);
|
|
||||||
}
|
|
||||||
free(strconst);
|
|
||||||
}
|
}
|
||||||
close(fd);
|
close(fd);
|
||||||
return (n);
|
return (n);
|
||||||
|
|||||||
@@ -1 +1,2 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<yes><nop/></yes>
|
<yes><nop/></yes>
|
||||||
Reference in New Issue
Block a user