summaryrefslogtreecommitdiff
path: root/src/lib/efl/interfaces/efl_file.c
blob: 3acdc17ce885dab842933593c4791eaa829ff402 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <Efl.h>

typedef struct _Efl_File_Data Efl_File_Data;
struct _Efl_File_Data
{
   Eina_Stringshare *vpath; /* efl_file_set */
   Eina_Stringshare *key; /* efl_file_key_set */
   Eina_File *file; /* efl_file_mmap_set */
   Eina_Bool file_opened : 1; /* if `file` was opened implicitly during load */
   Eina_Bool setting : 1; /* set when this file is internally calling methods to avoid infinite recursion */
   Eina_Bool loaded : 1; /* whether the currently set file properties have been loaded */
};

EOLIAN static void
_efl_file_unload(Eo *obj, Efl_File_Data *pd)
{
   if (!pd->loaded) return;
   if (!pd->file) return;
   if (!pd->file_opened) return;
   pd->setting = 1;
   eina_file_close(pd->file);
   efl_file_mmap_set(obj, NULL);
   pd->setting = 0;
   pd->loaded = pd->file_opened = EINA_FALSE;
}

EOLIAN static Eina_Error
_efl_file_load(Eo *obj, Efl_File_Data *pd)
{
   Eina_Error ret = 0;

   if (pd->loaded) return 0;
   EINA_SAFETY_ON_NULL_RETURN_VAL(pd->vpath, ENOENT);
   errno = 0;
   if (!pd->file)
     {
        Eina_File *f;
        f = eina_file_open(pd->vpath, EINA_FALSE);
        if (!f) return errno;
        pd->file_opened = EINA_TRUE;
        pd->setting = 1;
        ret = efl_file_mmap_set(obj, f);
        pd->setting = 0;
        if (ret) pd->file_opened = EINA_FALSE;
        eina_file_close(f);
     }
   pd->loaded = !ret;
   return ret;
}

EOLIAN static Eina_Error
_efl_file_mmap_set(Eo *obj, Efl_File_Data *pd, const Eina_File *f)
{
   Eina_Error err = 0;
   Eina_File *file = NULL;

   if (f == pd->file) return 0;
   if (f)
     {
        file = eina_file_dup(f);
        if (!file) return errno;
     }
   if (pd->file) eina_file_close(pd->file);
   pd->file = file;
   pd->loaded = EINA_FALSE;
   
   if (!pd->setting)
     {
        /* avoid infinite recursion */
        pd->setting = 1;
        err = efl_file_set(obj, eina_file_filename_get(pd->file));
        pd->setting = 0;
     }
   return err;
}

EOLIAN static const Eina_File *
_efl_file_mmap_get(const Eo *obj EINA_UNUSED, Efl_File_Data *pd)
{
   return pd->file;
}

EOLIAN static Eina_Error
_efl_file_file_set(Eo *obj, Efl_File_Data *pd, const char *file)
{
   char *tmp;
   Eina_Error err = 0;
   Eina_Bool same;

   tmp = (char*)(file);
   if (tmp)
     tmp = eina_vpath_resolve(tmp);

   same = !eina_stringshare_replace(&pd->vpath, tmp ?: file);
   free(tmp);
   if (same) return err;
   pd->loaded = EINA_FALSE;
   if (!pd->setting)
     {
        pd->setting = 1;
        err = efl_file_mmap_set(obj, NULL);
        pd->setting = 0;
     }
   return err;
}

EOLIAN static Eina_Stringshare *
_efl_file_file_get(const Eo *obj EINA_UNUSED, Efl_File_Data *pd)
{
   return pd->vpath;
}

EOLIAN static void
_efl_file_key_set(Eo *obj EINA_UNUSED, Efl_File_Data *pd, const char *key)
{
   if (eina_stringshare_replace(&pd->key, key))
     pd->loaded = 0;
}

EOLIAN static Eina_Stringshare *
_efl_file_key_get(const Eo *obj EINA_UNUSED, Efl_File_Data *pd)
{
   return pd->key;
}

EOLIAN static Eina_Bool 
_efl_file_loaded_get(const Eo *obj EINA_UNUSED, Efl_File_Data *pd)
{
   return pd->loaded;
}

EOLIAN static void
_efl_file_efl_object_destructor(Eo *obj, Efl_File_Data *pd)
{
   eina_stringshare_del(pd->vpath);
   eina_stringshare_del(pd->key);
   eina_file_close(pd->file);
   efl_destructor(efl_super(obj, EFL_FILE_MIXIN));
}

EOLIAN static Eo *
_efl_file_efl_object_finalize(Eo *obj, Efl_File_Data *pd)
{
   obj = efl_finalize(efl_super(obj, EFL_FILE_MIXIN));
   if (!obj) return NULL;
   if (pd->file || pd->vpath) efl_file_load(obj);
   return obj;
}

////////////////////////////////////////////////////////////////////////////

EAPI Eina_Bool
efl_file_simple_load(Eo *obj, const char *file, const char *key)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(obj, EINA_FALSE);
   efl_ref(obj);
   EINA_SAFETY_ON_TRUE_GOTO(efl_file_set(obj, file), fail);
   efl_file_key_set(obj, key);
   if (file)
     {
        if (efl_file_load(obj)) goto fail;
        efl_unref(obj);
        return EINA_TRUE;
     }
   efl_file_unload(obj);
   efl_unref(obj);
   return EINA_TRUE;
fail:
   efl_unref(obj);
   return EINA_FALSE;
}

EAPI Eina_Bool
efl_file_simple_mmap_load(Eo *obj, const Eina_File *file, const char *key)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(obj, EINA_FALSE);
   efl_ref(obj);
   EINA_SAFETY_ON_TRUE_GOTO(efl_file_mmap_set(obj, file), fail);
   efl_file_key_set(obj, key);
   if (file)
     {
        if (efl_file_load(obj)) goto fail;
        efl_unref(obj);
        return EINA_TRUE;
     }
   efl_file_unload(obj);
   efl_unref(obj);
   return EINA_TRUE;
fail:
   efl_unref(obj);
   return EINA_FALSE;
}

EAPI void
efl_file_simple_get(const Eo *obj, const char **file, const char **key)
{
   efl_ref((Eo*)obj);
   if (file) *file = efl_file_get(obj);
   if (key) *key = efl_file_key_get(obj);
   efl_unref((Eo*)obj);
}

EAPI void
efl_file_simple_mmap_get(const Eo *obj, const Eina_File **file, const char **key)
{
   efl_ref((Eo*)obj);
   if (file) *file = efl_file_mmap_get(obj);
   if (key) *key = efl_file_key_get(obj);
   efl_unref((Eo*)obj);
}

#include "interfaces/efl_file.eo.c"
#include "interfaces/efl_file_save.eo.c"