home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 4 / CDPD_IV.bin / utilities / hypertext / adtoht / source / readdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-22  |  3.8 KB  |  123 lines

  1. /* Make a list of files */
  2.  
  3. #include "global.h"
  4. #include "misc.h"
  5. #include "readdir.h"
  6.  
  7. /***********************************************/
  8.  
  9. struct FileNode *FileList;
  10.  
  11. /***********************************************/
  12.  
  13. BOOL AddFile(char *Directory, char *File)
  14.  
  15. {
  16.    struct FileNode *NewNode, *OldNode;
  17.  
  18.    if (NewNode=AllocVec(sizeof(struct FileNode)+strlen(Directory)+strlen(File)+1,0))
  19.       {
  20.          NewNode->Node.Name=(char *)(NewNode+1);
  21.          stpcpy(stpcpy(NewNode->Node.Name,Directory),File);
  22.          OldNode=FileList;
  23.          while (OldNode && Stricmp(OldNode->Node.Name,NewNode->Node.Name))
  24.             {
  25.                OldNode=(struct FileNode *)(OldNode->Node.Next);
  26.             }
  27.          if (OldNode)
  28.             {
  29.                printf("Error: file %s found twice.\n",NewNode->Node.Name);
  30.                FreeVec(NewNode);
  31.                return(FALSE);
  32.             }
  33.          NewNode->Node.Next=FileList;
  34.          FileList=NewNode;
  35.          return(TRUE);
  36.       }
  37.    PrintFault(ERROR_NO_FREE_STORE,NULL);
  38.    return(FALSE);
  39. }
  40.  
  41. /***********************************************/
  42.  
  43. BOOL ReadDir(BPTR Directory, char *DirName, char *Extension, BOOL (*Function)(char *Name))
  44.  
  45. {
  46.    struct FileInfoBlock __aligned FileInfoBlock;
  47.    BPTR OldCurrentDir;
  48.    long Length;
  49.    int ExtensionLength;
  50.    char *NewName;
  51.    BPTR NewDirectory;
  52.    BOOL Success;
  53.  
  54.    OldCurrentDir=CurrentDir(Directory);
  55.    if (Examine(Directory,&FileInfoBlock))
  56.       {
  57.          Success=TRUE;
  58.          ExtensionLength=strlen(Extension);
  59.          while (Success && ExNext(Directory,&FileInfoBlock))
  60.             {
  61.                if (Break())
  62.                   {
  63.                      Success=FALSE;
  64.                   }
  65.                else
  66.                   {
  67.                      if (FileInfoBlock.fib_DirEntryType<0)
  68.                         {
  69.                            Length=strlen(FileInfoBlock.fib_FileName);
  70.                            if (Length>=ExtensionLength && !Stricmp(FileInfoBlock.fib_FileName+Length-ExtensionLength,Extension))
  71.                               {
  72.                                  if (!AddFile(DirName,FileInfoBlock.fib_FileName) || !Function(FileInfoBlock.fib_FileName))
  73.                                     {
  74.                                        Success=FALSE;
  75.                                     }
  76.                               }
  77.                         }
  78.                      else
  79.                         {
  80.                            if (NewName=CreateString37("%s%s/",DirName,FileInfoBlock.fib_FileName))
  81.                               {
  82.                                  if (NewDirectory=Lock(FileInfoBlock.fib_FileName,ACCESS_READ))
  83.                                     {
  84.                                        Success=ReadDir(NewDirectory,NewName,Extension,Function);
  85.                                        UnLock(NewDirectory);
  86.                                     }
  87.                                  else
  88.                                     {
  89.                                        PrintFault(IoErr(),NewName);
  90.                                        Success=FALSE;
  91.                                     }
  92.                                  FreeVec(NewName);
  93.                               }
  94.                            else
  95.                               {
  96.                                  PrintFault(ERROR_NO_FREE_STORE,NULL);
  97.                                  Success=FALSE;
  98.                               }
  99.                         }
  100.                   }
  101.             }
  102.          if (Success)
  103.             {
  104.                if (IoErr()==ERROR_NO_MORE_ENTRIES)
  105.                   {
  106.                      Success=TRUE;
  107.                   }
  108.                else
  109.                   {
  110.                      PrintFault(IoErr(),DirName);
  111.                      Success=FALSE;
  112.                   }
  113.             }
  114.       }
  115.    else
  116.       {
  117.          PrintFault(IoErr(),DirName);
  118.          Success=FALSE;
  119.       }
  120.    CurrentDir(OldCurrentDir);
  121.    return(Success);
  122. }
  123.