Rewrote ZIP_enumerateFiles() again. Hopefully it sucks less this time.

This commit is contained in:
Ryan C. Gordon 2002-07-25 04:41:43 +00:00
parent fa77075d7c
commit 8eeaf6c6d7
1 changed files with 31 additions and 40 deletions

View File

@ -1230,9 +1230,13 @@ static PHYSFS_sint32 zip_find_start_of_dir(ZIPinfo *info, const char *path,
rc = 1; rc = 1;
else else
{ {
if ((name[dlen + 1] == '\0') || (stop_on_first_find))
if (stop_on_first_find) /* Just checking dir's existance? */
return(middle); return(middle);
if (name[dlen + 1] == '\0') /* Skip initial dir entry. */
return(middle + 1);
/* there might be more entries earlier in the list. */ /* there might be more entries earlier in the list. */
retval = middle; retval = middle;
hi = middle - 1; hi = middle - 1;
@ -1253,60 +1257,47 @@ static LinkedStringList *ZIP_enumerateFiles(DirHandle *h,
const char *dirname, const char *dirname,
int omitSymLinks) int omitSymLinks)
{ {
#if 1
return(NULL); /* !!! FIXME */
#else
ZIPinfo *info = ((ZIPinfo *) h->opaque); ZIPinfo *info = ((ZIPinfo *) h->opaque);
PHYSFS_sint32 i, tmp, max;
LinkedStringList *retval = NULL, *p = NULL; LinkedStringList *retval = NULL, *p = NULL;
PHYSFS_uint32 dlen = strlen(dirname); PHYSFS_sint32 dlen, dlen_inc, max, i;
if ((dlen > 0) && (dirname[dlen - 1] == '/'))
dlen--;
i = zip_find_start_of_dir(info, dirname, 0); i = zip_find_start_of_dir(info, dirname, 0);
BAIL_IF_MACRO(i == -1, ERR_NO_SUCH_FILE, NULL); BAIL_IF_MACRO(i == -1, ERR_NO_SUCH_FILE, NULL);
for (max = (PHYSFS_sint32) info->entryCount; i < max; i++) dlen = strlen(dirname);
if ((dlen > 0) && (dirname[dlen - 1] == '/')) /* ignore trailing slash. */
dlen--;
dlen_inc = ((dlen > 0) ? 1 : 0) + dlen;
max = (PHYSFS_sint32) info->entryCount;
while (i < max)
{ {
ZIPentry *entry = &info->entries[i]; char *e = info->entries[i].name;
const char *add_file; if ((dlen) && ((strncmp(e, dirname, dlen) != 0) || (e[dlen] != '/')))
size_t strsize; break; /* past end of this dir; we're done. */
char *slash;
if ((dlen > 0) && (strncmp(entry->name, dirname, dlen) != 0)) if ((omitSymLinks) && (zip_entry_is_symlink(&info->entries[i])))
break; /* we're past this dir's entries. */ i++;
else
add_file = entry->name + dlen + ((dlen > 0) ? 1 : 0);
if ( ((omitSymLinks) && (zip_entry_is_symlink(entry))) ||
(*add_file == '\0') ) /* skip links and the dir entry itself. */
{ {
continue; char *add = e + dlen_inc;
} /* if */ char *ptr = strchr(add, '/');
PHYSFS_sint32 ln = (PHYSFS_sint32) ((ptr) ? ptr-add : strlen(add));
retval = __PHYSFS_addToLinkedStringList(retval, &p, add, ln);
ln += dlen_inc; /* point past entry to children... */
slash = strchr(add_file, '/'); /* handle subdirs under dirname... */ /* increment counter and skip children of subdirs... */
strsize = (size_t) ((slash) ? (slash - add_file) : strlen(add_file)); while ((++i < max) && (ptr != NULL))
retval = __PHYSFS_addToLinkedStringList(retval, &p, add_file, strsize);
tmp = i;
/* We added a subdir? Skip its children. */
while ((slash != NULL) && (i < max))
{
if (strncmp(info->entries[i].name, info->entries[tmp].name, dlen + strsize + 1) == 0)
{ {
if (info->entries[i].name[dlen + strsize + 1] == '/') char *e_new = info->entries[i].name;
{ if ((strncmp(e, e_new, ln) == 0) && (e_new[ln] == '/'))
i++;
continue; continue;
} /* if */ ptr = NULL;
} /* if */ } /* while */
i--; /* for loop will increment. */ } /* else */
slash = NULL;
} /* while */
} /* while */ } /* while */
return(retval); return(retval);
#endif
} /* ZIP_enumerateFiles */ } /* ZIP_enumerateFiles */