AmendHub

Download:

nulleric

/

Fix-a-Fork

/

amendments

/

4

Open and DND mostly working.

Refactored magic checks
Moved buffer to global

eric made amendment 4 about 1 year ago
--- main.c Wed Nov 8 19:01:28 2023 +++ main.c Wed Nov 8 20:30:04 2023 @@ -9,7 +9,7 @@ pascal OSErr DoOpenDoc(AppleEvent *event, AppleEvent * Size actualSize; AEKeyword keywd; DescType returnedType; - //short frefNum; + short fRefNum; err = AEGetParamDesc(event, keyDirectObject, typeAEList, &docList); if(err != noErr) return err; @@ -19,10 +19,14 @@ pascal OSErr DoOpenDoc(AppleEvent *event, AppleEvent * for(index = 1; index <= itemsInList; index++) { err = AEGetNthPtr(&docList, index, typeFSS, &keywd, &returnedType, (Ptr)&fss, sizeof(fss), &actualSize); - if(err != noErr) return err; - - // got file by drag and drop - gHandledByDnD = true; + if(err) return err; + + // Handled file by drag and drop + err = openFile(fss.name, 0, 0); + if(err) + return err; + else + gHandledByDnD = true; } AEDisposeDesc(&docList); return noErr; @@ -41,36 +45,88 @@ void InstallEventHandlers() } else { return; } - WaitNextEvent(highLevelEventMask, &event, 30, 0); + do { + WaitNextEvent(highLevelEventMask, &event, 30, 0); + i = i+1; + } while(event.what != kHighLevelEvent && i < 5); if(event.what == kHighLevelEvent) AEProcessAppleEvent(&event); } void OpenFileDialog() { - SFReply tr; - StandardFileReply reply; + SFReply tr = {0}; + //StandardFileReply reply; Point where; where.h = 100; where.v = 50; do { - //SFGetFile(where, nil, nil, -1, nil, nil, &tr); - StandardGetFile(nil, -1, nil, &reply); - if(reply.sfGood) + SFGetFile(where, nil, nil, -1, nil, nil, &tr); + //StandardGetFile(nil, -1, nil, &reply); + if(tr.good) { - openFile(reply.sfFile.name, 0, reply.sfFile.vRefNum); + openFile(tr.fName, 0, tr.vRefNum); } - } while(reply.sfGood); + } while(tr.good); } +Boolean magicCheck(char *magic, short offset, OSType type, OSType creator) +{ + short i; + for(i = 0; i < sizeof(magic) - 1; i++) + if(gBuf[offset+i] != magic[i]) + return false; + gType = type; + gCreator = creator; + return true; +} -void openFile(unsigned char *fName, short fRefNum, short vRefNum) +Boolean isBinHex4() { - short err; + //char magic[] = ; + return magicCheck("BinHex 4.0", 34, 'TEXT', 'BnHq'); +} + +Boolean isSit5() +{ + return magicCheck("SIT!", 0, 'SIT!', 'SIT!'); +} + +OSErr openFile(unsigned char *fName, short fRefNum, short vRefNum) +{ + short err; + long count = BUF_SIZE; + FInfo fi = {0}; + Boolean found = false; + err = FSOpen(fName, vRefNum, &fRefNum); - ParamText(fName, "\p", "\p", "\p"); - Alert(128, nil); - FSClose(fRefNum); + if(err) return err; + + FSRead(fRefNum, &count, gBuf); + + // Check for magic in first 1024 bytes + if(isBinHex4()) + found = true; + else if(isSit5()) + found = true; + + if(found) + { + if(gCreator != 0 && gType != 0) + { + err = GetFInfo(fName, vRefNum, &fi); + if(err) return err; + fi.fdType = gType; + fi.fdCreator = gCreator; + + err = SetFInfo(fName, vRefNum, &fi); + } + // else unkown type/creator + } else { + ParamText("\pUnknown file type for ", fName, "\p", "\p"); + Alert(128, nil); + } + return FSClose(fRefNum); } void main() --- main.h Wed Nov 8 19:01:14 2023 +++ main.h Wed Nov 8 20:07:14 2023 @@ -6,10 +6,13 @@ #include <Dialogs.h> #include <Types.h> +#define BUF_SIZE 1024 + // Globals +Byte gBuf[BUF_SIZE] = {0}; OSType gType = 0; OSType gCreator = 0; Boolean gHandledByDnD = false; -void openFile(unsigned char *fName, short fRefNum, short vRefNum); +OSErr openFile(unsigned char *fName, short fRefNum, short vRefNum); pascal OSErr DoOpenDoc(AppleEvent *event, AppleEvent *reply, long handlerRefcon);