using System.IO; namespace Com.Xagasoft.Gats { public class GatsInteger : GatsObject { public long Value { get; set; } public GatsInteger( long val=0 ) { Value = val; } public override string ToString() { return Value.ToString(); } public override void Read( Stream s, byte cType ) { Value = ReadPackedInt( s ); } public override void Write( Stream s ) { s.WriteByte( (int)'i' ); WritePackedInt( s, Value ); } public static long ReadPackedInt( Stream s ) { int b; long rOut = 0; bool bNeg; b = s.ReadByte(); if( b == -1 ) throw new GatsException("Premature end of stream encountered."); bNeg = (b&0x40) == 0x40; rOut |= ((long)b)&0x3F; int c = 0; while( (b&0x80) == 0x80 ) { b = s.ReadByte(); if( b == -1 ) throw new GatsException("Premature end of stream encountered."); rOut |= (long)(b&0x7F) << (6+7*(c++)); } if( bNeg ) return -rOut; return rOut; } public static void WritePackedInt( Stream s, long iIn ) { byte b; if( iIn < 0 ) { iIn = -iIn; b = (byte)(iIn&0x3F); if( iIn > b ) b |= 0x80 | 0x40; else b |= 0x40; } else { b = (byte)(iIn&0x3F); if( iIn > b ) b |= 0x80; } s.WriteByte( b ); iIn = iIn >> 6; while( iIn > 0 ) { b = (byte)(iIn&0x7F); if( iIn > b ) b |= 0x80; s.WriteByte( b ); iIn = iIn >> 7; } } }; }