summaryrefslogtreecommitdiff
path: root/doc/eio_examples.dox
blob: 587cb39ccec0fc5ab4c4a8279f804a08de0edc04 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/**
 * @page eio_examples Eio Examples
 *
 * Here is a page with some Eio examples explained:
 *
 * @li @ref eio_file_ls.c
 *
 * Tutorials:
 * @li @ref tutorial_dir_copy
 * @li @ref tutorial_dir_stat_ls
 * @li @ref tutorial_file_ls
 * @li @ref tutorial_dir_direct_ls
 * @li @ref tutorial_monitor_add
 *
 * @example eio_file_ls.c
 */

/**
 * @page tutorial_dir_copy eio_dir_copy() tutorial
 *
 * To use eio_dir_copy(), you basically need the source and
 * destination files (or directories), and set three callbacks:
 *
 * @li The notification callback, which allows you to know if a file or
 * a directory is copied, and the progress of the copy.
 * @li The end callback, which is called when the copy is finished.
 * @li The error callback, which is called if an error occurred. You
 * can then retrieve the error type as an errno error.
 *
 * @warning It is the user's duty to provide the "right target". It
 * means that copying to '.' will copy the content directly inside '.'
 * and not in a subdirectory.
 *
 * Here is a simple example:
 *
 * @code
 * #include <Ecore.h>
 * #include <Eio.h>
 *
 * static void
 * _test_notify_cb(void *data, Eio_File *handler, const Eio_Progress *info)
 * {
 *    switch (info->op)
 *       {
 *       case EIO_FILE_COPY:
 *          printf("[%s] %f%%\n", info->dest, info->percent);
 *          break;
 *       case EIO_DIR_COPY:
 *          printf("global [%li/%li] %f%%\n", info->current, info->max, info->percent);
 *          break;
 *       }
 * }
 *
 * static void
 * _test_done_cb(void *data, Eio_File *handler)
 * {
 *    printf("copy done\n");
 *    ecore_main_loop_quit();
 * }
 *
 * static void
 * _test_error_cb(int error, Eio_File *handler, void *data)
 * {
 *    fprintf(stderr, "error: [%s]\n", strerror(error));
 *     ecore_main_loop_quit();
 * }
 *
 * int
 * main(int argc, char **argv)
 * {
 *    Eio_File *cp;
 *
 *    if (argc != 3)
 *      {
 *         fprintf(stderr, "eio_cp source_file destination_file\n");
 *         return -1;
 *      }
 *
 *    ecore_init();
 *    eio_init();
 *
 *    cp = eio_dir_copy(argv[1], argv[2],
 *                      _test_notify_cb,
 *                      _test_done_cb,
 *                      _test_error_cb,
 *                      NULL);
 *
 *    ecore_main_loop_begin();
 *
 *    eio_shutdown();
 *    ecore_shutdown();
 *
 *    return 0;
 * }
 * @endcode
 */

/**
 * @page tutorial_dir_stat_ls eio_dir_stat_ls() tutorial
 *
 * @li The filter callback, which allow or not a file to be seen
 * by the main loop handler. This callback run in a separated thread.
 * @li The main callback, which receive in the main loop all the file
 * that are allowed by the filter. If you are updating a user interface
 * it make sense to delay the insertion a little, so you get a chance
 * to update the canvas for a bunch of file instead of one by one.
 * @li The end callback, which is called in the main loop when the
 * content of the directory has been correctly scanned and all the
 * file notified to the main loop.
 * @li The error callback, which is called if an error occurred or
 * if the listing was cancelled during it's run. You can then retrieve
 * the error type as an errno error.
 *
 * Here is a simple example that implement a stupidly simple replacement for find:
 *
 * @code
 * #include <Ecore.h>
 * #include <Eio.h>
 *
 * static Eina_Bool
 * _test_filter_cb(void *data, Eio_File *handler, const Eina_File_Direct_Info *info)
 * {
 *    fprintf(stderr, "ACCEPTING: %s\n", info->path);
 *    return EINA_TRUE;
 * }
 *
 * static void
 * _test_main_cb(void *data, Eio_File *handler, const Eina_File_Direct_Info *info)
 * {
 *    fprintf(stderr, "PROCESS: %s\n", info->path);
 * }
 *
 * static void
 * _test_done_cb(void *data, Eio_File *handler)
 * {
 *    printf("ls done\n");
 *    ecore_main_loop_quit();
 * }
 *
 * static void
 * _test_error_cb(void *data, Eio_File *handler, int error)
 * {
 *    fprintf(stderr, "error: [%s]\n", strerror(error));
 *    ecore_main_loop_quit();
 * }
 *
 * int
 * main(int argc, char **argv)
 * {
 *    Eio_File *cp;
 *
 *    if (argc != 2)
 *      {
 * 	fprintf(stderr, "eio_ls directory\n");
 * 	return -1;
 *      }
 *
 *    ecore_init();
 *    eio_init();
 *
 *    cp = eio_dir_stat_ls(argv[1],
 *                         _test_filter_cb,
 *                         _test_main_cb,
 *                         _test_done_cb,
 *                         _test_error_cb,
 *                         NULL);
 *
 *    ecore_main_loop_begin();
 *
 *    eio_shutdown();
 *    ecore_shutdown();
 *
 *    return 0;
 * }
 *
 * @endcode
 */

/**
 * @page tutorial_file_ls eio_file_ls() tutorial
 *
 * To use eio_file_ls(), you just need to define four callbacks:
 *
 * @li The filter callback, which allows a file to be seen (or not)
 * by the main loop handler. This callback runs in a separate thread.
 * @li The main callback, which receive in the main loop all the file
 * that are allowed by the filter. If you are updating a user interface
 * it makes sense to delay the insertion a little, so you get a chance
 * to update the canvas for a bunch of files instead of one by one.
 * @li The end callback, which is called in the main loop when the
 * content of the directory has been correctly scanned and all the
 * file notified to the main loop.
 * @li The error callback, which is called if an error occurred or
 * if the listing was cancelled during its run. You can then retrieve
 * the error type as an errno error.
 *
 * Here is a simple example:
 *
 * @code
 * #include <Ecore.h>
 * #include <Eio.h>
 *
 * static Eina_Bool
 * _test_filter_cb(void *data, Eio_File *handler, const char *file)
 * {
 *    fprintf(stderr, "ACCEPTING: %s\n", file);
 *    return EINA_TRUE;
 * }
 *
 * static void
 * _test_main_cb(void *data, Eio_File *handler, const char *file)
 * {
 *    fprintf(stderr, "PROCESS: %s\n", file);
 * }
 *
 * static void
 * _test_done_cb(void *data, Eio_File *handler)
 * {
 *    printf("ls done\n");
 *    ecore_main_loop_quit();
 * }
 *
 * static void
 * _test_error_cb(void *data, Eio_File *handler, int error)
 * {
 *    fprintf(stderr, "error: [%s]\n", strerror(error));
 *    ecore_main_loop_quit();
 * }
 *
 * int
 * main(int argc, char **argv)
 * {
 *    Eio_File *cp;
 *
 *    if (argc != 2)
 *      {
 * 	fprintf(stderr, "eio_ls directory\n");
 * 	return -1;
 *      }
 *
 *    ecore_init();
 *    eio_init();
 *
 *    cp = eio_file_ls(argv[1],
 *                     _test_filter_cb,
 *                     _test_main_cb,
 *                     _test_done_cb,
 *                     _test_error_cb,
 *                     NULL);
 *
 *    ecore_main_loop_begin();
 *
 *    eio_shutdown();
 *    ecore_shutdown();
 *
 *    return 0;
 * }
 *
 * @endcode
 */

/**
 * @page tutorial_monitor_add eio_monitor_add() tutorial
 *
 * To use eio_monitor_add(), you have to define callbacks
 * for events declared by eio.
 * Available events are :
 * - EIO_MONITOR_FILE_CREATED
 * - EIO_MONITOR_FILE_DELETED
 * - EIO_MONITOR_FILE_MODIFIED
 * - EIO_MONITOR_FILE_CLOSED
 * - EIO_MONITOR_DIRECTORY_CREATED
 * - EIO_MONITOR_DIRECTORY_DELETED
 * - EIO_MONITOR_DIRECTORY_CLOSED
 * - EIO_MONITOR_SELF_RENAME
 * - EIO_MONITOR_SELF_DELETED
 *
 * As nothing is worth an example, here it is :
 * @code
 * #include <Eina.h>
 * #include <Ecore.h>
 * #include <Eio.h>
 *
 * void file_modified(void *data, int type, void *event)
 * {
 *    const char *filename = (const char *)data;
 *    printf("file %s ", filename);
 *    if( type == EIO_MONITOR_FILE_MODIFIED )
 *       printf("is being modified");
 *    else if( type == EIO_MONITOR_FILE_CLOSED )
 *       printf("is no longer being modified");
 *    else printf("got unexpected changes");
 *    printf("\n");
 * }
 *
 * int main(int argc, char **argv) {
 *    Eio_Monitor *monitor  = NULL,
 *                *monitor2 = NULL;
 *    eio_init();
 *    const char *filename = eina_stringshare_add("/tmp/eio_notify_testfile");
 *
 *    monitor  = eio_monitor_add(filename);
 *    ecore_event_handler_add(EIO_MONITOR_FILE_MODIFIED, (Ecore_Event_Handler_Cb)file_modified, filename);
 *    ecore_event_handler_add(EIO_MONITOR_FILE_CLOSED, (Ecore_Event_Handler_Cb)file_modified, filename);
 *
 *    ecore_main_loop_begin();
 *    eio_shutdown();
 *    eina_stringshare_del(filename);
 * }
 * @endcode
 * Build the example doing :
 * @verbatim gcc -o tutorial_monitor_add tutorial_monitor_add.c `pkg-config --libs --cflags eio ecore ecore-file eina`
 * then create the file /tmp/eio_notify_testfile :
 * touch /tmp/eio_notify_testfile
 * and launch tutorial_monitor_add, and in another terminal, write into /tmp/eio_notify_testfile, doing for example :
 * echo "test" >> /tmp/eio_notify_testfile
 * @endverbatim
 */

/**
 * @page tutorial_dir_direct_ls eio_dir_direct_ls() tutorial
 *
 * @li The filter callback, which allow or not a file to be seen
 * by the main loop handler. This callback run in a separated thread.
 * It also take care of getting a stat buffer needed by the main callback
 * to display the file size.
 * @li The main callback, which receive in the main loop all the file
 * that are allowed by the filter. If you are updating a user interface
 * it make sense to delay the insertion a little, so you get a chance
 * to update the canvas for a bunch of file instead of one by one.
 * @li The end callback, which is called in the main loop when the
 * content of the directory has been correctly scanned and all the
 * file notified to the main loop.
 * @li The error callback, which is called if an error occurred or
 * if the listing was cancelled during it's run. You can then retrieve
 * the error type as an errno error.
 *
 * Here is a simple example that implement a stupidly simple recursive ls that display file size:
 *
 * @code
 * #include <Eina.h>
 * #include <Ecore.h>
 * #include <Eio.h>
 *
 * static Eina_Bool
 * _test_filter_cb(void *data, Eio_File *handler, Eina_File_Direct_Info *info)
 * {
 *    Eina_Stat *buffer;
 *    Eina_Bool isdir;
 *
 *    isdir = info->type == EINA_FILE_DIR;
 *
 *    buffer = malloc(sizeof (Eina_Stat));
 *    if (eina_file_statat(eio_file_container_get(handler), info, buffer))
 *      {
 *         free(buffer);
 *         return EINA_FALSE;
 *      }
 *
 *    if (!isdir && info->type == EINA_FILE_DIR)
 *      {
 *         struct stat st;
 *         if (lstat(info->path, &st) == 0)
 *           {
 *              if (S_ISLNK(st.st_mode))
 *                info->type = EINA_FILE_LNK;
 *           }
 *      }
 *
 *    eio_file_associate_direct_add(handler, "stat", buffer, free);
 *    fprintf(stdout, "ACCEPTING: %s\n", info->path);
 *    return EINA_TRUE;
 * }
 *
 * static void
 * _test_main_cb(void *data, Eio_File *handler, const Eina_File_Direct_Info *info)
 * {
 *    struct stat *buffer;
 *
 *    buffer = eio_file_associate_find(handler, "stat");
 *    fprintf(stdout, "PROCESS: %s of size %li\n", info->path, buffer->st_size);
 * }
 *
 * static void
 * _test_done_cb(void *data, Eio_File *handler)
 * {
 *    printf("ls done\n");
 *    ecore_main_loop_quit();
 * }
 *
 * static void
 * _test_error_cb(void *data, Eio_File *handler, int error)
 * {
 *    fprintf(stdout, "error: [%s]\n", strerror(error));
 *    ecore_main_loop_quit();
 * }
 *
 * int
 * main(int argc, char **argv)
 * {
 *    Eio_File *cp;
 *
 *    if (argc != 2)
 *      {
 * 	fprintf(stdout, "eio_ls directory\n");
 * 	return -1;
 *      }
 *
 *    ecore_init();
 *    eio_init();
 *
 *    cp = eio_dir_direct_ls(argv[1],
 * 			  _test_filter_cb,
 * 			  _test_main_cb,
 * 			  _test_done_cb,
 * 			  _test_error_cb,
 * 			  NULL);
 *
 *    ecore_main_loop_begin();
 *
 *    eio_shutdown();
 *    ecore_shutdown();
 *
 *    return 0;
 * }
 * @endcode
 */