Skip to content

Commit

Permalink
Split FileList and XList
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbatalov committed May 18, 2022
1 parent d71dbac commit 4f229f1
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 51 deletions.
27 changes: 14 additions & 13 deletions src/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -608,21 +608,22 @@ int fileNameListInit(const char* pattern, char*** fileNameListPtr, int a3, int a

memset(fileList, 0, sizeof(*fileList));

if (!_xbuild_filelist(pattern, fileList)) {
XList* xlist = &(fileList->xlist);
if (!xlistInit(pattern, xlist)) {
free(fileList);
return 0;
}

int length = 0;
if (fileList->fileNamesLength != 0) {
qsort(fileList->fileNames, fileList->fileNamesLength, sizeof(*fileList->fileNames), _db_list_compare);
if (xlist->fileNamesLength != 0) {
qsort(xlist->fileNames, xlist->fileNamesLength, sizeof(*xlist->fileNames), _db_list_compare);

int fileNamesLength = fileList->fileNamesLength;
int fileNamesLength = xlist->fileNamesLength;
for (int index = 0; index < fileNamesLength - 1; index++) {
if (stricmp(fileList->fileNames[index], fileList->fileNames[index + 1]) == 0) {
char* temp = fileList->fileNames[index + 1];
memmove(&(fileList->fileNames[index + 1]), &(fileList->fileNames[index + 2]), sizeof(*fileList->fileNames) * (fileList->fileNamesLength - index - 1));
fileList->fileNames[fileList->fileNamesLength - 1] = temp;
if (stricmp(xlist->fileNames[index], xlist->fileNames[index + 1]) == 0) {
char* temp = xlist->fileNames[index + 1];
memmove(&(xlist->fileNames[index + 1]), &(xlist->fileNames[index + 2]), sizeof(*xlist->fileNames) * (xlist->fileNamesLength - index - 1));
xlist->fileNames[xlist->fileNamesLength - 1] = temp;

fileNamesLength--;
index--;
Expand All @@ -632,7 +633,7 @@ int fileNameListInit(const char* pattern, char*** fileNameListPtr, int a3, int a
bool v1 = *pattern == '*';

for (int index = 0; index < fileNamesLength; index += 1) {
const char* name = fileList->fileNames[index];
const char* name = xlist->fileNames[index];
char dir[_MAX_DIR];
char fileName[_MAX_FNAME];
char extension[_MAX_EXT];
Expand All @@ -648,7 +649,7 @@ int fileNameListInit(const char* pattern, char*** fileNameListPtr, int a3, int a
}

if (!v2) {
sprintf(fileList->fileNames[index], "%s%s", fileName, extension);
sprintf(xlist->fileNames[index], "%s%s", fileName, extension);
length++;
}
}
Expand All @@ -657,7 +658,7 @@ int fileNameListInit(const char* pattern, char*** fileNameListPtr, int a3, int a
fileList->next = gFileListHead;
gFileListHead = fileList;

*fileNameListPtr = fileList->fileNames;
*fileNameListPtr = xlist->fileNames;

return length;
}
Expand All @@ -671,7 +672,7 @@ void fileNameListFree(char*** fileNameListPtr, int a2)

FileList* currentFileList = gFileListHead;
FileList* previousFileList = gFileListHead;
while (*fileNameListPtr != currentFileList->fileNames) {
while (*fileNameListPtr != currentFileList->xlist.fileNames) {
previousFileList = currentFileList;
currentFileList = currentFileList->next;
if (currentFileList == NULL) {
Expand All @@ -685,7 +686,7 @@ void fileNameListFree(char*** fileNameListPtr, int a2)
previousFileList->next = currentFileList->next;
}

fileListFree(currentFileList);
xlistFree(&(currentFileList->xlist));

free(currentFileList);
}
Expand Down
5 changes: 5 additions & 0 deletions src/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ typedef XFile File;
typedef void FileReadProgressHandler();
typedef char* StrdupProc(const char* string);

typedef struct FileList {
XList xlist;
struct FileList* next;
} FileList;

extern FileReadProgressHandler* gFileReadProgressHandler;
extern int gFileReadProgressBytesRead;

Expand Down
54 changes: 27 additions & 27 deletions src/xfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -530,15 +530,16 @@ bool xbaseOpen(const char* path)
return true;
}

int _xenumfiles(const char* pattern, XFileEnumerationHandler* handler, FileList* fileList)
// 0x4DFB3C
bool xlistEnumerate(const char* pattern, XListEnumerationHandler* handler, XList* xlist)
{
assert(pattern); // "filespec", "xfile.c", 845
assert(handler); // "enumfunc", "xfile.c", 846

DirectoryFileFindData directoryFileFindData;
XFileEnumerationContext context;
XListEnumerationContext context;

context.fileList = fileList;
context.xlist = xlist;

char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
Expand Down Expand Up @@ -674,29 +675,27 @@ int _xenumfiles(const char* pattern, XFileEnumerationHandler* handler, FileList*
return findFindClose(&directoryFileFindData);
}

int _xbuild_filelist(const char* pattern, FileList* fileList)
// 0x4DFF28
bool xlistInit(const char* pattern, XList* xlist)
{
_xenumfiles(pattern, _xlistenumfunc, fileList);
return fileList->fileNamesLength != -1;
xlistEnumerate(pattern, xlistEnumerateHandler, xlist);
return xlist->fileNamesLength != -1;
}

// 0x4DFF48
void fileListFree(FileList* fileList)
void xlistFree(XList* xlist)
{
assert(fileList); // "list", "xfile.c", 949
assert(xlist); // "list", "xfile.c", 949

for (int index = 0; index < fileList->fileNamesLength; index++) {
if (fileList->fileNames[index] != NULL) {
free(fileList->fileNames[index]);
for (int index = 0; index < xlist->fileNamesLength; index++) {
if (xlist->fileNames[index] != NULL) {
free(xlist->fileNames[index]);
}
}

free(fileList->fileNames);
free(xlist->fileNames);

// NOTE: Reset 8 bytes at 0x4DFF90, not sure how to express that clearly.
// I guess the idea was to clear everything but the pointer to next node in
// list.
memset(fileList, 0, sizeof(*fileList) - sizeof(fileList->next));
memset(xlist, 0, sizeof(*xlist));
}

// Recursively creates specified file path.
Expand Down Expand Up @@ -799,31 +798,32 @@ void xbaseExitHandler(void)
xbaseCloseAll();
}

bool _xlistenumfunc(XFileEnumerationContext* context)
// 0x4E0278
bool xlistEnumerateHandler(XListEnumerationContext* context)
{
if (context->type == XFILE_ENUMERATION_ENTRY_TYPE_DIRECTORY) {
return true;
}

FileList* fileList = context->fileList;
XList* xlist = context->xlist;

char** fileNames = (char**)realloc(fileList->fileNames, sizeof(*fileNames) * (fileList->fileNamesLength + 1));
char** fileNames = (char**)realloc(xlist->fileNames, sizeof(*fileNames) * (xlist->fileNamesLength + 1));
if (fileNames == NULL) {
fileListFree(fileList);
fileList->fileNamesLength = -1;
xlistFree(xlist);
xlist->fileNamesLength = -1;
return false;
}

fileList->fileNames = fileNames;
xlist->fileNames = fileNames;

fileNames[fileList->fileNamesLength] = strdup(context->name);
if (fileNames[fileList->fileNamesLength] == NULL) {
fileListFree(fileList);
fileList->fileNamesLength = -1;
fileNames[xlist->fileNamesLength] = strdup(context->name);
if (fileNames[xlist->fileNamesLength] == NULL) {
xlistFree(xlist);
xlist->fileNamesLength = -1;
return false;
}

fileList->fileNamesLength++;
xlist->fileNamesLength++;

return true;
}
21 changes: 10 additions & 11 deletions src/xfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,24 @@ typedef struct XFile {
};
} XFile;

typedef struct FileList {
typedef struct XList {
int fileNamesLength;
char** fileNames;
struct FileList* next;
} FileList;
} XList;

typedef enum XFileEnumerationEntryType {
XFILE_ENUMERATION_ENTRY_TYPE_FILE,
XFILE_ENUMERATION_ENTRY_TYPE_DIRECTORY,
XFILE_ENUMERATION_ENTRY_TYPE_DFILE,
} XFileEnumerationEntryType;

typedef struct XFileEnumerationContext {
typedef struct XListEnumerationContext {
char name[FILENAME_MAX];
unsigned char type;
FileList* fileList;
} XFileEnumerationContext;
XList* xlist;
} XListEnumerationContext;

typedef bool XFileEnumerationHandler(XFileEnumerationContext* context);
typedef bool(XListEnumerationHandler)(XListEnumerationContext* context);

extern XBase* gXbaseHead;
extern bool gXbaseExitHandlerRegistered;
Expand All @@ -80,12 +79,12 @@ int xfileEof(XFile* stream);
long xfileGetSize(XFile* stream);
bool xbaseReopenAll(char* paths);
bool xbaseOpen(const char* path);
int _xenumfiles(const char* pattern, XFileEnumerationHandler* handler, FileList* fileList);
int _xbuild_filelist(const char* pattern, FileList* fileList);
void fileListFree(FileList* fileList);
bool xlistEnumerate(const char* pattern, XListEnumerationHandler* handler, XList* xlist);
bool xlistInit(const char* pattern, XList* xlist);
void xlistFree(XList* xlist);
int xbaseMakeDirectory(const char* path);
void xbaseCloseAll();
void xbaseExitHandler(void);
bool _xlistenumfunc(XFileEnumerationContext* context);
bool xlistEnumerateHandler(XListEnumerationContext* context);

#endif /* XFILE_H */

0 comments on commit 4f229f1

Please sign in to comment.