summaryrefslogtreecommitdiff
path: root/src/lib/eio/eio_map.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/eio/eio_map.c')
-rw-r--r--src/lib/eio/eio_map.c316
1 files changed, 316 insertions, 0 deletions
diff --git a/src/lib/eio/eio_map.c b/src/lib/eio/eio_map.c
new file mode 100644
index 0000000..8efa48c
--- /dev/null
+++ b/src/lib/eio/eio_map.c
@@ -0,0 +1,316 @@
1/* EIO - EFL data type library
2 * Copyright (C) 2010 Enlightenment Developers:
3 * Cedric Bail <cedric.bail@free.fr>
4 * Vincent "caro" Torri <vtorri at univ-evry dot fr>
5 * Stephen "okra" Houston <UnixTitan@gmail.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library;
19 * if not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "eio_private.h"
23#include "Eio.h"
24
25
26/*============================================================================*
27 * Local *
28 *============================================================================*/
29
30/**
31 * @cond LOCAL
32 */
33
34static void
35_eio_file_open_job(void *data, Ecore_Thread *thread)
36{
37 Eio_File_Map *map = data;
38
39 map->result = eina_file_open(map->name, map->shared);
40 if (!map->result) eio_file_thread_error(&map->common, thread);
41}
42
43static void
44_eio_file_open_free(Eio_File_Map *map)
45{
46 if (map->name) eina_stringshare_del(map->name);
47 free(map);
48}
49
50static void
51_eio_file_open_end(void *data, Ecore_Thread *thread EINA_UNUSED)
52{
53 Eio_File_Map *map = data;
54
55 map->open_cb((void*) map->common.data, &map->common, map->result);
56 _eio_file_open_free(map);
57}
58
59static void
60_eio_file_open_cancel(void *data, Ecore_Thread *thread EINA_UNUSED)
61{
62 Eio_File_Map *map = data;
63
64 eio_file_error(&map->common);
65 _eio_file_open_free(map);
66}
67
68static void
69_eio_file_close_job(void *data, Ecore_Thread *thread EINA_UNUSED)
70{
71 Eio_File_Map *map = data;
72
73 eina_file_close(map->result);
74}
75
76static void
77_eio_file_close_end(void *data, Ecore_Thread *thread EINA_UNUSED)
78{
79 Eio_File_Map *map = data;
80
81 map->common.done_cb((void*) map->common.data, &map->common);
82 _eio_file_open_free(map);
83}
84
85static void
86_eio_file_close_cancel(void *data, Ecore_Thread *thread EINA_UNUSED)
87{
88 Eio_File_Map *map = data;
89
90 eio_file_error(&map->common);
91 _eio_file_open_free(map);
92}
93
94static void
95_eio_file_map_all_job(void *data, Ecore_Thread *thread)
96{
97 Eio_File_Map_Rule *map = data;
98
99 eio_file_container_set(&map->common, map->file);
100 map->result = eina_file_map_all(map->common.container, map->rule);
101 if (map->result && map->filter_cb)
102 {
103 if (!map->filter_cb((void*) map->common.data,
104 &map->common,
105 map->result,
106 map->length))
107 {
108 eina_file_map_free(map->common.container, map->result);
109 map->result = NULL;
110 }
111 }
112
113 if (!map->result)
114 eio_file_thread_error(&map->common, thread);
115}
116
117static void
118_eio_file_map_new_job(void *data, Ecore_Thread *thread)
119{
120 Eio_File_Map_Rule *map = data;
121
122 eio_file_container_set(&map->common, map->file);
123 map->result = eina_file_map_new(map->common.container, map->rule,
124 map->offset, map->length);
125 if (map->result && map->filter_cb)
126 {
127 if (!map->filter_cb((void*) map->common.data,
128 &map->common,
129 map->result,
130 map->length))
131 {
132 eina_file_map_free(map->common.container, map->result);
133 map->result = NULL;
134 }
135 }
136
137 if (!map->result)
138 eio_file_thread_error(&map->common, thread);
139}
140
141static void
142_eio_file_map_end(void *data, Ecore_Thread *thread EINA_UNUSED)
143{
144 Eio_File_Map_Rule *map = data;
145
146 map->map_cb((void*) map->common.data, &map->common, map->result, map->length);
147 free(map);
148}
149
150static void
151_eio_file_map_cancel(void *data, Ecore_Thread *thread EINA_UNUSED)
152{
153 Eio_File_Map_Rule *map = data;
154
155 eio_file_error(&map->common);
156 free(map);
157}
158
159/**
160 * @endcond
161 */
162
163
164/*============================================================================*
165 * Global *
166 *============================================================================*/
167
168/**
169 * @cond LOCAL
170 */
171
172
173/**
174 * @endcond
175 */
176
177/*============================================================================*
178 * API *
179 *============================================================================*/
180
181EAPI Eio_File *
182eio_file_open(const char *name, Eina_Bool shared,
183 Eio_Open_Cb open_cb,
184 Eio_Error_Cb error_cb,
185 const void *data)
186{
187 Eio_File_Map *map;
188
189 EINA_SAFETY_ON_NULL_RETURN_VAL(name, NULL);
190 EINA_SAFETY_ON_NULL_RETURN_VAL(open_cb, NULL);
191 EINA_SAFETY_ON_NULL_RETURN_VAL(error_cb, NULL);
192
193 map = malloc(sizeof (Eio_File_Map));
194 EINA_SAFETY_ON_NULL_RETURN_VAL(map, NULL);
195
196 map->open_cb = open_cb;
197 map->name = eina_stringshare_add(name);
198 map->shared = shared;
199 map->result = NULL;
200
201 if (!eio_file_set(&map->common,
202 NULL,
203 error_cb,
204 data,
205 _eio_file_open_job,
206 _eio_file_open_end,
207 _eio_file_open_cancel))
208 return NULL;
209
210 return &map->common;
211}
212
213EAPI Eio_File *
214eio_file_close(Eina_File *f,
215 Eio_Done_Cb done_cb,
216 Eio_Error_Cb error_cb,
217 const void *data)
218{
219 Eio_File_Map *map;
220
221 EINA_SAFETY_ON_NULL_RETURN_VAL(f, NULL);
222 EINA_SAFETY_ON_NULL_RETURN_VAL(done_cb, NULL);
223 EINA_SAFETY_ON_NULL_RETURN_VAL(error_cb, NULL);
224
225 map = malloc(sizeof (Eio_File_Map));
226 EINA_SAFETY_ON_NULL_RETURN_VAL(map, NULL);
227
228 map->name = NULL;
229 map->result = f;
230
231 if (!eio_file_set(&map->common,
232 done_cb,
233 error_cb,
234 data,
235 _eio_file_close_job,
236 _eio_file_close_end,
237 _eio_file_close_cancel))
238 return NULL;
239
240 return &map->common;
241}
242
243EAPI Eio_File *
244eio_file_map_all(Eina_File *f,
245 Eina_File_Populate rule,
246 Eio_Filter_Map_Cb filter_cb,
247 Eio_Map_Cb map_cb,
248 Eio_Error_Cb error_cb,
249 const void *data)
250{
251 Eio_File_Map_Rule *map;
252
253 EINA_SAFETY_ON_NULL_RETURN_VAL(f, NULL);
254 EINA_SAFETY_ON_NULL_RETURN_VAL(map_cb, NULL);
255 EINA_SAFETY_ON_NULL_RETURN_VAL(error_cb, NULL);
256
257 map = malloc(sizeof (Eio_File_Map_Rule));
258 EINA_SAFETY_ON_NULL_RETURN_VAL(map, NULL);
259
260 map->file = f;
261 map->filter_cb = filter_cb;
262 map->map_cb = map_cb;
263 map->rule = rule;
264 map->result = NULL;
265 map->length = eina_file_size_get(f);
266
267 if (!eio_file_set(&map->common,
268 NULL,
269 error_cb,
270 data,
271 _eio_file_map_all_job,
272 _eio_file_map_end,
273 _eio_file_map_cancel))
274 return NULL;
275
276 return &map->common;
277}
278
279EAPI Eio_File *
280eio_file_map_new(Eina_File *f,
281 Eina_File_Populate rule,
282 unsigned long int offset,
283 unsigned long int length,
284 Eio_Filter_Map_Cb filter_cb,
285 Eio_Map_Cb map_cb,
286 Eio_Error_Cb error_cb,
287 const void *data)
288{
289 Eio_File_Map_Rule *map;
290
291 EINA_SAFETY_ON_NULL_RETURN_VAL(f, NULL);
292 EINA_SAFETY_ON_NULL_RETURN_VAL(map_cb, NULL);
293 EINA_SAFETY_ON_NULL_RETURN_VAL(error_cb, NULL);
294
295 map = malloc(sizeof (Eio_File_Map_Rule));
296 EINA_SAFETY_ON_NULL_RETURN_VAL(map, NULL);
297
298 map->common.container = f;
299 map->filter_cb = filter_cb;
300 map->map_cb = map_cb;
301 map->rule = rule;
302 map->result = NULL;
303 map->offset = offset;
304 map->length = length;
305
306 if (!eio_file_set(&map->common,
307 NULL,
308 error_cb,
309 data,
310 _eio_file_map_new_job,
311 _eio_file_map_end,
312 _eio_file_map_cancel))
313 return NULL;
314
315 return &map->common;
316}