Read the directory (cpPath) and place the directory entries for the sub-directories and files, where they meet the supplied filename criteria (cpCriteria), in a single supplied vector. This function does not recurse into sub-directories.
| Return Type | Function name | Arguments |
|---|---|---|
| hzEcode | ReadDir | (hzVect<hzDirent>&,const char*,const char*,) |
Declared in file: hzDirectory.h
Defined in file : hzDirectory.cpp
Function Logic:
Function body:
hzEcode ReadDir (hzVect<hzDirent>& entries)const char* cpPath, const char* cpCriteria,
{
// Category: Directory
//
// Read the directory (cpPath) and place the directory entries for the sub-directories and files, where they meet the supplied
// filename criteria (cpCriteria), in a single supplied vector. This function does not recurse into sub-directories.
//
// Arguments: 1) entries The vector for the sub-directories and files
// 2) cpPath The directory to be examined (null is taken as the current working directory)
// 3) cpCriteria The file selection criteria (null equates to *)
//
// Returns: E_NOTFOUND If the requested directory does not exist.
// E_OPENFAIL If the requested directory could not be opened (eg insufficient permissions)
// E_CORRUPT If an entry found in the directory could not subsequently be stat-ed.
// E_OK The operation was successful.
_hzfunc("ReadDir(2)") ;
FSTAT fs ; // File status
dirent* pDE ; // Directory entry
DIR* pDir ; // Directory pointer
hzDirent meta ; // Directory entry metadata
hzString thePath ; // Path of directory being read
hzString teststr ; // Test string (filenames)
hzString filename ; // Filename
hzEcode rc ; // Return code
char fnBuf[320]; // Filename buffer
threadLog("Reading Dir [%s] for files matching [%s]\n", cpPath, cpCriteria) ;
entries.Clear() ;
// Establish applicable directory
if (cpPath && cpPath[0])
{
// Directory supplied
rc = GetAbsPath(thePath, cpPath) ;
if (rc != E_OK)
return hzwarn(rc, "Could not obtain absolute path for (%s)", cpPath) ;
threadLog("Reading Dir [%s] (absolute), for files matching [%s]\n", *thePath, cpCriteria) ;
if (lstat(*thePath, &fs) < 0)
return hzwarn(E_NOTFOUND, "No such directory or file exists (%s)", *thePath) ;
if (!S_ISDIR(fs.st_mode))
return hzwarn(E_TYPE, "Given path (%s) is not a directory", *thePath) ;
pDir = opendir(*thePath) ;
}
else
{
// No directory supplied, use current
GetCurrDir(thePath) ;
pDir = opendir(".") ;
}
if (!pDir)
return hzwarn(E_OPENFAIL, "Directory (%s) could not be opened", *thePath) ;
// Perform the directory read
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 (!FormCheckCstr(pDE->d_name, cpCriteria))
continue ;
// {
// threadLog("Rejected entry [%s]\n", pDE->d_name) ;
// continue ;
// }
// threadLog("Considering entry [%s]\n", pDE->d_name) ;
if (thePath)
{
sprintf(fnBuf, "%s/%s", *thePath, pDE->d_name) ;
// teststr = thePath ;
// teststr += "/" ;
// teststr += pDE->d_name ;
}
else
{
strcpy(fnBuf, pDE->d_name) ;
// teststr = pDE->d_name ;
}
if (stat(fnBuf, &fs) == -1)
{
closedir(pDir) ;
threadLog("CORRUPT DIR: Could not stat directory entry %s\n", fnBuf) ;
continue ;
}
filename = pDE->d_name ;
meta.InitStat(thePath, filename, fs) ;
entries.Add(meta) ;
}
closedir(pDir) ;
return E_OK ;
}