forked from enlightenment/efl
Add a routine to extract path information.
SVN revision: 35814
This commit is contained in:
parent
7da98019b3
commit
a257cf02f3
|
@ -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);
|
||||
|
||||
/** @} */
|
||||
|
||||
|
|
|
@ -35,7 +35,6 @@
|
|||
# include <Evil.h>
|
||||
#endif /* _WIN2 */
|
||||
|
||||
|
||||
#ifdef HAVE_ALLOCA_H
|
||||
# include <alloca.h>
|
||||
#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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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 }
|
||||
};
|
||||
|
||||
|
|
|
@ -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_ */
|
||||
|
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue