Optimize directory search

This commit is contained in:
Arun Prakash Jana 2017-04-02 14:18:54 +05:30
parent 691291245a
commit 4bc2ce0fbc
No known key found for this signature in database
GPG Key ID: A75979F35C080412
1 changed files with 20 additions and 12 deletions

32
nnn.c
View File

@ -515,6 +515,9 @@ canopendir(char *path)
return 1; return 1;
} }
/*
* Returns "dir/name or "/name"
*/
static char * static char *
mkpath(char *dir, char *name, char *out, size_t n) mkpath(char *dir, char *name, char *out, size_t n)
{ {
@ -656,20 +659,25 @@ dentfree(struct entry *dents)
/* Return the position of the matching entry or 0 otherwise */ /* Return the position of the matching entry or 0 otherwise */
static int static int
dentfind(struct entry *dents, int n, char *cwd, char *path) dentfind(struct entry *dents, int n, char *path)
{ {
char tmp[PATH_MAX]; if (!path)
int i;
if (path == NULL)
return 0; return 0;
for (i = 0; i < n; i++) {
mkpath(cwd, dents[i].name, tmp, sizeof(tmp)); char *p = xmemrchr(path, '/', strlen(path));
DPRINTF_S(path); if (!p)
DPRINTF_S(tmp); p = path;
if (strcmp(tmp, path) == 0) else
/* We are assuming an entry with actual
name ending in '/' will not appear */
p++;
DPRINTF_S(p);
for (int i = 0; i < n; i++)
if (strcmp(p, dents[i].name) == 0)
return i; return i;
}
return 0; return 0;
} }
@ -698,7 +706,7 @@ populate(char *path, char *oldpath, char *fltr)
qsort(dents, ndents, sizeof(*dents), entrycmp); qsort(dents, ndents, sizeof(*dents), entrycmp);
/* Find cur from history */ /* Find cur from history */
cur = dentfind(dents, ndents, path, oldpath); cur = dentfind(dents, ndents, oldpath);
return 0; return 0;
} }