home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / comm / revrdist.sit / RevRdist / RevRdist src / catalog.c next >
Encoding:
C/C++ Source or Header  |  1990-06-06  |  5.5 KB  |  222 lines  |  [TEXT/KAHL]

  1. /*
  2.  * catalog.c - routines associated with accessing Mac directory
  3.  *    catalogs and manipulating the cat_node structures.
  4.  */
  5.  
  6. #include "RevRdist.h"
  7. #include <unix.h>
  8. #include <TransSkelProto.h>
  9. #include <TransDisplayProto.h>
  10.  
  11. extern void lastpart (StringPtr);
  12.  
  13.  
  14. /*
  15.  *=========================================================================
  16.  * freeList (l) - free cat_node list
  17.  * entry:    l = head of cat_node list to be freed
  18.  * exit:    all nodes in list returned to memory pool
  19.  *=========================================================================
  20.  */
  21.  
  22. void
  23. freeList (l)
  24.     cnode_t    *l;
  25. {
  26.     cnode_t *n;
  27.  
  28.     while (l)
  29.     {
  30.         n = l->link;                /* remember next node */
  31.         DisposPtr ((Ptr)l);            /* free this node */
  32.         l = n;
  33.     }
  34. }
  35.  
  36.  
  37.  
  38. /*
  39.  *=========================================================================
  40.  * getInfo (name, vol, dirID, cp) - get catalog info about named file/folder
  41.  * entry:    name = ptr to name of file/folder to get info about
  42.  *            vol = VRefNum where file/folder resides
  43.  *            dirID = directory ID name is a partial path relative to
  44.  *            cp = ptr to cnode_t to put catalog information into
  45.  * returns:    0 if no error, else OSErr
  46.  *=========================================================================
  47.  */
  48. OSErr
  49. getInfo (name, vol, dirID, cp)
  50.         StringPtr        name;
  51.         Integer            vol;
  52.         Longint            dirID;
  53.         cnode_t *        cp;
  54. {
  55.     CInfoPBRec            ci;
  56.     OSErr                error;
  57.     Str255                s;
  58.  
  59.     ZERO (ci);
  60.     COPYPS (name, s);
  61.     ci.hFileInfo.ioNamePtr = s;
  62.     ci.hFileInfo.ioVRefNum = vol;
  63.     ci.hFileInfo.ioDirID = dirID;
  64.     if (error = PBGetCatInfo (&ci, false))
  65.         return error;
  66.     lastpart (s);
  67.     saveCatInfo (&ci, cp);
  68.     return 0;
  69. }
  70.  
  71.  
  72.  
  73. /*
  74.  *=========================================================================
  75.  * listFolder (vol, dir) - build cat_node list of folder
  76.  * entry:    vol = volume refnum for folder
  77.  *            dir = directory id within vol of folder to catalog
  78.  * returns:    ptr to cat_node list of files and folders in dir, list is
  79.  *                in alphabetical order
  80.  *            NULL if cannot.  ClueID set to STR# number of reason.
  81.  *=========================================================================
  82.  */
  83.  
  84. cnode_t *
  85. listFolder (vol, dir)
  86.     Integer    vol;
  87.     Longint    dir;
  88. {
  89.     OSErr            error;
  90.     int                idx;            /* index into folder */
  91.     int                len;            /* name length */
  92.     cnode_t            *cur, *node, *prev;        /* pointers into list */
  93. register CInfoPBPtr    cp;                /* ptr to catalog entry */
  94.     HParamBlockRec    pb;                /* param block for file sys calls */
  95.     cnode_t            head;            /* dummy head of list */
  96.     Str255            name;            /* current entry name */
  97.     
  98.     prev = cur = &head;
  99.     cp = (CInfoPBPtr) &pb;
  100.     ClueID = 0;
  101.     Clue0 = (SP) "\plistFolder";
  102.     ZERO (head);
  103.     ZERO (pb);
  104.     pb.fileParam.ioNamePtr = name;
  105.     /*
  106.      * Call GetCatInfo until we run out of directory entries
  107.      */
  108.     for (idx = 1, error = 0; !error && !Quit; idx++)
  109.     {
  110.         name[0] = 0;                /* specify entry by idx */
  111.         cp->hFileInfo.ioVRefNum = vol;
  112.         cp->hFileInfo.ioFDirIndex = idx;
  113.         cp->hFileInfo.ioDirID = dir;
  114.         error = PBGetCatInfo (cp, false);
  115.         if (error)
  116.         {
  117.             Clue1 = (SP) "\pPBGetCatInfo";
  118.             break;
  119.         }
  120.         len = name[0];
  121.         if (len == 0 || len > 31)
  122.             continue;                /* should not happen */
  123.         /*
  124.          * allocate a new node and copy information to it
  125.          */
  126.         node = (cnode_t *) NewPtr (sizeof (* node));
  127.         if (!node)
  128.         {
  129.             Clue1 = (SP) "\pNewPtr";
  130.             error = MemError ();
  131.             if (error == 0)
  132.                 error = memFullErr;
  133.             break;
  134.         }
  135.         ZEROAT(node);
  136.         saveCatInfo (cp, node);
  137.         /*
  138.          * Insertion sort the name into current cat_list.
  139.          * Mac file system catalogs are already in the correct order,
  140.          * so start at the front only if the new name is before the
  141.          * previous one.
  142.          */
  143.         if (RelString (name, prev->name, false, true) < 0)
  144.             prev = cur = &head;
  145.         for (; cur; prev = cur, cur = prev->link)
  146.             if (RelString (name, cur->name, false, true) < 0)
  147.                 break;
  148.         node->link = cur;
  149.         prev->link = node;
  150.         prev = node;
  151.     }
  152.     node = head.link;
  153.     if (error && error != fnfErr)
  154.     {
  155.         ClueID = error;
  156.         freeList (node);
  157.         node = nil;
  158.     }
  159.     if (!error && Quit)
  160.     {
  161.         freeList (node);
  162.         node = nil;
  163.     }
  164.     return node;
  165. }
  166.  
  167.  
  168. /*
  169.  *=========================================================================
  170.  * saveCatInfo (cp, node) - save catalog information in catalog node
  171.  * entry:    cp = ptr to CInfoPBRec with information
  172.  *            node = ptr to catalog node to copy information to
  173.  *=========================================================================
  174.  */
  175. void
  176. saveCatInfo (cp, node)
  177. register    CInfoPBRec *    cp;
  178. register    cnode_t *        node;
  179. {
  180. register    StringPtr        name;
  181.  
  182.     name = cp->hFileInfo.ioNamePtr;
  183.     if (name)
  184.     {
  185.         if (name[0] > 31)
  186.         {
  187.             panic (false, E_NAMELEN, (SP)"\psaveCatInfo", name, nil);
  188.             if (Quit)
  189.                 return;
  190.             name[0] = 31;
  191.         }
  192.         COPYPS (name, node->name);
  193.     }
  194.     if (cp->hFileInfo.ioFlAttrib & 0x10)
  195.     {
  196.         /* folder */
  197.         node->ctype = C_FOLDER;
  198.         node->dirID = cp->dirInfo.ioDrDirID;
  199.         node->crDate = cp->dirInfo.ioDrCrDat;
  200.         node->mdDate = cp->dirInfo.ioDrMdDat;
  201.         node->parID = cp->dirInfo.ioDrParID;
  202.         node->in.d.dinfo = cp->dirInfo.ioDrUsrWds;
  203.         node->in.d.frScroll = cp->dirInfo.ioDrFndrInfo.frScroll;
  204.         node->in.d.frOChain = cp->dirInfo.ioDrFndrInfo.frOpenChain;
  205.         node->attrib = cp->dirInfo.ioFlAttrib;
  206.         node->access = cp->dirInfo.ioACUser;
  207.     }
  208.     else
  209.     {
  210.         /* file */
  211.         node->ctype = C_FILE;
  212.         node->dirID = cp->hFileInfo.ioDirID;
  213.         node->parID = cp->hFileInfo.ioFlParID;
  214.         node->crDate = cp->hFileInfo.ioFlCrDat;
  215.         node->mdDate = cp->hFileInfo.ioFlMdDat;
  216.         node->in.f.fileLen = cp->hFileInfo.ioFlLgLen;
  217.         node->in.f.rsrcLen = cp->hFileInfo.ioFlRLgLen;
  218.         node->in.f.finfo = cp->hFileInfo.ioFlFndrInfo;
  219.         node->attrib = cp->hFileInfo.ioFlAttrib;
  220.         node->access = cp->hFileInfo.ioACUser;
  221.     }
  222. }