\hypertarget{eina_simple_xml_parser_01_8c-example}{}\section{eina\+\_\+simple\+\_\+xml\+\_\+parser\+\_\+01.\+c}

\begin{DoxyCodeInclude}
\textcolor{comment}{//Compile with:}
\textcolor{comment}{//gcc -Wall -o eina\_simple\_xml\_parser\_01 eina\_simple\_xml\_parser\_01.c `pkg-config --cflags --libs eina`}

\textcolor{preprocessor}{#include <\mbox{\hyperlink{Eina_8h}{Eina.h}}>}
\textcolor{preprocessor}{#include <stdio.h>}
\textcolor{preprocessor}{#include <string.h>}

\textcolor{keyword}{static} \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}} \_xml\_attr\_cb(\textcolor{keywordtype}{void} *data, \textcolor{keyword}{const} \textcolor{keywordtype}{char} *key, \textcolor{keyword}{const} \textcolor{keywordtype}{char} *value);
\textcolor{keyword}{static} \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}} \_xml\_tag\_cb(\textcolor{keywordtype}{void} *data, Eina\_Simple\_XML\_Type type,
                \textcolor{keyword}{const} \textcolor{keywordtype}{char} *content, \textcolor{keywordtype}{unsigned} offset, \textcolor{keywordtype}{unsigned} length);
\textcolor{keyword}{static} \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}} \_print(\textcolor{keyword}{const} \textcolor{keywordtype}{void} *container, \textcolor{keywordtype}{void} *data, \textcolor{keywordtype}{void} *fdata);

\mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}} tag\_login   = \mbox{\hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE}};
\mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}} tag\_message = \mbox{\hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE}};

\textcolor{keywordtype}{int}
main(\textcolor{keywordtype}{void})
\{
   FILE *file;
   \textcolor{keywordtype}{long} size;
   \textcolor{keywordtype}{char} *buffer;
   \mbox{\hyperlink{struct__Eina__Array}{Eina\_Array}} *array;

   \mbox{\hyperlink{group__Eina__Main__Group_gafee4bfc0e509b6ffed6598edf6f4b3de}{eina\_init}}();

   \textcolor{keywordflow}{if} ((file = fopen(\textcolor{stringliteral}{"chat.xml"}, \textcolor{stringliteral}{"rb"})))
     \{
        fseek(file, 0, SEEK\_END);
        size = ftell(file);
        fseek(file, 0, SEEK\_SET);

        \textcolor{keywordflow}{if} ((buffer = malloc(size)))
          \{
             \textcolor{keywordflow}{if} (fread(buffer, 1, size, file) != size)
               \{
                  \mbox{\hyperlink{group__Eina__Log__Group_ga2e33caeeeb6051526c01b7fea4d6c91b}{EINA\_LOG\_ERR}}(\textcolor{stringliteral}{"Can't read chat.xml"});
               \}

             array = \mbox{\hyperlink{group__Eina__Array__Group_ga4a389204db37b44b31d29f581258ac7d}{eina\_array\_new}}(10);
             \mbox{\hyperlink{group__Eina__Simple__XML__Group_gacbbb2b975f6f3e1d0a3d2215cc37da82}{eina\_simple\_xml\_parse}}(buffer, size, \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}},
                                   \_xml\_tag\_cb, array);

             \mbox{\hyperlink{group__Eina__Array__Group_ga8de2d191a0fffceb5bdc6376540b8db1}{eina\_array\_foreach}}(array, \_print, NULL);
        
             \mbox{\hyperlink{group__Eina__Array__Group_gacd0118606f9e5b42839b91ed0da8b168}{eina\_array\_free}}(array);
             free(buffer);
          \}
        \textcolor{keywordflow}{else}
          \{
             \mbox{\hyperlink{group__Eina__Log__Group_ga2e33caeeeb6051526c01b7fea4d6c91b}{EINA\_LOG\_ERR}}(\textcolor{stringliteral}{"Can't allocate memory!"});
          \}
        fclose(file);
     \}
   \textcolor{keywordflow}{else}
     \{
        \mbox{\hyperlink{group__Eina__Log__Group_ga2e33caeeeb6051526c01b7fea4d6c91b}{EINA\_LOG\_ERR}}(\textcolor{stringliteral}{"Can't open chat.xml!"});
     \}
   \mbox{\hyperlink{group__Eina__Main__Group_ga2c8b4020427108a30c44707af22c99d3}{eina\_shutdown}}();

   \textcolor{keywordflow}{return} 0;
\}

\textcolor{keyword}{static} \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}
\_xml\_tag\_cb(\textcolor{keywordtype}{void} *data, Eina\_Simple\_XML\_Type type, \textcolor{keyword}{const} \textcolor{keywordtype}{char} *content,
            \textcolor{keywordtype}{unsigned} offset \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}}, \textcolor{keywordtype}{unsigned} length)
\{
   \textcolor{keywordtype}{char} buffer[length+1];
   \mbox{\hyperlink{struct__Eina__Array}{Eina\_Array}} *array = data;
   \textcolor{keywordtype}{char} str[512] = \{\textcolor{charliteral}{'\(\backslash\)0'}\};

   \textcolor{keywordflow}{if} (type == \mbox{\hyperlink{group__Eina__Simple__XML__Group_ggad43c24562b801f797d8f7cb8b3fe2d4ca5be26ebfdfdefbc411adde618c4c2dd4}{EINA\_SIMPLE\_XML\_OPEN}})
     \{
        \textcolor{keywordflow}{if}(!strncmp(\textcolor{stringliteral}{"post"}, content, strlen(\textcolor{stringliteral}{"post"})))
          \{
             \textcolor{keyword}{const} \textcolor{keywordtype}{char} *tags = \mbox{\hyperlink{group__Eina__Simple__XML__Group_gaa356517d0c834904432275d7edd2a892}{eina\_simple\_xml\_tag\_attributes\_find}}(
      content,
                                                                    length);
             \mbox{\hyperlink{group__Eina__Simple__XML__Group_ga9525b33f8d78eb23def4f660b290f794}{eina\_simple\_xml\_attributes\_parse}}(tags, length - (tags - 
      content),
                                              \_xml\_attr\_cb, str);
          \}
        \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (!strncmp(\textcolor{stringliteral}{"login>"}, content, strlen(\textcolor{stringliteral}{"login>"})))
          \{
             tag\_login = \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}};
          \}
        \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (!strncmp(\textcolor{stringliteral}{"message>"}, content, strlen(\textcolor{stringliteral}{"message>"})))
          \{
             tag\_message = \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}};
          \}
     \}
   \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (type == \mbox{\hyperlink{group__Eina__Simple__XML__Group_ggad43c24562b801f797d8f7cb8b3fe2d4caea1f4a70cdfcfba5c19ce1abe363c688}{EINA\_SIMPLE\_XML\_DATA}})
     \{
        \textcolor{keywordflow}{if} (tag\_login == \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}})
          \{
             \mbox{\hyperlink{group__Eina__String__Group_ga6bda31d2e1b6d2737b7fe1784780cd2f}{eina\_strlcpy}}(buffer, content, \textcolor{keyword}{sizeof}(buffer));
             \mbox{\hyperlink{group__Eina__String__Group_ga6c9583940f9f6b3c886a19f22f398ff2}{eina\_strlcat}}(str, \textcolor{stringliteral}{"<"}, 1);
             \mbox{\hyperlink{group__Eina__String__Group_ga6c9583940f9f6b3c886a19f22f398ff2}{eina\_strlcat}}(str, buffer, \textcolor{keyword}{sizeof}(str));
             \mbox{\hyperlink{group__Eina__String__Group_ga6c9583940f9f6b3c886a19f22f398ff2}{eina\_strlcat}}(str, \textcolor{stringliteral}{"> "}, 2);
             tag\_login = \mbox{\hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE}};
          \}
        \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (tag\_message == \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}})
          \{
             \mbox{\hyperlink{group__Eina__String__Group_ga6bda31d2e1b6d2737b7fe1784780cd2f}{eina\_strlcpy}}(buffer, content, \textcolor{keyword}{sizeof}(buffer));
             \mbox{\hyperlink{group__Eina__String__Group_ga6c9583940f9f6b3c886a19f22f398ff2}{eina\_strlcat}}(str, buffer, \textcolor{keyword}{sizeof}(str));
             tag\_message = \mbox{\hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE}};
             \mbox{\hyperlink{group__Eina__Array__Group_gaf2f4a5907f1826354977adbd63e3379a}{eina\_array\_push}}(array, strdup(str));
          \}
     \}

   \textcolor{keywordflow}{return} \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}};
\}

\textcolor{keyword}{static} \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}
\_xml\_attr\_cb(\textcolor{keywordtype}{void} *data, \textcolor{keyword}{const} \textcolor{keywordtype}{char} *key, \textcolor{keyword}{const} \textcolor{keywordtype}{char} *value)
\{
   \textcolor{keywordtype}{char} *str = data;

   \textcolor{keywordflow}{if}(!strcmp(\textcolor{stringliteral}{"id"}, key))
   \{
      snprintf(str, \textcolor{keyword}{sizeof}(value) + 3, \textcolor{stringliteral}{"(%s) "}, value);
   \}

   \textcolor{keywordflow}{return} \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}};
\}

\textcolor{keyword}{static} \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}
\_print(\textcolor{keyword}{const} \textcolor{keywordtype}{void} *container \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}}, \textcolor{keywordtype}{void} *data, \textcolor{keywordtype}{void} *fdata 
      \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}})
\{
   printf(\textcolor{stringliteral}{"%s\(\backslash\)n"}, (\textcolor{keywordtype}{char} *)data);

   \textcolor{keywordflow}{return} \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}};
\}
\end{DoxyCodeInclude}
 