Populate vectors of directory entries for the sub-directories and files in a given directory where these meet the supplied selection criteria. This function will not recurse into the sub-directories.
| Return Type | Function name | Arguments |
|---|---|---|
| hzEcode | ReadDir | (hzVect<hzDirent>&,hzVect<hzDirent>&,const char*,const char*,) |
Declared in file: hzDirectory.h
Defined in file : hzDirectory.cpp
Function Logic:
Function body:
hzEcode ReadDir (hzVect<hzDirent>& Dirs)hzVect<hzDirent>& Files, const char* cpPath, const char* cpCriteria,
{
// Category: Directory
//
// Populate vectors of directory entries for the sub-directories and files in a given directory where these meet the supplied
// selection criteria. This function will not recurse into the sub-directories.
//
// Arguments: 1) Dirs The vector for the sub-directories
// 2) Files The vector for the files
// 3) cpPath The directory to be examined (null is taken as the current working directory)
// 4) cpCriteria The file selection criteria (null equates to *)
//
// Returns: E_OPENFAIL If the directory cannot be opened
// E_CORRUPT If a directory entry could not be found by the stat function
// E_OK If operation successful (even if nothing is found)
_hzfunc("ReadDir(1)") ;
FSTAT fs ; // File status
dirent* pDE ; // Diectory entry
DIR* pDir ; // Directory
hzDirent meta ; // Directory entry meta data
hzString thePath ; // The path to here
hzString teststr ; // Fullpath test value
hzString de_name ; // Directory entry name
hzEcode rc ; // Return code
/*
** ** Move thru current directory and read files and sub directories
** */
Dirs.Clear() ;
Files.Clear() ;
rc = GetAbsPath(thePath, cpPath) ;
if (rc != E_OK)
return hzwarn(rc, "Could not obtain absolute path for (%s)", cpPath) ;
if (lstat(*thePath, &fs) < 0)
return E_NOTFOUND ;
if (!S_ISDIR(fs.st_mode))
return hzwarn(E_TYPE, "Given path (%s) is not a directory", cpPath) ;
pDir = opendir(*thePath) ;
if (!pDir)
return hzwarn(E_OPENFAIL, "Directory (%s) could not be opened", cpPath) ;
for (; pDE = readdir(pDir) ;)
{
if (pDE->d_name[0]== ''.''&&(pDE->d_name[1]== 0|| (pDE->d_name[1]== ''.''&&pDE->d_name[2]== 0)))
continue ;
if (cpCriteria)
{
if (!FormCheckCstr(pDE->d_name, cpCriteria))
continue ;
}
if (cpPath && cpPath[0])
{
teststr = cpPath ;
teststr += "/" ;
teststr += pDE->d_name ;
}
else
teststr = pDE->d_name ;
if (stat(*teststr, &fs) == -1)
{
closedir(pDir) ;
hzerr(E_CORRUPT, "Could not stat directory entry %s", *teststr) ;
return E_CORRUPT ;
}
if (S_ISDIR(fs.st_mode))
{
// The entry is a directory
de_name = pDE->d_name ;
meta.InitStat(thePath, de_name, fs) ;
Dirs.Add(meta) ;
continue ;
}
if (S_ISREG(fs.st_mode))
{
de_name = pDE->d_name ;
meta.InitStat(thePath, de_name, fs) ;
Files.Add(meta) ;
continue ;
}
}
closedir(pDir) ;
return E_OK ;
}