Tizen Native API  3.0
Simple data example
00001 //Compile with:
00002 // gcc -o eet-data-simple eet-data-simple.c `pkg-config --cflags --libs eet eina`
00003 
00004 #include <Eina.h>
00005 #include <Eet.h>
00006 #include <stdio.h>
00007 #include <limits.h>
00008 #include <sys/types.h>
00009 #include <sys/stat.h>
00010 #include <unistd.h>
00011 
00012 // The struct that will be loaded and saved.
00013 // note that only the members described in the eet_data_descriptor
00014 // will be automatically handled. The other members will have their
00015 // space reserved and zeroed (as it uses calloc()), but not
00016 // saved or loaded from eet files.
00017 typedef struct
00018 {
00019    unsigned int version; // it is recommended to use versioned configuration!
00020    const char  *name;
00021    int          id;
00022    int          not_saved_value; // example of not saved data inside!
00023    Eina_Bool    enabled;
00024 } My_Conf_Type;
00025 
00026 // string that represents the entry in eet file, you might like to have
00027 // different profiles or so in the same file, this is possible with
00028 // different strings
00029 static const char MY_CONF_FILE_ENTRY[] = "config";
00030 
00031 // keep the descriptor static global, so it can be
00032 // shared by different functions (load/save) of this and only this
00033 // file.
00034 static Eet_Data_Descriptor *_my_conf_descriptor;
00035 
00036 static void
00037 _my_conf_descriptor_init(void)
00038 {
00039    Eet_Data_Descriptor_Class eddc;
00040 
00041    // The class describe the functions to use to create the type and its
00042    // full allocated size.
00043    //
00044    // Eina types are very convenient, so use them to create the descriptor,
00045    // so we get eina_list,  eina_hash and eina_stringshare automatically!
00046    //
00047    // The STREAM variant is better for configuration files as the values
00048    // will likely change a lot.
00049    //
00050    // The other variant, FILE, is good for caches and things that are just
00051    // appended, but needs to take care when changing strings and files must
00052    // be kept open so mmap()ed strings will be kept alive.
00053    EET_EINA_STREAM_DATA_DESCRIPTOR_CLASS_SET(&eddc, My_Conf_Type);
00054    _my_conf_descriptor = eet_data_descriptor_stream_new(&eddc);
00055 
00056    // Describe the members to be saved:
00057    // Use a temporary macro so we don't type a lot, also avoid errors:
00058 #define MY_CONF_ADD_BASIC(member, eet_type) \
00059   EET_DATA_DESCRIPTOR_ADD_BASIC             \
00060     (_my_conf_descriptor, My_Conf_Type, # member, member, eet_type)
00061 
00062    MY_CONF_ADD_BASIC(version, EET_T_UINT);
00063    MY_CONF_ADD_BASIC(name, EET_T_STRING);
00064    MY_CONF_ADD_BASIC(id, EET_T_INT);
00065    MY_CONF_ADD_BASIC(enabled, EET_T_UCHAR);
00066 
00067 #undef MY_CONF_ADD_BASIC
00068 } /* _my_conf_descriptor_init */
00069 
00070 static void
00071 _my_conf_descriptor_shutdown(void)
00072 {
00073    eet_data_descriptor_free(_my_conf_descriptor);
00074 } /* _my_conf_descriptor_shutdown */
00075 
00076 static My_Conf_Type *
00077 _my_conf_new(void)
00078 {
00079    My_Conf_Type *my_conf = calloc(1, sizeof(My_Conf_Type));
00080    if (!my_conf)
00081      {
00082         fprintf(stderr, "ERROR: could not calloc My_Conf_Type\n");
00083         return NULL;
00084      }
00085 
00086    my_conf->version = 0x112233;
00087    my_conf->enabled = EINA_TRUE;
00088    return my_conf;
00089 } /* _my_conf_new */
00090 
00091 static void
00092 _my_conf_free(My_Conf_Type *my_conf)
00093 {
00094    eina_stringshare_del(my_conf->name);
00095    free(my_conf);
00096 } /* _my_conf_free */
00097 
00098 static My_Conf_Type *
00099 _my_conf_load(const char *filename)
00100 {
00101    My_Conf_Type *my_conf;
00102    Eet_File *ef = eet_open(filename, EET_FILE_MODE_READ);
00103    if (!ef)
00104      {
00105         fprintf(stderr, "ERROR: could not open '%s' for read\n", filename);
00106         return NULL;
00107      }
00108 
00109    my_conf = eet_data_read(ef, _my_conf_descriptor, MY_CONF_FILE_ENTRY);
00110    if (!my_conf)
00111      goto end;
00112 
00113    if (my_conf->version < 0x112233)
00114      {
00115         fprintf(stderr,
00116                 "WARNING: version %#x was too old, upgrading it to %#x\n",
00117                 my_conf->version, 0x112233);
00118 
00119         my_conf->version = 0x112233;
00120         my_conf->enabled = EINA_TRUE;
00121      }
00122 
00123 end:
00124    eet_close(ef);
00125    return my_conf;
00126 } /* _my_conf_load */
00127 
00128 static Eina_Bool
00129 _my_conf_save(const My_Conf_Type *my_conf,
00130               const char         *filename)
00131 {
00132    char tmp[PATH_MAX];
00133    Eet_File *ef;
00134    Eina_Bool ret;
00135    unsigned int i, len;
00136    struct stat st;
00137 
00138    len = eina_strlcpy(tmp, filename, sizeof(tmp));
00139    if (len + 12 >= (int)sizeof(tmp))
00140      {
00141         fprintf(stderr, "ERROR: file name is too big: %s\n", filename);
00142         return EINA_FALSE;
00143      }
00144 
00145    i = 0;
00146    do
00147      {
00148         snprintf(tmp + len, 12, ".%u", i);
00149         i++;
00150      }
00151    while (stat(tmp, &st) == 0);
00152 
00153    ef = eet_open(tmp, EET_FILE_MODE_WRITE);
00154    if (!ef)
00155      {
00156         fprintf(stderr, "ERROR: could not open '%s' for write\n", tmp);
00157         return EINA_FALSE;
00158      }
00159 
00160    ret = eet_data_write
00161        (ef, _my_conf_descriptor, MY_CONF_FILE_ENTRY, my_conf, EINA_TRUE);
00162    eet_close(ef);
00163 
00164    if (ret)
00165      {
00166         unlink(filename);
00167         rename(tmp, filename);
00168      }
00169 
00170    return ret;
00171 } /* _my_conf_save */
00172 
00173 int
00174 main(int   argc,
00175      char *argv[])
00176 {
00177    My_Conf_Type *my_conf;
00178    int ret = 0;
00179 
00180    if (argc != 3)
00181      {
00182         fprintf(stderr, "Usage:\n\t%s <input> <output>\n\n", argv[0]);
00183         return -1;
00184      }
00185 
00186    eina_init();
00187    eet_init();
00188    _my_conf_descriptor_init();
00189 
00190    my_conf = _my_conf_load(argv[1]);
00191    if (!my_conf)
00192      {
00193         printf("creating new configuration.\n");
00194         my_conf = _my_conf_new();
00195         if (!my_conf)
00196           {
00197              ret = -2;
00198              goto end;
00199           }
00200      }
00201 
00202    printf("My_Conf_Type:\n"
00203           "\tversion: %#x\n"
00204           "\tname...: '%s'\n"
00205           "\tid.....: %d\n"
00206           "\tenabled: %hhu\n",
00207           my_conf->version,
00208           my_conf->name ? my_conf->name : "",
00209           my_conf->id,
00210           my_conf->enabled);
00211 
00212    if (!_my_conf_save(my_conf, argv[2]))
00213      ret = -3;
00214 
00215    _my_conf_free(my_conf);
00216 
00217 end:
00218    _my_conf_descriptor_shutdown();
00219    eet_shutdown();
00220    eina_shutdown();
00221 
00222    return ret;
00223 } /* main */
00224