Created
January 18, 2019 20:17
-
-
Save rbtylee/89c8c31242303789538550f8271324d5 to your computer and use it in GitHub Desktop.
An illustration of using Eina Simple XML functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* forecast_xml_parser.c | |
* | |
* An illustration of using Eina Simple XML functions to parse | |
* the forecast.xml file provided by yr.no | |
* (delivered by the Norwegian Meteorological Institute) | |
* | |
* For example: | |
* https://www.yr.no/place/United_States/California/Cambria/forecast.xml | |
* | |
* Requires Eina provided by EFL: | |
* https://www.enlightenment.org/download | |
* | |
* Tested under efl version 1.20.7, Bodhi linux 5.0 | |
* | |
* Reference: https://www.enlightenment.org/_legacy_embed/group__Eina__Simple__XML__Group.html | |
* | |
* Copyright 2019 ylee@bodhilinux.com | |
* | |
* This work is free. You can redistribute it and/or modify it under the | |
* terms of the Do What The Fuck You Want To Public License, Version 2, | |
* as published by Sam Hocevar. See http://www.wtfpl.net/ for more details. | |
* | |
* Compile with: | |
* gcc -Wall -o forecast_xml_parser forecast_xml_parser.c `pkg-config --cflags --libs eina` | |
*/ | |
#include <Eina.h> | |
#include <stdio.h> | |
#include <string.h> | |
static Eina_Bool _xml_tag_cb(void *data, Eina_Simple_XML_Type type, | |
const char *content, unsigned offset, unsigned length); | |
static Eina_Bool _time_attr(void *data, const char *key, const char *value); | |
static Eina_Bool _sun_attr(void *data EINA_UNUSED, const char *key, const char *value); | |
static Eina_Bool _symbol_attr(void *data EINA_UNUSED, const char *key, const char *value); | |
static Eina_Bool _precip_attr(void *data EINA_UNUSED, const char *key, const char *value); | |
static Eina_Bool _wind_dir_attr(void *data EINA_UNUSED, const char *key, const char *value); | |
static Eina_Bool _wind_speed_attr(void *data EINA_UNUSED, const char *key, const char *value); | |
static Eina_Bool _temp_attr(void *data EINA_UNUSED, const char *key, const char *value); | |
static Eina_Bool _pressure_attr(void *data EINA_UNUSED, const char *key, const char *value); | |
Eina_Bool tag_location = EINA_FALSE; | |
int | |
main(void) | |
{ | |
FILE *file; | |
long size; | |
char *buffer; | |
eina_init(); | |
if ((file = fopen("forecast.xml", "rb"))) | |
{ | |
fseek(file, 0, SEEK_END); | |
size = ftell(file); | |
fseek(file, 0, SEEK_SET); | |
if ((buffer = malloc(size))) | |
{ | |
fread(buffer, 1, size, file); | |
eina_simple_xml_parse(buffer, size, EINA_TRUE, _xml_tag_cb, NULL); | |
free(buffer); | |
} | |
else | |
{ | |
EINA_LOG_ERR("Can't allocate memory!"); | |
} | |
fclose(file); | |
} | |
else | |
{ | |
EINA_LOG_ERR("Can't open forecast.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]; | |
char str[512]; | |
//printf(" %d\n",length); | |
if (type == EINA_SIMPLE_XML_OPEN) | |
{ | |
if (!strncmp("location>", content, strlen("location>"))) | |
{ | |
tag_location = EINA_TRUE; | |
} | |
else if (!strncmp(content, "time", sizeof("time") - 1)) | |
{ | |
const char *tags = eina_simple_xml_tag_attributes_find(content, length); | |
eina_simple_xml_attributes_parse(tags, | |
length - (tags - content), | |
_time_attr, str); | |
} | |
} | |
else if (type == EINA_SIMPLE_XML_OPEN_EMPTY) | |
{ | |
if (!strncmp(content, "sun", sizeof("sun") - 1)) | |
{ | |
const char *tags = eina_simple_xml_tag_attributes_find(content, length); | |
eina_simple_xml_attributes_parse(tags, | |
length - (tags - content), | |
_sun_attr, str); | |
} | |
else if (!strncmp(content, "symbol", sizeof("symbol") - 1)) | |
{ | |
const char *tags = eina_simple_xml_tag_attributes_find(content, length); | |
eina_simple_xml_attributes_parse(tags, | |
length - (tags - content), | |
_symbol_attr, str); | |
} | |
else if (!strncmp(content, "precipitation", sizeof("precipitation") - 1)) | |
{ | |
const char *tags = eina_simple_xml_tag_attributes_find(content, length); | |
eina_simple_xml_attributes_parse(tags, | |
length - (tags - content), | |
_precip_attr, str); | |
} | |
else if (!strncmp(content, "windDirection", sizeof("windDirection") - 1)) | |
{ | |
const char *tags = eina_simple_xml_tag_attributes_find(content, length); | |
eina_simple_xml_attributes_parse(tags, | |
length - (tags - content), | |
_wind_dir_attr, str); | |
} | |
else if (!strncmp(content, "windSpeed", sizeof("windSpeed") - 1)) | |
{ | |
const char *tags = eina_simple_xml_tag_attributes_find(content, length); | |
eina_simple_xml_attributes_parse(tags, | |
length - (tags - content), | |
_wind_speed_attr, str); | |
} | |
else if (!strncmp(content, "temperature", sizeof("temperature") - 1)) | |
{ | |
const char *tags = eina_simple_xml_tag_attributes_find(content, length); | |
eina_simple_xml_attributes_parse(tags, | |
length - (tags - content), | |
_temp_attr, str); | |
} | |
else if (!strncmp(content, "pressure", sizeof("pressure") - 1)) | |
{ | |
const char *tags = eina_simple_xml_tag_attributes_find(content, length); | |
eina_simple_xml_attributes_parse(tags, | |
length - (tags - content), | |
_pressure_attr, str); | |
} | |
} | |
else if (type == EINA_SIMPLE_XML_DATA) | |
{ | |
if (tag_location == EINA_TRUE) | |
{ | |
eina_strlcpy(buffer, content, sizeof(buffer)); | |
eina_strlcat(str, buffer, sizeof(str)); | |
tag_location = EINA_FALSE; | |
printf("Location: %s\n\n", str); | |
} | |
} | |
return EINA_TRUE; | |
} | |
static Eina_Bool | |
_time_attr(void *data EINA_UNUSED, const char *key, const char *value) | |
{ | |
if (!strncmp(key, "from", strlen("from"))) | |
printf("\nTime from %s\n", value); | |
else if (!strncmp(key, "to", strlen("to"))) | |
printf("Time to %s\n", value); | |
return EINA_TRUE; | |
} | |
static Eina_Bool | |
_sun_attr(void *data EINA_UNUSED, const char *key, const char *value) | |
{ | |
if (!strncmp(key, "rise", strlen("rise"))) | |
printf("Sun rise %s\n", value); | |
else if (!strncmp(key, "set", strlen("set"))) | |
printf("Sun set %s\n", value); | |
return EINA_TRUE; | |
} | |
static Eina_Bool | |
_symbol_attr(void *data EINA_UNUSED, const char *key, const char *value) | |
{ | |
if (!strncmp(key, "name", strlen("name"))) | |
printf("symbol desc %s\n", value); | |
else if (!strncmp(key, "var", strlen("var"))) | |
printf("symbol code %s\n", value); | |
// ignore other values | |
return EINA_TRUE; | |
} | |
static Eina_Bool | |
_precip_attr(void *data EINA_UNUSED, const char *key, const char *value) | |
{ | |
if (!strncmp(key, "value", strlen("value"))) | |
printf("precipitation %s\n", value); | |
return EINA_TRUE; | |
} | |
static Eina_Bool | |
_wind_dir_attr(void *data EINA_UNUSED, const char *key, const char *value) | |
{ | |
if (!strncmp(key, "name", strlen("name"))) | |
printf("windDirection %s\n", value); | |
// ignore other values | |
return EINA_TRUE; | |
} | |
static Eina_Bool | |
_wind_speed_attr(void *data EINA_UNUSED, const char *key, const char *value) | |
{ | |
if (!strncmp(key, "mps", strlen("mps"))) | |
printf("windSpeed %s\n", value); | |
else if (!strncmp(key, "name", strlen("name"))) | |
printf("windSpeed desc %s\n", value); | |
return EINA_TRUE; | |
} | |
static Eina_Bool | |
_temp_attr(void *data EINA_UNUSED, const char *key, const char *value) | |
{ | |
if (!strncmp(key, "unit", strlen("unit"))) | |
printf("Temperature unit %s\n", value); | |
else if (!strncmp(key, "value", strlen("value"))) | |
printf("Temperature %s\n", value); | |
return EINA_TRUE; | |
} | |
static Eina_Bool | |
_pressure_attr(void *data EINA_UNUSED, const char *key, const char *value) | |
{ | |
if (!strncmp(key, "unit", strlen("unit"))) | |
printf("Pressure unit %s\n", value); | |
else if (!strncmp(key, "value", strlen("value"))) | |
printf("Pressure %s\n", value); | |
return EINA_TRUE; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment