Tizen 2.1 base
[framework/uifw/ecore.git] / src / lib / ecore_config / ecore_config_storage.c
1 #ifdef HAVE_CONFIG_H
2 # include <config.h>
3 #endif
4
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <limits.h>
9
10 #include <sys/param.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <unistd.h>
14
15 #include "Ecore_Config.h"
16 #include "ecore_config_private.h"
17
18 /**
19  * Loads the default configuration.
20  * @return  @c ECORE_CONFIG_ERR_SUCC on success.  @c ECORE_CONFIG_ERR_NODATA
21  *          is returned if the file cannot be loaded.
22  * @ingroup Ecore_Config_File_Group
23  */
24 EAPI int
25 ecore_config_load(void)
26 {
27    char                file[PATH_MAX];
28
29    if (!__ecore_config_app_name)
30      return ECORE_CONFIG_ERR_FAIL;
31
32    snprintf(file, PATH_MAX, "%s/.e/apps/%s/config.eet", getenv("HOME"),
33             __ecore_config_app_name);
34    return ecore_config_file_load(file);
35 }
36
37 /**
38  * Saves the current configuration to the default file.
39  * @return  @c ECORE_CONFIG_ERR_SUCC is returned on success.
40  *          @c ECORE_CONFIG_ERR_FAIL is returned if the data cannot be
41  *          saved.
42  * @ingroup Ecore_Config_File_Group
43  */
44 EAPI int
45 ecore_config_save(void)
46 {
47    char                file[PATH_MAX];
48
49    if (!__ecore_config_app_name)
50      return ECORE_CONFIG_ERR_FAIL;
51
52    snprintf(file, PATH_MAX, "%s/.e/apps/%s/config.eet", getenv("HOME"),
53             __ecore_config_app_name);
54    return ecore_config_file_save(file);
55 }
56
57 /**
58  * Load the given configuration file to the local configuration.
59  * @param   file Name of the file to load.
60  * @return  @c ECORE_CONFIG_ERR_SUCC on success.  @c ECORE_CONFIG_ERR_NODATA
61  *          is returned if the file cannot be loaded.
62  * @ingroup Ecore_Config_File_Group
63  */
64 EAPI int
65 ecore_config_file_load(const char *file)
66 {
67    Ecore_Config_DB_File  *db;
68    char                 **keys;
69    int                    key_count;
70    int                    x;
71    // double                 ftmp;      UNUSED
72    // int                    pt;        UNUSED
73    // int                    itmp;      UNUSED
74    // Ecore_Config_Type      type;      UNUSED
75    char                   *data;
76
77    db = NULL;
78    data = NULL;
79
80    db = _ecore_config_db_open_read(file);
81    if (!db)
82      {
83         ERR("Cannot open database from file %s!", file);
84         return ECORE_CONFIG_ERR_NODATA;
85      }
86    key_count = 0;
87    keys = _ecore_config_db_keys_get(db, &key_count);
88    if (keys)
89      {
90         for (x = 0; x < key_count; x++)
91           {
92             _ecore_config_db_read(db, keys[x]);
93           }
94      }
95    _ecore_config_db_close(db);
96    if (keys)
97      {
98         for (x = 0; x < key_count; x++)
99           {
100              free(keys[x]);
101           }
102         free(keys);
103      }
104    return ECORE_CONFIG_ERR_SUCC;
105 }
106
107 static void
108 _ecore_config_recurse_mkdir(const char *file)
109 {
110    char               *file_ptr;
111    char               *file_tmp;
112    struct stat         status;
113
114    file_tmp = strdup(file);
115    file_ptr = file_tmp + strlen(file_tmp);
116    while (*file_ptr != '/' && file_ptr > file_tmp)
117       file_ptr--;
118    *file_ptr = '\0';
119
120    if ((file_tmp[0] != 0) && stat(file_tmp, &status))
121      {
122         _ecore_config_recurse_mkdir(file_tmp);
123         mkdir(file_tmp, S_IRUSR | S_IWUSR | S_IXUSR);
124      }
125    free(file_tmp);
126 }
127
128 /**
129  * Saves the local configuration to the given file.
130  * @param   file Name of the file to save to.
131  * @return  @c ECORE_CONFIG_ERR_SUCC is returned on success.
132  *          @c ECORE_CONFIG_ERR_FAIL is returned if the data cannot be
133  *          saved.
134  * @ingroup Ecore_Config_File_Group
135  */
136 EAPI int
137 ecore_config_file_save(const char *file)
138 {
139    Ecore_Config_Prop    *next;
140    Ecore_Config_DB_File *db;
141    struct stat           status;
142
143    next = __ecore_config_bundle_local->data;
144    db = NULL;
145
146    /* if file does not exist check to see if the dirs exist, creating if not */
147    if (stat(file, &status))
148       _ecore_config_recurse_mkdir(file);
149
150    db = _ecore_config_db_open_write(file);
151    if (!db)
152      {
153         ERR("Cannot open database from file %s!", file);
154         return ECORE_CONFIG_ERR_FAIL;
155      }
156
157    while (next)
158      {
159         /* let the config_db deal with this
160          * handyande: hmm, not sure that it ever does - reinstating until
161          * further discussions satisfy me!
162          */
163         if (!(next->flags & ECORE_CONFIG_FLAG_MODIFIED) || next->flags & ECORE_CONFIG_FLAG_CMDLN)
164           {
165              next = next->next;
166              continue;
167           }
168
169         _ecore_config_db_write(db, next);
170
171         next = next->next;
172      }
173
174    _ecore_config_db_close(db);
175    return ECORE_CONFIG_ERR_SUCC;
176 }