home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Libraries / UResourceParser / UResourceParser.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-14  |  6.7 KB  |  360 lines  |  [TEXT/CWIE]

  1. // =================================================================================
  2. //    Copyright © 1996 Michael Schürig
  3. //
  4. //    You may copy this file, rip it apart or use it in derivative work as long
  5. //    as you don't change the original file and this message remains intact.
  6. // =================================================================================
  7.  
  8. #define Debug_Signal
  9. #define Debug_Throw
  10.  
  11. #include <UException.h>
  12. #include <string.h>
  13.  
  14.  
  15. class UResourceParser {
  16.  
  17.     class ListUntilZero;
  18.     class ListUntilEnd;
  19.     class ListZeroBased;
  20.     class ListOneBased;
  21.  
  22.     friend class UResourceParser::ListUntilZero;
  23.     friend class UResourceParser::ListUntilEnd;
  24.     friend class UResourceParser::ListZeroBased;
  25.     friend class UResourceParser::ListOneBased;
  26.  
  27. public:
  28.  
  29.             UResourceParser(const Handle inResH) throw();
  30.     virtual    ~UResourceParser() throw();
  31.  
  32.     virtual    void Parse() = 0;
  33.  
  34. protected:
  35.  
  36.     //// Atomic types
  37.  
  38.     // Alignment
  39.     inline void            AWRD();
  40.     inline void            ALNG();
  41.     
  42.     // Fillers
  43.     inline void            FBYT();
  44.     inline void            FWRD();
  45.     inline void            FLNG();
  46.  
  47.     // Integers
  48.     inline SInt8        SBYT();
  49.     inline SInt16        SWRD();
  50.     inline SInt32        SLNG();
  51.     inline UInt8        UBYT();
  52.     inline UInt16        UWRD();
  53.     inline UInt32        ULNG();
  54.  
  55.     // Pascal strings
  56.     inline StringPtr    PSTR();
  57. #if 0 //###
  58.     inline void    WSTR();
  59.     inline void    LSTR();
  60. #endif
  61.     inline StringPtr    P0nn(const UInt8 inNN);
  62.  
  63.     // C strings
  64.     inline char*        CSTR();
  65.     inline char*        Cnnn(const UInt16 inNNN);
  66.     
  67.     // Hex dump
  68.     inline Ptr            Hnnn(const UInt16 inNNN);
  69.     inline Ptr            HEXD();
  70.     
  71.     // Boolean
  72.     inline Boolean        BOOL();
  73. #if 0
  74.     inline void            BBIT(BitFieldT &outBits);
  75. #endif
  76.     inline UInt8        BBIT();
  77.         
  78.     // Character
  79.     inline char            CHAR();
  80.     
  81.     // Rectangle
  82.     inline Rect*        RECT();
  83.     
  84.     // OSType
  85.     inline OSType        TNAM();
  86.  
  87.     
  88.     //// Lists
  89.  
  90.     class List {
  91.     public:
  92.                         List(UResourceParser *inRP);
  93.         virtual            ~List();
  94.         virtual Boolean    Advance() = 0;
  95.     protected:
  96.         UResourceParser    *mRP;
  97.     };
  98.  
  99.     class ListUntilZero : public UResourceParser::List {
  100.     public:
  101.                         ListUntilZero(UResourceParser *inRP);
  102.         virtual            ~ListUntilZero();
  103.         Boolean    Advance();
  104.     };
  105.  
  106.     class ListZeroBased : public UResourceParser::List {
  107.     public:
  108.                         ListZeroBased(UResourceParser *inRP);
  109.         virtual            ~ListZeroBased();
  110.         Boolean    Advance();
  111.     protected:
  112.         SInt16    mCount;
  113.     };
  114.  
  115.     class ListOneBased : public UResourceParser::List {
  116.     public:
  117.                         ListOneBased(UResourceParser *inRP);
  118.         virtual            ~ListOneBased();
  119.         Boolean    Advance();
  120.     protected:
  121.         SInt16    mCount;
  122.     };
  123.  
  124.     class ListUntilEnd : public UResourceParser::List {
  125.     public:
  126.                         ListUntilEnd(UResourceParser *inRP);
  127.         virtual            ~ListUntilEnd();
  128.         Boolean    Advance();
  129.     };    
  130.     
  131.     
  132.     //// Data members
  133.         
  134.     Handle    mResH;
  135.     Ptr        mCursorP;
  136.     Ptr        mEndP;
  137.     SInt8    mOrigResState;
  138.  
  139. };
  140.  
  141. inline void UResourceParser::AWRD()
  142. {
  143.     Assert_(mCursorP < mEndP);
  144.     mCursorP += static_cast<long>(mCursorP) & 0x1;
  145. }
  146.  
  147. inline void UResourceParser::ALNG()
  148. {
  149.     Assert_(mCursorP < mEndP);
  150.     mCursorP += (4 - (static_cast<long>(mCursorP) & 0x3));
  151. }
  152.  
  153. inline void UResourceParser::FBYT()
  154. {
  155.     Assert_(mCursorP < mEndP);
  156.     mCursorP += 1;
  157. }
  158.  
  159. inline void UResourceParser::FWRD()
  160. {
  161.     Assert_(mCursorP < mEndP);
  162.     mCursorP += 2;
  163. }
  164.  
  165. inline void UResourceParser::FLNG()
  166. {
  167.     Assert_(mCursorP < mEndP);
  168.     mCursorP += 4;
  169. }
  170.  
  171. inline SInt8 UResourceParser::SBYT()
  172. {
  173.     SInt8    outByte;
  174.     Assert_(mCursorP < mEndP);
  175.     outByte = *static_cast<SInt8 *>(mCursorP);
  176.     mCursorP += sizeof(SInt8);
  177.     return outByte;
  178. }
  179.  
  180. inline SInt16 UResourceParser::SWRD()
  181. {
  182.     SInt16    outWord;
  183.     Assert_(mCursorP < mEndP);
  184.     outWord = *static_cast<SInt16 *>(mCursorP);
  185.     mCursorP += sizeof(SInt16);
  186.     return outWord;
  187. }
  188.  
  189. inline SInt32 UResourceParser::SLNG()
  190. {
  191.     SInt32    outLong;
  192.     Assert_(mCursorP < mEndP);
  193.     outLong = *static_cast<SInt32 *>(mCursorP);
  194.     mCursorP += sizeof(SInt32);
  195.     return outLong;
  196. }
  197.  
  198. inline UInt8 UResourceParser::UBYT()
  199. {
  200.     UInt8    outByte;
  201.     Assert_(mCursorP < mEndP);
  202.     outByte = *static_cast<UInt8 *>(mCursorP);
  203.     mCursorP += sizeof(UInt8);
  204.     return outByte;
  205. }
  206.  
  207. inline UInt16 UResourceParser::UWRD()
  208. {
  209.     UInt16    outWord;
  210.     Assert_(mCursorP < mEndP);
  211.     outWord = *static_cast<UInt16 *>(mCursorP);
  212.     mCursorP += sizeof(UInt16);
  213.     return outWord;
  214. }
  215.  
  216. inline UInt32 UResourceParser::ULNG()
  217. {
  218.     UInt32    outLong;
  219.     Assert_(mCursorP < mEndP);
  220.     outLong = *static_cast<UInt32 *>(mCursorP);
  221.     mCursorP += sizeof(UInt32);
  222.     return outLong;
  223. }
  224.  
  225. inline StringPtr UResourceParser::PSTR()
  226. {
  227.     StringPtr    outString;
  228.     Assert_(mCursorP < mEndP);
  229.     outString = static_cast<StringPtr>(mCursorP);
  230.     mCursorP += StrLength(outString) + 1;
  231.     return outString;
  232. }
  233.  
  234. #if 0
  235. inline void    UResourceParser::WSTR()
  236. {
  237.     Assert_(mCursorP < mEndP);
  238.     //###
  239. }
  240.  
  241. inline void    UResourceParser::LSTR()
  242. {
  243.     Assert_(mCursorP < mEndP);
  244.     //###
  245. }
  246. #endif
  247.  
  248. inline StringPtr UResourceParser::P0nn(const UInt8 inNN)
  249. {
  250.     StringPtr    outString;
  251.     Assert_(mCursorP < mEndP);
  252.     outString = static_cast<StringPtr>(mCursorP);
  253.     mCursorP += inNN;
  254.     return outString;
  255. }
  256.  
  257. inline char* UResourceParser::CSTR()
  258. {
  259.     char    *outString;
  260.     Assert_(mCursorP < mEndP);
  261.     outString = static_cast<char *>(mCursorP);
  262.     mCursorP += strlen(outString) + 1;
  263.     return outString;
  264. }
  265.  
  266. inline char* UResourceParser::Cnnn(const UInt16 inNNN)
  267. {
  268.     char    *outString;
  269.     Assert_(mCursorP < mEndP);
  270.     outString = static_cast<char *>(mCursorP);
  271.     mCursorP += inNNN;
  272.     return outString;
  273. }
  274.  
  275. inline Ptr UResourceParser::Hnnn(const UInt16 inNNN)
  276. {
  277.     Ptr outData;
  278.     Assert_(mCursorP < mEndP);
  279.     outData = static_cast<Ptr>(mCursorP);
  280.     mCursorP += inNNN;
  281.     return outData;
  282. }
  283.  
  284. inline Ptr UResourceParser::HEXD()
  285. {
  286.     Ptr outData;
  287.     Assert_(mCursorP < mEndP);
  288.     outData = static_cast<Ptr>(mCursorP);
  289.     mCursorP = mEndP;
  290.     return outData;
  291. }
  292.  
  293. inline Boolean UResourceParser::BOOL()
  294. {
  295.     Boolean    outBool;
  296.     Assert_(mCursorP < mEndP);
  297.     outBool = static_cast<SInt16>(mCursorP) != 0;
  298.     mCursorP += 2;
  299.     return outBool;
  300. }
  301.  
  302. inline UInt8 UResourceParser::BBIT()
  303. {
  304.     UInt8    outBits;
  305.     Assert_(mCursorP < mEndP);
  306.     outBits = *static_cast<UInt8 *>(mCursorP);
  307.     mCursorP += sizeof(UInt8);
  308.     return outBits;
  309. }
  310.  
  311. inline char    UResourceParser::CHAR()
  312. {
  313.     char outChar;
  314.     Assert_(mCursorP < mEndP);
  315.     outChar = *static_cast<char *>(mCursorP);
  316.     mCursorP += sizeof(char);
  317.     return outChar;
  318. }
  319.  
  320. inline Rect* UResourceParser::RECT()
  321. {
  322.     Rect    *outRect;
  323.     Assert_(mCursorP < mEndP);
  324.     outRect = static_cast<Rect *>(mCursorP);
  325.     mCursorP += sizeof(Rect);
  326.     return outRect;
  327. }
  328.  
  329. inline OSType UResourceParser::TNAM()
  330. {
  331.     OSType outOSType;
  332.     Assert_(mCursorP < mEndP);
  333.     outOSType = *static_cast<OSType *>(mCursorP);
  334.     mCursorP += sizeof(OSType);
  335.     return outOSType;
  336. }
  337.  
  338.  
  339. inline Boolean UResourceParser::ListUntilZero::Advance()
  340. {
  341.     return (*static_cast<SInt8 *>(mRP->mCursorP) != 0);
  342. }
  343.  
  344. inline Boolean UResourceParser::ListZeroBased::Advance()
  345. {
  346.     --mCount;
  347.     return (mCount >= -1);
  348. }
  349.  
  350. inline Boolean UResourceParser::ListOneBased::Advance()
  351. {
  352.     --mCount;
  353.     return (mCount >= 0);
  354. }
  355.  
  356. inline Boolean UResourceParser::ListUntilEnd::Advance()
  357. {
  358.     return (mRP->mCursorP < mRP->mEndP);
  359. }
  360.