home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Disk / Directory / Directory.c next >
Encoding:
Text File  |  1993-02-03  |  7.8 KB  |  397 lines  |  [TEXT/KAHL]

  1. //-- Directory.c --//
  2.  
  3. // This takes the directory specified and produces a picture in the window 
  4. // outlined with the contents of that picture.
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <String.h>
  9. #include "struct.h"
  10.  
  11. HParamBlockRec    myCPB;
  12. char            fName [255];
  13. struct DrawWindow *myWindow;
  14. long            curPLine;
  15. long            totFiles, totDirect;
  16. extern struct DrawWindow *drawList;
  17.  
  18. //-- CanEject --//
  19.  
  20. //- Return 1 if this drive can be ejected and unmounted.
  21.  
  22. int CanEject (i)
  23. short i;
  24. {
  25.     char buffer [64];
  26.     short vRefNum;
  27.     long fileSize;
  28.     QHdrPtr fqueue;
  29.     struct DRIVE {
  30.         QElemPtr qLink;
  31.         short qType;
  32.         short dQDrive;
  33.         short dQRefNum;
  34.         short dQFSID;
  35.         short dQDrvSz;
  36.         short dQDrvSz2;
  37.     } *drive;
  38.     
  39. // Figure out what drive this thing is on by searching the mounted drive queue.
  40.         
  41.     fqueue = GetDrvQHdr ();
  42.     drive = (struct DRIVE *) (fqueue->qHead);    // Offset -4.
  43.     while (drive != NULL) {
  44.         GetVInfo (drive->dQDrive, buffer, &vRefNum, &fileSize);
  45.         if (vRefNum == i) {
  46.             if (0x08 & ((unsigned char *) drive) [-3]) return 0;
  47.             else return 1;
  48.         }
  49.         drive = (struct DRIVE *)(drive->qLink);
  50.     }
  51.     return 1;
  52. }
  53.  
  54. //-- TAdd --//
  55.  
  56. // This adds data with the tab indentation specified to the window.
  57.  
  58. TAdd (w, t, s)
  59. struct DrawWindow *w;
  60. short t;
  61. char *s;
  62. {
  63.     struct DirectData d;
  64.  
  65.     curPLine = GetHandleSize (w->data) / sizeof (struct DirectData);
  66.     strcpy (d.data, s);
  67.     CtoPstr (d.data);
  68.     d.indent = t;
  69.     d.auxdata [0] = '\0';
  70.     d.auxdata2 [0] = '\0';
  71.  
  72.     PtrAndHand (&d, w->data, sizeof (d));
  73. }
  74.  
  75. //-- TAppend --//
  76.  
  77. // Append string to the data line.
  78.  
  79. TAppend (w, l, s)
  80. struct DrawWindow *w;
  81. long l;
  82. char *s;
  83. {
  84.     struct DirectData *ptr;
  85.  
  86.     HLock (w->data);
  87.     ptr = * (w->data);
  88.     PtoCstr (ptr [l].auxdata);
  89.     strcpy (ptr [l].auxdata, s);
  90.     CtoPstr (ptr [l].auxdata);
  91.     HUnlock (w->data);
  92. }
  93.  
  94. //-- TAppend2 --//
  95.  
  96. // Append string to the data line for file use.
  97.  
  98. TAppend2 (w, l, s)
  99. struct DrawWindow *w;
  100. long l;
  101. char *s;
  102. {
  103.     struct DirectData *ptr;
  104.  
  105.     HLock (w->data);
  106.     ptr = * (w->data);
  107.     PtoCstr (ptr [l].auxdata2);
  108.     strcpy (ptr [l].auxdata2, s);
  109.     CtoPstr (ptr [l].auxdata2);
  110.     HUnlock (w->data);
  111. }
  112.  
  113. //-- SearchDisks --//
  114.  
  115. // This scans all the open windows to assure that the list of disks and windows
  116. // do indeed match.  If they don't, this routine closes and opens as appropriate.
  117.  
  118. SearchDisks ()
  119. {
  120.     HParamBlockRec myHParam;
  121.     int i;
  122.     int j;
  123.     
  124.     for (j = 0; j < MAXWINDOWS; j++) if (drawList [j].inuse) drawList [j].inuse = 2;
  125.  
  126.     for (i = 1; ; i++) {
  127.         myHParam.volumeParam.ioCompletion = NULL;
  128.         myHParam.volumeParam.ioNamePtr = NULL;
  129.         myHParam.volumeParam.ioVRefNum = 0;
  130.         myHParam.volumeParam.ioVolIndex = i;
  131.         if (PBHGetVInfo (&myHParam, 0)) break;
  132.         for (j = 0; j < MAXWINDOWS; j++) {
  133.             if ((myHParam.volumeParam.ioVRefNum == drawList [j].vRefNum) &&
  134.                  (drawList [j].inuse != 0)) {
  135.                     drawList [j].inuse = 1;
  136.                     break;
  137.             }
  138.         }
  139.         if (j == MAXWINDOWS) {
  140.             for (j = 0; j < MAXWINDOWS; j++) if (drawList [j].inuse == 0) break;
  141.             if (j != 0) NewPlan (myHParam.volumeParam.ioVRefNum);
  142.         }
  143.     }
  144.     
  145.     for (j = 0; j < MAXWINDOWS; j++) {
  146.         if (drawList [j].inuse == 2) {
  147.             DisposHandle (drawList [j].data);    /* Close without unmounting */
  148.             CloseWindow (& (drawList [j]));    /* as this disk doesn't exist */
  149.             FreeWind (& (drawList [j]));
  150.         }
  151.     }
  152. }
  153.  
  154. //-- GetMounted --//
  155.  
  156. // This opens windows for all mounted volumes.
  157.  
  158. GetMounted ()
  159. {
  160.     HParamBlockRec myHParam;
  161.     int i;
  162.  
  163.     for (i = 1; ; i++) {
  164.         myHParam.volumeParam.ioCompletion = NULL;
  165.         myHParam.volumeParam.ioNamePtr = NULL;
  166.         myHParam.volumeParam.ioVRefNum = 0;
  167.         myHParam.volumeParam.ioVolIndex = i;
  168.         if (PBHGetVInfo (&myHParam, 0)) break;
  169.         NewPlan (myHParam.volumeParam.ioVRefNum);
  170.     }
  171. }
  172.  
  173. //-- MyGetDInfo --//
  174.  
  175. // Print the directory information.
  176.  
  177. MyGetDInfo (t)
  178. short t;
  179. {
  180.     char buffer [128];
  181.  
  182.     PtoCstr (fName);
  183.     sprintf (buffer, ":%s:", fName);
  184.     TAdd (myWindow, t, buffer);
  185.     CtoPstr (fName);
  186. }
  187.  
  188. //-- AddDirSize --//
  189.  
  190. // This adds the directory size to the file name stuff.
  191.  
  192. AddDirSize (l, s)
  193. long l;
  194. long s;
  195. {
  196.     char buffer [64];
  197.  
  198.     sprintf (buffer, "%ld", s);
  199.     TAppend (myWindow, l, buffer);
  200. }
  201.  
  202. //-- MyGetFInfo
  203.  
  204. // Print the file information.
  205.  
  206. MyGetFInfo (t)
  207. short t;
  208. {
  209.     char buffer [128];
  210.     union {
  211.         OSType type;
  212.         char name [6];
  213.     } u;
  214.     
  215.     switch (myWindow->state) {
  216.         case 2:
  217.             break;
  218.         case 1:
  219.             if (myCPB.fileParam.ioFlFndrInfo.fdType != 'APPL') return;
  220.             break;
  221.         case 0:
  222.             return;
  223.     }
  224.  
  225.     PtoCstr (fName);
  226.     sprintf (buffer, "%s", fName);
  227.     TAdd (myWindow, t, buffer);
  228.     CtoPstr (fName);
  229.     
  230.     AddDirSize (curPLine, myCPB.fileParam.ioFlRPyLen + myCPB.fileParam.ioFlPyLen);
  231.     
  232.     u.name [4] = '\0';
  233.     u.type = myCPB.fileParam.ioFlFndrInfo.fdType;
  234.     sprintf (buffer, "%s ", u.name);
  235.     u.type = myCPB.fileParam.ioFlFndrInfo.fdCreator;
  236.     strcat (buffer, u.name);
  237.     TAppend2 (myWindow, curPLine, buffer);
  238. }
  239.  
  240.  
  241.  
  242. long lenMem;                    /* Don't need to recursively save in Enumer... */
  243.  
  244.  
  245.  
  246. //-- UpdateStatus
  247.  
  248. // Update what's going on in the current window.
  249.  
  250. UpdateStatus ()
  251. {
  252.     Rect r;
  253.     char buffer [128];
  254.  
  255.     TextFont (1);
  256.     TextSize (12);
  257.     r.top = 0;
  258.     r.left = 0;
  259.     r.right = 500;
  260.     r.bottom = 20;
  261.     EraseRect (&r);
  262.     MoveTo (10, 15);
  263.     sprintf (buffer, "Found %ld files, %ld directories, %ld lines", totFiles, totDirect, curPLine);
  264.     CtoPstr (buffer);
  265.     DrawString (buffer);
  266. }
  267.  
  268. //-- EnumerateCatalog
  269.  
  270. // Right out of TN68.
  271.  
  272. long EnumerateCatalog (t, dirIDToSearch)
  273. short t;
  274. long dirIDToSearch;
  275. {
  276.     short    index = 1;
  277.     OSErr    err;
  278.     long    curLine;
  279.     long    curmem = 0;
  280.  
  281.     do {
  282.         myCPB.fileParam.ioFDirIndex = index;
  283.         myCPB.fileParam.ioDirID = dirIDToSearch;
  284.         err = PBGetCatInfo (&myCPB, 0);
  285.         if (err == noErr) {
  286.             if (((myCPB.fileParam.ioFlAttrib >> 4) & 0x01) == 1) {
  287.                 MyGetDInfo (t);
  288.                 curLine = curPLine;
  289.                 lenMem = EnumerateCatalog (t+1, myCPB.fileParam.ioDirID);
  290.                 err = 0;
  291.                 AddDirSize (curLine, lenMem);
  292.                 curmem += lenMem;
  293.                 totDirect++;
  294.             } else {
  295.                 MyGetFInfo (t);
  296.                 curmem += myCPB.fileParam.ioFlRPyLen + myCPB.fileParam.ioFlPyLen;
  297.                 totFiles++;
  298.             }
  299.         }
  300.         index++;
  301.     } while (err != fnfErr);
  302.     UpdateStatus ();
  303.     return curmem;
  304. }
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311. //-- ComputePict (name, w);
  312.  
  313. // This computes the content of the window specified.
  314.  
  315. ComputePict (name, w)
  316. short name;
  317. struct DrawWindow *w;
  318. {
  319.     char buffer [255];
  320.     char vname [64];
  321.     Rect r;
  322.     HParamBlockRec myHParam;
  323.     long curLine;
  324.     CursHandle cursor;
  325.  
  326.  
  327.     cursor = GetCursor (watchCursor);
  328.     HLock (cursor);
  329.     SetCursor (*cursor);
  330.     HUnlock (cursor);
  331.     HPurge (cursor);
  332.  
  333.     w->vRefNum = name;
  334.     w->data = (struct DirectData **)NewHandle (0);
  335.  
  336. // Get volume name and information block.
  337.  
  338.     myHParam.volumeParam.ioCompletion = NULL;
  339.     vname [0] = '\0';
  340.     myHParam.volumeParam.ioNamePtr = (unsigned char *)vname;
  341.     myHParam.volumeParam.ioVRefNum = name;
  342.     myHParam.volumeParam.ioVolIndex = 0;
  343.     PBHGetVInfo (&myHParam, 0);
  344.  
  345.     PtoCstr (vname);
  346.     
  347.     strcpy (buffer, vname);
  348.     switch (w->state) {
  349.         case 0:
  350.             strcat (buffer, ":  Directories");
  351.             break;
  352.         case 1:
  353.             strcat (buffer, ":  Applications");
  354.             break;
  355.         case 2:
  356.             strcat (buffer, ":  All files");
  357.             break;
  358.     }
  359.     CtoPstr (buffer);
  360.     SetWTitle (w, buffer);
  361.     strcpy (w->vName, vname);
  362.     sprintf (buffer, "Volume \"%s\"", vname);
  363.     TAdd (w, 0, buffer);
  364.     sprintf (buffer, "Total files on volume:  %ld", myHParam.volumeParam.ioVFilCnt);
  365.     TAdd (w, 1, buffer);
  366.     sprintf (buffer, "Total directories:      %ld", myHParam.volumeParam.ioVDirCnt);
  367.     TAdd (w, 1, buffer);
  368.     if (myHParam.volumeParam.ioVSigWord == 0xD2D7) {
  369.         sprintf (buffer, "This is an MFS volume");
  370.         TAdd (w, 1, buffer);
  371.     } else {
  372.         sprintf (buffer, "This is an HFS volume");
  373.         TAdd (w, 1, buffer);
  374.     }
  375.     buffer [0] = '\0';
  376.     TAdd (w, 0, buffer);
  377.     
  378. // Scan the total directory, as in a Mac Technical Note #68.
  379.      
  380.     sprintf (buffer, "%s:", vname);
  381.     TAdd (w, 0, buffer);
  382.     myCPB.fileParam.ioNamePtr = (unsigned char *)fName;
  383.     myCPB.fileParam.ioVRefNum = name;
  384.     myWindow = w;
  385.     curLine = curPLine;
  386.     SetPort (w);
  387.     totFiles = 0;
  388.     totDirect = 0;
  389.     lenMem = EnumerateCatalog (1, 2L);
  390.     AddDirSize (curLine, lenMem);
  391.     
  392. // Prepare for updating window.
  393.     
  394.     EraseRect (& (w->w.port.portRect));
  395.     InvalRect (& (w->w.port.portRect));
  396.     InitCursor ();
  397. }