TweetFollow Us on Twitter

Vol Search XFCN
Volume Number:7
Issue Number:6
Column Tag:HyperChat

Related Info: File Manager

HFS Volume Search XFCN

By Mark Armstrong, Pharos Technologies

Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.

The SearchVol XFCN

The SearchVol XFCN searches the specified volume and returns the full path name of the file. This is extremely useful if you want to launch a document from within Hypercard but you are not sure where the file is located on the volume. SearchVol will return the full pathname of the file which you can then pass directly to the Hypertalk open command.

SearchVol uses a recursive algorithm to walk through the hierarchical file structure looking for a match. If it finds a match, it constructs the full path name by walking back up the tree. Once it has the full path name, it returns it to Hypercard. If no match is found, the XFCN will return empty. See the Other Issues section of this article for further discussion of the recursive nature of the algorithm.

SearchVol searches in the specified volume, or, if no volume is specified, in the system volume. Since PBGetVInfo prefers to have the volume specified in the form “volname:” (and since I am in the unfortunate habit of just passing “volume” without the trailing colon,) we simply check to see if there is a trailing colon on the specified volume name. If there is not, we add one.

Once we have the volume reference numbers we can begin the search. We start by determining the number of files and directories in the root directory by calling PBGetCatInfo and then we make the initial call to SearchFile, which is the real guts of the XFCN.

The Search Engine

The SearchFile function is very similar to Clifford Story’s walktree function in the October ’88 MacTutor (Programmer’s Workshop; HFS Transfer DA) Cliff wrote his in Pascal - this one is in C. But the structure of the routines is quite similar. For the gospel on searching HFS volumes, refer to Apple’s Technical Note 68.

You will notice that SearchFile consists of two for loops. The first for loop is looking for files. We look for file by accepting only the cases in which bit 4 of ioFlAttrib is not set. The second for loop is looking strictly for directories. If it is a file, then we check to see if there is a match. If it is a directory, then we look inside that directory with a recursive call to search file.

So, why the two for loops? Well, I wanted the routine to be equivalently fast for any file in a given directory. With just a single for loop, a file in the route directory named “AAAA” would most likely be found very quickly. Where as, a file named “ZZZZ” would take much longer, assuming, of course, that the volume in question was an average user’s 40 meg hard disk. Consequently, if we look through all the files first for a given directory before diving down to the next level in the hierarchy, we should be able to get a more consistent search time for files at the same hierarchical level. Once the recursive searching is complete, we check to see if a file was found. If a file was not found, fErr will contain fnfErr. On the other hand, if a file was found then we need to walk back up the hierarchy to construct the full path name that Hypercard requires for its file commands such as open, read, and print. We build the path name in the returnValue handle so it is available when we return to Hypercard.

Error Handling

In the SearchVol XFCN listed below I have included only a skeletal version of the required error handling. I have done this so as not to cloud the concepts on which I am trying to focus. However, I do have a few words about error handling that I would like to share.

There is an interesting catch-22 situation that arises for the author of external functions. The situation is this. If Hypercard is expecting you to return a value from an external function, then Hypercard needs a way to determine if the returned value is an error string or the expected result. In many XFCN’s available today, it is difficult (if not impossible) to determine in the general case if the string returned from the external is an error or not. As an example, suppose there is an XFCN that returns a file name. Furthermore, lets say that in case of error, the XFCN returns the error number. Now lets say that the XFCn is called from Hypertalk and the returned result is “-43”. Is this an error code for file not found or is it just a file that happens to be named “-43”. To avoid these problematic circumstances, the XFCN designer must make it very easy for the Hypertalk programmer to ascertain:

1) If an error occurred

2) What was the nature of the error

Another common approach is to return empty if an error occurred. Unfortunately, empty does not tell the Hypertalk programmer (to say nothing of the user) what went wrong. Consequently, it is difficult to take appropriate action.

Some programmers have circumnavigated the problem by forcing the declaration of a global variable. If an error occurs, then the global variable is set to notify Hypertalk that things did not go as planned. This method works has its advantages, but it can be an extra hassle if the Hypertalk programmer does not know he is required to declare the global. Whatever you decide to do, make your error checking rigorous and complete. Everyone will benefit.

Other Issues

SearchVol, as it is, is a foundation on which one can build. For example, one could easily search all mounted volumes by creating an outside loop that walked through the volume queue. For example:

/* 1 */

QHdrPtr QQ;
VCB     *Cur;

QQ = GetVCBQHdr();
Cur = (VCB *)(QQ->qHead);
   
do {
   /* Use Cur->vcbVN as the current volName */
   /* Put search volume code here */
   Cur = (VCB *)(Cur)->qLink;
   } while (Cur != 0L);

Another possibility is to extend the XFCN so that it returns all occurrences of the specified file - rather than just the first occurrence. In such a case, you would not return after finding a file but would simply store the full pathname of the file and then continue the search down the hierarchy. In this way, you could duplicate in Hypercard the functionality of the Find File desk accessory.

Other ideas include filtering out files by type or modification date, cataloging subdirectories on a volume, and the list goes on.

Finally, it is important to discuss the advantages and limitations of using a recursive algorithm for hierarchical searching. The advantages are that the code is small and simple. The primary limitation is that the stack grows with each recursive call. On a large hard disk or CD-ROM this could be a problem. I have used the recursive algorithm as a demonstration of recursive methods in an ideal world. Reality dictates that machines have a finite amount of memory. It is more prudent to employ methods which are not recursive if there is any possibility of exceeding available stack space.

/*------------------------------------------------
 SearchVol XFCN
 © 1989 MacTutor
 by Mark Armstrong    Pharos Technologies, Inc
 written in Think’s LightspeedC 3.0
------------------------------------------------*/

#include “HyperXCmd.h”
#include “FileMgr.h”
#include “HFS.h”
#include “ResourceMgr.h”
#include “SetUpA4.h”

#define False    0
#define True!False
#define Nil 0L

/*--------------------------------------
XFCN main function
--------------------------------------*/
pascal main(paramPtr)
   XCmdBlockPtr  paramPtr;
   {
   Str255 fName,str,fullPath,vName;
   HParamBlockRecMyHPB;
   CInfoPBRec    MyCIPB;
   OSErrfErr;
   shorttheVol;
   Handle nameH;
   long theDir,foundDir;
   
   RememberA0();
   SetUpA4();
   
   if ((paramPtr->paramCount < 1) || 
 (paramPtr->paramCount > 2)) 
   {
   SysBeep(10);
   /* return error string */
   goto Done;
   }
   
 ZeroToPas(paramPtr,*((unsigned char **)           paramPtr->params[0]), 
fName);
   if (paramPtr->paramCount == 2)
   {
   ZeroToPas(paramPtr,*((unsigned char **)               paramPtr->params[1]),vName);
   if (vName[vName[0]] != ‘:’)
   {
   vName[0]++;
   vName[vName[0]] = ‘:’;
   }
 MyHPB.volumeParam.ioCompletion = Nil;
 MyHPB.volumeParam.ioNamePtr = vName;
 MyHPB.volumeParam.ioVRefNum = 0;
 MyHPB.volumeParam.ioVolIndex = -1;
 fErr = PBHGetVInfo(&MyHPB,False);
 if (fErr)
 {
        SysBeep(10);
        /* return error string */
        goto Done;
 }
 theVol = MyHPB.volumeParam.ioVRefNum;
 }
   else theVol = GetSysVol();
   
   MyCIPB.dirInfo.ioCompletion = 0L;
   MyCIPB.dirInfo.ioNamePtr = 0L;
   MyCIPB.dirInfo.ioVRefNum = theVol;
   MyCIPB.dirInfo.ioFDirIndex = 0;
   MyCIPB.dirInfo.ioDrDirID = 2L;
   fErr = PBGetCatInfo(&MyCIPB,False);
   
   if (fErr)
 {
   SysBeep(10);
   /* return error string */
   goto Done;
 }
 else
   {
   fErr = 
 SearchFile(2L,
 MyCIPB.dirInfo.ioDrNmFls,
 theVol,
 &fName,
 &foundDir);
   if (fErr)
 {
   SysBeep(10);
   /* return error string */
   goto Done;
 }
 
   fullPath[0] = 0;
   PstrCopy(fullPath,fName);
   
   MyCIPB.dirInfo.ioCompletion = Nil;
   MyCIPB.dirInfo.ioNamePtr = str;
   MyCIPB.dirInfo.ioVRefNum = theVol;
   MyCIPB.dirInfo.ioFDirIndex = -1;
   MyCIPB.dirInfo.ioDrDirID = foundDir;
   fErr = PBGetCatInfo(&MyCIPB,False);
   PrependStr(MyCIPB.dirInfo.ioNamePtr,fullPath);
   
   do {
   MyCIPB.dirInfo.ioDrDirID =
 MyCIPB.dirInfo.ioDrParID;
   fErr = PBGetCatInfo(&MyCIPB,False);
   if (fErr == noErr)
 PrependStr(MyCIPB.dirInfo.ioNamePtr,fullPath);
   } while (fErr == noErr);
   
   paramPtr->returnValue =  PasToZero(paramPtr,(StringPtr)fullPath);
   }

Done:
   RestoreA4();
   }
  
/*--------------------------------------------
SearchFile is the recursive hierarchical search engine.  It looks at 
all the files and then all the folders in the directory specified by 
theVol and theDir for the file specified by fName
--------------------------------------------*/
SearchFile(theDir,count,theVol,fName,foundDir)
 long   theDir;
   shortcount,theVol;
   Str255 *fName;
   long *foundDir;
   {
   shortI;
   OSErrfErr;
   Str255 str;
   CInfoPBPtr    MyCIPB;
   
   MyCIPB = (CInfoPBPtr)NewPtr(sizeof(CInfoPBRec));
   for (I=1;I<=count;I++)
   {
   str[0] = 0;
   MyCIPB->dirInfo.ioCompletion = Nil;
   MyCIPB->dirInfo.ioNamePtr = str;
   MyCIPB->dirInfo.ioVRefNum = theVol;
   MyCIPB->dirInfo.ioFDirIndex = I;
   MyCIPB->dirInfo.ioDrDirID = theDir;
   fErr = PBGetCatInfo(MyCIPB,False);
   if (fErr) 
   {
   SysBeep(10);
   return (fErr);
   }
   else
   {
   if (!(MyCIPB->dirInfo.ioFlAttrib &  0x10))
   {
   if (EqualString(fName,
 MyCIPB->dirInfo.ioNamePtr,
 False,True))
   {
   *foundDir = 
 MyCIPB->hFileInfo.ioFlParID;
   return (0);
   }
   }
   }
   }
   
   for (I=1;I<=count;I++)
   {
   str[0] = 0;
   MyCIPB->dirInfo.ioCompletion = Nil;
   MyCIPB->dirInfo.ioNamePtr = str;
   MyCIPB->dirInfo.ioVRefNum = theVol;
   MyCIPB->dirInfo.ioFDirIndex = I;
   MyCIPB->dirInfo.ioDrDirID = theDir;
   fErr = PBGetCatInfo(MyCIPB,False);
   if (fErr) 
   {
   SysBeep(10);
   return (fErr);
   }
   else
   {
   if (MyCIPB->dirInfo.ioFlAttrib & 0x10)
   {
   fErr = 
 SearchFile(
 MyCIPB->dirInfo.ioDrDirID,
   MyCIPB->dirInfo.ioDrNmFls,
   theVol,
   fName,
   foundDir);
   if (!fErr) return (0);
   }
   }
   }
   
   DisposPtr(MyCIPB);
   return (fnfErr);
   }

/*--------------------------------------------
PrependStr puts string s1 and a colon before string s2.
--------------------------------------------*/
PrependStr(s1,s2)
 char   *s1,*s2;
 {
 Str255 temp;
 PstrCopy(temp,s2);
 s1[0]++;
 s1[s1[0]] = ‘:’;
 PstrCopy(s2,s1);
 BlockMove(&(temp[1]),&(s2[s2[0]+1]),
 (long)temp[0]);
 s2[0] += temp[0];
 }

/*--------------------------------------------
PstrCopy copies string s2 into string s1
--------------------------------------------*/
PstrCopy(s1,s2)
 char   *s1,*s2;
 {
 short  len;
 for (len=*s2;len>=0;--len) *s1++ = *s2++;
 }

/*--------------------------------------------
GetSysVol returns the vRefNum of the startup system volume.
--------------------------------------------*/
GetSysVol()
   {
   shortvRefNum;
   OSErrFErr;
   FErr = GetVRefNum(SysMap,&vRefNum);
   return vRefNum;
   }

[Mark Armstrong is presently the Vice President of Technical Operations for Pharos Technologies, Inc., a system integration and software development firm. He is the author of UNITize™, and has contributed to several other projects such as Milo™ and Marble Madness™.]

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Tokkun Studio unveils alpha trailer for...
We are back on the MMORPG news train, and this time it comes from the sort of international developers Tokkun Studio. They are based in France and Japan, so it counts. Anyway, semantics aside, they have released an alpha trailer for the upcoming... | Read more »
Win a host of exclusive in-game Honor of...
To celebrate its latest Jujutsu Kaisen crossover event, Honor of Kings is offering a bounty of login and achievement rewards kicking off the holiday season early. [Read more] | Read more »
Miraibo GO comes out swinging hard as it...
Having just launched what feels like yesterday, Dreamcube Studio is wasting no time adding events to their open-world survival Miraibo GO. Abyssal Souls arrives relatively in time for the spooky season and brings with it horrifying new partners to... | Read more »
Ditch the heavy binders and high price t...
As fun as the real-world equivalent and the very old Game Boy version are, the Pokemon Trading Card games have historically been received poorly on mobile. It is a very strange and confusing trend, but one that The Pokemon Company is determined to... | Read more »
Peace amongst mobile gamers is now shatt...
Some of the crazy folk tales from gaming have undoubtedly come from the EVE universe. Stories of spying, betrayal, and epic battles have entered history, and now the franchise expands as CCP Games launches EVE Galaxy Conquest, a free-to-play 4x... | Read more »
Lord of Nazarick, the turn-based RPG bas...
Crunchyroll and A PLUS JAPAN have just confirmed that Lord of Nazarick, their turn-based RPG based on the popular OVERLORD anime, is now available for iOS and Android. Starting today at 2PM CET, fans can download the game from Google Play and the... | Read more »
Digital Extremes' recent Devstream...
If you are anything like me you are impatiently waiting for Warframe: 1999 whilst simultaneously cursing the fact Excalibur Prime is permanently Vault locked. To keep us fed during our wait, Digital Extremes hosted a Double Devstream to dish out a... | Read more »
The Frozen Canvas adds a splash of colou...
It is time to grab your gloves and layer up, as Torchlight: Infinite is diving into the frozen tundra in its sixth season. The Frozen Canvas is a colourful new update that brings a stylish flair to the Netherrealm and puts creativity in the... | Read more »
Back When AOL WAS the Internet – The Tou...
In Episode 606 of The TouchArcade Show we kick things off talking about my plans for this weekend, which has resulted in this week’s show being a bit shorter than normal. We also go over some more updates on our Patreon situation, which has been... | Read more »
Creative Assembly's latest mobile p...
The Total War series has been slowly trickling onto mobile, which is a fantastic thing because most, if not all, of them are incredibly great fun. Creative Assembly's latest to get the Feral Interactive treatment into portable form is Total War:... | Read more »

Price Scanner via MacPrices.net

Early Black Friday Deal: Apple’s newly upgrad...
Amazon has Apple 13″ MacBook Airs with M2 CPUs and 16GB of RAM on early Black Friday sale for $200 off MSRP, only $799. Their prices are the lowest currently available for these newly upgraded 13″ M2... Read more
13-inch 8GB M2 MacBook Airs for $749, $250 of...
Best Buy has Apple 13″ MacBook Airs with M2 CPUs and 8GB of RAM in stock and on sale on their online store for $250 off MSRP. Prices start at $749. Their prices are the lowest currently available for... Read more
Amazon is offering an early Black Friday $100...
Amazon is offering early Black Friday discounts on Apple’s new 2024 WiFi iPad minis ranging up to $100 off MSRP, each with free shipping. These are the lowest prices available for new minis anywhere... Read more
Price Drop! Clearance 14-inch M3 MacBook Pros...
Best Buy is offering a $500 discount on clearance 14″ M3 MacBook Pros on their online store this week with prices available starting at only $1099. Prices valid for online orders only, in-store... Read more
Apple AirPods Pro with USB-C on early Black F...
A couple of Apple retailers are offering $70 (28%) discounts on Apple’s AirPods Pro with USB-C (and hearing aid capabilities) this weekend. These are early AirPods Black Friday discounts if you’re... Read more
Price drop! 13-inch M3 MacBook Airs now avail...
With yesterday’s across-the-board MacBook Air upgrade to 16GB of RAM standard, Apple has dropped prices on clearance 13″ 8GB M3 MacBook Airs, Certified Refurbished, to a new low starting at only $829... Read more
Price drop! Apple 15-inch M3 MacBook Airs now...
With yesterday’s release of 15-inch M3 MacBook Airs with 16GB of RAM standard, Apple has dropped prices on clearance Certified Refurbished 15″ 8GB M3 MacBook Airs to a new low starting at only $999.... Read more
Apple has clearance 15-inch M2 MacBook Airs a...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs now available starting at $929 and ranging up to $410 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at... Read more
Apple drops prices on 13-inch M2 MacBook Airs...
Apple has dropped prices on 13″ M2 MacBook Airs to a new low of only $749 in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, now available for $679 for 8-Core CPU/7-Core GPU/256GB models. Apple’s one-year warranty is included, shipping is free, and each... Read more

Jobs Board

Seasonal Cashier - *Apple* Blossom Mall - J...
Seasonal Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Seasonal Fine Jewelry Commission Associate -...
…Fine Jewelry Commission Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) Read more
Seasonal Operations Associate - *Apple* Blo...
Seasonal Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Read more
Hair Stylist - *Apple* Blossom Mall - JCPen...
Hair Stylist - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.