From 9d86cba840252b451c4b86d9ea16c821b6c97245 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 14 Nov 2012 23:11:45 +0000 Subject: Started a nice, native .net implementation in C# --- c++-libbu++/src/tests/int.cpp | 5 ++- cs-dotnet/default.bld | 25 ++++++++++++ cs-dotnet/src/gatsexception.cs | 12 ++++++ cs-dotnet/src/gatsinteger.cs | 86 +++++++++++++++++++++++++++++++++++++++++ cs-dotnet/src/gatsinteger.dll | Bin 0 -> 3072 bytes cs-dotnet/src/gatsobject.cs | 10 +++++ cs-dotnet/src/tests/ints.cs | 16 ++++++++ 7 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 cs-dotnet/default.bld create mode 100644 cs-dotnet/src/gatsexception.cs create mode 100644 cs-dotnet/src/gatsinteger.cs create mode 100644 cs-dotnet/src/gatsinteger.dll create mode 100644 cs-dotnet/src/gatsobject.cs create mode 100644 cs-dotnet/src/tests/ints.cs diff --git a/c++-libbu++/src/tests/int.cpp b/c++-libbu++/src/tests/int.cpp index 25ef831..fc411f4 100644 --- a/c++-libbu++/src/tests/int.cpp +++ b/c++-libbu++/src/tests/int.cpp @@ -9,6 +9,7 @@ #include #include +#include #include using namespace Bu; @@ -31,12 +32,12 @@ void hexdump( char *dat, int iSize ) int main( int argc, char *argv[] ) { + Bu::File mb("test.gats", Bu::File::WriteNew ); for( int j = 1; j < argc; j++ ) { int64_t i = strtoll( argv[j], NULL, 10 ); - MemBuf mb; Gats::Integer::writePackedInt( mb, i ); - hexdump( mb.getString().getStr(), mb.getString().getSize() ); +// hexdump( mb.getString().getStr(), mb.getString().getSize() ); } /* sio << "Before: " << i << sio.nl; diff --git a/cs-dotnet/default.bld b/cs-dotnet/default.bld new file mode 100644 index 0000000..4bc324e --- /dev/null +++ b/cs-dotnet/default.bld @@ -0,0 +1,25 @@ + +target "libgats.dll" +{ + input files("src/*.cs"); + + profile "build" + { + execute("mcs -pkg:dotnet -target:library -out:${OUTPUT} ${INPUT}"); + } +} + +for TEST in files("src/tests/*.cs") do +{ + target TEST.replace("src/tests/","").replace(".cs",".exe") + { + input TEST; + requires "libgats.dll"; + + profile "build" + { + execute("mcs -pkg:dotnet -lib:. -r:libgats.dll -out:${OUTPUT} ${INPUT}"); + } + } +} + diff --git a/cs-dotnet/src/gatsexception.cs b/cs-dotnet/src/gatsexception.cs new file mode 100644 index 0000000..ea665d0 --- /dev/null +++ b/cs-dotnet/src/gatsexception.cs @@ -0,0 +1,12 @@ +using System; + +namespace Com.Xagasoft.Gats +{ + public class GatsException : Exception + { + public GatsException( String sName ) : + base( sName ) + { + } + } +} diff --git a/cs-dotnet/src/gatsinteger.cs b/cs-dotnet/src/gatsinteger.cs new file mode 100644 index 0000000..cbb552e --- /dev/null +++ b/cs-dotnet/src/gatsinteger.cs @@ -0,0 +1,86 @@ +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; + } + } + }; +} diff --git a/cs-dotnet/src/gatsinteger.dll b/cs-dotnet/src/gatsinteger.dll new file mode 100644 index 0000000..d3c8e79 Binary files /dev/null and b/cs-dotnet/src/gatsinteger.dll differ diff --git a/cs-dotnet/src/gatsobject.cs b/cs-dotnet/src/gatsobject.cs new file mode 100644 index 0000000..f782f95 --- /dev/null +++ b/cs-dotnet/src/gatsobject.cs @@ -0,0 +1,10 @@ +using System.IO; + +namespace Com.Xagasoft.Gats +{ + public abstract class GatsObject + { + public abstract void Read( Stream s, byte cType ); + public abstract void Write( Stream s ); + } +} diff --git a/cs-dotnet/src/tests/ints.cs b/cs-dotnet/src/tests/ints.cs new file mode 100644 index 0000000..ea97820 --- /dev/null +++ b/cs-dotnet/src/tests/ints.cs @@ -0,0 +1,16 @@ +using System; +using System.IO; +using Com.Xagasoft.Gats; + +class Ints +{ + static void Main() + { + FileStream file = new FileStream("test.gats", FileMode.Open, + FileAccess.Write ); + long iVal = 0xfffffe; + GatsInteger i = new GatsInteger( iVal ); + i.Write( file ); + Console.WriteLine("Read int: " + i.Value ); + } +} -- cgit v1.2.3