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 TypeFunction nameArguments
hzEcodeReadDir(hzVect<hzDirent>&,hzVect<hzDirent>&,const char*,const char*,)

Declared in file: hzDirectory.h
Defined in file : hzDirectory.cpp

Function Logic:

0:START 1:items items rc 2:unknown 3:Return hzwarn(rc,Could not obtain absolute path for (%s),cpPath) 4:unknown 5:Return E_NOTFOUND 6:unknown 7:Return hzwarn(E_TYPE,Given path (%s) is not a directory,cpPath) 8:pDir 9:unknown 10:Return hzwarn(E_OPENFAIL,Directory (%s) could not be opened,cpPath) 11:unknown 12:unknown 13:unknown 14:unknown 15:unknown 16:teststr teststr teststr 17:teststr 18:unknown 19:items items 20:Return E_CORRUPT 21:unknown 22:de_name items items 23:unknown 24:de_name items items 25:items 26:Return E_OK

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 ;
}