Add eet examples documentation and update html doc menu to add 'Examples' tab.
authorcaro <caro>
Thu, 18 Mar 2010 21:09:13 +0000 (21:09 +0000)
committercaro <caro@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Thu, 18 Mar 2010 21:09:13 +0000 (21:09 +0000)
Gustavo: documentation of examples should be added in examples.dox.
         There is one page for each example in that file.

git-svn-id: http://svn.enlightenment.org/svn/e/trunk/eet@47328 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33

configure.ac
doc/Doxyfile
doc/examples.dox [new file with mode: 0644]
doc/head.html
src/Makefile.am
src/examples/Makefile.am [new file with mode: 0644]
src/examples/eet-data-file_descriptor.c [new file with mode: 0644]
src/examples/eet-data-nested.c [new file with mode: 0644]
src/examples/eet-data-simple.c [new file with mode: 0644]

index dbc0ad5..e7c55da 100644 (file)
@@ -151,6 +151,34 @@ AC_ARG_ENABLE([assert],
    [prefer_assert=$enableval]
 )
 
+# Examples
+
+AC_ARG_ENABLE([install-examples],
+   [AC_HELP_STRING([--disable-install-examples],
+       [disable installing examples (compiled or just source). @<:@default==enabled@:>@])],
+   [
+    if test "x${enableval}" = "xyes" ; then
+       install_examples="yes"
+    else
+       install_examples="no"
+    fi
+   ],
+   [install_examples="yes"])
+AM_CONDITIONAL([INSTALL_EXAMPLES], [test "x${install_examples}" = "xyes"])
+
+AC_ARG_ENABLE([build-examples],
+   [AC_HELP_STRING([--enable-build-examples],
+       [enable building examples. @<:@default==disabled@:>@])],
+   [
+    if test "x${enableval}" = "xyes" ; then
+       build_examples="yes"
+    else
+       build_examples="no"
+    fi
+   ],
+   [build_examples="no"])
+AM_CONDITIONAL([BUILD_EXAMPLES], [test "x${build_examples}" = "xyes"])
+
 # Unit tests, coverage and benchmarking
 
 EFL_CHECK_TESTS([enable_tests="yes"], [enable_tests="no"])
@@ -395,6 +423,7 @@ src/Makefile
 src/lib/Makefile
 src/bin/Makefile
 src/tests/Makefile
+src/examples/Makefile
 README
 eet.spec
 ])
@@ -427,6 +456,13 @@ echo
 echo "  Build eet............: $have_eet"
 echo
 echo "  Documentation........: ${build_doc}"
+if test "x${build_doc}" = "xyes" ; then
+   echo "    Building...........: make doc"
+fi
+echo "  Examples.............: ${build_examples}"
+if test "x${build_examples}" = "xyes" ; then
+   echo "    Install............: ${install_examples}"
+fi
 echo
 echo "Compilation............: make (or gmake)"
 echo "  CPPFLAGS.............: $CPPFLAGS"
index 03bce5b..7339619 100644 (file)
@@ -1,7 +1,7 @@
 PROJECT_NAME           = Eet
 PROJECT_NUMBER         =
 OUTPUT_DIRECTORY       = .
-INPUT                  = eet.dox ../src/lib
+INPUT                  = eet.dox examples.dox ../src/lib
 IMAGE_PATH             = img
 OUTPUT_LANGUAGE        = English
 GENERATE_HTML          = YES
@@ -67,7 +67,7 @@ RECURSIVE              = NO
 EXCLUDE                = 
 EXCLUDE_SYMLINKS       = NO
 EXCLUDE_PATTERNS       = 
-EXAMPLE_PATH           = 
+EXAMPLE_PATH           = ../src/examples/
 EXAMPLE_PATTERNS       = 
 EXAMPLE_RECURSIVE      = NO
 INPUT_FILTER           = 
diff --git a/doc/examples.dox b/doc/examples.dox
new file mode 100644 (file)
index 0000000..69010e5
--- /dev/null
@@ -0,0 +1,34 @@
+/**
+ * @page Examples Examples
+ *
+ * Here is a page with examples.
+ *
+ * @ref Example_Eet_Data_Simple
+ *
+ * @ref Example_Eet_Data_Nested
+ *
+ * @ref Example_Eet_Data_File_Descriptor
+ *
+ * <a href="examples.html">List of examples</a>
+ */
+
+/**
+ * @page Example_Eet_Data_Simple Simple data example
+ *
+ * @includelineno eet-data-simple.c
+ * @example eet-data-simple.c
+ */
+
+/**
+ * @page Example_Eet_Data_Nested Nested data example
+ *
+ * @includelineno eet-data-nested.c
+ * @example eet-data-nested.c
+ */
+
+/**
+ * @page Example_Eet_Data_File_Descriptor File descriptor data example
+ *
+ * @includelineno eet-data-file_descriptor.c
+ * @example eet-data-file_descriptor.c
+ */
index 44fc594..519631b 100644 (file)
@@ -46,7 +46,7 @@
     <div class="menu-container">
         <div class="submenu">
             <ul class="current">
-                <li><a href="todo.html">Todo</a></li>
+                <li><a href="Examples.html">Examples</a></li>
                 <li><a href="files.html">Files</a></li>
                 <li><a href="modules.html">Modules</a></li>
                 <li><a href="globals.html">Globals</a></li>
index a6fc38f..211b035 100644 (file)
@@ -1,3 +1,9 @@
 MAINTAINERCLEANFILES = Makefile.in
 
 SUBDIRS = lib bin tests
+
+if BUILD_EXAMPLES
+
+SUBDIRS += examples
+
+endif
diff --git a/src/examples/Makefile.am b/src/examples/Makefile.am
new file mode 100644 (file)
index 0000000..c760295
--- /dev/null
@@ -0,0 +1,32 @@
+MAINTAINERCLEANFILES = Makefile.in
+
+pkglibdir = $(datadir)/$(PACKAGE)/examples
+
+AM_CPPFLAGS = \
+-I. \
+-I$(top_srcdir)/src/lib \
+@EINA_CFLAGS@
+
+pkglib_PROGRAMS = eet_data_simple eet_data_nested eet_data_file_descriptor
+
+eet_data_simple_SOURCES = eet-data-simple.c
+eet_data_simple_LDADD = $(top_builddir)/src/lib/libeet.la @EINA_LIBS@
+
+eet_data_nested_SOURCES = eet-data-nested.c
+eet_data_nested_LDADD = $(top_builddir)/src/lib/libeet.la @EINA_LIBS@
+
+eet_data_file_descriptor_SOURCES = eet-data-file_descriptor.c
+eet_data_file_descriptor_LDADD = $(top_builddir)/src/lib/libeet.la @EINA_LIBS@
+
+
+filesdir = $(datadir)/$(PACKAGE)/examples
+files_DATA =
+
+if INSTALL_EXAMPLES
+
+files_DATA += \
+eet-data-simple.c \
+eet-data-nested.c \
+eet-data-file_descriptor.c
+
+endif
diff --git a/src/examples/eet-data-file_descriptor.c b/src/examples/eet-data-file_descriptor.c
new file mode 100644 (file)
index 0000000..c97b398
--- /dev/null
@@ -0,0 +1,490 @@
+#include <Eina.h>
+#include <Eet.h>
+#include <stdio.h>
+#include <limits.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+// complex real-world structures based on elmdentica database
+typedef struct {
+   const char *screen_name;
+   const char *name;
+   const char *message;
+   unsigned int id;
+   unsigned int status_id;
+   unsigned int date;
+   unsigned int timeline;
+} My_Message;
+
+typedef struct {
+   const char *dm_to;
+   const char *message;
+} My_Post;
+
+typedef struct {
+   unsigned int id;
+   const char *name;
+   Eina_List *messages;
+   Eina_List *posts;
+} My_Account;
+
+typedef struct {
+   unsigned int version; // it is recommended to use versioned configuration!
+   Eina_List *accounts;
+} My_Cache;
+
+// string that represents the entry in eet file, you might like to have
+// different profiles or so in the same file, this is possible with
+// different strings
+static const char MY_CACHE_FILE_ENTRY[] = "cache";
+
+// keep the descriptor static global, so it can be
+// shared by different functions (load/save) of this and only this
+// file.
+static Eet_Data_Descriptor *_my_cache_descriptor;
+static Eet_Data_Descriptor *_my_account_descriptor;
+static Eet_Data_Descriptor *_my_message_descriptor;
+static Eet_Data_Descriptor *_my_post_descriptor;
+
+// keep file handle alive, so mmap()ed strings are all alive as well
+static Eet_File *_my_cache_file = NULL;
+static Eet_Dictionary *_my_cache_dict = NULL;
+
+static void
+_my_cache_descriptor_init(void)
+{
+   Eet_Data_Descriptor_Class eddc;
+
+   // The FILE variant is good for caches and things that are just
+   // appended, but needs to take care when changing strings and files must
+   // be kept open so mmap()ed strings will be kept alive.
+   EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET(&eddc, My_Cache);
+   _my_cache_descriptor = eet_data_descriptor_file_new(&eddc);
+
+   EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET(&eddc, My_Account);
+   _my_account_descriptor = eet_data_descriptor_file_new(&eddc);
+
+   EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET(&eddc, My_Message);
+   _my_message_descriptor = eet_data_descriptor_file_new(&eddc);
+
+   EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET(&eddc, My_Post);
+   _my_post_descriptor = eet_data_descriptor_file_new(&eddc);
+
+   // Describe the members to be saved:
+   // Use a temporary macro so we don't type a lot, also avoid errors:
+
+#define ADD_BASIC(member, eet_type)                                    \
+   EET_DATA_DESCRIPTOR_ADD_BASIC                                       \
+     (_my_message_descriptor, My_Message, #member, member, eet_type)
+   ADD_BASIC(screen_name, EET_T_STRING);
+   ADD_BASIC(name, EET_T_STRING);
+   ADD_BASIC(message, EET_T_STRING);
+   ADD_BASIC(id, EET_T_UINT);
+   ADD_BASIC(status_id, EET_T_UINT);
+   ADD_BASIC(date, EET_T_UINT);
+   ADD_BASIC(timeline, EET_T_UINT);
+#undef ADD_BASIC
+
+#define ADD_BASIC(member, eet_type)                                    \
+   EET_DATA_DESCRIPTOR_ADD_BASIC                                       \
+     (_my_post_descriptor, My_Post, #member, member, eet_type)
+   ADD_BASIC(dm_to, EET_T_STRING);
+   ADD_BASIC(message, EET_T_STRING);
+#undef ADD_BASIC
+
+#define ADD_BASIC(member, eet_type)                                    \
+   EET_DATA_DESCRIPTOR_ADD_BASIC                                       \
+     (_my_account_descriptor, My_Account, #member, member, eet_type)
+   ADD_BASIC(name, EET_T_STRING);
+   ADD_BASIC(id, EET_T_UINT);
+#undef ADD_BASIC
+
+   EET_DATA_DESCRIPTOR_ADD_LIST
+     (_my_account_descriptor, My_Account, "messages", messages,
+      _my_message_descriptor);
+   EET_DATA_DESCRIPTOR_ADD_LIST
+     (_my_account_descriptor, My_Account, "posts", posts,
+      _my_post_descriptor);
+
+#define ADD_BASIC(member, eet_type)                                    \
+   EET_DATA_DESCRIPTOR_ADD_BASIC                                       \
+     (_my_cache_descriptor, My_Cache, #member, member, eet_type)
+   ADD_BASIC(version, EET_T_UINT);
+#undef ADD_BASIC
+
+   EET_DATA_DESCRIPTOR_ADD_LIST
+     (_my_cache_descriptor, My_Cache, "accounts", accounts,
+      _my_account_descriptor);
+}
+
+static void
+_my_cache_descriptor_shutdown(void)
+{
+   eet_data_descriptor_free(_my_cache_descriptor);
+   eet_data_descriptor_free(_my_account_descriptor);
+   eet_data_descriptor_free(_my_message_descriptor);
+   eet_data_descriptor_free(_my_post_descriptor);
+}
+
+// need to check if the pointer came from mmaped area in eet_dictionary
+// or it was allocated with eina_stringshare_add()
+static void
+_eet_string_free(const char *str)
+{
+   if (!str)
+     return;
+   if ((_my_cache_dict) && (eet_dictionary_string_check(_my_cache_dict, str)))
+     return;
+   eina_stringshare_del(str);
+}
+
+static My_Message *
+_my_message_new(const char *message)
+{
+   My_Message *msg = calloc(1, sizeof(My_Message));
+   if (!msg)
+     {
+       fprintf(stderr, "ERROR: could not calloc My_Message\n");
+       return NULL;
+     }
+   msg->message = eina_stringshare_add(message);
+   return msg;
+}
+
+static void
+_my_message_free(My_Message *msg)
+{
+   _eet_string_free(msg->screen_name);
+   _eet_string_free(msg->name);
+   _eet_string_free(msg->message);
+   free(msg);
+}
+
+static My_Post *
+_my_post_new(const char *message)
+{
+   My_Post *post = calloc(1, sizeof(My_Post));
+   if (!post)
+     {
+       fprintf(stderr, "ERROR: could not calloc My_Post\n");
+       return NULL;
+     }
+   post->message = eina_stringshare_add(message);
+   return post;
+}
+
+static void
+_my_post_free(My_Post *post)
+{
+   _eet_string_free(post->dm_to);
+   _eet_string_free(post->message);
+   free(post);
+}
+
+static My_Account *
+_my_account_new(const char *name)
+{
+   My_Account *acc = calloc(1, sizeof(My_Account));
+   if (!acc)
+     {
+       fprintf(stderr, "ERROR: could not calloc My_Account\n");
+       return NULL;
+     }
+   acc->name = eina_stringshare_add(name);
+   return acc;
+}
+
+static void
+_my_account_free(My_Account *acc)
+{
+   My_Message *m;
+   My_Post *p;
+
+   _eet_string_free(acc->name);
+
+   EINA_LIST_FREE(acc->messages, m)
+     _my_message_free(m);
+
+   EINA_LIST_FREE(acc->posts, p)
+     _my_post_free(p);
+
+   free(acc);
+}
+
+static My_Cache *
+_my_cache_new(void)
+{
+   My_Cache *my_cache = calloc(1, sizeof(My_Cache));
+   if (!my_cache)
+     {
+       fprintf(stderr, "ERROR: could not calloc My_Cache\n");
+       return NULL;
+     }
+
+   my_cache->version = 1;
+   return my_cache;
+}
+
+static void
+_my_cache_free(My_Cache *my_cache)
+{
+   My_Account *acc;
+   EINA_LIST_FREE(my_cache->accounts, acc)
+     _my_account_free(acc);
+   free(my_cache);
+}
+
+static My_Account *
+_my_cache_account_find(My_Cache *my_cache, const char *name)
+{
+   My_Account *acc;
+   Eina_List *l;
+   EINA_LIST_FOREACH(my_cache->accounts, l, acc)
+     if (strcmp(acc->name, name) == 0)
+       return acc;
+   return NULL;
+}
+
+static My_Cache *
+_my_cache_load(const char *filename)
+{
+   My_Cache *my_cache;
+   Eet_File *ef = eet_open(filename, EET_FILE_MODE_READ);
+   if (!ef)
+     {
+       fprintf(stderr, "ERROR: could not open '%s' for read\n", filename);
+       return NULL;
+     }
+
+   my_cache = eet_data_read(ef, _my_cache_descriptor, MY_CACHE_FILE_ENTRY);
+   if (!my_cache)
+     {
+       eet_close(ef);
+       return NULL;
+     }
+
+   if (my_cache->version < 1)
+     {
+       fprintf(stderr,
+               "WARNING: version %#x was too old, upgrading it to %#x\n",
+               my_cache->version, 1);
+
+       my_cache->version = 1;
+     }
+
+   if (_my_cache_file)
+     eet_close(_my_cache_file);
+   _my_cache_file = ef;
+   _my_cache_dict = eet_dictionary_get(ef);
+
+   return my_cache;
+}
+
+static Eina_Bool
+_my_cache_save(const My_Cache *my_cache, const char *filename)
+{
+   char tmp[PATH_MAX];
+   Eet_File *ef;
+   Eina_Bool ret;
+   unsigned int i, len;
+   struct stat st;
+
+   len = eina_strlcpy(tmp, filename, sizeof(tmp));
+   if (len + 12 >= (int)sizeof(tmp))
+     {
+       fprintf(stderr, "ERROR: file name is too big: %s\n", filename);
+       return EINA_FALSE;
+     }
+
+   i = 0;
+   do
+     {
+       snprintf(tmp + len, 12, ".%u", i);
+       i++;
+     }
+   while (stat(tmp, &st) == 0);
+
+   ef = eet_open(tmp, EET_FILE_MODE_WRITE);
+   if (!ef)
+     {
+       fprintf(stderr, "ERROR: could not open '%s' for write\n", tmp);
+       return EINA_FALSE;
+     }
+
+   ret = eet_data_write
+     (ef, _my_cache_descriptor, MY_CACHE_FILE_ENTRY, my_cache, EINA_TRUE);
+
+   // VERY IMPORTANT NOTE:
+   // after eet_close(), all strings mmaped from file will be GONE, invalid!
+   // you'll need to free the old cache and open the new one.
+   // For cache this is okay, as you should be saving not so often or just
+   // at end.
+   //
+   // This is a trade off, you save memory by using mmap()ed strings, but
+   // you have to care about this.
+   eet_close(ef);
+
+   if (ret)
+     {
+       unlink(filename);
+       rename(tmp, filename);
+     }
+
+   return ret;
+}
+
+int main(int argc, char *argv[])
+{
+   My_Cache *my_cache;
+   const Eina_List *l_acc;
+   My_Account *acc;
+   int ret = 0;
+
+   if (argc < 3)
+     {
+       fprintf(stderr,
+               "Usage:\n\t%s <input> <output> [action] [action-params]\n\n"
+               "Where actions and their parameters:\n"
+               "\tacc <name>\n"
+               "\tpost <account-name> <message>\n"
+               "\tmessage <account-name> <message>\n"
+               "\n",
+               argv[0]);
+       return -1;
+     }
+
+   eina_init();
+   eet_init();
+   _my_cache_descriptor_init();
+
+   my_cache = _my_cache_load(argv[1]);
+   if (!my_cache)
+     {
+       printf("creating new cache.\n");
+       my_cache = _my_cache_new();
+       if (!my_cache)
+         {
+            ret = -2;
+            goto end;
+         }
+     }
+
+   if (argc > 3)
+     {
+       if (strcmp(argv[3], "acc") == 0)
+         {
+            if (argc == 5)
+              {
+                 My_Account *acc = _my_cache_account_find(my_cache, argv[4]);
+                 if (!acc)
+                   {
+                      acc = _my_account_new(argv[4]);
+                      my_cache->accounts = eina_list_append
+                        (my_cache->accounts, acc);
+                   }
+                 else
+                   fprintf(stderr, "ERROR: account '%s' already exists.\n",
+                           argv[4]);
+              }
+            else
+              fprintf(stderr, "ERROR: wrong number of parameters (%d).\n",
+                      argc);
+         }
+       else if (strcmp(argv[3], "post") == 0)
+         {
+            if (argc == 6)
+              {
+                 My_Account *acc = _my_cache_account_find(my_cache, argv[4]);
+                 if (acc)
+                   {
+                      My_Post *post = _my_post_new(argv[5]);
+                      acc->posts = eina_list_append(acc->posts, post);
+                   }
+                 else
+                   fprintf(stderr, "ERROR: unknown account: '%s'\n", argv[4]);
+              }
+            else
+              fprintf(stderr, "ERROR: wrong number of parameters (%d).\n",
+                      argc);
+         }
+       else if (strcmp(argv[3], "message") == 0)
+         {
+            if (argc == 6)
+              {
+                 My_Account *acc = _my_cache_account_find(my_cache, argv[4]);
+                 if (acc)
+                   {
+                      My_Message *msg = _my_message_new(argv[5]);
+                      acc->messages = eina_list_append(acc->messages, msg);
+                   }
+                 else
+                   fprintf(stderr, "ERROR: unknown account: '%s'\n", argv[4]);
+              }
+            else
+              fprintf(stderr, "ERROR: wrong number of parameters (%d).\n",
+                      argc);
+         }
+       else
+         fprintf(stderr, "ERROR: unknown action '%s'\n", argv[2]);
+     }
+
+   printf("My_Cache:\n"
+         "\tversion.: %#x\n"
+         "\taccounts: %u\n",
+         my_cache->version,
+         eina_list_count(my_cache->accounts));
+   EINA_LIST_FOREACH(my_cache->accounts, l_acc, acc)
+     {
+       const My_Post *post;
+
+       printf("\t  > %-#8x '%.20s' stats: m=%u, p=%u\n",
+              acc->id, acc->name ? acc->name : "",
+              eina_list_count(acc->messages),
+              eina_list_count(acc->posts));
+
+       if (eina_list_count(acc->messages))
+         {
+            const Eina_List *l;
+            const My_Message *msg;
+            printf("\t  |messages:\n");
+
+            EINA_LIST_FOREACH(acc->messages, l, msg)
+              {
+                 printf("\t  |   %-8x '%s' [%s]: '%.20s'\n",
+                        msg->id,
+                        msg->name ? msg->name : "",
+                        msg->screen_name ? msg->screen_name : "",
+                        msg->message ? msg->message : "");
+              }
+         }
+
+       if (eina_list_count(acc->posts))
+         {
+            const Eina_List *l;
+            const My_Post *post;
+            printf("\t  |posts:\n");
+
+            EINA_LIST_FOREACH(acc->posts, l, post)
+              {
+                 if (post->dm_to)
+                   printf("\t  |  @%s: '%.20s'\n", post->dm_to, post->message);
+                 else
+                   printf("\t  |  '%.20s'\n", post->message);
+              }
+         }
+       printf("\n");
+     }
+
+   if (!_my_cache_save(my_cache, argv[2]))
+     ret = -3;
+
+   _my_cache_free(my_cache);
+
+ end:
+   _my_cache_descriptor_shutdown();
+   eet_shutdown();
+   eina_shutdown();
+
+   return ret;
+}
diff --git a/src/examples/eet-data-nested.c b/src/examples/eet-data-nested.c
new file mode 100644 (file)
index 0000000..52a095d
--- /dev/null
@@ -0,0 +1,264 @@
+#include <Eina.h>
+#include <Eet.h>
+#include <stdio.h>
+#include <limits.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+// The struct that will be loaded and saved.
+// note that only the members described in the eet_data_descriptor
+// will be automatically handled. The other members will have their
+// space reserved and zeroed (as it uses calloc()), but not
+// saved or loaded from eet files.
+typedef struct {
+   unsigned int version; // it is recommended to use versioned configuration!
+   const char *name;
+   int id;
+   int not_saved_value; // example of not saved data inside!
+   Eina_Bool enabled;
+   Eina_List *subs;
+} My_Conf_Type;
+
+typedef struct {
+   const char *server;
+   int port;
+} My_Conf_Subtype;
+
+// string that represents the entry in eet file, you might like to have
+// different profiles or so in the same file, this is possible with
+// different strings
+static const char MY_CONF_FILE_ENTRY[] = "config";
+
+
+// keep the descriptor static global, so it can be
+// shared by different functions (load/save) of this and only this
+// file.
+static Eet_Data_Descriptor *_my_conf_descriptor;
+static Eet_Data_Descriptor *_my_conf_sub_descriptor;
+
+static void
+_my_conf_descriptor_init(void)
+{
+   Eet_Data_Descriptor_Class eddc;
+
+   // The class describe the functions to use to create the type and its
+   // full allocated size.
+   //
+   // Eina types are very convenient, so use them to create the descriptor,
+   // so we get eina_list,  eina_hash and eina_stringshare automatically!
+   //
+   // The STREAM variant is better for configuration files as the values
+   // will likely change a lot.
+   //
+   // The other variant, FILE, is good for caches and things that are just
+   // appended, but needs to take care when changing strings and files must
+   // be kept open so mmap()ed strings will be kept alive.
+   EET_EINA_STREAM_DATA_DESCRIPTOR_CLASS_SET(&eddc, My_Conf_Type);
+   _my_conf_descriptor = eet_data_descriptor_stream_new(&eddc);
+
+   EET_EINA_STREAM_DATA_DESCRIPTOR_CLASS_SET(&eddc, My_Conf_Subtype);
+   _my_conf_sub_descriptor = eet_data_descriptor_stream_new(&eddc);
+
+   // Describe the members to be saved:
+   // Use a temporary macro so we don't type a lot, also avoid errors:
+#define MY_CONF_ADD_BASIC(member, eet_type)                            \
+   EET_DATA_DESCRIPTOR_ADD_BASIC                                       \
+     (_my_conf_descriptor, My_Conf_Type, #member, member, eet_type)
+#define MY_CONF_SUB_ADD_BASIC(member, eet_type)                                \
+   EET_DATA_DESCRIPTOR_ADD_BASIC                                       \
+     (_my_conf_sub_descriptor, My_Conf_Subtype, #member, member, eet_type)
+
+   MY_CONF_SUB_ADD_BASIC(server, EET_T_STRING);
+   MY_CONF_SUB_ADD_BASIC(port, EET_T_INT);
+
+   MY_CONF_ADD_BASIC(version, EET_T_UINT);
+   MY_CONF_ADD_BASIC(name, EET_T_STRING);
+   MY_CONF_ADD_BASIC(id, EET_T_INT);
+   MY_CONF_ADD_BASIC(enabled, EET_T_UCHAR);
+
+   // And add the sub descriptor as a linked list at 'subs' in the main struct
+   EET_DATA_DESCRIPTOR_ADD_LIST
+     (_my_conf_descriptor, My_Conf_Type, "subs", subs, _my_conf_sub_descriptor);
+
+#undef MY_CONF_ADD_BASIC
+#undef MY_CONF_SUB_ADD_BASIC
+}
+
+static void
+_my_conf_descriptor_shutdown(void)
+{
+   eet_data_descriptor_free(_my_conf_sub_descriptor);
+   eet_data_descriptor_free(_my_conf_descriptor);
+}
+
+static My_Conf_Type *
+_my_conf_new(void)
+{
+   My_Conf_Type *my_conf = calloc(1, sizeof(My_Conf_Type));
+   My_Conf_Subtype *sub;
+   if (!my_conf)
+     {
+       fprintf(stderr, "ERROR: could not calloc My_Conf_Type\n");
+       return NULL;
+     }
+
+   my_conf->version = 0x112233;
+   my_conf->enabled = EINA_TRUE;
+
+   sub = calloc(1, sizeof(My_Conf_Subtype));
+   if (sub)
+     {
+       sub->server = eina_stringshare_add("my-server.com");
+       sub->port = 1234;
+       my_conf->subs = eina_list_append(my_conf->subs, sub);
+     }
+
+   return my_conf;
+}
+
+static void
+_my_conf_free(My_Conf_Type *my_conf)
+{
+   My_Conf_Subtype *sub;
+   EINA_LIST_FREE(my_conf->subs, sub)
+     {
+       eina_stringshare_del(sub->server);
+       free(sub);
+     }
+
+   eina_stringshare_del(my_conf->name);
+   free(my_conf);
+}
+
+static My_Conf_Type *
+_my_conf_load(const char *filename)
+{
+   My_Conf_Type *my_conf;
+   Eet_File *ef = eet_open(filename, EET_FILE_MODE_READ);
+   if (!ef)
+     {
+       fprintf(stderr, "ERROR: could not open '%s' for read\n", filename);
+       return NULL;
+     }
+
+   my_conf = eet_data_read(ef, _my_conf_descriptor, MY_CONF_FILE_ENTRY);
+   if (!my_conf)
+     goto end;
+
+   if (my_conf->version < 0x112233)
+     {
+       fprintf(stderr,
+               "WARNING: version %#x was too old, upgrading it to %#x\n",
+               my_conf->version, 0x112233);
+
+       my_conf->version = 0x112233;
+       my_conf->enabled = EINA_TRUE;
+     }
+
+ end:
+   eet_close(ef);
+   return my_conf;
+}
+
+static Eina_Bool
+_my_conf_save(const My_Conf_Type *my_conf, const char *filename)
+{
+   char tmp[PATH_MAX];
+   Eet_File *ef;
+   Eina_Bool ret;
+   unsigned int i, len;
+   struct stat st;
+
+   len = eina_strlcpy(tmp, filename, sizeof(tmp));
+   if (len + 12 >= (int)sizeof(tmp))
+     {
+       fprintf(stderr, "ERROR: file name is too big: %s\n", filename);
+       return EINA_FALSE;
+     }
+
+   i = 0;
+   do
+     {
+       snprintf(tmp + len, 12, ".%u", i);
+       i++;
+     }
+   while (stat(tmp, &st) == 0);
+
+   ef = eet_open(tmp, EET_FILE_MODE_WRITE);
+   if (!ef)
+     {
+       fprintf(stderr, "ERROR: could not open '%s' for write\n", tmp);
+       return EINA_FALSE;
+     }
+
+   ret = eet_data_write
+     (ef, _my_conf_descriptor, MY_CONF_FILE_ENTRY, my_conf, EINA_TRUE);
+   eet_close(ef);
+
+   if (ret)
+     {
+       unlink(filename);
+       rename(tmp, filename);
+     }
+
+   return ret;
+}
+
+int main(int argc, char *argv[])
+{
+   My_Conf_Type *my_conf;
+   const My_Conf_Subtype *sub;
+   const Eina_List *l;
+   int ret = 0;
+
+   if (argc != 3)
+     {
+       fprintf(stderr, "Usage:\n\t%s <input> <output>\n\n", argv[0]);
+       return -1;
+     }
+
+   eina_init();
+   eet_init();
+   _my_conf_descriptor_init();
+
+   my_conf = _my_conf_load(argv[1]);
+   if (!my_conf)
+     {
+       printf("creating new configuration.\n");
+       my_conf = _my_conf_new();
+       if (!my_conf)
+         {
+            ret = -2;
+            goto end;
+         }
+     }
+
+   printf("My_Conf_Type:\n"
+         "\tversion: %#x\n"
+         "\tname...: '%s'\n"
+         "\tid.....: %d\n"
+         "\tenabled: %hhu\n"
+         "\tsubs...:\n",
+         my_conf->version,
+         my_conf->name ? my_conf->name : "",
+         my_conf->id,
+         my_conf->enabled);
+
+   EINA_LIST_FOREACH(my_conf->subs, l, sub)
+     printf("\t\tserver: '%s', port: %d\n",
+           sub->server ? sub->server : "",
+           sub->port);
+
+   if (!_my_conf_save(my_conf, argv[2]))
+     ret = -3;
+
+   _my_conf_free(my_conf);
+
+ end:
+   _my_conf_descriptor_shutdown();
+   eet_shutdown();
+   eina_shutdown();
+
+   return ret;
+}
diff --git a/src/examples/eet-data-simple.c b/src/examples/eet-data-simple.c
new file mode 100644 (file)
index 0000000..72d00c9
--- /dev/null
@@ -0,0 +1,217 @@
+#include <Eina.h>
+#include <Eet.h>
+#include <stdio.h>
+#include <limits.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+// The struct that will be loaded and saved.
+// note that only the members described in the eet_data_descriptor
+// will be automatically handled. The other members will have their
+// space reserved and zeroed (as it uses calloc()), but not
+// saved or loaded from eet files.
+typedef struct {
+   unsigned int version; // it is recommended to use versioned configuration!
+   const char *name;
+   int id;
+   int not_saved_value; // example of not saved data inside!
+   Eina_Bool enabled;
+} My_Conf_Type;
+
+// string that represents the entry in eet file, you might like to have
+// different profiles or so in the same file, this is possible with
+// different strings
+static const char MY_CONF_FILE_ENTRY[] = "config";
+
+
+// keep the descriptor static global, so it can be
+// shared by different functions (load/save) of this and only this
+// file.
+static Eet_Data_Descriptor *_my_conf_descriptor;
+
+static void
+_my_conf_descriptor_init(void)
+{
+   Eet_Data_Descriptor_Class eddc;
+
+   // The class describe the functions to use to create the type and its
+   // full allocated size.
+   //
+   // Eina types are very convenient, so use them to create the descriptor,
+   // so we get eina_list,  eina_hash and eina_stringshare automatically!
+   //
+   // The STREAM variant is better for configuration files as the values
+   // will likely change a lot.
+   //
+   // The other variant, FILE, is good for caches and things that are just
+   // appended, but needs to take care when changing strings and files must
+   // be kept open so mmap()ed strings will be kept alive.
+   EET_EINA_STREAM_DATA_DESCRIPTOR_CLASS_SET(&eddc, My_Conf_Type);
+   _my_conf_descriptor = eet_data_descriptor_stream_new(&eddc);
+
+   // Describe the members to be saved:
+   // Use a temporary macro so we don't type a lot, also avoid errors:
+#define MY_CONF_ADD_BASIC(member, eet_type)                            \
+   EET_DATA_DESCRIPTOR_ADD_BASIC                                       \
+     (_my_conf_descriptor, My_Conf_Type, #member, member, eet_type)
+
+   MY_CONF_ADD_BASIC(version, EET_T_UINT);
+   MY_CONF_ADD_BASIC(name, EET_T_STRING);
+   MY_CONF_ADD_BASIC(id, EET_T_INT);
+   MY_CONF_ADD_BASIC(enabled, EET_T_UCHAR);
+
+#undef MY_CONF_ADD_BASIC
+}
+
+static void
+_my_conf_descriptor_shutdown(void)
+{
+   eet_data_descriptor_free(_my_conf_descriptor);
+}
+
+static My_Conf_Type *
+_my_conf_new(void)
+{
+   My_Conf_Type *my_conf = calloc(1, sizeof(My_Conf_Type));
+   if (!my_conf)
+     {
+       fprintf(stderr, "ERROR: could not calloc My_Conf_Type\n");
+       return NULL;
+     }
+
+   my_conf->version = 0x112233;
+   my_conf->enabled = EINA_TRUE;
+   return my_conf;
+}
+
+static void
+_my_conf_free(My_Conf_Type *my_conf)
+{
+   eina_stringshare_del(my_conf->name);
+   free(my_conf);
+}
+
+static My_Conf_Type *
+_my_conf_load(const char *filename)
+{
+   My_Conf_Type *my_conf;
+   Eet_File *ef = eet_open(filename, EET_FILE_MODE_READ);
+   if (!ef)
+     {
+       fprintf(stderr, "ERROR: could not open '%s' for read\n", filename);
+       return NULL;
+     }
+
+   my_conf = eet_data_read(ef, _my_conf_descriptor, MY_CONF_FILE_ENTRY);
+   if (!my_conf)
+     goto end;
+
+   if (my_conf->version < 0x112233)
+     {
+       fprintf(stderr,
+               "WARNING: version %#x was too old, upgrading it to %#x\n",
+               my_conf->version, 0x112233);
+
+       my_conf->version = 0x112233;
+       my_conf->enabled = EINA_TRUE;
+     }
+
+ end:
+   eet_close(ef);
+   return my_conf;
+}
+
+static Eina_Bool
+_my_conf_save(const My_Conf_Type *my_conf, const char *filename)
+{
+   char tmp[PATH_MAX];
+   Eet_File *ef;
+   Eina_Bool ret;
+   unsigned int i, len;
+   struct stat st;
+
+   len = eina_strlcpy(tmp, filename, sizeof(tmp));
+   if (len + 12 >= (int)sizeof(tmp))
+     {
+       fprintf(stderr, "ERROR: file name is too big: %s\n", filename);
+       return EINA_FALSE;
+     }
+
+   i = 0;
+   do
+     {
+       snprintf(tmp + len, 12, ".%u", i);
+       i++;
+     }
+   while (stat(tmp, &st) == 0);
+
+   ef = eet_open(tmp, EET_FILE_MODE_WRITE);
+   if (!ef)
+     {
+       fprintf(stderr, "ERROR: could not open '%s' for write\n", tmp);
+       return EINA_FALSE;
+     }
+
+   ret = eet_data_write
+     (ef, _my_conf_descriptor, MY_CONF_FILE_ENTRY, my_conf, EINA_TRUE);
+   eet_close(ef);
+
+   if (ret)
+     {
+       unlink(filename);
+       rename(tmp, filename);
+     }
+
+   return ret;
+}
+
+int main(int argc, char *argv[])
+{
+   My_Conf_Type *my_conf;
+   int ret = 0;
+
+   if (argc != 3)
+     {
+       fprintf(stderr, "Usage:\n\t%s <input> <output>\n\n", argv[0]);
+       return -1;
+     }
+
+   eina_init();
+   eet_init();
+   _my_conf_descriptor_init();
+
+   my_conf = _my_conf_load(argv[1]);
+   if (!my_conf)
+     {
+       printf("creating new configuration.\n");
+       my_conf = _my_conf_new();
+       if (!my_conf)
+         {
+            ret = -2;
+            goto end;
+         }
+     }
+
+   printf("My_Conf_Type:\n"
+         "\tversion: %#x\n"
+         "\tname...: '%s'\n"
+         "\tid.....: %d\n"
+         "\tenabled: %hhu\n",
+         my_conf->version,
+         my_conf->name ? my_conf->name : "",
+         my_conf->id,
+         my_conf->enabled);
+
+   if (!_my_conf_save(my_conf, argv[2]))
+     ret = -3;
+
+   _my_conf_free(my_conf);
+
+ end:
+   _my_conf_descriptor_shutdown();
+   eet_shutdown();
+   eina_shutdown();
+
+   return ret;
+}