EFL 1.7 svn doobies
[profile/ivi/eina.git] / src / examples / eina_simple_xml_parser_01.c
1 //Compile with:
2 //gcc -Wall -o eina_simple_xml_01 eina_simple_xml_01.c `pkg-config --cflags --libs eina`
3
4 #include <Eina.h>
5 #include <stdio.h>
6 #include <string.h>
7
8 static Eina_Bool _xml_attr_cb(void *data, const char *key, const char *value);
9 static Eina_Bool _xml_tag_cb(void *data, Eina_Simple_XML_Type type,
10                 const char *content, unsigned offset, unsigned length);
11 static Eina_Bool _print(const void *container, void *data, void *fdata);
12
13 Eina_Bool tag_login   = EINA_FALSE;
14 Eina_Bool tag_message = EINA_FALSE;
15
16 int
17 main(void)
18 {
19    FILE *file;
20    long size;
21    char *buffer;
22    Eina_Array *array;
23
24    eina_init();
25
26    if ((file = fopen("chat.xml", "rb")))
27      {
28         fseek(file, 0, SEEK_END);
29         size = ftell(file);
30         fseek(file, 0, SEEK_SET);
31
32         if ((buffer = malloc(size)))
33           {
34              fread(buffer, 1, size, file);
35
36              array = eina_array_new(10);
37              eina_simple_xml_parse(buffer, size, EINA_TRUE,
38                                    _xml_tag_cb, array);
39
40              eina_array_foreach(array, _print, NULL);
41         
42              eina_array_free(array);
43              free(buffer);
44           }
45         else
46           {
47              EINA_LOG_ERR("Can't allocate memory!");
48           }
49         fclose(file);
50      }
51    else
52      {
53         EINA_LOG_ERR("Can't open chat.xml!");
54      }
55    eina_shutdown();
56
57    return 0;
58 }
59
60 static Eina_Bool
61 _xml_tag_cb(void *data, Eina_Simple_XML_Type type, const char *content,
62             unsigned offset, unsigned length)
63 {
64    char buffer[length+1];
65    Eina_Array *array = data;
66    char str[512];
67
68    if (type == EINA_SIMPLE_XML_OPEN)
69      {
70         if(!strncmp("post", content, strlen("post")))
71           {
72              const char *tags = eina_simple_xml_tag_attributes_find(content,
73                                                                     length);
74              eina_simple_xml_attributes_parse(tags, length - (tags - content),
75                                               _xml_attr_cb, str);
76           }
77         else if (!strncmp("login>", content, strlen("login>")))
78           {
79              tag_login = EINA_TRUE;
80           }
81         else if (!strncmp("message>", content, strlen("message>")))
82           {
83              tag_message = EINA_TRUE;
84           }
85      }
86    else if (type == EINA_SIMPLE_XML_DATA)
87      {
88         if (tag_login == EINA_TRUE)
89           {
90              snprintf(buffer, sizeof(buffer), content);
91              strncat(str, "<", 1);
92              strncat(str, buffer, sizeof(buffer));
93              strncat(str, "> ", 2);
94              tag_login = EINA_FALSE;
95           }
96         else if (tag_message == EINA_TRUE)
97           {
98              snprintf(buffer, sizeof(buffer), content);
99              strncat(str, buffer, sizeof(buffer));
100              tag_message = EINA_FALSE;
101              eina_array_push(array, strdup(str));
102           }
103      }
104
105    return EINA_TRUE;
106 }
107
108 static Eina_Bool
109 _xml_attr_cb(void *data, const char *key, const char *value)
110 {
111    char *str = data;
112
113    if(!strcmp("id", key))
114    {
115       snprintf(str, sizeof(value) + 3, "(%s) ", value);
116    }
117
118    return EINA_TRUE;
119 }
120
121 static Eina_Bool
122 _print(const void *container, void *data, void *fdata)
123 {
124    printf("%s\n", (char *)data);
125
126    return EINA_TRUE;
127 }