aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/ecore/src/lib/ecore_config/ecore_config_storage.c
blob: d059645bc0a0b0ddaa21bc9be89aae21b6b29d59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>

#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "Ecore_Config.h"
#include "ecore_config_private.h"

/**
 * Loads the default configuration.
 * @return  @c ECORE_CONFIG_ERR_SUCC on success.  @c ECORE_CONFIG_ERR_NODATA
 *          is returned if the file cannot be loaded.
 * @ingroup Ecore_Config_File_Group
 */
EAPI int
ecore_config_load(void)
{
   char                file[PATH_MAX];

   if (!__ecore_config_app_name)
     return ECORE_CONFIG_ERR_FAIL;

   snprintf(file, PATH_MAX, "%s/.e/apps/%s/config.eet", getenv("HOME"),
	    __ecore_config_app_name);
   return ecore_config_file_load(file);
}

/**
 * Saves the current configuration to the default file.
 * @return  @c ECORE_CONFIG_ERR_SUCC is returned on success.
 *          @c ECORE_CONFIG_ERR_FAIL is returned if the data cannot be
 *          saved.
 * @ingroup Ecore_Config_File_Group
 */
EAPI int
ecore_config_save(void)
{
   char                file[PATH_MAX];

   if (!__ecore_config_app_name)
     return ECORE_CONFIG_ERR_FAIL;

   snprintf(file, PATH_MAX, "%s/.e/apps/%s/config.eet", getenv("HOME"),
	    __ecore_config_app_name);
   return ecore_config_file_save(file);
}

/**
 * Load the given configuration file to the local configuration.
 * @param   file Name of the file to load.
 * @return  @c ECORE_CONFIG_ERR_SUCC on success.  @c ECORE_CONFIG_ERR_NODATA
 *          is returned if the file cannot be loaded.
 * @ingroup Ecore_Config_File_Group
 */
EAPI int
ecore_config_file_load(const char *file)
{
   Ecore_Config_DB_File  *db;
   char                 **keys;
   int                    key_count;
   int                    x;
   // double                 ftmp;	UNUSED
   // int                    pt;	UNUSED
   // int                    itmp; 	UNUSED
   // Ecore_Config_Type      type;	UNUSED
   char                   *data;

   db = NULL;
   data = NULL;

   db = _ecore_config_db_open_read(file);
   if (!db)
     {
	ERR("Cannot open database from file %s!", file);
	return ECORE_CONFIG_ERR_NODATA;
     }
   key_count = 0;
   keys = _ecore_config_db_keys_get(db, &key_count);
   if (keys)
     {
	for (x = 0; x < key_count; x++)
	  {
	    _ecore_config_db_read(db, keys[x]);
	  }
     }
   _ecore_config_db_close(db);
   if (keys)
     {
	for (x = 0; x < key_count; x++)
	  {
	     free(keys[x]);
	  }
	free(keys);
     }
   return ECORE_CONFIG_ERR_SUCC;
}

static void
_ecore_config_recurse_mkdir(const char *file)
{
   char               *file_ptr;
   char               *file_tmp;
   struct stat         status;

   file_tmp = strdup(file);
   file_ptr = file_tmp + strlen(file_tmp);
   while (*file_ptr != '/' && file_ptr > file_tmp)
      file_ptr--;
   *file_ptr = '\0';

   if ((file_tmp[0] != 0) && stat(file_tmp, &status))
     {
	_ecore_config_recurse_mkdir(file_tmp);
	mkdir(file_tmp, S_IRUSR | S_IWUSR | S_IXUSR);
     }
   free(file_tmp);
}

/**
 * Saves the local configuration to the given file.
 * @param   file Name of the file to save to.
 * @return  @c ECORE_CONFIG_ERR_SUCC is returned on success.
 *          @c ECORE_CONFIG_ERR_FAIL is returned if the data cannot be
 *          saved.
 * @ingroup Ecore_Config_File_Group
 */
EAPI int
ecore_config_file_save(const char *file)
{
   Ecore_Config_Prop    *next;
   Ecore_Config_DB_File *db;
   struct stat           status;

   next = __ecore_config_bundle_local->data;
   db = NULL;

   /* if file does not exist check to see if the dirs exist, creating if not */
   if (stat(file, &status))
      _ecore_config_recurse_mkdir(file);

   db = _ecore_config_db_open_write(file);
   if (!db)
     {
	ERR("Cannot open database from file %s!", file);
	return ECORE_CONFIG_ERR_FAIL;
     }

   while (next)
     {
	/* let the config_db deal with this
	 * handyande: hmm, not sure that it ever does - reinstating until
	 * further discussions satisfy me!
	 */
	if (!(next->flags & ECORE_CONFIG_FLAG_MODIFIED) || next->flags & ECORE_CONFIG_FLAG_CMDLN)
	  {
	     next = next->next;
	     continue;
	  }

	_ecore_config_db_write(db, next);

	next = next->next;
     }

   _ecore_config_db_close(db);
   return ECORE_CONFIG_ERR_SUCC;
}