home *** CD-ROM | disk | FTP | other *** search
- // =================================================================================
- // Copyright © 1996 Michael Schürig
- //
- // You may copy this file, rip it apart or use it in derivative work as long
- // as you don't change the original file and this message remains intact.
- // =================================================================================
-
- #define Debug_Signal
- #define Debug_Throw
-
- #include <UException.h>
- #include <string.h>
-
-
- class UResourceParser {
-
- class ListUntilZero;
- class ListUntilEnd;
- class ListZeroBased;
- class ListOneBased;
-
- friend class UResourceParser::ListUntilZero;
- friend class UResourceParser::ListUntilEnd;
- friend class UResourceParser::ListZeroBased;
- friend class UResourceParser::ListOneBased;
-
- public:
-
- UResourceParser(const Handle inResH) throw();
- virtual ~UResourceParser() throw();
-
- virtual void Parse() = 0;
-
- protected:
-
- //// Atomic types
-
- // Alignment
- inline void AWRD();
- inline void ALNG();
-
- // Fillers
- inline void FBYT();
- inline void FWRD();
- inline void FLNG();
-
- // Integers
- inline SInt8 SBYT();
- inline SInt16 SWRD();
- inline SInt32 SLNG();
- inline UInt8 UBYT();
- inline UInt16 UWRD();
- inline UInt32 ULNG();
-
- // Pascal strings
- inline StringPtr PSTR();
- #if 0 //###
- inline void WSTR();
- inline void LSTR();
- #endif
- inline StringPtr P0nn(const UInt8 inNN);
-
- // C strings
- inline char* CSTR();
- inline char* Cnnn(const UInt16 inNNN);
-
- // Hex dump
- inline Ptr Hnnn(const UInt16 inNNN);
- inline Ptr HEXD();
-
- // Boolean
- inline Boolean BOOL();
- #if 0
- inline void BBIT(BitFieldT &outBits);
- #endif
- inline UInt8 BBIT();
-
- // Character
- inline char CHAR();
-
- // Rectangle
- inline Rect* RECT();
-
- // OSType
- inline OSType TNAM();
-
-
- //// Lists
-
- class List {
- public:
- List(UResourceParser *inRP);
- virtual ~List();
- virtual Boolean Advance() = 0;
- protected:
- UResourceParser *mRP;
- };
-
- class ListUntilZero : public UResourceParser::List {
- public:
- ListUntilZero(UResourceParser *inRP);
- virtual ~ListUntilZero();
- Boolean Advance();
- };
-
- class ListZeroBased : public UResourceParser::List {
- public:
- ListZeroBased(UResourceParser *inRP);
- virtual ~ListZeroBased();
- Boolean Advance();
- protected:
- SInt16 mCount;
- };
-
- class ListOneBased : public UResourceParser::List {
- public:
- ListOneBased(UResourceParser *inRP);
- virtual ~ListOneBased();
- Boolean Advance();
- protected:
- SInt16 mCount;
- };
-
- class ListUntilEnd : public UResourceParser::List {
- public:
- ListUntilEnd(UResourceParser *inRP);
- virtual ~ListUntilEnd();
- Boolean Advance();
- };
-
-
- //// Data members
-
- Handle mResH;
- Ptr mCursorP;
- Ptr mEndP;
- SInt8 mOrigResState;
-
- };
-
- inline void UResourceParser::AWRD()
- {
- Assert_(mCursorP < mEndP);
- mCursorP += static_cast<long>(mCursorP) & 0x1;
- }
-
- inline void UResourceParser::ALNG()
- {
- Assert_(mCursorP < mEndP);
- mCursorP += (4 - (static_cast<long>(mCursorP) & 0x3));
- }
-
- inline void UResourceParser::FBYT()
- {
- Assert_(mCursorP < mEndP);
- mCursorP += 1;
- }
-
- inline void UResourceParser::FWRD()
- {
- Assert_(mCursorP < mEndP);
- mCursorP += 2;
- }
-
- inline void UResourceParser::FLNG()
- {
- Assert_(mCursorP < mEndP);
- mCursorP += 4;
- }
-
- inline SInt8 UResourceParser::SBYT()
- {
- SInt8 outByte;
- Assert_(mCursorP < mEndP);
- outByte = *static_cast<SInt8 *>(mCursorP);
- mCursorP += sizeof(SInt8);
- return outByte;
- }
-
- inline SInt16 UResourceParser::SWRD()
- {
- SInt16 outWord;
- Assert_(mCursorP < mEndP);
- outWord = *static_cast<SInt16 *>(mCursorP);
- mCursorP += sizeof(SInt16);
- return outWord;
- }
-
- inline SInt32 UResourceParser::SLNG()
- {
- SInt32 outLong;
- Assert_(mCursorP < mEndP);
- outLong = *static_cast<SInt32 *>(mCursorP);
- mCursorP += sizeof(SInt32);
- return outLong;
- }
-
- inline UInt8 UResourceParser::UBYT()
- {
- UInt8 outByte;
- Assert_(mCursorP < mEndP);
- outByte = *static_cast<UInt8 *>(mCursorP);
- mCursorP += sizeof(UInt8);
- return outByte;
- }
-
- inline UInt16 UResourceParser::UWRD()
- {
- UInt16 outWord;
- Assert_(mCursorP < mEndP);
- outWord = *static_cast<UInt16 *>(mCursorP);
- mCursorP += sizeof(UInt16);
- return outWord;
- }
-
- inline UInt32 UResourceParser::ULNG()
- {
- UInt32 outLong;
- Assert_(mCursorP < mEndP);
- outLong = *static_cast<UInt32 *>(mCursorP);
- mCursorP += sizeof(UInt32);
- return outLong;
- }
-
- inline StringPtr UResourceParser::PSTR()
- {
- StringPtr outString;
- Assert_(mCursorP < mEndP);
- outString = static_cast<StringPtr>(mCursorP);
- mCursorP += StrLength(outString) + 1;
- return outString;
- }
-
- #if 0
- inline void UResourceParser::WSTR()
- {
- Assert_(mCursorP < mEndP);
- //###
- }
-
- inline void UResourceParser::LSTR()
- {
- Assert_(mCursorP < mEndP);
- //###
- }
- #endif
-
- inline StringPtr UResourceParser::P0nn(const UInt8 inNN)
- {
- StringPtr outString;
- Assert_(mCursorP < mEndP);
- outString = static_cast<StringPtr>(mCursorP);
- mCursorP += inNN;
- return outString;
- }
-
- inline char* UResourceParser::CSTR()
- {
- char *outString;
- Assert_(mCursorP < mEndP);
- outString = static_cast<char *>(mCursorP);
- mCursorP += strlen(outString) + 1;
- return outString;
- }
-
- inline char* UResourceParser::Cnnn(const UInt16 inNNN)
- {
- char *outString;
- Assert_(mCursorP < mEndP);
- outString = static_cast<char *>(mCursorP);
- mCursorP += inNNN;
- return outString;
- }
-
- inline Ptr UResourceParser::Hnnn(const UInt16 inNNN)
- {
- Ptr outData;
- Assert_(mCursorP < mEndP);
- outData = static_cast<Ptr>(mCursorP);
- mCursorP += inNNN;
- return outData;
- }
-
- inline Ptr UResourceParser::HEXD()
- {
- Ptr outData;
- Assert_(mCursorP < mEndP);
- outData = static_cast<Ptr>(mCursorP);
- mCursorP = mEndP;
- return outData;
- }
-
- inline Boolean UResourceParser::BOOL()
- {
- Boolean outBool;
- Assert_(mCursorP < mEndP);
- outBool = static_cast<SInt16>(mCursorP) != 0;
- mCursorP += 2;
- return outBool;
- }
-
- inline UInt8 UResourceParser::BBIT()
- {
- UInt8 outBits;
- Assert_(mCursorP < mEndP);
- outBits = *static_cast<UInt8 *>(mCursorP);
- mCursorP += sizeof(UInt8);
- return outBits;
- }
-
- inline char UResourceParser::CHAR()
- {
- char outChar;
- Assert_(mCursorP < mEndP);
- outChar = *static_cast<char *>(mCursorP);
- mCursorP += sizeof(char);
- return outChar;
- }
-
- inline Rect* UResourceParser::RECT()
- {
- Rect *outRect;
- Assert_(mCursorP < mEndP);
- outRect = static_cast<Rect *>(mCursorP);
- mCursorP += sizeof(Rect);
- return outRect;
- }
-
- inline OSType UResourceParser::TNAM()
- {
- OSType outOSType;
- Assert_(mCursorP < mEndP);
- outOSType = *static_cast<OSType *>(mCursorP);
- mCursorP += sizeof(OSType);
- return outOSType;
- }
-
-
- inline Boolean UResourceParser::ListUntilZero::Advance()
- {
- return (*static_cast<SInt8 *>(mRP->mCursorP) != 0);
- }
-
- inline Boolean UResourceParser::ListZeroBased::Advance()
- {
- --mCount;
- return (mCount >= -1);
- }
-
- inline Boolean UResourceParser::ListOneBased::Advance()
- {
- --mCount;
- return (mCount >= 0);
- }
-
- inline Boolean UResourceParser::ListUntilEnd::Advance()
- {
- return (mRP->mCursorP < mRP->mEndP);
- }
-