home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 576.lha / kd_freq.library_v3.01 / Programming.LZH / WFind.c < prev   
C/C++ Source or Header  |  1991-11-16  |  3KB  |  166 lines

  1. /*
  2.  *  kd_freq.library NewPatMatch() Test
  3.  *
  4.  *  This demonstrates the use of NewPatMatch() in reading the
  5.  *  FindDB:find.codes file.
  6.  *
  7.  *  You need to assemble and link  glue.asm with this, or use the
  8.  *  appropriate #pragmas!
  9.  *
  10.  */
  11.  
  12.  
  13. #include "KDBase.h"
  14.  
  15. struct Library *KD_FReqBase, *OpenLibrary();
  16.  
  17. main(argc,argv)
  18.     int argc;
  19.     char *argv[];
  20.     {
  21.     if (argc < 2)
  22.         {
  23.         print("WFind:\n\nUsage:   WFind pattern\n");
  24.         }
  25.     else if (KD_FReqBase = OpenLibrary(KLIBNAME,KLIBVERSION))
  26.         {
  27.         FindFiles(argv[1]);
  28.         CloseLibrary(KD_FReqBase);
  29.         }
  30.     else
  31.         {
  32.         print("Can't Open 'kd_freq.library'.  Make sure it is in LIBS:\n");
  33.         }
  34.     }
  35.  
  36.  
  37. long FileSize(filename)
  38.     char *filename;
  39.     {
  40.     struct FileInfoBlock *FIB;
  41.     struct FileLock *lock;
  42.     long size = 0L;
  43.     
  44.     FIB = (struct FileInfoBlock *)
  45.         AllocMem((long) sizeof(struct FileInfoBlock), MEMF_PUBLIC|MEMF_CLEAR);
  46.         
  47.     if (FIB)
  48.         {
  49.         if (lock = (struct FileLock *) Lock(filename,ACCESS_READ))
  50.             {
  51.             if (Examine(lock,FIB))
  52.                 size = FIB->fib_Size;
  53.             UnLock(lock);
  54.             }
  55.         FreeMem(FIB, (long) sizeof(struct FileInfoBlock));
  56.         }
  57.  
  58.     return(size);
  59.     }
  60.  
  61. #define FILE "FindDB:find.codes"
  62.  
  63. FindFiles(pattern)
  64.     UBYTE *pattern;
  65.     {
  66.     long status, buffersize;
  67.     struct FileHandle *in, *Open();
  68.     UBYTE *buffer;
  69.  
  70.     buffersize = FileSize(FILE);
  71.  
  72.     if (in = (struct FileHandle *)Open(FILE,MODE_OLDFILE))
  73.         {
  74.         if (buffer = (UBYTE *) AllocMem(buffersize,MEMF_PUBLIC|MEMF_CLEAR))
  75.             {
  76.             status = Read(in,buffer,buffersize);
  77.  
  78.             print("\n\nDirectories:\n------------");
  79.  
  80.             ScanFiles(pattern,buffer,status,1);
  81.  
  82.             print("\n\nFiles:\n------");
  83.             ScanFiles(pattern,buffer,status,2);
  84.  
  85.             FreeMem(buffer,buffersize);
  86.             }
  87.  
  88.         Close(in);
  89.         }
  90.     else
  91.         print("Couldn't find FindDB:find.codes!!");
  92.     }
  93.  
  94.  
  95. /* pass 1  shows directories,  pass 2 shows files..
  96.    this code is slow... and could be improved a lot with some
  97.    work...  You are very welcome to optimize it...
  98.    I'm too busy on something else.. (grin)
  99.  
  100. */
  101.  
  102. ScanFiles(pattern,buf,size,pass)
  103.     UBYTE *pattern;
  104.     UBYTE *buf;
  105.     LONG size;
  106.     USHORT pass;
  107.     {
  108.     UBYTE *ptr = buf, temp;
  109.     ULONG len;
  110.     UBYTE directory[128];
  111.     UBYTE path[160];
  112.     long i;
  113.  
  114.     while(ptr < (buf+size))
  115.         {
  116.         len  = strlen(ptr);
  117.  
  118.         temp = ptr[len-1];
  119.  
  120.         if ((temp == '/') || (temp == ':'))
  121.             {
  122.             strcpy(directory,ptr);
  123.             directory[strlen(directory)-1] = 0;
  124.  
  125.             for (i = strlen(directory); i >= 0; i--)
  126.                 {
  127.                 temp = directory[i];                
  128.                 if ((temp == '/') || (temp == ':'))
  129.                     break;
  130.                 }
  131.  
  132.             i++;
  133.  
  134.             if (pass == 1)
  135.                 if (NewPatMatch(pattern, directory+i) == 1)
  136.                     print(directory);
  137.  
  138.             strcpy(directory,ptr);
  139.             }
  140.         else 
  141.             if (pass == 2)
  142.                 if (NewPatMatch(pattern, ptr) == 1)
  143.                 {
  144.                 strcpy(path,directory);
  145.                 strcat(path,ptr);
  146.                 print(path);                    
  147.                 }
  148.  
  149.         while(*ptr++)
  150.             ;
  151.  
  152.         }
  153.     }
  154.  
  155.  
  156. /* to save some code size... */
  157.  
  158. print(string)
  159.     UBYTE *string;
  160.     {
  161.     struct FileHandle *Output();
  162.  
  163.     Write(Output(),string,strlen(string));
  164.     Write(Output(),"\n",1);
  165.     }
  166.