#include "cw_common.h" #include "cos.h" extern Boolean systemHandles; static StringPtr COS_pstrcpy(StringPtr dst, ConstStringPtr src) { short cnt; for (cnt = src[0]; cnt >= 0; cnt--) { *(dst++) = *(src++); } return dst; } static StringPtr COS_pstrcat(StringPtr dst, ConstStringPtr src) { // not matching short as; short bs; bs = *(src++); as = dst[0]; if ((bs + as) > 255) bs = 255 - as; dst[0] += bs; dst = &dst[as]; dst++; while (bs-- > 0) { *(dst++) = *(src++); } return dst; } static void COS_pstrcharcat(StringPtr to, char ch) { if (to[0] < 255) { to[0]++; to[to[0]] = ch; } } static short COS_pstrcmp(ConstStringPtr a, ConstStringPtr b) { short as, bs; if ((bs = *(b++)) != (as = *(a++))) return 1; while (as-- > 0) { if (*(a++) != *(b++)) return 1; } return 0; } Handle COS_NewHandle(SInt32 byteCount) { return NewHandle(byteCount); } Handle COS_NewOSHandle(SInt32 logicalSize) { OSErr err; Handle h; if (systemHandles) { h = TempNewHandle(logicalSize, &err); if (!err) return h; } return NULL; } void COS_FreeHandle(Handle handle) { DisposeHandle(handle); } Boolean COS_ResizeHandle(Handle handle, SInt32 newSize) { SetHandleSize(handle, newSize); return LMGetMemErr() == noErr; } SInt32 COS_GetHandleSize(Handle handle) { return GetHandleSize(handle); } void COS_LockHandle(Handle handle) { HLock(handle); } void COS_LockHandleHi(Handle handle) { HLockHi(handle); } void COS_UnlockHandle(Handle handle) { HUnlock(handle); } int COS_GetHandleState(Handle handle) { return HGetState(handle); } void COS_SetHandleState(Handle handle, int state) { HSetState(handle, state); } Boolean COS_IsLockedState(int state) { return (state & 0x80) >> 7; } char *COS_NewPtr(SInt32 byteCount) { return NewPtr(byteCount); } char *COS_NewPtrClear(SInt32 byteCount) { return NewPtrClear(byteCount); } void COS_FreePtr(char *ptr) { DisposePtr(ptr); } void COS_AppendPtrToHandle(char *ptr1, Handle hand2, SInt32 size) { PtrAndHand(ptr1, hand2, size); } OSErr COS_GetMemErr(void) { return LMGetMemErr(); } SInt32 COS_GetTicks(void) { return LMGetTicks(); } SInt32 COS_GetTime(void) { return LMGetTime(); } void COS_GetString(char *buffer, SInt16 strListID, SInt16 index) { GetIndString((StringPtr) buffer, strListID, index); p2cstr((StringPtr) buffer); } void COS_GetPString(StringPtr buffer, SInt16 strListID, SInt16 index) { GetIndString((StringPtr) buffer, strListID, index); } Boolean COS_IsMultiByte(const void *str1, const void *str2) { return OS_IsMultiByte(str1, str2); } OSErr COS_FileNew(const FSSpec *spec, SInt16 *refNum, OSType creator, OSType fileType) { OSErr err; HDelete(spec->vRefNum, spec->parID, spec->name); err = HCreate(spec->vRefNum, spec->parID, spec->name, creator, fileType); if (!err) err = HOpenDF(spec->vRefNum, spec->parID, spec->name, fsRdWrPerm, refNum); return err; } OSErr COS_FileOpen(const FSSpec *spec, SInt16 *refNum) { return HOpenDF(spec->vRefNum, spec->parID, spec->name, fsRdPerm, refNum); } OSErr COS_FileGetType(const FSSpec *spec, OSType *fileType) { OSErr err; FInfo fi; err = HGetFInfo(spec->vRefNum, spec->parID, spec->name, &fi); *fileType = fi.fdType; return err; } OSErr COS_FileGetSize(SInt16 refNum, SInt32 *logEOF) { return GetEOF(refNum, logEOF); } OSErr COS_FileRead(SInt16 refNum, void *buffPtr, SInt32 count) { return FSRead(refNum, &count, buffPtr); } OSErr COS_FileWrite(SInt16 refNum, const void *buffPtr, SInt32 count) { return FSWrite(refNum, &count, buffPtr); } OSErr COS_FileGetPos(SInt16 refNum, SInt32 *filePos) { return GetFPos(refNum, filePos); } OSErr COS_FileSetPos(SInt16 refNum, SInt32 filePos) { return SetFPos(refNum, fsFromStart, filePos); } OSErr COS_FileClose(SInt16 refNum) { return FSClose(refNum); } void COS_FileSetFSSpec(FSSpec *spec, ConstStringPtr path) { char buf[256]; OSSpec ospec; memcpy(buf, path + 1, path[0]); buf[path[0]] = 0; if (OS_MakeSpec(buf, &ospec, NULL) || OS_OSSpec_To_FSSpec(&ospec, spec)) { spec->vRefNum = 0; spec->parID = 0; c2pstrcpy(spec->name, buf); } } OSErr COS_FileMakeFSSpec(SInt16 vRefNum, SInt32 dirID, ConstStringPtr fileName, FSSpec *spec) { int oerr; OSPathSpec opspec; char buf[256]; OSSpec ospec; oerr = OS_VolDir_To_OSPathSpec(vRefNum, dirID, &opspec); if (oerr) return OS_MacError(oerr); p2cstrcpy(buf, fileName); oerr = OS_MakeSpecWithPath(&opspec, buf, 0, &ospec); if (oerr) { pstrncpy(spec->name, fileName, 256); return OS_MacError(oerr); } else { return OS_MacError(OS_OSSpec_To_FSSpec(&ospec, spec)); } } OSErr COS_FileMakeFSSpecWithPath(const FSSpec *inputSpec, ConstStringPtr fileName, FSSpec *spec) { spec->vRefNum = inputSpec->vRefNum; spec->parID = inputSpec->parID; COS_pstrcpy(spec->name, fileName); return noErr; } OSErr COS_FileGetFileInfo(const FSSpec *spec, OSType *creator, OSType *fileType) { OSErr err; FInfo fi; err = HGetFInfo(spec->vRefNum, spec->parID, spec->name, &fi); if (fileType) *fileType = fi.fdType; if (creator) *creator = fi.fdCreator; return err; } void COS_FileGetFSSpecInfo(const FSSpec *spec, SInt16 *vRefNum, SInt32 *dirID, StringPtr fileName) { if (vRefNum) *vRefNum = spec->vRefNum; if (dirID) *dirID = spec->parID; if (fileName) COS_pstrcpy(fileName, spec->name); } static void COS_MakePath(SInt16 vRefNum, SInt32 dirID, StringPtr path) { FSSpec fss; OSSpec oss; fss.vRefNum = vRefNum; fss.parID = dirID; COS_pstrcpy(fss.name, path); if (!OS_FSSpec_To_OSSpec(&fss, &oss)) { OS_SpecToString(&oss, (char *) path, 256); c2pstr((char *) path); } } void COS_FileGetPathName(char *buffer, const FSSpec *spec, SInt32 *mdDat) { HParamBlockRec rec; if (mdDat) { rec.fileParam.ioNamePtr = (StringPtr) spec->name; rec.fileParam.ioVRefNum = spec->vRefNum; rec.fileParam.ioDirID = spec->parID; rec.fileParam.ioFDirIndex = 0; if (!PBHGetFInfoSync(&rec)) *mdDat = rec.fileParam.ioFlMdDat; else *mdDat = 0; } COS_pstrcpy((StringPtr) buffer, spec->name); COS_MakePath(spec->vRefNum, spec->parID, (StringPtr) buffer); p2cstr((StringPtr) buffer); } int COS_EqualFileSpec(const FSSpec *a, const FSSpec *b) { if (a->vRefNum != b->vRefNum) return 0; if (a->parID != b->parID) return 0; return COS_pstrcmp(a->name, b->name) == 0; }