eina_simple_xml_parser_example_01
//Compile with:
//gcc -Wall -o eina_simple_xml_01 eina_simple_xml_01.c `pkg-config --cflags --libs eina`
#include <Eina.h>
#include <stdio.h>
#include <string.h>
static Eina_Bool _xml_attr_cb(void *data, const char *key, const char *value);
static Eina_Bool _xml_tag_cb(void *data, Eina_Simple_XML_Type type,
const char *content, unsigned offset, unsigned length);
static Eina_Bool _print(const void *container, void *data, void *fdata);
Eina_Bool tag_login = EINA_FALSE;
Eina_Bool tag_message = EINA_FALSE;
int
main(void)
{
FILE *file;
long size;
char *buffer;
Eina_Array *array;
if ((file = fopen("chat.xml", "rb")))
{
fseek(file, 0, SEEK_END);
size = ftell(file);
fseek(file, 0, SEEK_SET);
if ((buffer = malloc(size)))
{
fread(buffer, 1, size, file);
array = eina_array_new(10);
_xml_tag_cb, array);
eina_array_foreach(array, _print, NULL);
free(buffer);
}
else
{
EINA_LOG_ERR("Can't allocate memory!");
}
fclose(file);
}
else
{
EINA_LOG_ERR("Can't open chat.xml!");
}
return 0;
}
static Eina_Bool
_xml_tag_cb(void *data, Eina_Simple_XML_Type type, const char *content,
unsigned offset, unsigned length)
{
char buffer[length+1];
Eina_Array *array = data;
char str[512];
if (type == EINA_SIMPLE_XML_OPEN)
{
if(!strncmp("post", content, strlen("post")))
{
const char *tags = eina_simple_xml_tag_attributes_find(content,
length);
eina_simple_xml_attributes_parse(tags, length - (tags - content),
_xml_attr_cb, str);
}
else if (!strncmp("login>", content, strlen("login>")))
{
tag_login = EINA_TRUE;
}
else if (!strncmp("message>", content, strlen("message>")))
{
tag_message = EINA_TRUE;
}
}
else if (type == EINA_SIMPLE_XML_DATA)
{
if (tag_login == EINA_TRUE)
{
snprintf(buffer, sizeof(buffer), content);
strncat(str, "<", 1);
strncat(str, buffer, sizeof(buffer));
strncat(str, "> ", 2);
tag_login = EINA_FALSE;
}
else if (tag_message == EINA_TRUE)
{
snprintf(buffer, sizeof(buffer), content);
strncat(str, buffer, sizeof(buffer));
tag_message = EINA_FALSE;
eina_array_push(array, strdup(str));
}
}
return EINA_TRUE;
}
static Eina_Bool
_xml_attr_cb(void *data, const char *key, const char *value)
{
char *str = data;
if(!strcmp("id", key))
{
snprintf(str, sizeof(value) + 3, "(%s) ", value);
}
return EINA_TRUE;
}
static Eina_Bool
_print(const void *container, void *data, void *fdata)
{
printf("%s\n", (char *)data);
return EINA_TRUE;
}
Eina Utility library.
static Eina_Bool eina_array_foreach(Eina_Array *array, Eina_Each_Cb cb, void *fdata)
Provide a safe way to iterate over an array.
Eina_Array * eina_array_new(unsigned int step)
Create a new array.
Definition eina_array.c:265
void eina_array_free(Eina_Array *array)
Free an array.
Definition eina_array.c:289
#define EINA_LOG_ERR(fmt,...)
Logs a message with level ERROR on the default domain with the specified format.
Definition eina_log.h:376
int eina_shutdown(void)
Shut down the Eina library.
Definition eina_main.c:315
int eina_init(void)
Initialize the Eina library.
Definition eina_main.c:244
const char * eina_simple_xml_tag_attributes_find(const char *buf, unsigned buflen)
Given the contents of a tag, find where the attributes start.
Definition eina_simple_xml_parser.c:473
Eina_Bool eina_simple_xml_attributes_parse(const char *buf, unsigned buflen, Eina_Simple_XML_Attribute_Cb func, const void *data)
Given a buffer with xml attributes, parse them to key=value pairs.
Definition eina_simple_xml_parser.c:498
Eina_Bool eina_simple_xml_parse(const char *buf, unsigned buflen, Eina_Bool strip, Eina_Simple_XML_Cb func, const void *data)
Parse a section of XML string text.
Definition eina_simple_xml_parser.c:289
@ EINA_SIMPLE_XML_OPEN
Definition eina_simple_xml_parser.h:202
@ EINA_SIMPLE_XML_DATA
Definition eina_simple_xml_parser.h:205
#define EINA_TRUE
boolean value TRUE (numerical value 1)
Definition eina_types.h:299
#define EINA_FALSE
boolean value FALSE (numerical value 0)
Definition eina_types.h:293
unsigned char Eina_Bool
Type to mimic a boolean.
Definition eina_types.h:287
Type for an array of data.
Definition eina_array.h:233