2008-08-06 11:15:24 -07:00
|
|
|
/* EINA - EFL data type library
|
|
|
|
* Copyright (C) 2007-2008 Jorge Luis Zapata Muga
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-07-30 06:35:49 -07:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "eina_file.h"
|
2008-07-30 05:46:55 -07:00
|
|
|
#include "eina_private.h"
|
2008-08-08 22:47:15 -07:00
|
|
|
|
2008-07-30 05:46:55 -07:00
|
|
|
/*============================================================================*
|
|
|
|
* API *
|
|
|
|
*============================================================================*/
|
2008-08-08 22:47:15 -07:00
|
|
|
|
2008-07-30 05:46:55 -07:00
|
|
|
/**
|
|
|
|
* Lis all files on the directory calling the function for every file found
|
2008-08-08 22:47:15 -07:00
|
|
|
* @param dir The directory name
|
2008-07-30 05:46:55 -07:00
|
|
|
* @param recursive Iterate recursively in the directory
|
2008-08-08 22:47:15 -07:00
|
|
|
* @param cb The callback to be called
|
|
|
|
* @param data The data to pass to the callback
|
2008-07-30 05:46:55 -07:00
|
|
|
*/
|
|
|
|
EAPI void eina_file_dir_list(const char *dir, int recursive, Eina_File_Dir_List_Cb cb, void *data)
|
|
|
|
{
|
|
|
|
struct dirent *de;
|
|
|
|
DIR *d;
|
|
|
|
|
|
|
|
assert(cb);
|
|
|
|
|
|
|
|
d = opendir(dir);
|
|
|
|
if (!d)
|
|
|
|
return;
|
|
|
|
|
2008-07-30 06:35:49 -07:00
|
|
|
while ((de = readdir(d)))
|
2008-07-30 05:46:55 -07:00
|
|
|
{
|
|
|
|
if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
cb(de->d_name, dir, data);
|
|
|
|
/* d_type is only available on linux and bsd (_BSD_SOURCE) */
|
|
|
|
if ((recursive) && (de->d_type == DT_DIR))
|
|
|
|
{
|
|
|
|
char path[PATH_MAX];
|
|
|
|
|
|
|
|
snprintf(path, PATH_MAX, "%s/%s", dir, de->d_name);
|
|
|
|
eina_file_dir_list(path, recursive, cb, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(d);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
EAPI void eina_file_path_nth_get(const char *path, int n, char **left, char **right)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
char *end;
|
|
|
|
char *tmp;
|
|
|
|
char *delim;
|
|
|
|
int inc;
|
|
|
|
int num = 0;
|
|
|
|
|
|
|
|
if (!left && !right)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (n > 0)
|
|
|
|
{
|
|
|
|
p = (char *)path;
|
|
|
|
inc = 1;
|
|
|
|
end = (char *)path + strlen(path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p = (char *)path + strlen(path);
|
|
|
|
inc = -1;
|
|
|
|
end = (char *)path;
|
|
|
|
}
|
2008-07-30 06:35:49 -07:00
|
|
|
for (tmp = p, delim = p; tmp != end && num != n; tmp += inc)
|
2008-07-30 05:46:55 -07:00
|
|
|
{
|
|
|
|
if (*tmp == '/')
|
|
|
|
{
|
|
|
|
num += inc;
|
|
|
|
delim = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (left)
|
|
|
|
{
|
|
|
|
*left = strndup(path, delim - path + 1);
|
|
|
|
}
|
|
|
|
if (right)
|
|
|
|
{
|
|
|
|
*right = strdup(delim + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|