home *** CD-ROM | disk | FTP | other *** search
- // ModDateFixer.cpp
-
- #include <stdio.h>
- #include <string.h>
- #include <AppleEvents.h>
-
-
- static volatile Boolean running = true;
- static WindowPtr statusWindow = NULL;
- static unsigned long curModDate = 0;
-
- #define Error(e) _Error(e,__FILE__,__LINE__)
- static void
- _Error(
- OSErr err,
- const char *file,
- int line)
- {
- char msg[200];
- sprintf(msg, "error %d file %s line %d", err, file, line);
- SetPort(statusWindow);
- Rect r;
- SetRect(&r, 0,0, 500,18);
- EraseRect(&r);
- MoveTo(5,15);
- TextFace(bold);
- DrawText(msg, 0, strlen(msg));
- TextFace(normal);
- long endtime;
- Delay(200, &endtime);
- }
-
-
- static void
- InitToolbox()
- {
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(NULL);
-
- InitCursor();
- statusWindow = GetNewCWindow(128, NULL, NULL);
- ShowWindow(statusWindow);
- }
-
-
- static void
- Report(
- FSSpec *ptr,
- int level)
- {
- Rect r;
- SetRect(&r, 5,18+15*level, 500,1000);
- EraseRect(&r);
- MoveTo(5+15*level, 30+15*level);
- DrawString(ptr->name);
- }
-
-
- static void
- FixModDate(
- FSSpec *ptr)
- {
- CInfoPBRec rec;
- rec.hFileInfo.ioFDirIndex = 0;
- rec.hFileInfo.ioVRefNum = ptr->vRefNum;
- rec.hFileInfo.ioDirID = ptr->parID;
- rec.hFileInfo.ioNamePtr = ptr->name;
- OSErr err = PBGetCatInfoSync(&rec);
- if (err)
- {
- Error(err);
- }
- else
- {
- Boolean doIt = false;
- if (rec.hFileInfo.ioFlMdDat > curModDate)
- {
- rec.hFileInfo.ioFlMdDat = curModDate;
- doIt = true;
- }
- if (rec.hFileInfo.ioFlCrDat > rec.hFileInfo.ioFlMdDat)
- {
- rec.hFileInfo.ioFlCrDat = rec.hFileInfo.ioFlMdDat;
- doIt = true;
- }
- if (doIt)
- {
- rec.hFileInfo.ioDirID = ptr->parID;
- rec.hFileInfo.ioFDirIndex = 0;
- err = PBSetCatInfoSync(&rec);
- if (err)
- {
- Error(err);
- }
- }
- }
- }
-
-
- static Boolean
- IsFolder(
- FSSpec *spec)
- {
- CInfoPBRec rec;
- rec.hFileInfo.ioFDirIndex = 0;
- rec.hFileInfo.ioVRefNum = spec->vRefNum;
- rec.hFileInfo.ioDirID = spec->parID;
- rec.hFileInfo.ioNamePtr = spec->name;
- OSErr err = PBGetCatInfoSync(&rec);
- if (err)
- {
- Error(err);
- return false;
- }
- return (rec.hFileInfo.ioFlAttrib&0x10) != 0;
- }
-
-
- static Boolean
- GetNthItem(
- FSSpec *spec,
- int ix,
- FSSpec *out)
- {
- CInfoPBRec rec;
- rec.hFileInfo.ioFDirIndex = 0;
- rec.hFileInfo.ioVRefNum = spec->vRefNum;
- rec.hFileInfo.ioDirID = spec->parID;
- rec.hFileInfo.ioNamePtr = spec->name;
- OSErr err = PBGetCatInfoSync(&rec);
- if (err)
- {
- Error(err);
- return false;
- }
- if (!(rec.hFileInfo.ioFlAttrib&0x10))
- {
- return false;
- }
- long dirID = rec.hFileInfo.ioDirID;
- rec.hFileInfo.ioFDirIndex = ix;
- rec.hFileInfo.ioVRefNum = spec->vRefNum;
- rec.hFileInfo.ioDirID = dirID;
- rec.hFileInfo.ioNamePtr = out->name;
- err = PBGetCatInfoSync(&rec);
- if (err)
- {
- if (err != fnfErr)
- {
- Error(err);
- }
- return false;
- }
- out->vRefNum = spec->vRefNum;
- out->parID = dirID;
- return true;
- }
-
-
- static void
- Recurse(
- FSSpec *ptr,
- int level)
- {
- Report(ptr, level);
- FSSpec newSpec;
- if (IsFolder(ptr))
- {
- for (int ix=0; GetNthItem(ptr, ix+1, &newSpec); ix++)
- {
- Recurse(&newSpec, level+1);
- }
- }
- else
- {
- FixModDate(ptr);
- }
- }
-
-
- static OSErr
- DoOpen(
- AppleEvent *event,
- AppleEvent * /* reply */,
- long /* refCon */)
- {
- AEDesc list = { 0, 0 };
- OSErr err = AEGetKeyDesc(event, keyDirectObject, typeAEList, &list);
- if (err)
- {
- Error(err);
- return err;
- }
- long count = 0;
- err = AECountItems(&list, &count);
- if (err)
- {
- Error(err);
- return err;
- }
- for (int ix=0; ix<count; ix++)
- {
- FSSpec topLevel;
- AEKeyword word;
- OSType type;
- Size actualSize;
- err = AEGetNthPtr(&list, ix+1, typeFSS, &word, &type, (Ptr)&topLevel, sizeof(topLevel), &actualSize);
- if (err)
- {
- Error(err);
- }
- else
- Recurse(&topLevel, 0);
- }
- running = false;
- return noErr;
- }
-
-
- static OSErr
- DoQuit(
- AppleEvent * /* event */,
- AppleEvent * /* reply */,
- long /* refCon */)
- {
- running = false;
- return noErr;
- }
-
-
- void
- main()
- {
- InitToolbox();
- AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerProc(DoOpen), 0, false);
- AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, NewAEEventHandlerProc(DoQuit), 0, false);
- GetDateTime(&curModDate);
- while (running)
- {
- EventRecord er;
- er.what = 0;
- WaitNextEvent(-1, &er, 50, NULL);
- SetPort(statusWindow);
- if (er.what == kHighLevelEvent)
- {
- AEProcessAppleEvent(&er);
- }
- }
- }
-