home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / System / Directory source / Directory.c next >
Encoding:
C/C++ Source or Header  |  1988-10-28  |  7.6 KB  |  438 lines  |  [TEXT/KAHL]

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