UCStack.h

Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2002-2004 The Pentagram team
00003 
00004 This program is free software; you can redistribute it and/or
00005 modify it under the terms of the GNU General Public License
00006 as published by the Free Software Foundation; either version 2
00007 of the License, or (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 */
00018 
00019 #ifndef BaseUCStack_H
00020 #define BaseUCStack_H
00021 
00022 class IDataSource;
00023 class ODataSource;
00024 
00025 // A little-endian stack for use with usecode
00026 class BaseUCStack
00027 {
00028 protected:
00029         uint8* buf;
00030         uint8* buf_ptr;
00031         uint32 size;
00032 public:
00033 
00034         BaseUCStack(uint32 len, uint8 *b) : buf(b), size(len)
00035         {
00036                 // stack grows downward, so start at the end of the buffer
00037                 buf_ptr = buf + size;
00038         }
00039         virtual ~BaseUCStack() { }
00040 
00041         inline uint32 getSize() const {
00042                 return size;
00043         }
00044 
00045         inline uint32 stacksize() const {
00046                 return size - (buf_ptr - buf);
00047         }
00048 
00049         inline void addSP(const sint32 offset) {
00050                 buf_ptr += offset;
00051         }
00052 
00053         inline unsigned int getSP() const {
00054                 return static_cast<unsigned int>(buf_ptr - buf);
00055         }
00056 
00057         inline void setSP(unsigned int pos) {
00058                 buf_ptr = buf + pos;
00059         }
00060 
00061         //
00062         // Push values to the stack
00063         //
00064 
00065         inline void push1(uint8 val) {
00066                 buf_ptr--;
00067                 buf_ptr[0] = val;
00068         }
00069         
00070         inline void push2(uint16 val) {
00071                 buf_ptr-=2;
00072                 buf_ptr[0] = static_cast<uint8>( val     & 0xFF);
00073                 buf_ptr[1] = static_cast<uint8>((val>>8) & 0xFF);
00074         }
00075         inline void push4(uint32 val) {
00076                 buf_ptr-=4;
00077                 buf_ptr[0] = static_cast<uint8>( val      & 0xFF);
00078                 buf_ptr[1] = static_cast<uint8>((val>>8)  & 0xFF);
00079                 buf_ptr[2] = static_cast<uint8>((val>>16) & 0xFF);
00080                 buf_ptr[3] = static_cast<uint8>((val>>24) & 0xFF);
00081         }
00082         // Push an arbitrary number of bytes of 0
00083         inline void push0(const uint32 size) { 
00084                 buf_ptr -= size;
00085                 std::memset (buf_ptr, 0, size);
00086         }
00087         // Push an arbitrary number of bytes
00088         inline void push(const uint8 *in, const uint32 size) { 
00089                 buf_ptr -= size;
00090                 std::memcpy (buf_ptr, in, size);
00091         }
00092 
00093         //
00094         // Pop values from the stack
00095         //
00096 
00097         inline uint16 pop2() {
00098                 uint8 b0, b1;
00099                 b0 = *buf_ptr++;
00100                 b1 = *buf_ptr++;
00101                 return (b0 | (b1 << 8));
00102         }
00103         inline uint32 pop4() {
00104                 uint8 b0, b1, b2, b3;
00105                 b0 = *buf_ptr++;
00106                 b1 = *buf_ptr++;
00107                 b2 = *buf_ptr++;
00108                 b3 = *buf_ptr++;
00109                 return (b0 | (b1<<8) | (b2<<16) | (b3<<24));
00110         }
00111         inline void pop(uint8 *out, const uint32 size) {
00112                 std::memcpy(out, buf_ptr, size);
00113                 buf_ptr += size;
00114         }
00115 
00116         //
00117         // Access a value from a location in the stacck
00118         //
00119 
00120         inline uint8 access1(const uint32 offset) const {
00121                 return buf[offset];
00122         }
00123         inline uint16 access2(const uint32 offset) const {
00124                 return (buf[offset] | (buf[offset+1] << 8));
00125         }
00126         inline uint32 access4(const uint32 offset) const {
00127                 return buf[offset] | (buf[offset+1]<<8) |
00128                         (buf[offset+2]<<16) | (buf[offset+3]<<24);
00129         }
00130         inline uint8* access(const uint32 offset) {             
00131                 return buf+offset;
00132         }
00133         inline uint8* access() {
00134                 return buf_ptr;
00135         }
00136 
00137         //
00138         // Assign a value to a location in the stack
00139         //
00140 
00141         inline void assign1(const uint32 offset, const uint8 val) {
00142                 const_cast<uint8*>(buf)[offset]   = static_cast<uint8>( val     & 0xFF);
00143         }
00144         inline void assign2(const uint32 offset, const uint16 val) {
00145                 const_cast<uint8*>(buf)[offset]   = static_cast<uint8>( val     & 0xFF);
00146                 const_cast<uint8*>(buf)[offset+1] = static_cast<uint8>((val>>8) & 0xFF);
00147         }
00148         inline void assign4(const uint32 offset, const uint32 val) {
00149                 const_cast<uint8*>(buf)[offset]   = static_cast<uint8>( val      & 0xFF);
00150                 const_cast<uint8*>(buf)[offset+1] = static_cast<uint8>((val>>8)  & 0xFF);
00151                 const_cast<uint8*>(buf)[offset+2] = static_cast<uint8>((val>>16) & 0xFF);
00152                 const_cast<uint8*>(buf)[offset+3] = static_cast<uint8>((val>>24) & 0xFF);
00153         }
00154         inline void assign(const uint32 offset, const uint8 *in, const uint32 len)
00155         {
00156                 std::memcpy (const_cast<uint8*>(buf)+offset, in, len);
00157         }
00158 };
00159 
00160 class DynamicUCStack : public BaseUCStack
00161 {
00162 public:
00163         DynamicUCStack(uint32 len=0x1000) : BaseUCStack(len, new uint8[len]) { }
00164         virtual ~DynamicUCStack() { delete [] buf; }
00165 
00166 #ifdef USE_DYNAMIC_UCSTACK
00167 #define UCStack DynamicUCStack
00168         void save(ODataSource* ods);
00169         bool load(IDataSource* ids, uint32 version);
00170 #endif
00171 };
00172 
00173 #ifndef USE_DYNAMIC_UCSTACK
00174 class UCStack : public BaseUCStack
00175 {
00176         uint8   buf_array[0x1000];
00177 public:
00178         UCStack() : BaseUCStack(0x1000, buf_array) { }
00179         virtual ~UCStack() { }
00180 
00181         void save(ODataSource* ods);
00182         bool load(IDataSource* ids, uint32 version);
00183 };
00184 #endif
00185 
00186 #endif

Generated on Fri Jul 27 22:27:50 2007 for pentagram by  doxygen 1.4.7