home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / OpenDoc / CreditNow! / CreditNow! Source / $Utilities / PBCatSearch.c < prev    next >
Encoding:
Text File  |  1995-04-30  |  7.9 KB  |  200 lines  |  [TEXT/MMCC]

  1. /* This small sample shows how to use the PBCatSearch function.  */
  2. /* It presents a dialog where you can enter any Creator and File Type, then it */
  3. /* searches for one (1) file with that creator and type */
  4. /* When it finds it, it will show you the file name. */
  5. /* C.K. Haun */
  6. /* Apple Developer Tech Support */
  7. /* Comments or request, ALink C.K.HAUN */
  8. /* Oct 16, 1991, Tokyo */
  9.  
  10. //#include <Dialogs.h>
  11. //#include <Controls.h>
  12. //#include <QuickDraw.h>
  13. //#include <Windows.h>
  14. //#include <ToolUtils.h>
  15. //#include <OSUtils.h>
  16. //#include <Menus.h>
  17. //#include <Fonts.h>
  18. //#include <resources.h>
  19. #include <memory.H>
  20. #include <files.h>
  21. #include "Launchit.h"
  22.  
  23. /* constants for this application */
  24. /* two dialog boxes */
  25. //#define kSearch 128
  26. //#define kFoundIt 129
  27. /* one alert box */
  28. //#define kAsk 130
  29.  
  30. #define kOneKilobyte 1024
  31.  
  32. /* constants for key codes I will be using for the dialog filter */
  33. //enum  {
  34. //    kEnterKey = 0x03, kBackSpace = 8, kReturnKey = 0x0D, kTabKey, kEscKey = 0x1B, kLeftArrow, kRightArrow, kUpArrow, kDownArrow,
  35. //        kDeleteKey = 0x7F
  36. //};
  37. /* prototypes for this file */
  38.  
  39.  
  40. #ifdef powerc
  41.    QDGlobals    qd;
  42. #endif
  43.  
  44.  
  45.  
  46. /* DoCatSearch asks the user for the Creator and File type of the file to be found. */
  47. /* Then, it initializes the parameter blocks for the PBCatSearch call, and makes */
  48. /* the call.  When it finds a file with the correct creator and file type, it */
  49. /* shows the user */
  50. OSErr DoCatSearch( unsigned long creator, unsigned long filetype, FSSpec *myFS )
  51. {
  52.     short bufferSize = kOneKilobyte * 16; /* I will be using this to make  */
  53.                                             /* the optimization buffer */
  54.     OSErr myError;    /* general error variable */
  55.  
  56.     /* here I am initializing the cat search parameter block (csBlockPtr) using */
  57.     /* NewPointerClear so I know that all the fields in the parameter block */
  58.     /* will be clear before I start.  This helps prevent errors */
  59.     CSParamPtr csBlockPtr = (CSParamPtr)NewPtrClear(sizeof(CSParam));
  60.     
  61.     long dirIDUnused;    /* for GetVol use */
  62.  
  63.     Str32 nulString = "\p"; /* an empty string to pass to PBCatSearch */
  64.             
  65.     /* initialize the parameter block */
  66.     /* first I see if we got the memory we need */
  67.     if (csBlockPtr) {
  68.         /* yes, we have the memory to work.  Build the parameter block */
  69.         
  70.         /* First get the memory for the two other parameter blocks we need */
  71.         /* these blocks are used to give PBCatSearch to exact information */
  72.         /* it needs to match a file */
  73.         csBlockPtr->ioSearchInfo1 = (CInfoPBPtr)NewPtrClear(sizeof(CInfoPBRec));
  74.         csBlockPtr->ioSearchInfo2 = (CInfoPBPtr)NewPtrClear(sizeof(CInfoPBRec));
  75.         /*  check to be sure we got the memory we need */
  76.         if (csBlockPtr->ioSearchInfo1 && csBlockPtr->ioSearchInfo2) {
  77.             /* we have memory */
  78.             /* Now allocate a buffer to hold the result of the PBCatSearch call */
  79.             /* this is an array of ioMatchPtr, which is defined as an FSSpecPtr */
  80.             /* so we will get the size of an FSSpec record  */
  81.             /* I am only asking for one file, so I will only ask for that much memory */
  82.             csBlockPtr->ioMatchPtr = myFS;        // rgac (FSSpecPtr)NewPtrClear(sizeof(FSSpec) * 1);        /* only looking for 1 */
  83.             if (csBlockPtr->ioMatchPtr) {
  84.             /* got the memory for that */
  85.                 /* Now see if we can create an optimization buffer */
  86.                 /* I will start by asking for a 16K buffer */
  87.                 /* If I cannot get that, I will keep reducing my request  */
  88.                 /* until I get to zero.  A one  K buffer is better than no  */
  89.                 /* buffer at all */
  90.                 while(bufferSize){                
  91.                 csBlockPtr->ioOptBuffer = NewPtr(bufferSize);
  92.                 if(csBlockPtr->ioOptBuffer){
  93.                 break;
  94.                 } else {
  95.                 bufferSize -= kOneKilobyte;
  96.                 }
  97.                 }
  98.                 if (csBlockPtr->ioOptBuffer)
  99.                     csBlockPtr->ioOptBufSize = bufferSize;
  100.                 else
  101.                     csBlockPtr->ioOptBufSize = 0;           /* no buffer, sorry */
  102.                 /* I want to find one file */
  103.                 csBlockPtr->ioReqMatchCount = 1;
  104.                 /* No timeout.  PBCatSearch will keep running until it finds */
  105.                 /* a file or searches the whole volume */
  106.                 csBlockPtr->ioSearchTime = 0;               /* no timeout */
  107.                 /* Change this parameter in a real application.  In a real application, */
  108.                 /* you would want to come back periodically (every 2 seconds or so) */
  109.                 /* to do various things like; */
  110.                 /* 1) Spin a 'wait' cursor */
  111.                 /* 2) Update a thermometer bar */
  112.                 /* 3) Call WaitNextEvent, so the user can switch out */
  113.                 /* of your application while you are searching */
  114.                 /* Watch the Finder's "Find" command work so you can see how this */
  115.                 /* is done */
  116.             }
  117.         }
  118.     }
  119.     
  120.     /* This sample only works on the default directory, so I am getting the  */
  121.     /* volume reference number for that volume here */
  122.     HGetVol(nil, &csBlockPtr->ioVRefNum, &dirIDUnused);     /* get default volume for search */
  123.     /* make sure these two pointer are clear */
  124.     csBlockPtr->ioSearchInfo1->hFileInfo.ioNamePtr = nil;
  125.     csBlockPtr->ioSearchInfo2->hFileInfo.ioNamePtr = nil;
  126.     
  127.     /* Set-up the creator/filetype passed in to us */
  128.     csBlockPtr->ioSearchInfo1->hFileInfo.ioFlFndrInfo.fdCreator = creator;    //'CNOW';
  129.     csBlockPtr->ioSearchInfo1->hFileInfo.ioFlFndrInfo.fdType = filetype;    // 'APPL';
  130.     /* This is the paramter that specifies HOW to search */
  131.     csBlockPtr->ioSearchBits = fsSBFlFndrInfo;
  132.     /* in this case, I am saying to search by Finder information. */
  133.     /* there are MANY values allowed here, and you can also use */
  134.     /* many combinations, as*/
  135.     /*     fsSBPartialName    search for partial names
  136.     fsSBFullName    search for full names
  137.     fsSBFlAttrib    ""    directory flags
  138.     fsSBFlFndrInfo    Finder info
  139.     fsSBFlLgLen        data fork logical size
  140.     fsSBFlPyLen        dat fork physical size
  141.     fsSBFlRLgLen    resource fork logical size
  142.     fsSBFlRPyLen    resource fork physical size
  143.     fsSBFlCrDat        creation date
  144.     fsSBFlMdDat        modification date
  145.     fsSBFlBkDat        backup date
  146.     fsSBFlXFndrInfo    more finder info
  147.     fsSBFlParID        file parent ID (directory ID)
  148.     fsSBDrUsrWds    directory information
  149.     fsSBDrNmFls        number of files in a folder
  150.     fsSBDrCrDat        folder creation date
  151.     fsSBDrMdDat        ""       mod date
  152.     fsSBDrBkDat        ""        backup date
  153.     fsSBDrFndrInfo    finder folder information
  154.  */
  155.     /* and you can combine many of these, to create unique searches */
  156.     
  157.     /* Set up the second search parameter block */
  158.     /* For the search I am doing (creator/filetype) these need to be set to -1 */
  159.     csBlockPtr->ioSearchInfo2->hFileInfo.ioFlFndrInfo.fdCreator = 0xFFFFFFFF;
  160.     csBlockPtr->ioSearchInfo2->hFileInfo.ioFlFndrInfo.fdType = 0xFFFFFFFF;
  161.     /* For other searches (like by modification date) this would indicate the end of the */
  162.     /* search range.  This allows you to, for example, search for all the files  */
  163.     /* modified between October 1 1991 and October 12 1991 */    
  164.     /* Many more combinations are possible, see Inside Mac VI */
  165.        
  166.      /* Change the cursor to a watch */
  167.      SetCursor(*(GetCursor(4)));
  168.     
  169.      /* Search.  I am searching syncronously in this case */
  170.      myError = PBCatSearch(csBlockPtr, false);
  171.  
  172.         
  173.      /* change the cursor back to an arrow */
  174.      InitCursor();
  175.  
  176.     /* See if there were any errors.  Also, see if a file was found */
  177.     /* ioActMatchCount will show how many files were really found if  */
  178.     /* we had asked for more than one */
  179.     if (myError == noErr && csBlockPtr->ioActMatchCount != 0) {
  180.         /* found a file with no errors.  Put the filename in a ParamText */
  181.         /* string show we can show the user */
  182.     }
  183.     
  184.     /* no matter what happened, kill the memory we had allocated */
  185.     if (csBlockPtr) {
  186.         if (csBlockPtr->ioSearchInfo1)
  187.             DisposePtr((Ptr)csBlockPtr->ioSearchInfo1);
  188.         if (csBlockPtr->ioSearchInfo2)
  189.             DisposePtr((Ptr)csBlockPtr->ioSearchInfo2);
  190.         if (csBlockPtr->ioMatchPtr)
  191.             DisposePtr((Ptr)csBlockPtr->ioMatchPtr);
  192.         if (csBlockPtr->ioOptBuffer)
  193.             DisposePtr((Ptr)csBlockPtr->ioOptBuffer);
  194.         DisposePtr((Ptr)csBlockPtr);
  195.     }
  196.     
  197.     return myError;
  198.     /* catsearch section end */   
  199. }
  200.