Hi Guillaume,

On Fri, Sep 21, 2012 at 01:55:54PM +0200, Guillaume Friloux wrote:
> Hello e people,
> I have written a little example app for eio_file_stat_ls() that
> could have its place under "trunk/eio/src/examples/" or
> "trunk/EXAMPLES/eio/".
> I think this could help people that needs to use it.
> It also warns about the filter_cb being called in eio's thread. A
> trap i stepped on by badly reading the doc (totally my fault).
> 
> Example file is attached to this mail.

> /*
>  * Compile :
>  * gcc -g -o eio_file_stat_ls eio_file_stat_ls.c `pkg-config --cflags --libs 
> eina ecore eio`
>  *
>  * Prepare :
>  * mkdir test
>  * touch test/456
>  * touch test/excluded
>  * touch test/backuped
>  *
>  * Run :
>  * ./eio_file_stat_ls test
>  */
> 
> #include <Eio.h>
> #include <regex.h>
> 
> struct _Filter
> {
>    const char *name;
>    regex_t preg;
> };
> 
> Eina_List *l_filters;
> 
> unsigned int count_filtered,
>              count_passed,
>              jobs;
> 
> Eina_Bool _list_cb_filter(void *data, Eio_File *handler, const 
> Eina_File_Direct_Info *file);
> void _list_cb(void *data, Eio_File *handler, const Eina_File_Direct_Info 
> *file);
> void _list_cb_done(void *data, Eio_File *handler);
> void _list_cb_error(void *data, Eio_File *handler, int error);
> 



You don't need these prototypes, reorder your functions.



> void _free_cb(void *data)
> {
>    return;
> }
> 
> void _filter_add(const char *name)
> {
>    struct _Filter *obj;
> 
>    obj = calloc(1, sizeof(struct _Filter));
>    if (!obj)
>      return;
> 
>    if (regcomp(&(obj->preg), name, REG_EXTENDED))
>      {
>         fprintf(stderr, "Failed to compile regex for \"%s\"\n", name);
>         free(obj);
>         return;
>      }
>    obj->name = eina_stringshare_add(name);
>    l_filters = eina_list_append(l_filters, obj);
> }
> 
> /*
>  * This function is called inside eio's thread, so dont use anything not
>  * thread-safe inside!
>  */
> Eina_Bool
> _list_cb_filter(void *data, Eio_File *handler, const Eina_File_Direct_Info 
> *file)
> {
>    char *last_slash;
>    struct _Filter *obj_filter;
>    Eina_List *l;
> 
>    last_slash = strrchr(file->path, '/');
> 
>    if (file->type==EINA_FILE_DIR)
>      return EINA_TRUE;
> 
>    EINA_LIST_FOREACH(l_filters, l, obj_filter)
>      {
>         if (!regexec(&(obj_filter->preg), last_slash + 1, 0, 0, 0))
>           {
>              eio_file_associate_add(handler, file->path, obj_filter, 
> _free_cb);
>              return EINA_TRUE;
>           }
>      }
>    count_filtered++;
>    return EINA_FALSE;
> }
> 
> void
> _list_cb(void *data, Eio_File *handler, const Eina_File_Direct_Info *file)
> {
>    struct _Filter *obj;
> 
>    if (file->type==EINA_FILE_DIR)
>      {
>         jobs++;
>         eio_file_stat_ls(file->path,
>                          _list_cb_filter,
>                          _list_cb,
>                          _list_cb_done,
>                          _list_cb_error,
>                          NULL);
>         return;
>      }
>    count_passed++;
> 
>    obj = eio_file_associate_find(handler, file->path);
>    printf("File %s matched %s\n", file->path, obj->name);
> }
> 
> void
> _list_cb_done(void *data, Eio_File *handler)
> {
>    if (!--jobs)
>      ecore_main_loop_quit();
> }
> 
> void
> _list_cb_error(void *data, Eio_File *handler, int error)
> {
>    fprintf(stderr, "An error occured : %s\n", strerror(error));
> }
> 
> int main(int argc, char **argv)
> {
>    if (argc != 2)
>      {
>         fprintf(stderr, "Usage ./eio_file_stat_ls testdir\n");
>         return 1;
>      }
> 
>    eina_init();
>    ecore_init();
>    eio_init();
> 



Check for initializations.




>    l_filters = NULL;
>    count_filtered = 0;
>    count_passed = 0;
>    jobs = 1;



You could use a struct do describe your status data and pass it to
eio_file_stat_ls and appropriately handle it.


> 
>    _filter_add("^[0-9]+$");
>    _filter_add("^back.*");
> 
> 
>    eio_file_stat_ls(argv[1],
>                     _list_cb_filter,
>                     _list_cb,
>                     _list_cb_done,
>                     _list_cb_error,
>                     NULL);
> 
>    ecore_main_loop_begin();
> 
>    printf("Stats :\tFiltered = %d\tPassed = %d\n", count_filtered, 
> count_passed);
> 
>    eio_shutdown();
>    ecore_shutdown();
>    eina_shutdown();
>    return 0;
> }
> 


Regards....

-- 
Leandro Dorileo
ProFUSION embedded systems
http://profusion.mobi

------------------------------------------------------------------------------
Got visibility?
Most devs has no idea what their production app looks like.
Find out how fast your code is with AppDynamics Lite.
http://ad.doubleclick.net/clk;262219671;13503038;y?
http://info.appdynamics.com/FreeJavaPerformanceDownload.html
_______________________________________________
enlightenment-devel mailing list
enlightenment-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/enlightenment-devel

Reply via email to