TweetFollow Us on Twitter

Standard Search Unit
Volume Number:7
Issue Number:10
Column Tag:Pascal Forum

Standard Search Unit

By Ed Eichman, Mak Jukie, B & E Software, Germany

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

[Ed & Mak work as programmers for B & E Software, makers of the program RagTime 3. Ed is a former musician who was able to find another job that would let him sleep till noon every day, and Mak will not admit to having done anything prior to writing this article]

The Concept

While working on the latest release of RagTime 3, we realized that we needed a better way to organize the extra files that ship with the program. RagTime is an integrated page layout/spreadsheet/text processing/graphing/picture program which supports text import and direct scanning into the program via external code resources. The combination of scanner drivers, text filters, one or more dictionaries, PPD files, etc. would make for a large mess inside the users system folder, so we decided to use a standard folder into which all auxiliary files could be kept, and shared with other programs if desired.

This led to the development of a standard search unit (SSU) for our files. The SSU looks for a folder (for the remainder of this article, we will refer to this folder as the ‘Company Folder’) in which all the files which are needed by the program are kept. This folder will first be searched for in the folder from which the application was launched, and then in the current system folder. The first folder found with the specified name will then become the target folder. The SSU will not only access the files within the ‘Company Folder’, but also recursively check all folders within the ‘Company Folder’ hierarchy. This will allow the user to organize the target folder in any way that is convenient.

This folder could have a number of uses. If you are writing a number of applications that all need to use a dictionary, for example, you could place one dictionary into the ‘Company Folder’ in the system folder, and have all the applications access this folder. You could also define a interface for external code resources, and just pop them into the ‘Company Folder’ to access them while the program is running. Finally, the SSU can keep track of multiple resources in one or more files. This could be handy if, for instance, you have a game which uses a number of large sound resources; these resources could be put in one file and only used by the program if the user chooses to put them on his or her hard disk in the appropriate folder.

Set Up

In order to use the SSU, you must first call InitSSU to set up three global variables. The first, gCompanyFStruct, is initialized to NIL and will eventually contain the hierarchy within the ‘Company Folder’. Next, gAppWDRefNum and gSysWDRefNum will be initialized to contain the Working Directory Reference Number of the folder containing the current application and the current System Folder, respectively.

The List

The SSU stores information about specified files or resources contained in the company folder in a linked list. This list contains records of type ItemInfoRec which contains the following information:

il_nextItemInfo is of type ItemInfoHdl and contains a handle to the next record in the linked list. When this field is NIL, it indicates that you are at the end of the list.

il_privData is provided to the caller of the SSU to store any extra data. The caller may use this field in any way; usually to point to a block of memory that holds information about the item in the list. The best time to enter data in this field is during a call to your CallIdentifyProc (see below).

Please read the description of the killPrivData Procedure (below) to get a better understanding of when you can expect the il_privData field to change.

il_resourceType contains the resource type if this item is a resource, and will be NIL otherwise.

il_resID contains the resource number if this item is a resource, and will be NIL otherwise.

il_fileType will contain the file type of the file in the list, or the file type of the file which contains the specified resource.

il_fileSpec contains the vRefnum, the dirID, and the file name of either the file in the list or the file which contains the specified resource.

il_nickName may contain an alternate name for the item. For instance, if you request a list that contains all the sound resources from a particular file, this field will either contain the resource name, or the type and number of the specified resource if the resource has no name (i.e. ‘snd resource number 42’).

The SSU is capable of creating multiple lists from the ‘Company Folder’. For instance, in RagTime 3 we keep a list of all scanner drivers and a separate list of all text import filters which are available. Both list are created from one folder.

How a List is built

The list is created on the first call to SSU_BuildList:

{1}

PROCEDURE SSU_BuildList(VAR biiList BuildItemInfoAPT; numTypes:Integer; 
VAR result:SSUInfoRec; killPrivData:ProcPtr);

This procedure should be called each time you want to use the list. The first time SSU_BuildList is called, it puts the tick count of the last modification date of the ‘Company Folder’ into a field from gCompanyFStruct (this global is used by all list which the SSU builds). Thereafter, each time you call SSU_BuildList and pass in the list created on the first call to the procedure, it determines if the list must be rebuilt (based on comparing the previous modification tick count to the current one) and rebuilds the list if necessary.

biiList is an array of type BuildItemInfoAPT. Each member of the array contains criteria to decide if a particular file/resource should be added to the current list. Each search criteria must contain at least a file type. Files /resources for the list can then be chosen by creator, resource type, file name, or any combination of the three.

Additionally, any files that match the above search criteria can be passed to a caller supplied procedure (passed in the bi_procAddr as a ProcPtr) which can either accept or reject the file/resource, and/or which can be used to setup the il_privData field. The procedure should have the following setup:

{2}

PROCEDURE CallIdentifyProc(VAR theName: Str255; theResH:Handle; theID:Integer; 
VAR privData:LongInt);

theName will either be the file name or the il_nickName field from the FileInfoRec if the item in question is a resource. This argument is guaranteed not to be empty upon entry to this procedure. If the CallIdentifyProc wants to reject the item, an empty string should be returned here. The name may also be changed if a different name is desired.

If the current item is a resource, theResH will contain a handle to the resource, and theID will contain the resID. If the current item is a file, theResH will be NIL and theID will contain the file reference number of the file.

privData is a LongInt which will be 0 on entry, and may be used in any way by the application. This value will be put in the il_privData field of the ItemInfoRecord.

The next argument to the SSU_BuildList procedure is numTypes, which must contain the number of BuildItemInfoRT records that are being passed in the array.

The result argument must be cleared before calling this routine for the first time. Thereafter, result must contain the SSUInfoRec returned on the previous call to SSU_BuildList for the list in question. The argument result can be cleared with a call to BlockClear from the SSU like this:

{3}

BlockClear(@result, SizeOf(result));

BlockClear can be useful in a number of other situations (such as clearing a param block before giving it to the low level file manager calls), and the code for the call is listed below:

{4}

PROCEDURE BlockClear(thePtr:UNIV Ptr; count:Integer);
BEGIN
 WHILE count > 0 DO BEGIN
 count := count - 1;
 thePtr^ := 0;
 thePtr := Ptr(LongInt(thePtr)+1);
 END;
END;

Finally, in case the list should need to be rebuilt, your call to SSU_BuildList should include a ProcPtr to a procedure that can dispose of any private data that you have set up. The setup for your procedure must be:

{5}

PROCEDURE KillPrivData(VAR pData:LongInt);

Please notice that your private data could be destroyed any time that you call SSU_BuildList. If the SSU determines the list must be rebuilt (based on the tick count, described above), it totally destroys the old list before building the new list.

If you have not allocated any privData, this argument can be set to NIL.

Using items from a List

The list may contain both files and resources. However, the SSU only opens or closes files from the list, or files which contain the resource in the item list. These files may be opened with a call to the function SSU_OpenFile:

{6}

FUNCTION SSU_OpenFile(theItemInfoHdl: ItemInfoHdl; VAR fileRefNum:Integer; 
permission:Byte; openResFork: Boolean):OSErr;

theItemInfoHdl must contain a handle to the item that you want to open (if the item is a resource, all needed info to open the file is found in its ItemInfoHdl; you don’t have to create a separate handle for a call to SSU_OpenFile). The argument fileRefNum will be zeroed by the SSU before being used by the function, and will return the file reference number of the opened file. The permission argument specifies the access privileges that you are requesting for the file to be opened. The constants for this argument may be found in Inside Mac, Volume 2, page 100.

The openResFork boolean should be set to TRUE if you want the resource fork rather than the data fork of the file to be opened. Setting this boolean to TRUE is required if you are trying to access an item which is a resource, but could also be set to TRUE if you want to access resources from a file in the item list. Finally, the SSU_OpenFile function returns an OSErr indicating the success or failure of the function.

Each call to SSU_OpenFile should be offset by a call to SSU_CloseFile after you are done using the file/resource.

{7}

FUNCTION SSU_CloseFile(VAR fileRefNum: Integer; closeResFork:Boolean):OSErr;

This routine closes the file opened by SSU_OpenFile. The argument fileRefNum should be set to the file reference number returned in the call to SSU_OpenFile, and closeResFork should have the same value that was originally used for SSU_OpenFile.

fileRefNum is declared a VAR variable so that the file reference number of the file can be set to zero when the file is closed. SSU_CloseFile first checks to make sure fileRefNum is not zero before trying to close the file, so that multiple calls to SSU_CloseFile for the same file will not cause errors.

Disposing of a List

After you are done using a list, the memory allocated to the maintenance of the list must be disposed of. This is accomplished with a call to SSU_DisposList:

{8}

PROCEDURE SSU_DisposList(VAR theResult: SSUInfoRec; killPrivData:ProcPtr);

theResult is passed as a VAR parameter so that it may be set to NIL after the list is disposed of. This way, if theResult is passed again to SSU_BuildList, the SSU will build a new list instead of dying painfully.

The KillPrivData ProcPtr is explained above in the description of SSU_BuildList.

It should be noted here that a list should only be disposed of when you are completely done using it, or when you can not afford the memory that the list takes up, since rebuilding the list can be time consuming. Depending on the amount of files in the ‘Company Folder’ and the complexity of your CallIdentifyProc, the list could take a couple of seconds to build on each call to SSU_BuildList.

Depending on your application, it might be worth setting up a global SSUInfoRec for each of the lists that you will be using. This way, when you call SSU_BuildList, the list will only be rebuilt if the ‘Company Folder’ modification tick count has changed.

Possible additions

One of the first things you will notice when displaying a list from the SSU is that the items are not sorted. If you plan to use the list for display, a sorting procedure is a must.

Additionally, the SSU will be more than happy to add an identical name to the list if one is found. If this occurs with your list, you may want to check which of the two identical files/resources is the one you really need, and eliminate the other. This would usually be done by checking the version resource of a file to see which of the files is newer.

Another good idea might be to add the searching, not just of the ‘Company Folder’, but also of the System Folder and the folder from which the application was launched. Most of the work for this addition is already done for you in the routine SearchDirectory. Just be sure to set the recursive argument to FALSE, or you will end up searching the ‘Company Folder’ a second time.

Finally, you may want to have mercy on your mortal users by being a little flexible with the naming of the ‘Company Folder’. If you find something that is fairly close (i.e. ‘Dompany Folder’), you could put up a message asking if that is the correct folder, and warning them that you will rename the folder to the correct name.

UNIT SSU;

INTERFACE

USES
 MemTypes, OSIntf, ToolIntf, Packages;

TYPE
 NickNameT= Str63; {Maximum length of an 
 alternate name}
 
 CanonFileSpec = RECORD 
 cf_vRefNum : Integer;
 cf_dirID : LongInt;
 cf_fileName: Str63;
 END;

 ItemInfoHdl= ^ItemInfoPtr;
 ItemInfoPtr= ^ItemInfoRec;
 ItemInfoRec= RECORD
 il_nextItemInfo:ItemInfoHdl; {If NIL,last entry} 
 il_privData: LongInt;  {User definable}
 il_resourceType : OSType;{Resource OS Type  }
 il_resID : Integer; {Resource ID  }
 il_fileType: OSType;{File type    }
 il_fileSpec: CanonFileSpec;{dirId +
 vRefNum + fileName}
 il_nickName: NickNameT;  {Alternate name    }
 END;
 
 SSUInfoPtr = ^SSUInfoRec;
 SSUInfoRec = RECORD
 ssu_folderTime  : LongInt; 
 {Copy of DirInfoArr.di_time}
 ssu_firstItemInfo : ItemInfoHdl;  
 {Handle to the first item in the list }
 END;
 
 BuildItemInfoRT = RECORD
 bi_fileType: OSType;{File type. Required          }
 bi_crtrType: OSType;{Creator. Can be NIL          }
 bi_resType : OSType;{Res type. Can be NIL   }
 bi_fileName: StringPtr;{File name. Can be NIL     }
 bi_procAddr: ProcPtr;  {ID routine. Can be NIL    }
 END;
 
 BuildItemInfoAPT= ^BuildItemInfoAT;
 BuildItemInfoAT = ARRAY [1..4] OF BuildItemInfoRT;
 

PROCEDURE InitSSU;

PROCEDURE SSU_BuildList(  biiList:BuildItemInfoAPT; 
 numTypes:Integer; 
 VAR result:SSUInfoRec; killPrivData:ProcPtr);

PROCEDURE SSU_DisposList(VAR result:SSUInfoRec; 
 killPrivData:ProcPtr);

FUNCTIONSSU_OpenFile(theItemInfoHdl:ItemInfoHdl;
 VAR fileRefNum:Integer;
 permission:Byte;
 openResFork:Boolean):OSErr;

FUNCTIONSSU_CloseFile(  VAR fileRefNum:Integer;
 closeResFork:Boolean):OSErr;

PROCEDURE BlockClear(thePtr:UNIV Ptr;
 count:Integer);

VARgAppWDRefNum  : Integer; 
 {App working directory  refnum
 }
 gSysWDRefNum  : Integer; 
 {System Folder working directory refnum     }


IMPLEMENTATION
{$S SegSSU} {••••••••••••••••••••••••••••••••••••••••••••••••••}

TYPE
 DirectoryInfoRec = RECORD
 di_dirModTime : LongInt; {Mod date of dir                     }
 di_dirID : LongInt; {Directory ID }
 di_vRefNum : Integer;  {Volume ref number         }
 END;
 
 DirInfoArrHdl = ^DirInfoArrPtr;
 DirInfoArrPtr = ^DirInfoArr;
 DirInfoArr = RECORD
 di_time: LongInt; 
 {Time in ticks when this structure was built}
 di_count : Integer; {Number of entries in list    }
 di_list: ARRAY [1..512] OF DirectoryInfoRec;            
 END;

CONST
 kDirInfoArrHeadSize = 
 SizeOf(LongInt) + SizeOf(Integer);
 kCompanyFName   = ‘Company Folder’;
 eternity = False;
 
VARgCompanyFStruct : DirInfoArrHdl;

PROCEDURE CallIdentifyProc(VAR theName:Str255;
 theCodeH:Handle;
 theResID:Integer;
 VAR thePrivData:LongInt;
 theProcAddr:ProcPtr);
 INLINE $205F,   { MOVE.L (A7)+,A0 }
 $4E90; { JSR    (A0)}

PROCEDURE CallKillPrivData(VAR privData:LongInt;
 theProcAddr:ProcPtr);
 INLINE $205F,   { MOVE.L (A7)+,A0 }
 $4E90; { JSR    (A0)}

{Clears a block of memory of a specified size}
PROCEDURE BlockClear(thePtr:UNIV Ptr;  count:Integer);
BEGIN
 WHILE count > 0 DO BEGIN
 count  := count - 1;
 thePtr^  := 0;
 thePtr := Ptr(LongInt(thePtr)+1);
 END;
END;

{CleanDisposHandle works like DisposHandle but has two advantages. 1) 
If a handle is NIL, DisposHandle is not called and 2) If h is part of 
a dereferenced relocatable block, CleanDisposHandle first makes a copy 
of the handle before calling DisposHandle, avoiding a possible HEAP error 
 }
PROCEDURE CleanDisposHandle(VAR h:UNIV Handle);
VARtemp : Handle;
BEGIN
 IF h <> NIL THEN BEGIN
 temp := h; 
 h := NIL;
 DisposHandle(temp);
 END;
END;

{Identical to FSClose, except that it will not try to close if dataForkRefNum 
is equal to 0 on entry; also, dataForkRefNum is set to 0 on exit
 }
FUNCTION CleanFSClose(VAR dataForkRefNum:Integer) 
 :OSErr;
VARtempRefNum  : Integer;
BEGIN
 IF dataForkRefNum <> 0 THEN BEGIN
 tempRefNum := dataForkRefNum;
 dataForkRefNum  := 0;
 CleanFSClose  := FSClose(tempRefNum);
 END ELSE 
 CleanFSClose  := noErr;
END;
 
{Identical to CloseResFile, except that it will not try to close if resForkRefNum 
is equal to 0 on entry; also, resForkRefNum is set to 0 on exit
 }
FUNCTION CleanCloseResFile(VAR resForkRefNum:Integer):OSErr;
VARtempRefNum  : Integer;
BEGIN
 IF resForkRefNum <> 0 THEN BEGIN
 tempRefNum := resForkRefNum;
 resForkRefNum   := 0;
 CloseResFile(tempRefNum);
 CleanCloseResFile := ResError;
 END ELSE 
 CleanCloseResFile := noErr;
END;

{Identical to ReleaseResource, except that it will not try to release 
if resHandle is equal to 0 on entry; also, resHandle is set to NIL on 
exit    }
FUNCTION CleanReleaseResource(VAR resHandle:UNIV   
 Handle):OSErr;
VARtempH: Handle;
BEGIN
 IF resHandle <> NIL THEN BEGIN
 tempH  := resHandle;
 resHandle  := NIL;
 ReleaseResource(tempH);
 CleanReleaseResource   := ResError;
 END ELSE 
 CleanReleaseResource   := noErr;
END;

{CleanOpenWD opens a working directory so that files contained therein 
can be accessed or searched.
‘vRefNum’ is the working directory reference                   
 number of either the Application folder                       
 or the System Folder. Additionally, when                      
 recursively searching the ‘Company  Folder’, this can also be the wdRefNum 
of an enclosed folder.
‘dirID’ working directory reference number of the              
 folder in vRefNum
‘mustCloseWD’returns TRUE if we opened the working             
 directory and FALSE if it was already opened. If ‘mustCloseWD’ is FALSE, 
we should not call ‘CleanCloseWD’ (see comments in CleanCloseWD). 
This function returns an operating system error          }
FUNCTION CleanOpenWD(vRefNum:Integer;
 dirID:LongInt;
 VAR wdRefNum:Integer;
 VAR mustCloseWD:Boolean):OSErr;
VARwdInfo : WDPBRec;
 tempErr: OSErr;
BEGIN
 BlockClear(@wdInfo, SizeOf(wdInfo));
 
{According to the Macintosh Tech notes #77 and #190, only those working 
directories that were created by the app should be closed by the app. 
This routine checks if working directory for the specified volume/dirID 
has been already created by somebody. (perhaps by the Finder or any other 
application running under MultiFinder), and returns FALSE in mustCloseWD 
if we didn’t open it.}
 REPEAT
 wdInfo.ioVRefNum:= vRefNum;
 wdInfo.ioWdIndex:= wdInfo.ioWdIndex + 1;
 wdInfo.ioWDProcID := 0;
 wdInfo.ioWDVRefNum:= 0;
 tempErr := PBGetWDInfo(@wdInfo, False);
 UNTIL (tempErr <> noErr) | (wdInfo.ioWDDirID =                
 dirID);
 
 mustCloseWD     := tempErr <> noErr;
 
 IF mustCloseWD THEN BEGIN
 tempErr := OpenWD(vRefNum, dirID, LongInt(‘ERIK’), wdRefNum);
 IF tempErr <> noErr THEN 
 wdRefNum := 0;
 CleanOpenWD   := tempErr;
 END ELSE BEGIN
 CleanOpenWD   := noErr;
 wdRefNum := wdInfo.ioVRefNum;
 END;
END;

{CleanCloseWD closes a working directory that we opened.
‘wdRefNum’ is the reference number of the working              
 directory that should be closed
This function returns an operating system error.
WARNING:This function must never be called if the              
 variable mustCloseWD returned FALSE in                        
 call to CleanOpenWD }
FUNCTION CleanCloseWD(VAR wdRefNum:Integer):OSErr;
BEGIN
 IF wdRefNum <> 0 THEN BEGIN
 CleanCloseWD  := CloseWD(wdRefNum);
 wdRefNum := 0;
 END ELSE 
 CleanCloseWD  := noErr;
END;

{SSU_OpenFile opens file specified in ItemInfoHdl.
‘theItemInfoHdl’ is a handle to information about a            
 particular file
‘fileRefNum’returned file reference of the                     
 file ‘permission’ specifies access  privileges for the file   
 (Constants may be found in Inside Mac Volume 2 page 100)
‘openResFork’    if TRUE, the res fork should  be              
 opened instead of the data fork
This function returns an operating system error          }
FUNCTION SSU_OpenFile(theItemInfoHdl:ItemInfoHdl;
 VAR fileRefNum:Integer;
 permission:Byte;
 openResFork:Boolean):OSErr;
VARpbs  : HParamBlockRec;
 wdRefNum : Integer;
 tempErr: Integer;
 mustCloseWD: Boolean;
BEGIN
 fileRefNum := 0;
 IF theItemInfoHdl <> NIL THEN BEGIN
 IF openResFork THEN BEGIN
 IF Length
 (theItemInfoHdl^^.il_fileSpec.cf_fileName)  = 0   
 THEN BEGIN 
{This resource belongs to program resource fork}
 tempErr := 0;
 END ELSE BEGIN
 HLock(Handle(theItemInfoHdl));
 WITH theItemInfoHdl^^ DO BEGIN
 IF il_fileSpec.cf_dirID <> 0 THEN BEGIN                       
 tempErr := CleanOpenWD   (il_fileSpec.cf_vRefNum,             
 il_fileSpec.cf_dirID, wdRefNum,   mustCloseWD);
 
 IF tempErr = noErr THEN BEGIN
 fileRefNum := OpenRFPerm (StringPtr(@il_fileSpec.cf_fileName)^, 
 wdRefNum, permission);
 tempErr := ResError;
 
 IF tempErr <> noErr THEN BEGIN
 fileRefNum := 0;
 IF mustCloseWD & (CleanCloseWD    (wdRefNum) <> noErr) THEN ;
 END ELSE BEGIN
 IF mustCloseWD THEN
 tempErr :=  CleanCloseWD(wdRefNum);
 END;
 END;
 END ELSE BEGIN
 fileRefNum := OpenRFPerm (StringPtr(@il_fileSpec.cf_fileName)^, 
 il_fileSpec.cf_vRefNum, permission);
 tempErr := ResError;
 IF tempErr <> noErr THEN 
 fileRefNum := 0;
 END;
 END;
 HUnLock(Handle(theItemInfoHdl));
 END;
 END ELSE BEGIN
 IF Length  (theItemInfoHdl^^.il_fileSpec.cf_fileName) > 0     
 THEN BEGIN
 HLock(Handle(theItemInfoHdl));
 BlockClear(@pbs, SizeOf(pbs));
 WITH theItemInfoHdl^^ DO BEGIN
 pbs.ioNamePtr := @il_fileSpec.cf_fileName;
 pbs.ioVRefNum := il_fileSpec.cf_vRefNum;
 pbs.ioPermssn := permission;
 pbs.ioDirID:= il_fileSpec.cf_dirID; 
 END;
 tempErr := PBHOpen(@pbs, False);
 HUnLock(Handle(theItemInfoHdl));
 IF tempErr = noErr THEN 
 fileRefNum := pbs.ioRefNum;
 END ELSE tempErr := memFullErr;
 END;
 END ELSE tempErr := memFullErr;
 
 SSU_OpenFile := tempErr;
END;

{SSU_CloseFile closes file specified by fileRefNum
‘fileRefNum’file ref num of file to be closed
‘closeResFork’ if TRUE, then this routine closes the           
 res fork instead of the data fork
This function returns an operating system error          }
FUNCTION SSU_CloseFile( VAR fileRefNum:Integer;
 closeResFork:Boolean):OSErr;
BEGIN
 IF closeResFork THEN BEGIN
 SSU_CloseFile := CleanCloseResFile(fileRefNum);
 END ELSE BEGIN
 SSU_CloseFile := CleanFSClose(fileRefNum);
 END;
END;


FUNCTION GetDirModTime(vRefNum:Integer;                        
 directoryId:LongInt):LongInt;
VARmyCInfoPBRec  : CInfoPBRec;
BEGIN
 BlockClear(@myCInfoPBRec, SizeOf(myCInfoPBRec));
 myCInfoPBRec.ioVRefNum   := vRefNum;
 myCInfoPBRec.ioFDirIndex := -1;
 myCInfoPBRec.ioDrDirID   := directoryId;
 
 IF PBGetCatInfo(@myCInfoPBRec, False) = noErr THEN      BEGIN
 GetDirModTime := myCInfoPBRec.ioDrMdDat;
 END ELSE BEGIN 
 {could not get modTime for directory?  huh  }
 GetDirModTime := Random;
 END;
END;

PROCEDURE StuffDirInfo(vRefNum:Integer;                        
 dirID:LongInt);
VARcurrentInfo : DirectoryInfoRec;
 theTicks : LongInt;
BEGIN
 currentInfo.di_dirModTime:= GetDirModTime(vRefNum,            
 dirID);
 currentInfo.di_dirId:= dirID;
 currentInfo.di_vRefNum   := vRefNum;
 
 IF gCompanyFStruct = NIL THEN BEGIN
 theTicks := TickCount;
 gCompanyFStruct :=  DirInfoArrHdl(NewHandleClear
 (kDirInfoArrHeadSize));
 gCompanyFStruct^^.di_time := theTicks;
 END;

 SetHandleSize(Handle(gCompanyFStruct),                        
 ((gCompanyFStruct^^.di_count + 1)*  LongInt(SizeOf(DirectoryInfoRec))) 
 + kDirInfoArrHeadSize);
 WITH gCompanyFStruct^^ DO BEGIN
 di_count := di_count + 1;
 di_list[di_count] := currentInfo;
 END;
END;

{SearchForDirectory searches for “Company Folder” in the Working Directory 
specified by wdRefNum 
‘wdRefNum’is the working directory reference                   
 number of either the Application folder                       
 or the System Folder.
This function returns either the “Company Folder” directory ID, or zero 
if none is found }
FUNCTION SearchForDirectory(wdRefNum:Integer)                  
 :LongInt;
VARi    : Integer;
 dirIndex : Integer;
 len1   : Integer;
 tempName : Str255;
 searchName : Str255;
 cInfo  : CInfoPBRec;
BEGIN
 BlockClear(@cInfo, SizeOf(cInfo));
 
 dirIndex := 1;
 
 searchName := kCompanyFName;
 len1   := Length(kCompanyFName);
 
 REPEAT
 cInfo.ioNamePtr := @tempName;
 cInfo.ioDirID   := 0;
 cInfo.ioVRefNum := wdRefNum;
 cInfo.ioFDirIndex := dirIndex;
 
 IF PBGetCatInfo(@cInfo, False) = noErr THEN                   
 BEGIN
 IF BAnd(Integer(cInfo.ioFlAttrib), $010) <> 0                 
 THEN BEGIN { We’ve found a directory }
 IF (Length(tempName)=len1) THEN
 IF EqualString(tempName,searchName, False,True) THEN BEGIN
 SearchForDirectory := cInfo.ioDirID;
 LEAVE;
 END;   
 END;
 dirIndex := dirIndex + 1;
 END ELSE BEGIN 
{got error, so it looks like we did not find it}
 SearchForDirectory := 0;
 LEAVE;
 END;
 UNTIL eternity;
END;

FUNCTIONMin(a,b:Integer):Integer;
BEGIN
 IF a < b THEN 
 Min := a 
 ELSE 
 Min := b;
END;

PROCEDURE SSU_BuildList(  biiList:BuildItemInfoAPT;            
 numTypes:Integer;
 VAR result:SSUInfoRec;
 killPrivData:ProcPtr);

VAR   i : Integer;
 dummyOSErr : OSErr;
 buildFolderInfo : Boolean;
 cInfo  : CInfoPBRec;
 currFileName    : Str255;
 theResCount: Integer;
 resHdl : Handle;
 theName: NickNameT;
 theResID : Integer;
 theResType : ResType;
 theResName : STR255;
 thePrivData: LongInt;
 noTypeFlag : Boolean;
 
 PROCEDURE  SearchDirectory(theVRefNum:Integer;                
 theDirID:LongInt; 
 recursive:Boolean);
 VAR  dirIndex   : Integer;
 jj: Integer;
 refNum : Integer;
 wdRefNum : Integer;
 theResIndex: Integer;
 onlyFiles: Boolean;
 validFile: Boolean;
 mustCloseWD: Boolean;
 pbs    : HParamBlockRec;
 BEGIN  
 IF (theDirID=0) | (CleanOpenWD(theVRefNum,                    
 theDirID, wdRefNum,mustCloseWD) = noErr) THEN                 BEGIN
 IF theDirID = 0 THEN 
 wdRefNum := theVRefNum;
 
 IF buildFolderInfo THEN  StuffDirInfo(theVRefNum, theDirID);
 
 dirIndex := 1;
 REPEAT
 cInfo.ioNamePtr := @currFileName;
 cInfo.ioDirID   := theDirID;
 cInfo.ioVRefNum := wdRefNum;
 cInfo.ioFDirIndex := dirIndex;
 
 IF PBGetCatInfo(@cInfo, False) = noErr THEN                   
 BEGIN
 IF BAnd(Integer(cInfo.ioFlAttrib), $010) <>                   
 0 THEN BEGIN {We’ve found a directory}
 IF recursive THEN SearchDirectory (theVRefNum, cInfo.ioDirID, True);
 END ELSE BEGIN
 FOR jj := 1 TO numTypes DO
 IF LongInt(biiList^[jj].bi_fileType) =                        
 LongInt(cInfo.ioFlFndrInfo.fdType)  THEN LEAVE;
 
 IF jj <= numTypes THEN BEGIN
 WITH biiList^[jj] DO BEGIN
 onlyFiles := LongInt(bi_resType) = 0;
 
 validFile := 
 (LongInt(bi_crtrType) = 0) | (LongInt(bi_crtrType) =          
 LongInt(cInfo.ioFlFndrInfo.fdCreator));
 
 IF validFile THEN
 IF bi_fileName <> NIL THEN
 validFile := 
 (Length(bi_fileName^) =  Length(cInfo.ioNamePtr^)) &
 EqualString(bi_fileName^,  cInfo.ioNamePtr^, False, True);
 END;
 
 refNum := 0;
 IF validFile THEN BEGIN
 IF onlyFiles THEN BEGIN
 IF biiList^[jj].bi_procAddr <> NIL  THEN BEGIN
 BlockClear(@pbs, SizeOf(pbs));
 pbs.ioNamePtr := cInfo.ioNamePtr;
 pbs.ioVRefNum := wdRefNum;
 pbs.ioPermssn := fsRdPerm;
 pbs.ioDirID:= theDirID; 
 validFile:= PBHOpen(@pbs,  False) = noErr;
 
 IF validFile THEN 
 refNum :=pbs.ioRefNum;
 END;
 END ELSE BEGIN
 SetResLoad(False);
 refNum := OpenRFPerm     (cInfo.ioNamePtr^, wdRefNum, fsRdPerm);
 SetResLoad(True);
 validFile := ResError = noErr;
 END;
 
 IF validFile THEN BEGIN
 noTypeFlag := LongInt(biiList^[jj].bi_resType)=0;
 
 IF noTypeFlag
 THEN theResCount := 1
 ELSE theResCount := Count1Resources 
 (biiList^[jj].bi_resType);
 
 FOR theResIndex := 1 TO theResCount
 DO BEGIN
 
 IF noTypeFlag
 THEN resHdl := NIL
 ELSE resHdl := Get1IndResource
 (biiList^[jj].bi_resType,theResIndex);

 IF noTypeFlag | ((resHdl <> NIL) & 
 (ResError = noErr)) THEN BEGIN
 IF noTypeFlag THEN BEGIN
 theResID := 0;
 theResType:= ResType(NIL);
 theResName:=  cInfo.ioNamePtr^;
 END ELSE BEGIN
 HLock(resHdl);
 GetResInfo (resHdl, theResID,     theResType, theResName);    
 IF Length(theResName) = 0 THEN    BEGIN
 NumToString(theResID,    theResName);
 theResName := Concat(‘xxxx resource number ‘,                 
 theResName);
 BlockMove(@theResType,   @theResName[1],4);
 END;
 END;
 
 thePrivData:= 0;
 
{ now let’s call identify routine to see if it’s available }
 IF biiList^[jj].bi_procAddr <>    NIL THEN BEGIN
 IF noTypeFlag 
 THEN CallIdentifyProc    (theResName,NIL, refNum,             
 thePrivData,    biiList^[jj].bi_procAddr)
 ELSE CallIdentifyProc    (theResName, resHdl,                 
 theResID, thePrivData,
 biiList^[jj].bi_procAddr);
 END;
 
 IF NOT noTypeFlag THEN BEGIN
 HUnLock(resHdl);
 IF CleanReleaseResource (resHdl)  <> noErr THEN theResName := ‘’;
 END;
 
{This would be a good place to check for duplicate names in the list 
and decide what to do with them}
 
 IFLength(theResName)>0 THEN  BEGIN
 { so far this file is valid }
 resHdl := NewHandleClear (SizeOf(ItemInfoRec));
 WITH ItemInfoHdl(resHdl)^^ DO     BEGIN
 theName := Copy (theResName, 1,Min(SizeOf(il_nickName)-1,     
 Length(theResName)));

 il_nickName:= theName;
 END;
 
 WITH ItemInfoHdl (resHdl)^^ DO    BEGIN
 il_privData:= thePrivData;
 il_fileSpec.cf_fileName := cInfo.ioNamePtr^;
 il_fileSpec.cf_vRefNum :=  theVRefNum;
 il_fileSpec.cf_dirID:=   theDirID;
 il_fileType:=   cInfo.ioFlFndrInfo.fdType;
 il_nextItemInfo :=  result.ssu_firstItemInfo;
 il_resourceType :=  biiList^[jj].bi_resType;
 il_resID := theResID;    
 END;
 result.ssu_firstItemInfo :=  ItemInfoHdl (resHdl);
 END;
 END; { no resErr }
 END; { all files in this res file }

 IF onlyFiles
 THEN dummyOSErr := CleanFSClose   (refNum)
 ELSE dummyOSErr :=CleanCloseResFile (refNum);
 END ELSE refNum := 0;
 END;
 END;
 END;
 
 dirIndex := dirIndex + 1;
 END ELSE BEGIN
 theName := ‘’;
 LEAVE;
 END;
 UNTIL eternity;
 
 IF(theDirID<>0) & mustCloseWD &   (CleanCloseWD(wdRefNum) <> noErr) 
THEN BEGIN
 { process the error }
 END;
 END;
 END;
 
 VAR  folderDirID: LongInt;
 folderVRefNum : Integer;
 shouldBuild: Boolean;
BEGIN
{Let’s check if the item list needs to be updated}
 shouldBuild := gCompanyFStruct = NIL;
 
 IF NOT shouldBuild THEN BEGIN
 HLock(Handle(gCompanyFStruct));
 WITH gCompanyFStruct^^ DO
 FOR i:= 1 TO di_count DO WITH di_list[i] DO
 IF GetDirModTime(di_vRefNum,di_dirId) <>                      
 di_dirModTime THEN BEGIN
 shouldBuild := NOT shouldBuild;
 LEAVE;
 END;
 HUnLock(Handle(gCompanyFStruct));
 
 IF shouldBuild THEN CleanDisposHandle (gCompanyFStruct);
 END;
 
 buildFolderInfo := gCompanyFStruct = NIL;
 
 IF shouldBuild | (result.ssu_folderTime <>                    
 gCompanyFStruct^^.di_time) THEN BEGIN
 
 SSU_DisposList(result, killPrivData);
 
{First let’s look for the “Company Folder” inside the Application Folder}
 folderVRefNum := gAppWDRefNum;
 
 folderDirID :=  SearchForDirectory  (folderVRefNum);
 IF folderDirID = 0 THEN BEGIN 
{ or else let’s look for the “Company Folder” inside the System Folder};
 folderVRefNum := gSysWDRefNum;
 folderDirID:= SearchForDirectory  (folderVRefNum);
 END;
 
{If the “Company Folder” is found in either the Application or System 
Folder, then let’s look for the items }

 IF folderDirID <> 0 THEN
 SearchDirectory(folderVRefNum, folderDirID,                   
 True);
 
 result.ssu_folderTime := gCompanyFStruct^^.di_time;     END;
END;

PROCEDURE DisposeItemInfoHdl(VAR   theItemInfoHdl:ItemInfoHdl; 
 killPrivData:ProcPtr);
BEGIN
 IF theItemInfoHdl <> NIL THEN BEGIN
 HLock(Handle(theItemInfoHdl));
 DisposeItemInfoHdl( theItemInfoHdl^^.il_nextItemInfo,killPrivData);
 IF killPrivData <> NIL THEN  CallKillPrivData(theItemInfoHdl^^.il_privData, 
 killPrivData);
 HUnLock(Handle(theItemInfoHdl));
 CleanDisposHandle(theItemInfoHdl);
 END;
END;

PROCEDURE SSU_DisposList(VAR result:SSUInfoRec; 
 killPrivData:ProcPtr);
BEGIN
 result.ssu_folderTime := 0;
 DisposeItemInfoHdl(result.ssu_firstItemInfo,            killPrivData);
END;

{$S Init}
PROCEDURE InitSSU;
VARtheWorld : SysEnvRec;
BEGIN
 gCompanyFStruct := NIL;  
 IF GetVol(nil, gAppWDRefNum) <> noErr THEN 
 {Should never happen, but you may want to be defensive };
 IF SysEnvirons (2, theWorld) = noErr
 THEN gSysWDRefNum := theWorld.sysVRefNum
 ELSE gSysWDRefNum := gAppWDRefNum;
END;

END.

 

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.