First Checked In.

This commit is contained in:
sgehani%netscape.com 1999-10-26 00:35:41 +00:00
parent ca05b7e548
commit 58f51c78b3
11 changed files with 1435 additions and 0 deletions

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,83 @@
(*
* The contents of this file are subject to the Netscape Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released March
* 31, 1998.
*
* The Initial Developer of the Original Code is Netscape Communications
* Corporation. Portions created by Netscape are Copyright (C) 1999
* Netscape Communications Corporation. All Rights Reserved.
*
* Contributors:
* Samir Gehani <sgehani@netscape.com>
*)
--
-- Simple demo script of ASEncoder's usage from a a script component/client
--
-- Commands supported by ASEncoder:
--
----------------------------------------
-- Required Suite:
----------------------------------------
-- quit
-- Description: Quit ASEncoder (you can open it using activate)
-- Params: No params
-- Return value: No retval
--
----------------------------------------
-- ASEncoder Suite:
----------------------------------------
-- encode
-- Description: Encode a file in AppleSingle format in place
-- Params: A file system specification
-- Return value: A success boolean (false also returned if file didn't have a res fork)
--
-- decode
-- Description: Decode an AppleSingle file in place
-- Params: A file system specification
-- Return value: A success boolean (false also returned if file wasn't in AS format)
--
on run
set kTargetFile to "HD:genJS:ASEncoder"
tell application "ASEncoder"
activate
-- pre-condition: kTargetFile is set to a local un-encoded (containing a res fork) file
-- encode should succeed
set bSuccess to encode file kTargetFile
SUCCESS_MSG(kTargetFile, "encode", bSuccess)
-- encode should fail
set bSuccess to encode file kTargetFile
SUCCESS_MSG(kTargetFile, "encode", bSuccess)
-- decode should succeed
set bSuccess to decode file kTargetFile
SUCCESS_MSG(kTargetFile, "decode", bSuccess)
--decode should fail
set bSuccess to decode file kTargetFile
SUCCESS_MSG(kTargetFile, "decode", bSuccess)
-- post-condition: kTargetFile is a decoded (containing a res fork) file as it originally was
quit
end tell
end run
on SUCCESS_MSG(target, method, retval)
display dialog "ASEncoder's " & method & " command
of file " & target & "
returned success value: " & retval
end SUCCESS_MSG

View File

@ -0,0 +1,291 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* The contents of this file are subject to the Netscape Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released March
* 31, 1998.
*
* The Initial Developer of the Original Code is Netscape Communications
* Corporation. Portions created by Netscape are Copyright (C) 1999
* Netscape Communications Corporation. All Rights Reserved.
*
* Contributors:
* Samir Gehani <sgehani@netscape.com>
*/
#ifndef _NS_ASEAPP_H_
#include "nsASEApp.h"
#endif
#include <AppleEvents.h>
#include "nsEventHandler.h"
#include "nsAppleSingleEncoder.h"
#include "nsAppleSingleDecoder.h"
Boolean gDone;
nsASEApp::nsASEApp()
{
InitManagers();
InitAEHandlers();
mWindow = NULL;
SetCompletionStatus(false);
OSErr err = NavLoad();
if (err!= noErr)
FatalError(navLoadErr);
MakeMenus();
}
nsASEApp::~nsASEApp()
{
NavUnload();
}
void
nsASEApp::InitManagers(void)
{
MaxApplZone();
MoreMasters(); MoreMasters(); MoreMasters();
InitGraf(&qd.thePort);
InitFonts();
InitWindows();
InitMenus();
TEInit();
InitDialogs(NULL);
InitCursor();
FlushEvents(everyEvent, 0);
}
#pragma mark -
#pragma mark *** Apple Event Handlers ***
#pragma mark -
pascal OSErr
EncodeEvent(AppleEvent *appEvent, AppleEvent *reply, SInt32 handlerRefCon)
{
OSErr err = noErr;
FSSpec param;
Boolean result = false;
AEDesc fileDesc;
// extract FSSpec from params
err = AEGetParamDesc(appEvent, keyDirectObject, typeFSS, &fileDesc);
if (err != noErr)
goto reply;
BlockMoveData(*fileDesc.dataHandle, &param, sizeof(FSSpec));
// param check
err = nsASEApp::GotRequiredParams(appEvent);
if (err != noErr)
goto reply;
// check if given file has res fork (takes care of existence check)
if (nsAppleSingleEncoder::HasResourceFork(&param))
{
// encode given file
nsAppleSingleEncoder encoder;
err = encoder.Encode(&param);
}
else
err = -1; // so that result boolean is set to false
// if noErr thus far
if (err == noErr)
{
// then set result to true
result = true;
}
reply:
// package reply
AEPutParamPtr(reply, keyDirectObject, typeBoolean, &result, sizeof(result));
// set err to noErr -- boolean takes care of failures
err = noErr;
return err;
}
pascal OSErr
DecodeEvent(AppleEvent *appEvent, AppleEvent *reply, SInt32 handlerRefCon)
{
OSErr err = noErr;
FSSpec param, outFile;
Boolean result = false;
AEDesc fileDesc;
// extract FSSpec from params
err = AEGetParamDesc(appEvent, keyDirectObject, typeFSS, &fileDesc);
if (err != noErr)
goto reply;
BlockMoveData(*fileDesc.dataHandle, &param, sizeof(FSSpec));
// param check
err = nsASEApp::GotRequiredParams(appEvent);
if (err != noErr)
goto reply;
// check if given file is in AS format (takes care of existence check)
if (nsAppleSingleDecoder::IsAppleSingleFile(&param))
{
// decode given file
nsAppleSingleDecoder decoder;
err = decoder.Decode(&param, &outFile);
}
else
err = -1; // so that result boolean is set to false
// if noErr thus far
if (err == noErr)
{
// then set result to true
result = true;
}
reply:
// package reply
AEPutParamPtr(reply, keyDirectObject, typeBoolean, &result, sizeof(result));
// set err to noErr -- boolean takes care of failures
err = noErr;
return err;
}
pascal OSErr
QuitEvent(AppleEvent *appEvent, AppleEvent *reply, SInt32 handlerRefCon)
{
OSErr err = noErr;
nsASEApp::SetCompletionStatus(true);
return err;
}
#pragma mark -
void
nsASEApp::InitAEHandlers()
{
OSErr err = noErr;
mEncodeUPP = NewAEEventHandlerProc((ProcPtr) EncodeEvent);
err = AEInstallEventHandler(kASEncoderEventClass, kAEEncode,
mEncodeUPP, 0L, false);
if (err != noErr)
::CautionAlert(aeInitErr, nil);
mDecodeUPP = NewAEEventHandlerProc((ProcPtr) DecodeEvent);
err = AEInstallEventHandler(kASEncoderEventClass, kAEDecode,
mDecodeUPP, 0L, false);
if (err != noErr)
::CautionAlert(aeInitErr, nil);
mQuitUPP = NewAEEventHandlerProc((ProcPtr) QuitEvent);
err = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
mQuitUPP, 0L, false);
if (err != noErr)
::CautionAlert(aeInitErr, nil);
}
OSErr
nsASEApp::GotRequiredParams(AppleEvent *appEvent)
{
OSErr err = noErr;
DescType returnedType;
Size actualSize;
err = AEGetAttributePtr(appEvent, keyMissedKeywordAttr, typeWildCard,
&returnedType, NULL, 0, &actualSize);
if (err == errAEDescNotFound)
err = noErr;
else if (err == noErr)
err = errAEParamMissed;
return err;
}
void
nsASEApp::MakeMenus()
{
Handle mbarHdl;
MenuHandle menuHdl;
mbarHdl = ::GetNewMBar(rMenuBar);
::SetMenuBar(mbarHdl);
if (menuHdl = ::GetMenuHandle(rMenuApple))
{
::AppendResMenu(menuHdl, 'DRVR');
}
if (menuHdl = GetMenuHandle(rMenuEdit))
::DisableItem(menuHdl, 0);
::HMGetHelpMenuHandle(&menuHdl);
::DisableItem(menuHdl, 0);
::DrawMenuBar();
}
void
nsASEApp::SetCompletionStatus(Boolean aVal)
{
gDone = aVal;
}
Boolean
nsASEApp::GetCompletionStatus()
{
return gDone;
}
void
nsASEApp::FatalError(short aErrID)
{
::StopAlert(aErrID, nil);
SetCompletionStatus(true);
}
OSErr
nsASEApp::Run()
{
OSErr err = noErr;
EventRecord evt;
nsEventHandler handler;
while (!gDone)
{
if (::WaitNextEvent(everyEvent, &evt, 180, NULL))
{
handler.HandleNextEvent(&evt);
}
}
return err;
}
int
main(void)
{
nsASEApp app;
app.Run();
return 0;
}

View File

@ -0,0 +1,88 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* The contents of this file are subject to the Netscape Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released March
* 31, 1998.
*
* The Initial Developer of the Original Code is Netscape Communications
* Corporation. Portions created by Netscape are Copyright (C) 1999
* Netscape Communications Corporation. All Rights Reserved.
*
* Contributors:
* Samir Gehani <sgehani@netscape.com>
*/
#ifndef _NS_ASEAPP_H_
#define _NS_ASEAPP_H_
#include <Events.h>
#include <Dialogs.h>
#include <Navigation.h>
#include <MacTypes.h>
class nsASEApp
{
public:
nsASEApp();
~nsASEApp();
OSErr Run();
static void SetCompletionStatus(Boolean aVal);
static Boolean GetCompletionStatus(void);
static void FatalError(short aErrID);
static OSErr GotRequiredParams(AppleEvent *appEvent);
private:
void InitManagers(void);
void InitAEHandlers(void);
void MakeMenus(void);
WindowPtr mWindow;
AEEventHandlerUPP mEncodeUPP;
AEEventHandlerUPP mDecodeUPP;
AEEventHandlerUPP mQuitUPP;
};
extern Boolean gDone;
/*---------------------------------------------------------------*
* Errors
*---------------------------------------------------------------*/
#define navLoadErr 128
#define aeInitErr 130
/*---------------------------------------------------------------*
* Resources
*---------------------------------------------------------------*/
#define rMenuBar 128
#define rMenuApple 128
#define rMenuItemAbout 1
#define rMenuFile 129
#define rMenuItemASEncode 1
#define rMenuItemASDecode 2
#define rMenuItemQuit 4
#define rMenuEdit 130
#define rAboutBox 129
/*---------------------------------------------------------------*
* Constants
*---------------------------------------------------------------*/
#define kASEncoderEventClass FOUR_CHAR_CODE('ASEn')
#define kAEEncode FOUR_CHAR_CODE('enco')
#define kAEDecode FOUR_CHAR_CODE('deco')
#endif /* _NS_ASEAPP_H_ */

View File

@ -0,0 +1,388 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* The contents of this file are subject to the Netscape Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released March
* 31, 1998.
*
* The Initial Developer of the Original Code is Netscape Communications
* Corporation. Portions created by Netscape are Copyright (C) 1999
* Netscape Communications Corporation. All Rights Reserved.
*
* Contributors:
* Samir Gehani <sgehani@netscape.com>
*/
#ifndef _NS_APPLESINGLEENCODER_H_
#include "nsAppleSingleEncoder.h"
#endif
#include <Files.h>
#include <OSUtils.h>
#include "nsAppleSingleDecoder.h" /* AppleSingle struct definitions */
nsAppleSingleEncoder::nsAppleSingleEncoder()
{
mInFile = NULL;
mOutFile = NULL;
}
nsAppleSingleEncoder::~nsAppleSingleEncoder()
{
}
Boolean
nsAppleSingleEncoder::HasResourceFork(FSSpecPtr aFile)
{
OSErr err = noErr;
Boolean bHasResFork = false;
short refnum;
long bytes2Read = 1;
char buf[2];
err = FSpOpenRF(aFile, fsRdPerm, &refnum);
if (err == noErr)
{
err = FSRead(refnum, &bytes2Read, &buf[0]);
if (err == noErr) /* err==eofErr if all OK but no res fork */
bHasResFork = true;
}
FSClose(refnum);
return bHasResFork;
}
OSErr
nsAppleSingleEncoder::Encode(FSSpecPtr aFile)
{
OSErr err = noErr;
FSSpec transient, exists;
// handle init in lieu of overloading
if (!mOutFile || !mInFile)
{
mInFile = aFile;
mOutFile = aFile;
}
// param check
if (!mInFile)
return paramErr;
// stomp old temp file
mTransient = &transient;
err = FSMakeFSSpec( mInFile->vRefNum, mInFile->parID, kTransientName,
mTransient);
if (err == noErr)
FSpDelete(mTransient);
// write to transient file
ERR_CHECK( WriteHeader() );
ERR_CHECK( WriteEntryDescs() );
ERR_CHECK( WriteEntries() );
// rename transient file to out file
err = FSMakeFSSpec( mOutFile->vRefNum, mOutFile->parID, mOutFile->name,
&exists );
if (err == noErr)
FSpDelete(mOutFile);
FSpRename(mTransient, mOutFile->name);
return err;
}
OSErr
nsAppleSingleEncoder::Encode(FSSpecPtr aInFile, FSSpecPtr aOutFile)
{
OSErr err = noErr;
// param check
if (!aInFile || !aOutFile)
return paramErr;
mInFile = aInFile;
mOutFile = aOutFile;
err = Encode(NULL);
return err;
}
OSErr
nsAppleSingleEncoder::WriteHeader()
{
OSErr err = noErr;
ASHeader hdr;
int i;
long bytes2Wr;
// init header info
hdr.magicNum = kAppleSingleMagicNum;
hdr.versionNum = kAppleSingleVerNum;
for (i=0; i<16; i++)
hdr.filler[i] = 0;
hdr.numEntries = kNumASEntries;
/*
** Entries:
**
** 1> Real name
** 2> Finder info
** 3> File dates
** 4> Resource fork
** 5> Data fork
** 6> Mac info
*/
bytes2Wr = sizeof(ASHeader);
ERR_CHECK( FSpCreate(mTransient, 'MOZZ', '????', 0) );
ERR_CHECK( FSpOpenDF(mTransient, fsRdWrPerm, &mTransRefNum) );
// ERR_CHECK( SetFPos(mTransRefNum, fsFromStart, 0) ); // XXX revisit
ERR_CHECK( FSWrite(mTransRefNum, &bytes2Wr, &hdr) );
if (bytes2Wr != sizeof(ASHeader))
err = -1;
return err;
}
OSErr
nsAppleSingleEncoder::WriteEntryDescs()
{
OSErr err = noErr;
long offset = sizeof(ASHeader), bytes2Wr = kNumASEntries*sizeof(ASEntry);
ASEntry entries[kNumASEntries];
int i;
CInfoPBRec pb;
ERR_CHECK( FSpGetCatInfo(&pb, mInFile) );
// real name
entries[0].entryOffset = sizeof(ASHeader) + (sizeof(ASEntry) * kNumASEntries);
entries[0].entryID = AS_REALNAME;
entries[0].entryLength = pb.hFileInfo.ioNamePtr[0];
// finder info
entries[1].entryID = AS_FINDERINFO;
entries[1].entryLength = sizeof(FInfo) + sizeof(FXInfo);
// file dates
entries[2].entryID = AS_FILEDATES;
entries[2].entryLength = sizeof(ASFileDates);
// resource fork
entries[3].entryID = AS_RESOURCE;
entries[3].entryLength = pb.hFileInfo.ioFlRLgLen;
// data fork
entries[4].entryID = AS_DATA;
entries[4].entryLength = pb.hFileInfo.ioFlLgLen;
// mac info
entries[5].entryID = AS_MACINFO;
entries[5].entryLength = sizeof(ASMacInfo);
for (i=1; i<kNumASEntries; i++) /* start at 1, not 0 */
{
entries[i].entryOffset = entries[i-1].entryOffset + entries[i-1].entryLength;
}
// ERR_CHECK( SetFPos(mTransRefNum, fsFromStart, sizeof(ASHeader)) ); // revisit
ERR_CHECK( FSWrite(mTransRefNum, &bytes2Wr, &entries[0]) );
if (bytes2Wr != (kNumASEntries * sizeof(ASEntry)))
err = -1;
return err;
}
OSErr
nsAppleSingleEncoder::WriteEntries()
{
OSErr err = noErr;
long bytes2Wr;
DateTimeRec currTime;
ASFileDates asDates;
unsigned long currSecs;
ASMacInfo asMacInfo;
int i;
CInfoPBRec pb;
char name[32];
FSpGetCatInfo(&pb, mInFile);
// real name
bytes2Wr = pb.hFileInfo.ioNamePtr[0];
strncpy(name, (char*)(pb.hFileInfo.ioNamePtr+1), bytes2Wr);
ERR_CHECK( FSWrite(mTransRefNum, &bytes2Wr, name) );
FSpGetCatInfo(&pb, mInFile); // XXX refresh after write
if (bytes2Wr != pb.hFileInfo.ioNamePtr[0])
{
err = -1;
goto cleanup;
}
// finder info
bytes2Wr = sizeof(FInfo);
ERR_CHECK( FSWrite(mTransRefNum, &bytes2Wr, &pb.hFileInfo.ioFlFndrInfo) );
FSpGetCatInfo(&pb, mInFile); // XXX refresh after write
if (bytes2Wr != sizeof(FInfo))
{
err = -1;
goto cleanup;
}
bytes2Wr = sizeof(FXInfo);
ERR_CHECK( FSWrite(mTransRefNum, &bytes2Wr, &pb.hFileInfo.ioFlXFndrInfo) );
FSpGetCatInfo(&pb, mInFile); // XXX refresh after write
if (bytes2Wr != sizeof(FXInfo))
{
err = -1;
goto cleanup;
}
// file dates
GetTime(&currTime);
DateToSeconds(&currTime, &currSecs);
FSpGetCatInfo(&pb, mInFile); // XXX refresh after sys calls
asDates.create = pb.hFileInfo.ioFlCrDat + kConvertTime;
asDates.modify = pb.hFileInfo.ioFlMdDat + kConvertTime;
asDates.backup = pb.hFileInfo.ioFlBkDat + kConvertTime;
asDates.access = currSecs + kConvertTime;
bytes2Wr = sizeof(ASFileDates);
ERR_CHECK( FSWrite(mTransRefNum, &bytes2Wr, &asDates) );
FSpGetCatInfo(&pb, mInFile); // XXX refresh after write
if (bytes2Wr != sizeof(ASFileDates))
{
err = -1;
goto cleanup;
}
// resource fork
ERR_CHECK( WriteResourceFork() );
// data fork
ERR_CHECK( WriteDataFork() );
// mac info
for (i=0; i<3; i++)
asMacInfo.filler[i];
FSpGetCatInfo(&pb, mInFile); // XXX refresh
asMacInfo.ioFlAttrib = pb.hFileInfo.ioFlAttrib;
bytes2Wr = sizeof(ASMacInfo);
ERR_CHECK( FSWrite(mTransRefNum, &bytes2Wr, &asMacInfo) );
if (bytes2Wr != sizeof(ASMacInfo))
{
err = -1;
goto cleanup;
}
cleanup:
FSClose(mTransRefNum);
return err;
}
#define BUFSIZE 8192
OSErr
nsAppleSingleEncoder::WriteResourceFork()
{
OSErr err = noErr;
short refnum;
long bytes2Rd, bytes2Wr;
char buf[BUFSIZE];
// open infile's resource fork
ERR_CHECK( FSpOpenRF(mInFile, fsRdPerm, &refnum) );
while (err != eofErr)
{
// keep reading chunks till infile's eof encountered
bytes2Rd = BUFSIZE;
err = FSRead(refnum, &bytes2Rd, buf);
if (bytes2Rd <= 0)
goto cleanup;
if (err == noErr || err == eofErr)
{
// keep writing
bytes2Wr = bytes2Rd;
err = FSWrite(mTransRefNum, &bytes2Wr, buf);
if (err != noErr || bytes2Wr != bytes2Rd)
goto cleanup;
}
}
cleanup:
// close infile
FSClose(refnum);
// eof is OK so translate
if (err == eofErr)
err = noErr;
return err;
}
OSErr
nsAppleSingleEncoder::WriteDataFork()
{
OSErr err = noErr;
short refnum;
long bytes2Rd, bytes2Wr;
char buf[BUFSIZE];
// open infile's data fork
ERR_CHECK( FSpOpenDF(mInFile, fsRdPerm, &refnum) );
while (err != eofErr)
{
// keep reading chunks till infile's eof encountered
bytes2Rd = BUFSIZE;
err = FSRead(refnum, &bytes2Rd, buf);
if (bytes2Rd <= 0)
goto cleanup;
if (err == noErr || err == eofErr)
{
// keep writing
bytes2Wr = bytes2Rd;
err = FSWrite(mTransRefNum, &bytes2Wr, buf);
if (err != noErr || bytes2Wr != bytes2Rd)
goto cleanup;
}
}
cleanup:
// close infile
FSClose(refnum);
// eof is OK so translate
if (err == eofErr)
err = noErr;
return err;
}
OSErr
nsAppleSingleEncoder::FSpGetCatInfo(CInfoPBRec *pb, FSSpecPtr aFile)
{
OSErr err = noErr;
Str31 name;
nsAppleSingleDecoder::PLstrncpy(name, aFile->name, aFile->name[0]);
name[0] = aFile->name[0]; // XXX paranoia
pb->hFileInfo.ioNamePtr = name;
pb->hFileInfo.ioVRefNum = aFile->vRefNum;
pb->hFileInfo.ioDirID = aFile->parID;
pb->hFileInfo.ioFDirIndex = 0; // use ioNamePtr and ioDirID
ERR_CHECK( PBGetCatInfoSync(pb) );
return err;
}

View File

@ -0,0 +1,79 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* The contents of this file are subject to the Netscape Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released March
* 31, 1998.
*
* The Initial Developer of the Original Code is Netscape Communications
* Corporation. Portions created by Netscape are Copyright (C) 1999
* Netscape Communications Corporation. All Rights Reserved.
*
* Contributors:
* Samir Gehani <sgehani@netscape.com>
*/
/*----------------------------------------------------------------------*
* Implements a simple AppleSingle encoder, as described in RFC1740
* http://andrew2.andrew.cmu.edu/rfc/rfc1740.html
*----------------------------------------------------------------------*/
#ifndef macintosh
#error Sorry! This is Mac only functionality!
#endif
#pragma options align=mac68k
#ifndef _NS_APPLESINGLEENCODER_H_
#define _NS_APPLESINGLEENCODER_H_
class nsAppleSingleEncoder
{
public:
nsAppleSingleEncoder();
~nsAppleSingleEncoder();
static Boolean HasResourceFork(FSSpecPtr aFile);
OSErr Encode(FSSpecPtr aFile);
OSErr Encode(FSSpecPtr aInFile, FSSpecPtr aOutFile);
static OSErr FSpGetCatInfo(CInfoPBRec *pb, FSSpecPtr aFile);
private:
FSSpecPtr mInFile;
FSSpecPtr mOutFile;
FSSpecPtr mTransient;
short mTransRefNum;
OSErr WriteHeader();
OSErr WriteEntryDescs();
OSErr WriteEntries();
OSErr WriteResourceFork();
OSErr WriteDataFork();
};
#define kTransientName "\pzz__ASEncoder_TMP__zz"
#define kAppleSingleMagicNum 0x00051600
#define kAppleSingleVerNum 0x00020000
#define kNumASEntries 6
#define kConvertTime 1265437696L
#define ERR_CHECK(_func) \
err = _func; \
if (err != noErr) \
return err;
#pragma options align=reset
#endif /* _NS_APPLESINGLEENCODER_H_ */

View File

@ -0,0 +1,233 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* The contents of this file are subject to the Netscape Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released March
* 31, 1998.
*
* The Initial Developer of the Original Code is Netscape Communications
* Corporation. Portions created by Netscape are Copyright (C) 1999
* Netscape Communications Corporation. All Rights Reserved.
*
* Contributors:
* Samir Gehani <sgehani@netscape.com>
*/
#ifndef _NS_EVENTHANDLER_H_
#include "nsEventHandler.h"
#endif
#include "nsASEApp.h"
#include "nsFileSelector.h"
#include "nsAppleSingleEncoder.h"
#include "nsAppleSingleDecoder.h"
nsEventHandler::nsEventHandler()
{
}
nsEventHandler::~nsEventHandler()
{
}
OSErr
nsEventHandler::HandleNextEvent(EventRecord *aEvt)
{
OSErr err = noErr;
mCurrEvent = aEvt;
switch(aEvt->what)
{
case kHighLevelEvent:
err = AEProcessAppleEvent(aEvt);
if (err != errAEEventNotHandled)
break;
case mouseDown:
err = HandleMouseDown();
break;
case mouseUp:
break;
case autoKey:
case keyDown:
err = HandleKeyDown();
break;
case updateEvt:
err = HandleUpdateEvt();
break;
case activateEvt:
err = HandleActivateEvt();
break;
case osEvt:
err = HandleOSEvt();
break;
case nullEvent:
break;
default:
break;
}
return err;
}
OSErr
nsEventHandler::HandleMouseDown()
{
OSErr err = noErr;
WindowPtr currWindow;
SInt16 cntlPartCode;
SInt32 menuChoice;
cntlPartCode = FindWindow(mCurrEvent->where, &currWindow);
switch (cntlPartCode)
{
case inMenuBar:
menuChoice = MenuSelect(mCurrEvent->where);
HandleMenuChoice(menuChoice);
::InvalMenuBar();
break;
default:
break;
}
return err;
}
OSErr
nsEventHandler::HandleKeyDown()
{
OSErr err = noErr;
SInt8 charCode;
SInt32 menuChoice = 0;
charCode = mCurrEvent->message & charCodeMask;
if ((mCurrEvent->modifiers & cmdKey) != 0)
{
switch (charCode)
{
case 'E':
case 'e':
menuChoice = rMenuFile;
menuChoice <<= 16;
menuChoice |= rMenuItemASEncode;
break;
case 'D':
case 'd':
menuChoice = rMenuFile;
menuChoice <<= 16;
menuChoice |= rMenuItemASDecode;
break;
case 'Q':
case 'q':
menuChoice = rMenuFile;
menuChoice <<= 16;
menuChoice |= rMenuItemQuit;
break;
default:
menuChoice = 0;
break;
}
err = HandleMenuChoice(menuChoice);
}
return err;
}
OSErr
nsEventHandler::HandleUpdateEvt()
{
}
OSErr
nsEventHandler::HandleActivateEvt()
{
}
OSErr
nsEventHandler::HandleOSEvt()
{
::HiliteMenu(0);
}
OSErr
nsEventHandler::HandleInContent()
{
}
OSErr
nsEventHandler::HandleMenuChoice(SInt32 aChoice)
{
OSErr err = noErr;
long menuID = HiWord(aChoice);
long menuItem = LoWord(aChoice);
FSSpec file, outFile;
nsFileSelector selector;
switch (menuID)
{
case rMenuFile:
switch (menuItem)
{
case rMenuItemASEncode: /* AS Encode... */
err = selector.SelectFile(&file);
if (err == noErr)
{
if (nsAppleSingleEncoder::HasResourceFork(&file))
{
nsAppleSingleEncoder encoder;
err = encoder.Encode(&file);
}
}
break;
case rMenuItemASDecode: /* AS Decode... */
err = selector.SelectFile(&file);
if (err == noErr)
{
if (nsAppleSingleDecoder::IsAppleSingleFile(&file))
{
nsAppleSingleDecoder decoder;
err = decoder.Decode(&file, &outFile);
}
}
break;
case rMenuItemQuit: /* Quit */
nsASEApp::SetCompletionStatus(true);
break;
}
break;
case rMenuApple:
if (menuItem == rMenuItemAbout)
::Alert(rAboutBox, nil);
break;
default:
break;
}
return err;
}

View File

@ -0,0 +1,47 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* The contents of this file are subject to the Netscape Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released March
* 31, 1998.
*
* The Initial Developer of the Original Code is Netscape Communications
* Corporation. Portions created by Netscape are Copyright (C) 1999
* Netscape Communications Corporation. All Rights Reserved.
*
* Contributors:
* Samir Gehani <sgehani@netscape.com>
*/
#ifndef _NS_EVENTHANDLER_H_
#define _NS_EVENTHANDLER_H_
class nsEventHandler
{
public:
nsEventHandler();
~nsEventHandler();
OSErr HandleNextEvent(EventRecord *aEvt);
OSErr HandleMouseDown();
OSErr HandleKeyDown();
OSErr HandleUpdateEvt();
OSErr HandleActivateEvt();
OSErr HandleOSEvt();
OSErr HandleInContent();
OSErr HandleMenuChoice(SInt32 aChoice);
private:
EventRecord *mCurrEvent;
};
#endif /* _NS_EVENTHANDLER_H_ */

View File

@ -0,0 +1,176 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* The contents of this file are subject to the Netscape Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released March
* 31, 1998.
*
* The Initial Developer of the Original Code is Netscape Communications
* Corporation. Portions created by Netscape are Copyright (C) 1999
* Netscape Communications Corporation. All Rights Reserved.
*
* Contributors:
* Samir Gehani <sgehani@netscape.com>
*/
#ifndef _NS_FILESELECTOR_H_
#include "nsFileSelector.h"
#endif
#include "nsAppleSingleDecoder.h"
nsFileSelector::nsFileSelector()
{
mFile = NULL;
}
nsFileSelector::~nsFileSelector()
{
}
pascal void
OurNavEventFunction(NavEventCallbackMessage callBackSelector, NavCBRecPtr callBackParms,
NavCallBackUserData callBackUD)
{
WindowPtr windowPtr;
windowPtr = (WindowPtr) callBackParms->eventData.eventDataParms.event->message;
if (!windowPtr)
return;
switch(callBackSelector)
{
case kNavCBEvent:
switch(callBackParms->eventData.eventDataParms.event->what)
{
case updateEvt:
if(((WindowPeek) windowPtr)->windowKind != kDialogWindowKind)
// XXX irrelevant
// HandleUpdateEvt((EventRecord *) callBackParms->eventData.eventDataParms.event);
break;
}
break;
}
}
OSErr
nsFileSelector::SelectFile(FSSpecPtr aOutFile)
{
OSErr err = noErr;
NavReplyRecord reply;
NavDialogOptions dlgOpts;
NavEventUPP eventProc;
AEDesc resultDesc, initDesc;
FSSpec tmp;
short cwdVRefNum;
long cwdDirID, len;
mFile = aOutFile;
err = NavGetDefaultDialogOptions(&dlgOpts);
len = strlen("Please select a file");
nsAppleSingleDecoder::PLstrncpy(dlgOpts.message, "\pPlease select a file", len);
eventProc = NewNavEventProc( (ProcPtr) OurNavEventFunction );
ERR_CHECK( GetCWD(&cwdDirID, &cwdVRefNum) );
ERR_CHECK( FSMakeFSSpec(cwdVRefNum, cwdDirID, NULL, &tmp) );
ERR_CHECK( AECreateDesc(typeFSS, (void*) &tmp, sizeof(FSSpec), &initDesc) );
err = NavChooseFile( &initDesc, &reply, &dlgOpts, eventProc, NULL, NULL, NULL, NULL );
AEDisposeDesc(&initDesc);
DisposeRoutineDescriptor(eventProc);
if((reply.validRecord) && (err == noErr))
{
if((err = AECoerceDesc(&(reply.selection),typeFSS,&resultDesc)) == noErr)
{
BlockMoveData(*resultDesc.dataHandle,&tmp,sizeof(FSSpec));
/* forces name to get filled */
FSMakeFSSpec(tmp.vRefNum, tmp.parID, tmp.name, aOutFile);
}
AEDisposeDesc(&resultDesc);
NavDisposeReply(&reply);
}
return err;
}
OSErr
nsFileSelector::SelectFolder(FSSpecPtr aOutFolder)
{
OSErr err = noErr;
NavReplyRecord reply;
NavDialogOptions dlgOpts;
NavEventUPP eventProc;
AEDesc resultDesc, initDesc;
FSSpec tmp;
short cwdVRefNum;
long cwdDirID, len;
mFile = aOutFolder;
err = NavGetDefaultDialogOptions(&dlgOpts);
len = strlen("Please select a folder");
nsAppleSingleDecoder::PLstrncpy(dlgOpts.message, "\pPlease select a folder", len);
eventProc = NewNavEventProc( (ProcPtr) OurNavEventFunction );
ERR_CHECK( GetCWD(&cwdDirID, &cwdVRefNum) );
ERR_CHECK( FSMakeFSSpec(cwdVRefNum, cwdDirID, NULL, &tmp) );
ERR_CHECK( AECreateDesc(typeFSS, (void*) &tmp, sizeof(FSSpec), &initDesc) );
err = NavChooseFolder( &initDesc, &reply, &dlgOpts, eventProc, NULL, NULL );
AEDisposeDesc(&initDesc);
DisposeRoutineDescriptor(eventProc);
if((reply.validRecord) && (err == noErr))
{
if((err = AECoerceDesc(&(reply.selection),typeFSS,&resultDesc)) == noErr)
{
BlockMoveData(*resultDesc.dataHandle,&tmp,sizeof(FSSpec));
/* forces name to get filled */
FSMakeFSSpec(tmp.vRefNum, tmp.parID, tmp.name, aOutFolder);
}
AEDisposeDesc(&resultDesc);
NavDisposeReply(&reply);
}
return err;
}
OSErr
nsFileSelector::GetCWD(long *aOutDirID, short *aOutVRefNum)
{
OSErr err = noErr;
ProcessSerialNumber psn;
ProcessInfoRec pInfo;
FSSpec tmp;
/* get cwd based on curr ps info */
if (!(err = GetCurrentProcess(&psn)))
{
pInfo.processName = nil;
pInfo.processAppSpec = &tmp;
pInfo.processInfoLength = (sizeof(ProcessInfoRec));
if(!(err = GetProcessInformation(&psn, &pInfo)))
{
*aOutDirID = pInfo.processAppSpec->parID;
*aOutVRefNum = pInfo.processAppSpec->vRefNum;
}
}
return err;
}

View File

@ -0,0 +1,50 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* The contents of this file are subject to the Netscape Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released March
* 31, 1998.
*
* The Initial Developer of the Original Code is Netscape Communications
* Corporation. Portions created by Netscape are Copyright (C) 1999
* Netscape Communications Corporation. All Rights Reserved.
*
* Contributors:
* Samir Gehani <sgehani@netscape.com>
*/
#ifndef _NS_FILESELECTOR_H_
#define _NS_FILESELECTOR_H_
#include <Navigation.h>
class nsFileSelector
{
public:
nsFileSelector();
~nsFileSelector();
OSErr SelectFile(FSSpecPtr aOutFile);
OSErr SelectFolder(FSSpecPtr aOutFolder);
private:
OSErr GetCWD(long *aOutDirID, short *aOutVRefNum);
FSSpecPtr mFile;
};
#define ERR_CHECK(_func) \
err = _func; \
if (err != noErr) \
return err;
#endif _NS_FILESELECTOR_H_