Skip to content

Commit 9097307

Browse files
author
Pavel Kovalenko
committed
Optimize directory traversal & cleanup.
1 parent 1ef11cf commit 9097307

File tree

2 files changed

+51
-70
lines changed

2 files changed

+51
-70
lines changed

src/xrCore/LocatorAPI.cpp

Lines changed: 50 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -494,29 +494,27 @@ bool CLocatorAPI::load_all_unloaded_archives()
494494
}
495495

496496

497-
void CLocatorAPI::ProcessOne(LPCSTR path, void* _F)
497+
void CLocatorAPI::ProcessOne(LPCSTR path, const _finddata_t& entry)
498498
{
499-
_finddata_t& F = *((_finddata_t*)_F);
500-
501499
string_path N;
502500
xr_strcpy (N,sizeof(N),path);
503-
xr_strcat (N,F.name);
501+
xr_strcat (N,entry.name);
504502
xr_strlwr (N);
505503

506-
if (F.attrib&_A_HIDDEN) return;
504+
if (entry.attrib&_A_HIDDEN) return;
507505

508-
if (F.attrib&_A_SUBDIR) {
506+
if (entry.attrib&_A_SUBDIR) {
509507
if (bNoRecurse) return;
510-
if (0==xr_strcmp(F.name,".")) return;
511-
if (0==xr_strcmp(F.name,"..")) return;
508+
if (0==xr_strcmp(entry.name,".")) return;
509+
if (0==xr_strcmp(entry.name,"..")) return;
512510
xr_strcat (N,"\\");
513-
Register (N,0xffffffff,0,0,F.size,F.size,(u32)F.time_write);
511+
Register (N,0xffffffff,0,0,entry.size,entry.size,(u32)entry.time_write);
514512
Recurse (N);
515513
} else {
516514
if (strext(N) && (0==strncmp(strext(N),".db",3) || 0==strncmp(strext(N),".xdb",4)) )
517515
ProcessArchive (N);
518516
else
519-
Register (N,0xffffffff,0,0,F.size,F.size,(u32)F.time_write);
517+
Register (N,0xffffffff,0,0,entry.size,entry.size,(u32)entry.time_write);
520518
}
521519
}
522520

@@ -551,66 +549,49 @@ bool ignore_path(const char* _path){
551549

552550
bool CLocatorAPI::Recurse (const char* path)
553551
{
554-
_finddata_t sFile;
555-
intptr_t hFile;
556-
557-
string_path N;
558-
xr_strcpy (N,sizeof(N),path);
559-
xr_strcat (N,"*.*");
560-
561-
rec_files.reserve(1224);
562-
563-
// find all files
564-
if (-1==(hFile=_findfirst(N, &sFile)))
565-
{
566-
// Log ("! Wrong path: ",path);
567-
return false;
552+
string_path scanPath;
553+
xr_strcpy(scanPath, sizeof(scanPath), path);
554+
xr_strcat(scanPath, "*.*");
555+
_finddata_t findData;
556+
intptr_t handle = _findfirst(scanPath, &findData);
557+
if (handle == -1)
558+
{
559+
Log("! FS: Invalid path: ", path);
560+
return false;
568561
}
569-
570-
string1024 full_path;
571-
if (m_Flags.test(flNeedCheck))
572-
{
573-
xr_strcpy(full_path,sizeof(full_path), path);
574-
xr_strcat(full_path, sFile.name);
575-
576-
// çàãîíÿåì â âåêòîð äëÿ òîãî *.db* ïðèõîäèëè â ñîðòèðîâàííîì ïîðÿäêå
577-
if(!ignore_name(sFile.name) && !ignore_path(full_path))
578-
rec_files.push_back(sFile);
579-
580-
while ( _findnext( hFile, &sFile ) == 0 )
581-
{
582-
xr_strcpy(full_path,sizeof(full_path), path);
583-
xr_strcat(full_path, sFile.name);
584-
if(!ignore_name(sFile.name) && !ignore_path(full_path))
585-
rec_files.push_back(sFile);
586-
}
587-
}
588-
else
589-
{
590-
// çàãîíÿåì â âåêòîð äëÿ òîãî *.db* ïðèõîäèëè â ñîðòèðîâàííîì ïîðÿäêå
591-
if(!ignore_name(sFile.name))
592-
rec_files.push_back(sFile);
593-
594-
while ( _findnext( hFile, &sFile ) == 0 )
595-
{
596-
if(!ignore_name(sFile.name))
597-
rec_files.push_back(sFile);
598-
}
599-
600-
}
601-
602-
_findclose ( hFile );
603-
604-
FFVec buffer(rec_files);
605-
rec_files.clear_not_free();
606-
std::sort (buffer.begin(), buffer.end(), pred_str_ff);
607-
for (FFIt I = buffer.begin(), E = buffer.end(); I != E; ++I)
608-
ProcessOne (path, &*I);
609-
610-
// insert self
611-
if (path&&path[0])\
612-
Register (path,0xffffffff,0,0,0,0,0);
613-
562+
rec_files.reserve(256);
563+
size_t oldSize = rec_files.size();
564+
intptr_t done = handle;
565+
while (done != -1)
566+
{
567+
string1024 fullPath;
568+
bool ignore = false;
569+
if (m_Flags.test(flNeedCheck))
570+
{
571+
xr_strcpy(fullPath, sizeof(fullPath), path);
572+
xr_strcat(fullPath, findData.name);
573+
ignore = ignore_name(findData.name) || ignore_path(fullPath);
574+
}
575+
else
576+
{
577+
ignore = ignore_name(findData.name);
578+
}
579+
if (!ignore)
580+
rec_files.push_back(findData);
581+
done = _findnext(handle, &findData);
582+
}
583+
_findclose(handle);
584+
size_t newSize = rec_files.size();
585+
if (newSize > oldSize)
586+
{
587+
std::sort(rec_files.begin()+oldSize, rec_files.end(), pred_str_ff);
588+
for (size_t i = oldSize; i < newSize; i++)
589+
ProcessOne(path, rec_files[i]);
590+
rec_files.erase(rec_files.begin()+oldSize, rec_files.end());
591+
}
592+
// insert self
593+
if (path && path[0] != 0)
594+
Register(path, 0xffffffff, 0, 0, 0, 0, 0);
614595
return true;
615596
}
616597

src/xrCore/LocatorAPI.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class XRCORE_API CLocatorAPI
6969

7070
void Register (LPCSTR name, u32 vfs, u32 crc, u32 ptr, u32 size_real, u32 size_compressed, u32 modif);
7171
void ProcessArchive (LPCSTR path);
72-
void ProcessOne (LPCSTR path, void* F);
72+
void ProcessOne (LPCSTR path, const _finddata_t& entry);
7373
bool Recurse (LPCSTR path);
7474

7575
files_it file_find_it (LPCSTR n);

0 commit comments

Comments
 (0)