home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-04-30 | 7.9 KB | 200 lines | [TEXT/MMCC] |
- /* This small sample shows how to use the PBCatSearch function. */
- /* It presents a dialog where you can enter any Creator and File Type, then it */
- /* searches for one (1) file with that creator and type */
- /* When it finds it, it will show you the file name. */
- /* C.K. Haun */
- /* Apple Developer Tech Support */
- /* Comments or request, ALink C.K.HAUN */
- /* Oct 16, 1991, Tokyo */
-
- //#include <Dialogs.h>
- //#include <Controls.h>
- //#include <QuickDraw.h>
- //#include <Windows.h>
- //#include <ToolUtils.h>
- //#include <OSUtils.h>
- //#include <Menus.h>
- //#include <Fonts.h>
- //#include <resources.h>
- #include <memory.H>
- #include <files.h>
- #include "Launchit.h"
-
- /* constants for this application */
- /* two dialog boxes */
- //#define kSearch 128
- //#define kFoundIt 129
- /* one alert box */
- //#define kAsk 130
-
- #define kOneKilobyte 1024
-
- /* constants for key codes I will be using for the dialog filter */
- //enum {
- // kEnterKey = 0x03, kBackSpace = 8, kReturnKey = 0x0D, kTabKey, kEscKey = 0x1B, kLeftArrow, kRightArrow, kUpArrow, kDownArrow,
- // kDeleteKey = 0x7F
- //};
- /* prototypes for this file */
-
-
- #ifdef powerc
- QDGlobals qd;
- #endif
-
-
-
- /* DoCatSearch asks the user for the Creator and File type of the file to be found. */
- /* Then, it initializes the parameter blocks for the PBCatSearch call, and makes */
- /* the call. When it finds a file with the correct creator and file type, it */
- /* shows the user */
- OSErr DoCatSearch( unsigned long creator, unsigned long filetype, FSSpec *myFS )
- {
- short bufferSize = kOneKilobyte * 16; /* I will be using this to make */
- /* the optimization buffer */
- OSErr myError; /* general error variable */
-
- /* here I am initializing the cat search parameter block (csBlockPtr) using */
- /* NewPointerClear so I know that all the fields in the parameter block */
- /* will be clear before I start. This helps prevent errors */
- CSParamPtr csBlockPtr = (CSParamPtr)NewPtrClear(sizeof(CSParam));
-
- long dirIDUnused; /* for GetVol use */
-
- Str32 nulString = "\p"; /* an empty string to pass to PBCatSearch */
-
- /* initialize the parameter block */
- /* first I see if we got the memory we need */
- if (csBlockPtr) {
- /* yes, we have the memory to work. Build the parameter block */
-
- /* First get the memory for the two other parameter blocks we need */
- /* these blocks are used to give PBCatSearch to exact information */
- /* it needs to match a file */
- csBlockPtr->ioSearchInfo1 = (CInfoPBPtr)NewPtrClear(sizeof(CInfoPBRec));
- csBlockPtr->ioSearchInfo2 = (CInfoPBPtr)NewPtrClear(sizeof(CInfoPBRec));
- /* check to be sure we got the memory we need */
- if (csBlockPtr->ioSearchInfo1 && csBlockPtr->ioSearchInfo2) {
- /* we have memory */
- /* Now allocate a buffer to hold the result of the PBCatSearch call */
- /* this is an array of ioMatchPtr, which is defined as an FSSpecPtr */
- /* so we will get the size of an FSSpec record */
- /* I am only asking for one file, so I will only ask for that much memory */
- csBlockPtr->ioMatchPtr = myFS; // rgac (FSSpecPtr)NewPtrClear(sizeof(FSSpec) * 1); /* only looking for 1 */
- if (csBlockPtr->ioMatchPtr) {
- /* got the memory for that */
- /* Now see if we can create an optimization buffer */
- /* I will start by asking for a 16K buffer */
- /* If I cannot get that, I will keep reducing my request */
- /* until I get to zero. A one K buffer is better than no */
- /* buffer at all */
- while(bufferSize){
- csBlockPtr->ioOptBuffer = NewPtr(bufferSize);
- if(csBlockPtr->ioOptBuffer){
- break;
- } else {
- bufferSize -= kOneKilobyte;
- }
- }
- if (csBlockPtr->ioOptBuffer)
- csBlockPtr->ioOptBufSize = bufferSize;
- else
- csBlockPtr->ioOptBufSize = 0; /* no buffer, sorry */
- /* I want to find one file */
- csBlockPtr->ioReqMatchCount = 1;
- /* No timeout. PBCatSearch will keep running until it finds */
- /* a file or searches the whole volume */
- csBlockPtr->ioSearchTime = 0; /* no timeout */
- /* Change this parameter in a real application. In a real application, */
- /* you would want to come back periodically (every 2 seconds or so) */
- /* to do various things like; */
- /* 1) Spin a 'wait' cursor */
- /* 2) Update a thermometer bar */
- /* 3) Call WaitNextEvent, so the user can switch out */
- /* of your application while you are searching */
- /* Watch the Finder's "Find" command work so you can see how this */
- /* is done */
- }
- }
- }
-
- /* This sample only works on the default directory, so I am getting the */
- /* volume reference number for that volume here */
- HGetVol(nil, &csBlockPtr->ioVRefNum, &dirIDUnused); /* get default volume for search */
- /* make sure these two pointer are clear */
- csBlockPtr->ioSearchInfo1->hFileInfo.ioNamePtr = nil;
- csBlockPtr->ioSearchInfo2->hFileInfo.ioNamePtr = nil;
-
- /* Set-up the creator/filetype passed in to us */
- csBlockPtr->ioSearchInfo1->hFileInfo.ioFlFndrInfo.fdCreator = creator; //'CNOW';
- csBlockPtr->ioSearchInfo1->hFileInfo.ioFlFndrInfo.fdType = filetype; // 'APPL';
- /* This is the paramter that specifies HOW to search */
- csBlockPtr->ioSearchBits = fsSBFlFndrInfo;
- /* in this case, I am saying to search by Finder information. */
- /* there are MANY values allowed here, and you can also use */
- /* many combinations, as*/
- /* fsSBPartialName search for partial names
- fsSBFullName search for full names
- fsSBFlAttrib "" directory flags
- fsSBFlFndrInfo Finder info
- fsSBFlLgLen data fork logical size
- fsSBFlPyLen dat fork physical size
- fsSBFlRLgLen resource fork logical size
- fsSBFlRPyLen resource fork physical size
- fsSBFlCrDat creation date
- fsSBFlMdDat modification date
- fsSBFlBkDat backup date
- fsSBFlXFndrInfo more finder info
- fsSBFlParID file parent ID (directory ID)
- fsSBDrUsrWds directory information
- fsSBDrNmFls number of files in a folder
- fsSBDrCrDat folder creation date
- fsSBDrMdDat "" mod date
- fsSBDrBkDat "" backup date
- fsSBDrFndrInfo finder folder information
- */
- /* and you can combine many of these, to create unique searches */
-
- /* Set up the second search parameter block */
- /* For the search I am doing (creator/filetype) these need to be set to -1 */
- csBlockPtr->ioSearchInfo2->hFileInfo.ioFlFndrInfo.fdCreator = 0xFFFFFFFF;
- csBlockPtr->ioSearchInfo2->hFileInfo.ioFlFndrInfo.fdType = 0xFFFFFFFF;
- /* For other searches (like by modification date) this would indicate the end of the */
- /* search range. This allows you to, for example, search for all the files */
- /* modified between October 1 1991 and October 12 1991 */
- /* Many more combinations are possible, see Inside Mac VI */
-
- /* Change the cursor to a watch */
- SetCursor(*(GetCursor(4)));
-
- /* Search. I am searching syncronously in this case */
- myError = PBCatSearch(csBlockPtr, false);
-
-
- /* change the cursor back to an arrow */
- InitCursor();
-
- /* See if there were any errors. Also, see if a file was found */
- /* ioActMatchCount will show how many files were really found if */
- /* we had asked for more than one */
- if (myError == noErr && csBlockPtr->ioActMatchCount != 0) {
- /* found a file with no errors. Put the filename in a ParamText */
- /* string show we can show the user */
- }
-
- /* no matter what happened, kill the memory we had allocated */
- if (csBlockPtr) {
- if (csBlockPtr->ioSearchInfo1)
- DisposePtr((Ptr)csBlockPtr->ioSearchInfo1);
- if (csBlockPtr->ioSearchInfo2)
- DisposePtr((Ptr)csBlockPtr->ioSearchInfo2);
- if (csBlockPtr->ioMatchPtr)
- DisposePtr((Ptr)csBlockPtr->ioMatchPtr);
- if (csBlockPtr->ioOptBuffer)
- DisposePtr((Ptr)csBlockPtr->ioOptBuffer);
- DisposePtr((Ptr)csBlockPtr);
- }
-
- return myError;
- /* catsearch section end */
- }
-