short openappres (ControlHandle c) { /* TODO: check error conditions */ WDPBRec wdpb; short refnum; short resloadval; wdpb.ioVRefNum = (*(item **) (*c)->contrlData)->vrefnum; wdpb.ioWDDirID = (*(item **) (*c)->contrlData)->ioparid; wdpb.ioWDProcID = 0; wdpb.ioNamePtr = 0; PBOpenWD (&wdpb, false); #ifdef THINK_C resloadval = ResLoad; #else resloadval = LMGetResLoad (); #endif SetResLoad (false); refnum = OpenRFPerm ((*c)->contrlTitle, wdpb.ioVRefNum, fsRdPerm); SetResLoad (resloadval); PBCloseWD (&wdpb, false); if (refnum == -1) { /* todo: alert */ } return refnum; }
// Pass resFile as -1 to use the current resource file // true = exists Boolean IndResourceExists(short resFile,ResType resType,short inIndex) { short oldRes=::CurResFile(); Handle theRes=0L; if (resFile!=-1) { UseResFile(resFile); if (ResError()) return false; } SetResLoad(false); theRes=Get1IndResource(resType,inIndex); SetResLoad(true); UseResFile(oldRes); if (theRes) { ReleaseResource(theRes); return true; } else return false; }
long BetterSizeResource(short resFile,ResType resType,short resID) { Handle theHandle; long theSize=0L; short saveResLoad=LMGetResLoad(); SetResLoad(false); theHandle=BetterGetResource(resFile,resType,resID); if (theHandle) { theSize=SizeResource(theHandle); ReleaseResource(theHandle); } if (saveResLoad) SetResLoad(true); return theSize; }
short BetterGetResAttrs(short resFile,ResType theType,short resID) { short saveResLoad=LMGetResLoad(); Handle theHandle=0L; short theAttrs=0; SetResLoad(false); theHandle=BetterGetResource(resFile,theType,resID); if (theHandle) { theAttrs=GetResAttrs(theHandle); ReleaseResource(theHandle); } if (saveResLoad) SetResLoad(true); return theAttrs; }
// Parse all resources in a set bool XML_ResourceFork::ParseResourceSet(ResType Type) { short NumResources = Count1Resources(Type); if (NumResources <= 0) { return false; } // This sorting is necessary so that the resources will be read in ascending order of ID's short *IDList = new short[NumResources]; // Get the resource ID's SetResLoad(false); for (int ir=0; ir<NumResources; ir++) { // Zero-based to one-based indexing ResourceHandle = Get1IndResource(Type,ir+1); ResType _Type; Str255 Name; GetResInfo(ResourceHandle,&IDList[ir],&_Type,Name); ReleaseResource(ResourceHandle); } SetResLoad(true); // Using a better sorter; STL will succeed where Rhys Hill had failed :-) sort(IDList,IDList+NumResources); // Now get the resources for (int ir=0; ir<NumResources; ir++) { ParseResource(Type,IDList[ir]); } delete [] IDList; return true; }
OSErr AddToMasterFile(INTEGER from_file_rn, INTEGER master_file_rn) { OSErr err; mapping_t mapping; INTEGER type_num, type_num_max; ResType type; Handle h; Str255 name; BOOLEAN save_resload; err = setup_map (from_file_rn, master_file_rn, &mapping); if (err == noEErr) { type_num_max = CountTypesRN (from_file_rn); save_resload = ResLoad; SetResLoad (false); for (type_num = 1; type_num <= type_num_max; ++type_num) { GetIndTypeRN (from_file_rn, &type, type_num); res_num_max = CountResourcesRN (from_file_rn, type); for (res_num = 1; res_num <= res_num_max; ++res_num) { h = GetIndResourceRN (from_file_rn, type, res_num); GetResInfo (h, &id, &t, name); if (map_id (&id, from_file_rn, master_file_rn, &mapping)) { LoadResource(h); DetachResource(h); map_data (h, &mapping); AddResourceRN (master_file_rn, h, type, id, name); } } } SetResLoad (save_resload); } }
static PyObject *Res_SetResLoad(PyObject *_self, PyObject *_args) { PyObject *_res = NULL; Boolean load; #ifndef SetResLoad PyMac_PRECHECK(SetResLoad); #endif if (!PyArg_ParseTuple(_args, "b", &load)) return NULL; SetResLoad(load); { OSErr _err = ResError(); if (_err != noErr) return PyMac_Error(_err); } Py_INCREF(Py_None); _res = Py_None; return _res; }
int TclLoadFile( Tcl_Interp *interp, /* Used for error reporting. */ char *fileName, /* Name of the file containing the desired * code. */ char *sym1, char *sym2, /* Names of two procedures to look up in * the file's symbol table. */ Tcl_PackageInitProc **proc1Ptr, Tcl_PackageInitProc **proc2Ptr) /* Where to return the addresses corresponding * to sym1 and sym2. */ { CFragConnectionID connID; Ptr dummy; OSErr err; CFragSymbolClass symClass; FSSpec fileSpec; short fragFileRef, saveFileRef; Handle fragResource; UInt32 offset = 0; UInt32 length = kCFragGoesToEOF; char packageName[255]; Str255 errName; /* * First thing we must do is infer the package name from the sym1 * variable. This is kind of dumb since the caller actually knows * this value, it just doesn't give it to us. */ strcpy(packageName, sym1); *packageName = (char) tolower(*packageName); packageName[strlen(packageName) - 5] = NULL; err = FSpLocationFromPath(strlen(fileName), fileName, &fileSpec); if (err != noErr) { interp->result = "could not locate shared library"; return TCL_ERROR; } /* * See if this fragment has a 'cfrg' resource. It will tell us were * to look for the fragment in the file. If it doesn't exist we will * assume we have a ppc frag using the whole data fork. If it does * exist we find the frag that matches the one we are looking for and * get the offset and size from the resource. */ saveFileRef = CurResFile(); SetResLoad(false); fragFileRef = FSpOpenResFile(&fileSpec, fsRdPerm); SetResLoad(true); if (fragFileRef != -1) { UseResFile(fragFileRef); fragResource = Get1Resource(kCFragResourceType, kCFragResourceID); HLock(fragResource); if (ResError() == noErr) { CfrgItem* srcItem; long itemCount, index; Ptr itemStart; itemCount = (*(CfrgHeaderPtrHand)fragResource)->itemCount; itemStart = &(*(CfrgHeaderPtrHand)fragResource)->arrayStart; for (index = 0; index < itemCount; index++, itemStart += srcItem->itemSize) { srcItem = (CfrgItem*)itemStart; if (srcItem->archType != OUR_ARCH_TYPE) continue; if (!strncasecmp(packageName, (char *) srcItem->name + 1, srcItem->name[0])) { offset = srcItem->codeOffset; length = srcItem->codeLength; } } } /* * Close the resource file. If the extension wants to reopen the * resource fork it should use the tclMacLibrary.c file during it's * construction. */ HUnlock(fragResource); ReleaseResource(fragResource); CloseResFile(fragFileRef); UseResFile(saveFileRef); } /* * Now we can attempt to load the fragement using the offset & length * obtained from the resource. We don't worry about the main entry point * as we are going to search for specific entry points passed to us. */ c2pstr(packageName); err = GetDiskFragment(&fileSpec, offset, length, (StringPtr) packageName, kLoadCFrag, &connID, &dummy, errName); if (err != fragNoErr) { p2cstr(errName); Tcl_AppendResult(interp, "couldn't load file \"", fileName, "\": ", errName, (char *) NULL); return TCL_ERROR; } c2pstr(sym1); err = FindSymbol(connID, (StringPtr) sym1, (Ptr *) proc1Ptr, &symClass); p2cstr((StringPtr) sym1); if (err != fragNoErr || symClass == kDataCFragSymbol) { interp->result = "could not find Initialization routine in library"; return TCL_ERROR; } c2pstr(sym2); err = FindSymbol(connID, (StringPtr) sym2, (Ptr *) proc2Ptr, &symClass); p2cstr((StringPtr) sym2); if (err != fragNoErr || symClass == kDataCFragSymbol) { *proc2Ptr = NULL; } return TCL_OK; }
/* ** GetIconFromDesktopFile ** ** INPUT a pointer to a non-existent Handle, because we'll allocate one ** ** search each BNDL resource for the right fileCreator and once we get it ** find the 'FREF' type in BNDL ** for each localID in the type, open the FREF resource ** if the FREF is the desired fileType ** get its icon localID ** get the ICN# type in BNDL ** get the icon resource number from the icon localID ** get the icon resource type from the desktop mgr's iconType ** get the icon of that type and number */ static OSErr GetIconFromDesktopFile(ConstStr255Param volName, short vRefNum, short iconType, OSType fileCreator, OSType fileType, Handle *iconHandle) { OSErr error; short realVRefNum; Str255 desktopName; short savedResFile; short dfRefNum; BNDLRecHandle theBndl = NULL; BundleTypePtr theBundleType; short iconLocalID; short iconRsrcID; OSType iconRsrcType; Handle returnIconHandle; char bndlState; *iconHandle = NULL; error = DetermineVRefNum(volName, vRefNum, &realVRefNum); if ( error == noErr ) { error = GetDesktopFileName(realVRefNum, desktopName); if ( error == noErr ) { savedResFile = CurResFile(); /* ** Open the 'Desktop' file in the root directory. (because ** opening the resource file could preload unwanted resources, ** bracket the call with SetResLoad(s)) */ SetResLoad(false); dfRefNum = HOpenResFile(realVRefNum, fsRtDirID, desktopName, fsRdPerm); SetResLoad(true); if ( dfRefNum != -1 ) { /* ** Find the BNDL resource with the specified creator. */ error = FindBundleGivenCreator(fileCreator, &theBndl); if ( error == noErr ) { /* Lock the BNDL resource so it won't be purged when other resources are loaded */ bndlState = HGetState((Handle)theBndl); HLock((Handle)theBndl); /* Find the 'FREF' BundleType record in the BNDL resource. */ error = FindTypeInBundle(kFREFResType, theBndl, &theBundleType); if ( error == noErr ) { /* Find the local ID in the 'FREF' resource with the specified fileType */ error = GetLocalIDFromFREF(theBundleType, fileType, &iconLocalID); if ( error == noErr ) { /* Find the 'ICN#' BundleType record in the BNDL resource. */ error = FindTypeInBundle(kIconFamResType, theBndl, &theBundleType); if ( error == noErr ) { /* Find the icon's resource ID in the 'ICN#' BundleType record */ error = GetIconRsrcIDFromLocalID(theBundleType, iconLocalID, &iconRsrcID); if ( error == noErr ) { /* Map Desktop Manager icon type to resource type */ iconRsrcType = DTIconToResIcon(iconType); if ( iconRsrcType != (OSType)0 ) { /* Load the icon */ returnIconHandle = Get1Resource(iconRsrcType, iconRsrcID); if ( returnIconHandle != NULL ) { /* Copy the resource handle, and return the copy */ HandToHand(&returnIconHandle); if ( MemError() == noErr ) { *iconHandle = returnIconHandle; } else { error = afpItemNotFound; } } else { error = afpItemNotFound; } } } } } } /* Restore the state of the BNDL resource */ HSetState((Handle)theBndl, bndlState); } /* Restore the resource chain and close the Desktop file */ UseResFile(savedResFile); CloseResFile(dfRefNum); } else { error = ResError(); /* could not open Desktop file */ } } if ( (error != noErr) && (error != memFullErr) ) { error = afpItemNotFound; /* force an error we should return */ } } return ( error ); }
/* ** GetAPPLFromDesktopFile ** ** Get a application's location from the ** Desktop file's 'APPL' resources. */ static OSErr GetAPPLFromDesktopFile(ConstStr255Param volName, short vRefNum, OSType creator, short *applVRefNum, long *applParID, Str255 applName) { OSErr error; short realVRefNum; Str255 desktopName; short savedResFile; short dfRefNum; Handle applResHandle; Boolean foundCreator; Ptr applPtr; long applSize; error = DetermineVRefNum(volName, vRefNum, &realVRefNum); if ( error == noErr ) { error = GetDesktopFileName(realVRefNum, desktopName); if ( error == noErr ) { savedResFile = CurResFile(); /* ** Open the 'Desktop' file in the root directory. (because ** opening the resource file could preload unwanted resources, ** bracket the call with SetResLoad(s)) */ SetResLoad(false); dfRefNum = HOpenResFile(realVRefNum, fsRtDirID, desktopName, fsRdPerm); SetResLoad(true); if ( dfRefNum != -1) { /* Get 'APPL' resource ID 0 */ applResHandle = Get1Resource(kAPPLResType, 0); if ( applResHandle != NULL ) { applSize = GetHandleSize((Handle)applResHandle); if ( applSize != 0 ) /* make sure the APPL resource isn't empty */ { foundCreator = false; applPtr = *applResHandle; /* APPL's don't have a count so I have to use the size as the bounds */ while ( (foundCreator == false) && (applPtr < (*applResHandle + applSize)) ) { if ( ((APPLRecPtr)applPtr)->creator == creator ) { foundCreator = true; } else { /* fun with pointer math... */ applPtr += sizeof(OSType) + sizeof(long) + ((APPLRecPtr)applPtr)->applName[0] + 1; /* application mappings are word aligned within the resource */ if ( ((unsigned long)applPtr % 2) != 0 ) { applPtr += 1; } } } if ( foundCreator == true ) { *applVRefNum = realVRefNum; *applParID = ((APPLRecPtr)applPtr)->parID; BlockMoveData(((APPLRecPtr)applPtr)->applName, applName, ((APPLRecPtr)applPtr)->applName[0] + 1); /* error is already noErr */ } else { error = afpItemNotFound; /* didn't find a creator match */ } } else { error = afpItemNotFound; /* no APPL mapping available */ } } else { error = afpItemNotFound; /* no APPL mapping available */ } /* restore the resource chain and close the Desktop file */ UseResFile(savedResFile); CloseResFile(dfRefNum); } else { error = afpItemNotFound; } } } return ( error ); }
/* ** GetCommentFromDesktopFile ** ** Get a file or directory's Finder comment field (if any) from the ** Desktop file's 'FCMT' resources. */ static OSErr GetCommentFromDesktopFile(short vRefNum, long dirID, ConstStr255Param name, Str255 comment) { OSErr error; short commentID; short realVRefNum; Str255 desktopName; short savedResFile; short dfRefNum; StringHandle commentHandle; /* Get the comment ID number */ error = GetCommentID(vRefNum, dirID, name, &commentID); if ( error == noErr ) { if ( commentID != 0 ) /* commentID == 0 means there's no comment */ { error = DetermineVRefNum(name, vRefNum, &realVRefNum); if ( error == noErr ) { error = GetDesktopFileName(realVRefNum, desktopName); if ( error == noErr ) { savedResFile = CurResFile(); /* ** Open the 'Desktop' file in the root directory. (because ** opening the resource file could preload unwanted resources, ** bracket the call with SetResLoad(s)) */ SetResLoad(false); dfRefNum = HOpenResFile(realVRefNum, fsRtDirID, desktopName, fsRdPerm); SetResLoad(true); if ( dfRefNum != -1) { /* Get the comment resource */ commentHandle = (StringHandle)Get1Resource(kFCMTResType,commentID); if ( commentHandle != NULL ) { if ( GetHandleSize((Handle)commentHandle) > 0 ) { BlockMoveData(*commentHandle, comment, *commentHandle[0] + 1); } else { error = afpItemNotFound; /* no comment available */ } } else { error = afpItemNotFound; /* no comment available */ } /* restore the resource chain and close the Desktop file */ UseResFile(savedResFile); CloseResFile(dfRefNum); } else { error = afpItemNotFound; } } else { error = afpItemNotFound; } } } else { error = afpItemNotFound; /* no comment available */ } } return ( error ); }