home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-06-21 | 3.0 KB | 157 lines | [TEXT/CWIE] |
- // =============================================================================
- //
- // InitUtils.cp
- //
- // Author: Greg Friedman
- // Date: MacHack '96
- //
- // Do whatever you want with this source. Don't blame me if it doesn't work.
- //
- // =============================================================================
-
- #ifndef __INITUTILS__
- #include "InitUtils.h"
- #endif
-
- #ifndef __CONSTANTS__
- #include "Constants.h"
- #endif
-
- Handle gExclusionList = NULL;
- Handle gInclusionList = NULL;
-
- OSErr DetachInitResource(OSType resType, short resID)
- {
- OSErr err = noErr;
-
- Handle resH = Get1Resource(resType, resID);
- if (!h)
- err = (ResError() ? ResError() : resNotFound);
-
- if (err == noErr)
- {
- char state = HGetState(resH);
- if (!(state & kMemLockMask))
- {
- HLock(resH);
- err = MemError();
- }
- }
-
- if (err == noErr) // make sure it's in the system heap
- {
- THz zone = HandleZone(resH);
- err = MemError();
-
- if (err == noErr && zone != SystemZone())
- err = kError;
- }
-
- if (err == noErr)
- {
- DetachResource(resH);
- err = ResError();
- }
-
- return err;
- }
-
- void ExcludeCurrentProcess()
- {
- OSErr err;
- OSType signature;
-
- err = GetCurrentProcessSignature(&signature);
- if (err != noErr)
- return;
-
- if (!IsExcluded(signature))
- AddToList(gExclusionList, signature);
- }
-
- void AddToList(Handle list, OSType signature)
- {
- Size startSize = GetHandleSize(list);
- if (MemError() != noErr)
- return;
-
- SetHandleSize(list, startSize + sizeof(OSType));
- if (MemError() != noErr)
- return;
-
- *(OSType*)((long)(*list) + startSize) = signature;
- }
-
- Boolean IsInList(Handle list, OSType signature)
- {
- Size handleSize = GetHandleSize(list);
- OSType* curSig = *(OSType**)list;
- OSType* endBound = (OSType*)((long)curSig + handleSize);
- Boolean foundIt = FALSE;
-
- while (curSig < endBound && !foundIt)
- {
- if (*curSig++ == signature)
- foundIt = TRUE;
- }
-
- return foundIt;
- }
-
- OSErr GetCurrentProcessSignature(OSType* signature)
- {
- OSErr err;
- ProcessSerialNumber psn;
- ProcessInfoRec infoRec;
-
- infoRec.processInfoLength = sizeof(ProcessInfoRec);
- infoRec.processName = NULL;
- infoRec.processAppSpec = NULL;
- psn.highLongOfPSN = 0;
- psn.lowLongOfPSN = kCurrentProcess;
-
- err = GetProcessInformation(&psn, &infoRec);
- if (err == noErr)
- *signature = infoRec.processSignature;
- return err;
- }
-
- void InitializeLists()
- {
- gExclusionList = LoadAndDetachSys(kListType, kExclusionListID);
- if (!gExclusionList)
- gExclusionList = NewHandleSys(0);
-
- gInclusionList = LoadAndDetachSys(kListType, kInclusionListID);
- if (!gInclusionList)
- gInclusionList = NewHandleSys(0);
- }
-
- Handle LoadAndDetachSys(ResType type, short id)
- {
- Handle resHandle;
-
- SetResLoad(FALSE);
- resHandle = GetResource(type, id);
- SetResLoad(TRUE);
-
- if (resHandle)
- {
- short attributes = GetResAttrs(resHandle);
- SetResAttrs(resHandle, (attributes | resSysHeap));
- DetachResource(resHandle);
- DisposeHandle(resHandle);
-
- resHandle = GetResource(type, id);
- if (resHandle)
- {
- SetResAttrs(resHandle, attributes);
- DetachResource(resHandle);
- HNoPurge(resHandle);
- HUnlock(resHandle);
- }
- }
-
- return resHandle;
- }
-