diff --git a/ase/net/Awk.cpp b/ase/net/Awk.cpp index 8b657a67..c498307b 100644 --- a/ase/net/Awk.cpp +++ b/ase/net/Awk.cpp @@ -1,6 +1,6 @@ /* -* $Id: Awk.cpp,v 1.3 2007/07/15 16:31:59 bacon Exp $ -*/ + * $Id: Awk.cpp,v 1.5 2007/07/16 11:16:46 bacon Exp $ + */ #include "stdafx.h" #include "Awk.hpp" @@ -17,6 +17,7 @@ namespace ASE class StubAwk: public Awk { public: + StubAwk (Net::Awk^ wrapper): wrapper(wrapper) { } @@ -31,39 +32,101 @@ namespace ASE int openSource (Source& io) { - /* - Net::Awk::Source^ nio = gcnew Net::Awk::Source (); - int n = wrapper->OpenSource (nio); - return n; - */ if (io.getMode() == Source::READ) { - //wrapper->SourceInputStream->BeginRead (); + if (wrapper->SourceInputStream == nullptr) + { + return -1; + } + + if (!wrapper->SourceInputStream->CanRead) + { + wrapper->SourceInputStream->Close (); + return -1; + } + + //interior_ptr p = wrapper->SourceInputStream; + //io.setHandle (wrapper->SourceInputStream); + + System::IO::StreamReader^ reader = gcnew System::IO::StreamReader (wrapper->SourceInputStream); + System::Runtime::InteropServices::GCHandle* handle = (System::Runtime::InteropServices::GCHandle*)malloc (sizeof(System::Runtime::InteropServices::GCHandle)); + if (handle == NULL) + { + reader->Close (); + return -1; + } + + handle->Alloc (reader/*, System::Runtime::InteropServices::GCHandleType::Pinned */); + io.setHandle (handle); } else { - //wrapper->SourceOutputStream->BeginWrite (); + if (wrapper->SourceOutputStream == nullptr) + { + return -1; + } + + if (!wrapper->SourceOutputStream->CanWrite) + { + wrapper->SourceOutputStream->Close (); + return -1; + } + + System::IO::StreamWriter^ writer = gcnew System::IO::StreamWriter (wrapper->SourceOutputStream); + System::Runtime::InteropServices::GCHandle* handle = (System::Runtime::InteropServices::GCHandle*)::malloc (sizeof(System::Runtime::InteropServices::GCHandle)); + if (handle == NULL) + { + writer->Close (); + return -1; + } + + handle->Alloc (writer/*, System::Runtime::InteropServices::GCHandleType::Pinned*/); + io.setHandle (handle); } + return 1; } int closeSource (Source& io) { - //System::IO::Stream^ stream = io.getHandle(); - //stream->Close (); + System::Runtime::InteropServices::GCHandle* handle = (System::Runtime::InteropServices::GCHandle*)io.getHandle(); + + if (io.getMode() == Source::READ) + { + System::IO::StreamReader^ reader = (System::IO::StreamReader^)handle->Target; + reader->Close (); + } + else + { + System::IO::StreamWriter^ writer = (System::IO::StreamWriter^)handle->Target; + writer->Close (); + } + + handle->Free (); + ::free (handle); return 0; } ssize_t readSource (Source& io, char_t* buf, size_t len) { - //System::IO::Stream^ stream = io.getHandle(); - return 0; + System::Runtime::InteropServices::GCHandle* handle = (System::Runtime::InteropServices::GCHandle*)io.getHandle(); + System::IO::StreamReader^ reader = (System::IO::StreamReader^)handle->Target; + + cli::array^ b = gcnew cli::array(len); + int n = reader->Read (b, 0, len); + for (int i = 0; i < n; i++) buf[i] = b[i]; + return n; } ssize_t writeSource (Source& io, char_t* buf, size_t len) { - //System::IO::Stream^ stream = io.getHandle(); - return 0; + System::Runtime::InteropServices::GCHandle* handle = (System::Runtime::InteropServices::GCHandle*)io.getHandle(); + System::IO::StreamWriter^ writer = (System::IO::StreamWriter^)handle->Target; + + cli::array^ b = gcnew cli::array(len); + for (int i = 0; i < (int)len; i++) buf[i] = b[i]; + writer->Write (b, 0, len); + return len; } int openPipe (Pipe& io) {return 0; } @@ -129,13 +192,24 @@ namespace ASE Awk::Awk () { awk = new ASE::StubAwk (this); + if (awk->open () == -1) + { + // TODO:... + //throw new AwkException ("cannot open awk"); + } } Awk::~Awk () { + Close (); delete awk; } + void Awk::Close () + { + awk->close (); + } + bool Awk::Parse () { return awk->parse () == 0; diff --git a/ase/net/Awk.hpp b/ase/net/Awk.hpp index cb62f9c3..cf4e5c84 100644 --- a/ase/net/Awk.hpp +++ b/ase/net/Awk.hpp @@ -1,5 +1,5 @@ /* - * $Id: Awk.hpp,v 1.2 2007/07/15 16:31:59 bacon Exp $ + * $Id: Awk.hpp,v 1.3 2007/07/16 11:12:12 bacon Exp $ */ #pragma once @@ -16,6 +16,7 @@ namespace ASE public ref class Awk abstract { public: + /* ref class Source { public: @@ -33,7 +34,7 @@ namespace ASE private: MODE^ mode; - }; + };*/ ref class Extio { @@ -101,6 +102,9 @@ namespace ASE Awk (); virtual ~Awk (); + bool Open (); + void Close (); + bool Parse (); bool Run (); @@ -109,10 +113,6 @@ namespace ASE bool AddFunction (System::String^ name, int minArgs, int maxArgs, FunctionHandler^ handler); bool DeleteFunction (System::String^ name); - virtual int OpenSource (Source^ io) = 0; - virtual int CloseSource (Source^ io) = 0; - virtual int ReadSource (Source^ io, ASE::Awk::char_t* buf, ASE::Awk::size_t len) = 0; - virtual int WriteSource (Source^ io, ASE::Awk::char_t* buf, ASE::Awk::size_t len) = 0; property System::IO::Stream^ SourceInputStream { @@ -142,6 +142,7 @@ namespace ASE protected: ASE::Awk* awk; + System::IO::Stream^ sourceInputStream; System::IO::Stream^ sourceOutputStream; diff --git a/ase/net/StdAwk.hpp b/ase/net/StdAwk.hpp index aab5b87c..58a73f35 100644 --- a/ase/net/StdAwk.hpp +++ b/ase/net/StdAwk.hpp @@ -1,5 +1,5 @@ /* - * $Id: StdAwk.hpp,v 1.1 2007/07/15 16:31:59 bacon Exp $ + * $Id: StdAwk.hpp,v 1.2 2007/07/16 11:12:12 bacon Exp $ */ #include @@ -8,9 +8,11 @@ namespace ASE { namespace Net { + public ref class StdAwk: Awk { public: }; + } } \ No newline at end of file diff --git a/ase/net/assert.cpp b/ase/net/assert.cpp index bdf44ca1..19ca8dcb 100644 --- a/ase/net/assert.cpp +++ b/ase/net/assert.cpp @@ -1,5 +1,5 @@ /* - * $Id: assert.cpp,v 1.1 2007/07/15 16:31:59 bacon Exp $ + * $Id: assert.cpp,v 1.2 2007/07/16 11:12:12 bacon Exp $ */ #include "stdafx.h" @@ -12,6 +12,9 @@ #include #include +#pragma warning(disable:4996) +#pragma unmanaged + void ase_assert_abort (void) { ::abort (); diff --git a/ase/test/net/Form1.cs b/ase/test/net/Form1.cs index de8a6f0d..05f0ef97 100644 --- a/ase/test/net/Form1.cs +++ b/ase/test/net/Form1.cs @@ -17,7 +17,13 @@ namespace asetestnet private void button1_Click(object sender, EventArgs e) { - ASE.Net.Awk awk = new ASE.Net.Awk(); + ASE.Net.Awk awk = new ASE.Net.StdAwk(); + + awk.Open(); + awk.SourceInputStream = new System.IO.FileStream("t.awk", System.IO.FileMode.Open, System.IO.FileAccess.Read); + awk.SourceOutputStream = new System.IO.FileStream("t.out", System.IO.FileMode.Create, System.IO.FileAccess.Write); + + awk.Parse(); } } } \ No newline at end of file diff --git a/ase/test/net/asetestnet.csproj b/ase/test/net/asetestnet.csproj index 703e77de..0a013c0f 100644 --- a/ase/test/net/asetestnet.csproj +++ b/ase/test/net/asetestnet.csproj @@ -14,7 +14,7 @@ true full false - bin\Debug\ + ..\..\Debug\bin\ DEBUG;TRACE prompt 4 @@ -22,7 +22,7 @@ pdbonly true - bin\Release\ + ..\..\Release\bin\ TRACE prompt 4