MCPcopy Create free account
hub / github.com/assaultcube/AC / listdir

Function listdir

source/src/stream.cpp:361–414  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

359}
360
361bool listdir(const char *dir, const char *ext, vector<char *> &files)
362{
363 int extsize = ext ? (int)strlen(ext)+1 : 0;
364 #if defined(WIN32)
365 defformatstring(pathname)("%s\\*.%s", dir, ext ? ext : "*");
366 WIN32_FIND_DATA FindFileData;
367 HANDLE Find = FindFirstFile(path(pathname), &FindFileData);
368 if(Find != INVALID_HANDLE_VALUE)
369 {
370 do {
371 if(!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
372 files.add(newstring(FindFileData.cFileName, (int)strlen(FindFileData.cFileName) - extsize));
373 } while(FindNextFile(Find, &FindFileData));
374 FindClose(Find);
375 return true;
376 }
377 #else
378 string pathname;
379 copystring(pathname, dir);
380 DIR *d = opendir(path(pathname));
381 if(d)
382 {
383 struct dirent *de, b;
384 while(!readdir_r_(d, &b, &de) && de != NULL)
385 {
386 bool isreg = false;
387 #ifdef _DIRENT_HAVE_D_TYPE
388 if(de->d_type == DT_REG) isreg = true;
389 else if(de->d_type == DT_UNKNOWN)
390 #endif
391 {
392 struct stat s;
393 int dl = (int)strlen(pathname);
394 concatformatstring(pathname, "/%s", de->d_name);
395 isreg = !lstat(pathname, &s) && S_ISREG(s.st_mode);
396 pathname[dl] = '\0';
397 }
398 if(isreg)
399 {
400 if(!ext) files.add(newstring(de->d_name));
401 else
402 {
403 int namelength = (int)strlen(de->d_name) - extsize;
404 if(namelength > 0 && de->d_name[namelength] == '.' && strncmp(de->d_name+namelength+1, ext, extsize-1)==0)
405 files.add(newstring(de->d_name, namelength));
406 }
407 }
408 }
409 closedir(d);
410 return true;
411 }
412 #endif
413 else return false;
414}
415
416void listfiles(const char *dir, const char *ext, vector<char *> &files, int (__cdecl *sf)(const char **, const char **))
417{

Callers 2

listfilesFunction · 0.85
loopvFunction · 0.85

Calls 6

pathFunction · 0.85
newstringFunction · 0.85
copystringFunction · 0.85
readdir_r_Function · 0.85
concatformatstringFunction · 0.85
addMethod · 0.45

Tested by

no test coverage detected