mirror of
https://forge.fsky.io/lda/Parsee.git
synced 2026-03-13 21:35:10 +00:00
[MOD/ADD] Start element/XMPPwerk
XML gave me an aneurysm. I hate this.
This commit is contained in:
parent
79217d3608
commit
138932d637
4 changed files with 204 additions and 0 deletions
112
src/XML/Elements.c
Normal file
112
src/XML/Elements.c
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
#include <XML.h>
|
||||
|
||||
#include <Cytoplasm/Memory.h>
|
||||
#include <Cytoplasm/Str.h>
|
||||
|
||||
XMLElement *
|
||||
XMLCreateTag(char *name)
|
||||
{
|
||||
XMLElement *elem;
|
||||
if (!name)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
elem = Malloc(sizeof(*elem));
|
||||
|
||||
elem->type = XML_ELEMENT_TAG;
|
||||
elem->name = StrDuplicate(name);
|
||||
elem->attrs = HashMapCreate();
|
||||
elem->children = ArrayCreate();
|
||||
|
||||
elem->data = NULL;
|
||||
|
||||
return elem;
|
||||
}
|
||||
XMLElement *
|
||||
XMLCreateText(char *data)
|
||||
{
|
||||
XMLElement *elem;
|
||||
if (!data)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
elem = Malloc(sizeof(*elem));
|
||||
|
||||
elem->type = XML_ELEMENT_DATA;
|
||||
elem->name = NULL;
|
||||
elem->attrs = NULL;
|
||||
elem->children = NULL;
|
||||
|
||||
elem->data = StrDuplicate(data);
|
||||
|
||||
return elem;
|
||||
}
|
||||
|
||||
void
|
||||
XMLAddAttr(XMLElement *element, char *key, char *val)
|
||||
{
|
||||
if (!element || !key || !val)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (element->type != XML_ELEMENT_TAG)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
HashMapSet(element->attrs, key, StrDuplicate(val));
|
||||
}
|
||||
void
|
||||
XMLAddChild(XMLElement *element, XMLElement *child)
|
||||
{
|
||||
if (!element || !child)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (element->type != XML_ELEMENT_TAG)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ArrayAdd(element->children, child);
|
||||
}
|
||||
|
||||
void
|
||||
XMLFreeElement(XMLElement *element)
|
||||
{
|
||||
if (!element)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (element->name)
|
||||
{
|
||||
Free(element->name);
|
||||
}
|
||||
if (element->data)
|
||||
{
|
||||
Free(element->data);
|
||||
}
|
||||
if (element->attrs)
|
||||
{
|
||||
char *key, *val;
|
||||
while (HashMapIterate(element->attrs, &key, (void **) &val))
|
||||
{
|
||||
Free(val);
|
||||
}
|
||||
HashMapFree(element->attrs);
|
||||
}
|
||||
if (element->children)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < ArraySize(element->children); i++)
|
||||
{
|
||||
XMLFreeElement(ArrayGet(element->children, i));
|
||||
}
|
||||
ArrayFree(element->children);
|
||||
}
|
||||
|
||||
Free(element);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue