eina_simple_xml_parser_example_01

- Main Page
- Related Pages
- Modules
- Data Structures
- Files
- Examples

eina_simple_xml_parser_example_01
//Compile with:
//gcc -Wall -o eina_simple_xml_parser_01 eina_simple_xml_parser_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;
eina_init();
if ((file = fopen("chat.xml", "rb")))
{
fseek(file, 0, SEEK_END);
size = ftell(file);
fseek(file, 0, SEEK_SET);
if (size < 0)
{
EINA_LOG_ERR("Can't read chat.xml");
return 0;
}
if ((buffer = malloc(size)))
{
if (fread(buffer, 1, size, file) != (unsigned long)size)
{
EINA_LOG_ERR("Can't read chat.xml");
}
array = eina_array_new(10);
eina_simple_xml_parse(buffer, size, EINA_TRUE,
_xml_tag_cb, array);
eina_array_foreach(array, _print, NULL);
eina_array_free(array);
free(buffer);
}
else
{
EINA_LOG_ERR("Can't allocate memory!");
}
fclose(file);
}
else
{
EINA_LOG_ERR("Can't open chat.xml!");
}
eina_shutdown();
return 0;
}
static Eina_Bool
_xml_tag_cb(void *data, Eina_Simple_XML_Type type, const char *content,
unsigned offset EINA_UNUSED, unsigned length)
{
char buffer[length+1];
Eina_Array *array = data;
char str[512] = {'\0'};
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)
{
eina_strlcpy(buffer, content, sizeof(buffer));
eina_strlcat(str, "<", 1);
eina_strlcat(str, buffer, sizeof(str));
eina_strlcat(str, "> ", 2);
tag_login = EINA_FALSE;
}
else if (tag_message == EINA_TRUE)
{
eina_strlcpy(buffer, content, sizeof(buffer));
eina_strlcat(str, buffer, sizeof(str));
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 EINA_UNUSED, void *data, void *fdata EINA_UNUSED)
{
printf("%s\n", (char *)data);
return EINA_TRUE;
}
Eina.h
Eina Utility library.
eina_array_free
EINA_API void eina_array_free(Eina_Array *array)
Frees an array.
Definition: eina_array.c:295
eina_array_foreach
static Eina_Bool eina_array_foreach(Eina_Array *array, Eina_Each_Cb cb, void *fdata)
Iterates over an array using a callback function.
eina_array_push
static Eina_Bool eina_array_push(Eina_Array *array, const void *data)
Appends a data item to an array.
eina_array_new
EINA_API Eina_Array * eina_array_new(unsigned int step)
Creates a new array.
Definition: eina_array.c:276
EINA_LOG_ERR
#define EINA_LOG_ERR(fmt,...)
Logs a message with level ERROR on the default domain with the specified format.
Definition: eina_log.h:376
eina_shutdown
EINA_API int eina_shutdown(void)
Shuts down the Eina library.
Definition: eina_main.c:379
eina_init
EINA_API int eina_init(void)
Initializes the Eina library.
Definition: eina_main.c:291
eina_simple_xml_parse
EINA_API Eina_Bool eina_simple_xml_parse(const char *buf, unsigned buflen, Eina_Bool strip, Eina_Simple_XML_Cb func, const void *data)
Parses a section of XML string text.
Definition: eina_simple_xml_parser.c:284
eina_simple_xml_tag_attributes_find
EINA_API 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:476
eina_simple_xml_attributes_parse
EINA_API 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:501
EINA_SIMPLE_XML_OPEN
@ EINA_SIMPLE_XML_OPEN
Definition: eina_simple_xml_parser.h:195
EINA_SIMPLE_XML_DATA
@ EINA_SIMPLE_XML_DATA
Definition: eina_simple_xml_parser.h:198
eina_strlcat
EINA_API size_t eina_strlcat(char *dst, const char *src, size_t siz)
Appends a c-string.
Definition: eina_str.c:349
eina_strlcpy
EINA_API size_t eina_strlcpy(char *dst, const char *src, size_t siz)
Copies a c-string to another.
Definition: eina_str.c:317
EINA_TRUE
#define EINA_TRUE
boolean value TRUE (numerical value 1)
Definition: eina_types.h:539
EINA_FALSE
#define EINA_FALSE
boolean value FALSE (numerical value 0)
Definition: eina_types.h:533
Eina_Bool
unsigned char Eina_Bool
Type to mimic a boolean.
Definition: eina_types.h:527
EINA_UNUSED
#define EINA_UNUSED
Used to indicate that a function parameter is purposely unused.
Definition: eina_types.h:339
_Eina_Array
Type for an array of data.
Definition: eina_array.h:229