home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 May / macformat-024.iso / Shareware City / Developers / nshellmegasource1.50 / mega src / lib / walk_utl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-24  |  2.8 KB  |  135 lines  |  [TEXT/KAHL]

  1. /* ========================================
  2.  
  3.     walk_utl.c
  4.     
  5.     Copyright (c) 1994 Newport Software Development
  6.     
  7.     You may distribute unmodified copies of this file for
  8.     noncommercial purposes.  You may use this file as a
  9.     reference when writing your own programs.
  10.     
  11.     All other rights are reserved.
  12.     
  13.    ======================================== */
  14.    
  15. #include "nshc.h"
  16. #include "walk_utl.h"
  17. #include "walk_utl.proto.h"
  18. #include "str_utl.proto.h"
  19.  
  20. /* ======================================== */
  21.  
  22. void walk_copy( FSSpec *dest_fss, FSSpec *src_fss )
  23. {
  24.     dest_fss->vRefNum = src_fss->vRefNum;
  25.     dest_fss->parID = src_fss->parID;
  26.     pStrCopy( dest_fss->name, src_fss->name );
  27. }
  28.  
  29. /* ======================================== */
  30.  
  31. t_walk_hndl walk_init( FSSpec *start_fss )
  32. {
  33.     t_walk_hndl    wData;
  34.     
  35.     wData = (t_walk_hndl)NewHandleClear( sizeof( t_walk_data ) );
  36.     
  37.     // start with current fsspec as level 0
  38.     
  39.     if ( wData ) {
  40.         walk_copy( &(**wData).levels[0].fss, start_fss );
  41.         (**wData).levels[0].index = 0;
  42.         (**wData).level = 0;
  43.         }
  44.         
  45.     return( wData );
  46. }
  47.  
  48. /* ======================================== */
  49.  
  50. OSErr walk_next( t_walk_hndl wData, FSSpec *fss, short *level, short *isDir )
  51. {
  52.     short        this_level;
  53.     short        this_index;
  54.     short        this_vol;
  55.     long        this_dir;
  56.     Str32        this_name;
  57.     
  58.     short        new_level;
  59.  
  60.     OSErr        error;
  61.     CInfoPBRec    cipbr;
  62.     HFileInfo    *fpb;
  63.     short        idx;
  64.     long        foundDir;
  65.     
  66.     *level = 0;
  67.     
  68.     this_level = (**wData).level;
  69.  
  70.     do {    // find the next item (in this or a parent directory)
  71.     
  72.         // fill local variables
  73.         
  74.         this_index = (**wData).levels[this_level].index;
  75.         this_vol = (**wData).levels[this_level].fss.vRefNum;
  76.         this_dir = (**wData).levels[this_level].fss.parID;
  77.         pStrCopy( this_name, (**wData).levels[this_level].fss.name );
  78.         
  79.         // prepare for PBGetCatInfo
  80.         
  81.         fpb = (HFileInfo *)&cipbr;
  82.         fpb->ioVRefNum = this_vol;
  83.         fpb->ioDirID = this_dir;
  84.         fpb->ioFDirIndex = this_index;
  85.         fpb->ioNamePtr = this_name;
  86.         
  87.         // see what the current item is
  88.         
  89.         error = PBGetCatInfo( &cipbr, FALSE );
  90.         
  91.         if ( fpb->ioFlAttrib & 16 )
  92.             *isDir = 1;
  93.         else 
  94.             *isDir = 0;
  95.         
  96.         (**wData).levels[this_level].index++;
  97.     
  98.         if ( error )
  99.             if ( this_level > 1 )
  100.                 this_level--;
  101.             else
  102.                 return( error );
  103.                     
  104.     } while ( error );
  105.         
  106.     // if it is a directory, set up a new level
  107.     
  108.     if ( *isDir ) {
  109.     
  110.         new_level = this_level + 1;
  111.         
  112.         if ( new_level >= MAX_WALK_LEVELS )
  113.             return( tmfoErr );
  114.             
  115.         (**wData).levels[new_level].fss.vRefNum = fpb->ioVRefNum;
  116.         (**wData).levels[new_level].fss.parID = fpb->ioDirID;
  117.         pStrCopy( (**wData).levels[new_level].fss.name, this_name );
  118.         (**wData).levels[new_level].index = 1;
  119.         
  120.         (**wData).level = new_level;
  121.         
  122.         }
  123.  
  124.     // and set up return values
  125.     
  126.     fss->vRefNum = this_vol;
  127.     fss->parID = this_dir;
  128.     pStrCopy( fss->name, this_name );
  129.     *level = this_level;
  130.     
  131.     return( 0 );
  132. }
  133.  
  134. /* ======================================== */
  135.