diff options
author | Cedric BAIL <cedric.bail@free.fr> | 2010-07-02 15:25:22 +0000 |
---|---|---|
committer | Cedric BAIL <cedric.bail@free.fr> | 2010-07-02 15:25:22 +0000 |
commit | 87b85132ddf369e207e6e592099fa455719abbbf (patch) | |
tree | 9ee3678d1f80925fc60d64e8dc444ea8704008f1 /legacy/ecore/src/lib/ecore_file/ecore_file.c | |
parent | 062f4c39207a4cdc746ee01ff4eda80626fc05e4 (diff) |
* ecore: add ecore_file_ls_iterator.
SVN revision: 49997
Diffstat (limited to '')
-rw-r--r-- | legacy/ecore/src/lib/ecore_file/ecore_file.c | 84 |
1 files changed, 81 insertions, 3 deletions
diff --git a/legacy/ecore/src/lib/ecore_file/ecore_file.c b/legacy/ecore/src/lib/ecore_file/ecore_file.c index 8435c45ccc..fd4faa9e27 100644 --- a/legacy/ecore/src/lib/ecore_file/ecore_file.c +++ b/legacy/ecore/src/lib/ecore_file/ecore_file.c | |||
@@ -8,9 +8,6 @@ | |||
8 | 8 | ||
9 | #include <stdio.h> | 9 | #include <stdio.h> |
10 | #include <string.h> | 10 | #include <string.h> |
11 | #include <sys/types.h> | ||
12 | #include <sys/stat.h> | ||
13 | #include <dirent.h> | ||
14 | 11 | ||
15 | #ifndef _MSC_VER | 12 | #ifndef _MSC_VER |
16 | # include <unistd.h> | 13 | # include <unistd.h> |
@@ -32,6 +29,47 @@ | |||
32 | int _ecore_file_log_dom = -1; | 29 | int _ecore_file_log_dom = -1; |
33 | static int _ecore_file_init_count = 0; | 30 | static int _ecore_file_init_count = 0; |
34 | 31 | ||
32 | static Eina_Bool | ||
33 | _ecore_file_ls_iterator_next(Ecore_File_Iterator *it, void **data) | ||
34 | { | ||
35 | struct dirent *dp; | ||
36 | char *name; | ||
37 | size_t length; | ||
38 | |||
39 | do | ||
40 | { | ||
41 | dp = readdir(it->dirp); | ||
42 | if (!dp) return EINA_FALSE; | ||
43 | } | ||
44 | while (!strcmp(dp->d_name, ".") | ||
45 | || !strcmp(dp->d_name, "..")); | ||
46 | |||
47 | length = strlen(dp->d_name); | ||
48 | name = alloca(length + 2 + it->length); | ||
49 | |||
50 | memcpy(name, it->dir, it->length); | ||
51 | memcpy(name + it->length, "/", 1); | ||
52 | memcpy(name + it->length + 1, dp->d_name, length + 1); | ||
53 | |||
54 | *data = (char*) eina_stringshare_add(name); | ||
55 | return EINA_TRUE; | ||
56 | } | ||
57 | |||
58 | static char * | ||
59 | _ecore_file_ls_iterator_container(Ecore_File_Iterator *it) | ||
60 | { | ||
61 | return it->dir; | ||
62 | } | ||
63 | |||
64 | static void | ||
65 | _ecore_file_ls_iterator_free(Ecore_File_Iterator *it) | ||
66 | { | ||
67 | closedir(it->dirp); | ||
68 | |||
69 | EINA_MAGIC_SET(&it->iterator, 0); | ||
70 | free(it); | ||
71 | } | ||
72 | |||
35 | /* externally accessible functions */ | 73 | /* externally accessible functions */ |
36 | /** | 74 | /** |
37 | * Initialize Ecore_File and the services it will use. Call this function | 75 | * Initialize Ecore_File and the services it will use. Call this function |
@@ -703,6 +741,46 @@ ecore_file_ls(const char *dir) | |||
703 | } | 741 | } |
704 | 742 | ||
705 | /** | 743 | /** |
744 | * Get an iterator to list the content of a directory. Give a chance to interrupt it | ||
745 | * and make it completly asynchrone. | ||
746 | * The iterator will walk over '.' and '..' without returning them. | ||
747 | * @param dir The name of the directory to list | ||
748 | * @return Return an Eina_Iterator that will walk over the files and directory in the pointed | ||
749 | * directory. On failure it will return NULL. | ||
750 | */ | ||
751 | EAPI Eina_Iterator * | ||
752 | ecore_file_ls_iterator(const char *dir) | ||
753 | { | ||
754 | Ecore_File_Iterator *it; | ||
755 | size_t length; | ||
756 | |||
757 | if (!dir) return NULL; | ||
758 | |||
759 | length = strlen(dir); | ||
760 | |||
761 | it = malloc(sizeof (Ecore_File_Iterator) + length); | ||
762 | if (!it) return NULL; | ||
763 | |||
764 | EINA_MAGIC_SET(&it->iterator, EINA_MAGIC_ITERATOR); | ||
765 | |||
766 | it->dirp = opendir(dir); | ||
767 | if (!it->dirp) | ||
768 | { | ||
769 | free(it); | ||
770 | return NULL; | ||
771 | } | ||
772 | |||
773 | memcpy(it->dir, dir, length + 1); | ||
774 | it->length = length; | ||
775 | |||
776 | it->iterator.next = FUNC_ITERATOR_NEXT(_ecore_file_ls_iterator_next); | ||
777 | it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER(_ecore_file_ls_iterator_container); | ||
778 | it->iterator.free = FUNC_ITERATOR_FREE(_ecore_file_ls_iterator_free); | ||
779 | |||
780 | return &it->iterator; | ||
781 | } | ||
782 | |||
783 | /** | ||
706 | * FIXME: To be documented. | 784 | * FIXME: To be documented. |
707 | */ | 785 | */ |
708 | EAPI char * | 786 | EAPI char * |