Did some research, discovered implementing rewinddir() was as easy as setting a value to zero! Now we don't have to open and close the same folder, but instead just jump back to the beginning.

This commit is contained in:
toasterbabe 2017-04-29 16:40:07 +01:00
parent 66f56bbba3
commit 055b59f6fb
1 changed files with 28 additions and 4 deletions

View File

@ -256,6 +256,28 @@ readdir (DIR * dirp)
return (struct dirent *) 0; return (struct dirent *) 0;
} }
/*
* rewinddir
*
* Makes the next readdir start from the beginning.
*/
int
rewinddir (DIR * dirp)
{
errno = 0;
/* Check for valid DIR struct. */
if (!dirp)
{
errno = EFAULT;
return -1;
}
dirp->dd_stat = 0;
return 0;
}
/* /*
* closedir * closedir
* *
@ -537,12 +559,11 @@ boolean preparefilemenu(boolean samemenu)
} }
} }
closedir(dirhandle); // I don't know how to go back to the start of the folder without just opening and closing... if there's a way, it doesn't appear to be easily manipulatable
if (!sizedirmenu) if (!sizedirmenu)
{ {
if (tempname) if (tempname)
Z_Free(tempname); Z_Free(tempname);
closedir(dirhandle);
return false; return false;
} }
@ -554,9 +575,12 @@ boolean preparefilemenu(boolean samemenu)
} }
if (!(dirmenu = Z_Realloc(dirmenu, sizedirmenu*sizeof(char *), PU_STATIC, NULL))) if (!(dirmenu = Z_Realloc(dirmenu, sizedirmenu*sizeof(char *), PU_STATIC, NULL)))
{
closedir(dirhandle); // just in case
I_Error("Ran out of memory whilst preparing add-ons menu"); I_Error("Ran out of memory whilst preparing add-ons menu");
}
dirhandle = opendir(menupath); rewinddir(dirhandle);
while ((pos+folderpos) < sizedirmenu) while ((pos+folderpos) < sizedirmenu)
{ {
@ -635,7 +659,7 @@ boolean preparefilemenu(boolean samemenu)
dirmenu[0] = Z_StrDup("\1\5UP..."); dirmenu[0] = Z_StrDup("\1\5UP...");
menupath[menupathindex[menudepthleft]] = 0; menupath[menupathindex[menudepthleft]] = 0;
sizedirmenu = (numfolders+pos); // crash prevention if things change between openings somehow sizedirmenu = (numfolders+pos); // just in case things shrink between opening and rewind
if (tempname) if (tempname)
{ {