From a257cf02f303a6ddb073f2d63c03306c529f98a6 Mon Sep 17 00:00:00 2001 From: Cedric BAIL Date: Wed, 3 Sep 2008 14:40:12 +0000 Subject: [PATCH] Add a routine to extract path information. SVN revision: 35814 --- legacy/eina/src/include/eina_file.h | 2 + legacy/eina/src/lib/eina_file.c | 59 ++++++++++------------ legacy/eina/src/tests/eina_suite.c | 1 + legacy/eina/src/tests/eina_suite.h | 1 + legacy/eina/src/tests/eina_test_file.c | 68 ++++++++++++++++++++++++++ 5 files changed, 99 insertions(+), 32 deletions(-) create mode 100644 legacy/eina/src/tests/eina_test_file.c diff --git a/legacy/eina/src/include/eina_file.h b/legacy/eina/src/include/eina_file.h index 8204773e4f..b03dc68d71 100644 --- a/legacy/eina/src/include/eina_file.h +++ b/legacy/eina/src/include/eina_file.h @@ -20,6 +20,7 @@ #define EINA_FILE_H_ #include "eina_types.h" +#include "eina_array.h" /** * @defgroup File_Group Memory File @@ -30,6 +31,7 @@ typedef void (*Eina_File_Dir_List_Cb)(const char *name, const char *path, void * #define EINA_FILE_DIR_LIST_CB(Function) ((Eina_File_Dir_List_Cb)Function) EAPI Eina_Bool eina_file_dir_list(const char *dir, Eina_Bool recursive, Eina_File_Dir_List_Cb cb, void *data); +EAPI Eina_Array *eina_file_split(char *path); /** @} */ diff --git a/legacy/eina/src/lib/eina_file.c b/legacy/eina/src/lib/eina_file.c index 68ed02aede..56ac43209d 100644 --- a/legacy/eina/src/lib/eina_file.c +++ b/legacy/eina/src/lib/eina_file.c @@ -35,7 +35,6 @@ # include #endif /* _WIN2 */ - #ifdef HAVE_ALLOCA_H # include #elif defined __GNUC__ @@ -53,6 +52,11 @@ extern "C" void *alloca (size_t); #endif +#ifndef _WIN32 +# define PATH_DELIM '/' +#else +# define PATH_DELIM '\\' +#endif #include "eina_file.h" #include "eina_private.h" @@ -187,42 +191,33 @@ eina_file_dir_list(const char *dir, Eina_Bool recursive, Eina_File_Dir_List_Cb c /** * */ -EAPI void -eina_file_path_nth_get(const char *path, int n, char **left, char **right) +EAPI Eina_Array * +eina_file_split(char *path) { - char *p; - char *end; - char *tmp; - char *delim; - int inc; - int num = 0; + Eina_Array *ea; + char *current; + int length; - if (!left && !right) - return; + if (!path) return NULL; - if (n > 0) { - p = (char *)path; - inc = 1; - end = (char *)path + strlen(path); - } else { - p = (char *)path + strlen(path); - inc = -1; - end = (char *)path; - } + ea = eina_array_new(16); - for (tmp = p, delim = p; tmp != end && num != n; tmp += inc) + if (!ea) return NULL; + + for (current = strchr(path, PATH_DELIM); + current != NULL; + path = current + 1, current = strchr(path, PATH_DELIM)) { - if (*tmp == '/') { - num += inc; - delim = tmp; - } + length = current - path; + + if (length <= 0) continue ; + + eina_array_push(ea, path); + *current = '\0'; } - if (left) { - *left = strndup(path, delim - path + 1); - } - if (right) { - *right = strdup(delim + 1); - } + if (*path != '\0') + eina_array_push(ea, path); + + return ea; } - diff --git a/legacy/eina/src/tests/eina_suite.c b/legacy/eina/src/tests/eina_suite.c index b68f2f70fb..d641b54ebb 100644 --- a/legacy/eina/src/tests/eina_suite.c +++ b/legacy/eina/src/tests/eina_suite.c @@ -41,6 +41,7 @@ static const Eina_Test_Case etc[] = { { "Module", eina_test_module }, { "Convert", eina_test_convert }, { "Rbtree", eina_test_rbtree }, + { "File", eina_test_file }, { NULL, NULL } }; diff --git a/legacy/eina/src/tests/eina_suite.h b/legacy/eina/src/tests/eina_suite.h index 173542b256..db071fe1eb 100644 --- a/legacy/eina/src/tests/eina_suite.h +++ b/legacy/eina/src/tests/eina_suite.h @@ -40,5 +40,6 @@ void eina_test_accessor(TCase *tc); void eina_test_module(TCase *tc); void eina_test_convert(TCase *tc); void eina_test_rbtree(TCase *tc); +void eina_test_file(TCase *tc); #endif /* EINA_SUITE_H_ */ diff --git a/legacy/eina/src/tests/eina_test_file.c b/legacy/eina/src/tests/eina_test_file.c new file mode 100644 index 0000000000..2eaa11c562 --- /dev/null +++ b/legacy/eina/src/tests/eina_test_file.c @@ -0,0 +1,68 @@ +/* EINA - EFL data type library + * Copyright (C) 2008 Cedric Bail + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; + * if not, see . + */ + +#include "eina_suite.h" +#include "eina_file.h" + +START_TEST(eina_file_split_simple) +{ + Eina_Array *ea; + char *tmp; + Eina_Array_Iterator it; + unsigned int i; + + eina_array_init(); + + ea = eina_file_split(NULL); + fail_if(ea); + + ea = eina_file_split(strdup("/this/is/a/small/test")); + + fail_if(!ea); + fail_if(eina_array_count(ea) != 5); + fail_if(strcmp(eina_array_get(ea, 0), "this")); + fail_if(strcmp(eina_array_get(ea, 1), "is")); + fail_if(strcmp(eina_array_get(ea, 2), "a")); + fail_if(strcmp(eina_array_get(ea, 3), "small")); + fail_if(strcmp(eina_array_get(ea, 4), "test")); + + eina_array_free(ea); + + ea = eina_file_split(strdup("this//is///a /more/complex///case///")); + + fail_if(!ea); + fail_if(eina_array_count(ea) != 6); + fail_if(strcmp(eina_array_get(ea, 0), "this")); + fail_if(strcmp(eina_array_get(ea, 1), "is")); + fail_if(strcmp(eina_array_get(ea, 2), "a ")); + fail_if(strcmp(eina_array_get(ea, 3), "more")); + fail_if(strcmp(eina_array_get(ea, 4), "complex")); + fail_if(strcmp(eina_array_get(ea, 5), "case")); + + eina_array_free(ea); + + eina_array_shutdown(); +} +END_TEST + +void +eina_test_file(TCase *tc) +{ + tcase_add_test(tc, eina_file_split_simple); +} +