\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 <\hyperlink{Eina_8h}{Eina.h}>}
\textcolor{preprocessor}{#include <stdio.h>}
\textcolor{preprocessor}{#include <string.h>}

\textcolor{keyword}{static} \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} \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} \hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool} \_print(\textcolor{keyword}{const} \textcolor{keywordtype}{void} *container, \textcolor{keywordtype}{void} *data, \textcolor{keywordtype}{void} *fdata);

\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool} tag\_login   = \hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE};
\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool} tag\_message = \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;
   \hyperlink{struct__Eina__Array}{Eina\_Array} *array;

   \hyperlink{group__Eina__Main__Group_gabf448ada5bc81a07342b66c6f818ee5e}{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)))
          \{
             fread(buffer, 1, size, file);

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

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

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

\textcolor{keyword}{static} \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 EINA\_UNUSED, \textcolor{keywordtype}{unsigned} length)
\{
   \textcolor{keywordtype}{char} buffer[length+1];
   \hyperlink{struct__Eina__Array}{Eina\_Array} *array = data;
   \textcolor{keywordtype}{char} str[512];

   \textcolor{keywordflow}{if} (type == \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 = \hyperlink{group__Eina__Simple__XML__Group_gaa356517d0c834904432275d7edd2a892}{eina\_simple\_xml\_tag\_attributes\_find}(
      content,
                                                                    length);
             \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 = \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 = \hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE};
          \}
     \}
   \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (type == \hyperlink{group__Eina__Simple__XML__Group_ggad43c24562b801f797d8f7cb8b3fe2d4caea1f4a70cdfcfba5c19ce1abe363c688}{EINA\_SIMPLE\_XML\_DATA})
     \{
        \textcolor{keywordflow}{if} (tag\_login == \hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE})
          \{
             snprintf(buffer, \textcolor{keyword}{sizeof}(buffer), content);
             strncat(str, \textcolor{stringliteral}{"<"}, 1);
             strncat(str, buffer, \textcolor{keyword}{sizeof}(buffer));
             strncat(str, \textcolor{stringliteral}{"> "}, 2);
             tag\_login = \hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE};
          \}
        \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (tag\_message == \hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE})
          \{
             snprintf(buffer, \textcolor{keyword}{sizeof}(buffer), content);
             strncat(str, buffer, \textcolor{keyword}{sizeof}(buffer));
             tag\_message = \hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE};
             \hyperlink{group__Eina__Array__Group_gaf2f4a5907f1826354977adbd63e3379a}{eina\_array\_push}(array, strdup(str));
          \}
     \}

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

\textcolor{keyword}{static} \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} \hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE};
\}

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

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