2007-07-16 20:16:00 +00:00
|
|
|
/*
|
2007-07-25 18:53:00 +00:00
|
|
|
* $Id: StdAwk.cpp,v 1.3 2007/07/20 09:23:37 bacon Exp $
|
2007-07-16 20:16:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
2007-07-25 18:53:00 +00:00
|
|
|
#include "misc.h"
|
2007-07-16 20:16:00 +00:00
|
|
|
|
|
|
|
#include <ase/net/StdAwk.hpp>
|
2007-07-25 18:53:00 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <tchar.h>
|
|
|
|
#include <vcclr.h>
|
2007-07-16 20:16:00 +00:00
|
|
|
|
|
|
|
namespace ASE
|
|
|
|
{
|
|
|
|
namespace Net
|
|
|
|
{
|
|
|
|
|
2007-07-21 01:21:00 +00:00
|
|
|
StdAwk::StdAwk ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
StdAwk::~StdAwk ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-07-25 18:53:00 +00:00
|
|
|
int StdAwk::OpenFile (File^ file)
|
2007-07-21 01:21:00 +00:00
|
|
|
{
|
|
|
|
System::IO::FileMode mode;
|
|
|
|
System::IO::FileAccess access;
|
|
|
|
System::IO::FileStream^ fs;
|
|
|
|
|
2007-07-25 18:53:00 +00:00
|
|
|
if (file->Mode->Equals(File::MODE::READ))
|
2007-07-21 01:21:00 +00:00
|
|
|
{
|
2007-07-25 18:53:00 +00:00
|
|
|
mode = System::IO::FileMode::Open;
|
|
|
|
access = System::IO::FileAccess::Read;
|
2007-07-21 01:21:00 +00:00
|
|
|
|
2007-07-25 18:53:00 +00:00
|
|
|
fs = gcnew System::IO::FileStream (file->Name, mode, access);
|
|
|
|
System::IO::StreamReader^ rd = gcnew System::IO::StreamReader (fs);
|
|
|
|
file->Handle = rd;
|
|
|
|
}
|
|
|
|
else if (file->Mode->Equals(File::MODE::WRITE))
|
|
|
|
{
|
|
|
|
mode = System::IO::FileMode::Create;
|
|
|
|
access = System::IO::FileAccess::Write;
|
2007-07-21 01:21:00 +00:00
|
|
|
|
2007-07-25 18:53:00 +00:00
|
|
|
fs = gcnew System::IO::FileStream (file->Name, mode, access);
|
|
|
|
System::IO::StreamWriter^ wr = gcnew System::IO::StreamWriter (fs);
|
|
|
|
file->Handle = wr;
|
2007-07-21 01:21:00 +00:00
|
|
|
}
|
2007-07-25 18:53:00 +00:00
|
|
|
else /* File::MODE::APPEND */
|
2007-07-21 01:21:00 +00:00
|
|
|
{
|
2007-07-25 18:53:00 +00:00
|
|
|
mode = System::IO::FileMode::Append;
|
|
|
|
access = System::IO::FileAccess::Write;
|
|
|
|
|
|
|
|
fs = gcnew System::IO::FileStream (file->Name, mode, access);
|
|
|
|
System::IO::StreamWriter^ wr = gcnew System::IO::StreamWriter (fs);
|
|
|
|
file->Handle = wr;
|
2007-07-21 01:21:00 +00:00
|
|
|
}
|
2007-07-25 18:53:00 +00:00
|
|
|
|
|
|
|
return 1;
|
2007-07-21 01:21:00 +00:00
|
|
|
}
|
|
|
|
|
2007-07-25 18:53:00 +00:00
|
|
|
int StdAwk::CloseFile (File^ file)
|
2007-07-21 01:21:00 +00:00
|
|
|
{
|
|
|
|
if (file->Mode == File::MODE::READ)
|
|
|
|
{
|
|
|
|
System::IO::StreamReader^ sr = (System::IO::StreamReader^)file->Handle;
|
|
|
|
sr->Close ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
System::IO::StreamWriter^ sw = (System::IO::StreamWriter^)file->Handle;
|
|
|
|
sw->Close ();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-07-25 18:53:00 +00:00
|
|
|
int StdAwk::ReadFile (File^ file, cli::array<char_t>^ buf, int len)
|
2007-07-21 01:21:00 +00:00
|
|
|
{
|
|
|
|
System::IO::StreamReader^ sr = (System::IO::StreamReader^)file->Handle;
|
2007-07-25 18:53:00 +00:00
|
|
|
return sr->Read (buf, 0, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
int StdAwk::WriteFile (File^ file, cli::array<char_t>^ buf, int len)
|
|
|
|
{
|
|
|
|
System::IO::StreamWriter^ sw = (System::IO::StreamWriter^)file->Handle;
|
|
|
|
sw->Write (buf, 0, len);
|
|
|
|
return len;
|
2007-07-21 01:21:00 +00:00
|
|
|
}
|
|
|
|
|
2007-07-25 18:53:00 +00:00
|
|
|
int StdAwk::FlushFile (File^ file)
|
2007-07-21 01:21:00 +00:00
|
|
|
{
|
|
|
|
System::IO::StreamWriter^ sw = (System::IO::StreamWriter^)file->Handle;
|
2007-07-25 18:53:00 +00:00
|
|
|
sw->Flush ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int StdAwk::OpenPipe (Pipe^ pipe)
|
|
|
|
{
|
|
|
|
FILE* fp = NULL;
|
|
|
|
|
|
|
|
cli::pin_ptr<const wchar_t> name =
|
|
|
|
PtrToStringChars(pipe->Name);
|
|
|
|
|
|
|
|
if (pipe->Mode->Equals(Pipe::MODE::READ))
|
|
|
|
{
|
|
|
|
fp = _wpopen (name, L"r");
|
|
|
|
}
|
|
|
|
else // Pipe::MODE::WRITE
|
2007-07-21 01:21:00 +00:00
|
|
|
{
|
2007-07-25 18:53:00 +00:00
|
|
|
fp = _wpopen (name, L"w");
|
2007-07-21 01:21:00 +00:00
|
|
|
}
|
2007-07-25 18:53:00 +00:00
|
|
|
|
|
|
|
if (fp == NULL) return -1;
|
|
|
|
|
|
|
|
pipe->Handle = IntPtr ((void*)fp);
|
|
|
|
return 1;
|
2007-07-21 01:21:00 +00:00
|
|
|
}
|
|
|
|
|
2007-07-25 18:53:00 +00:00
|
|
|
int StdAwk::ClosePipe (Pipe^ pipe)
|
2007-07-21 01:21:00 +00:00
|
|
|
{
|
2007-07-25 18:53:00 +00:00
|
|
|
IntPtr ip = (IntPtr)pipe->Handle;
|
|
|
|
FILE* fp = (FILE*)ip.ToPointer();
|
|
|
|
return (::_pclose (fp) == EOF)? -1: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int StdAwk::ReadPipe (Pipe^ pipe, cli::array<char_t>^ buf, int len)
|
|
|
|
{
|
|
|
|
IntPtr ip = (IntPtr)pipe->Handle;
|
|
|
|
FILE* fp = (FILE*)ip.ToPointer();
|
|
|
|
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
while (n < len)
|
|
|
|
{
|
|
|
|
wint_t c = fgetwc (fp);
|
|
|
|
if (c == WEOF) break;
|
|
|
|
|
|
|
|
buf[n++] = c;
|
|
|
|
if (c == L'\n') break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
int StdAwk::WritePipe (Pipe^ pipe, cli::array<char_t>^ buf, int len)
|
|
|
|
{
|
|
|
|
IntPtr ip = (IntPtr)pipe->Handle;
|
|
|
|
FILE* fp = (FILE*)ip.ToPointer();
|
|
|
|
int left;
|
|
|
|
|
|
|
|
cli::pin_ptr<char_t> bp = &buf[0];
|
|
|
|
|
|
|
|
/* somehow, fwprintf returns 0 when non-ascii
|
|
|
|
* characters are included in the buffer.
|
|
|
|
while (left > 0)
|
2007-07-21 01:21:00 +00:00
|
|
|
{
|
2007-07-25 18:53:00 +00:00
|
|
|
if (*bp == ASE_T('\0'))
|
|
|
|
{
|
|
|
|
if (fputwc (*ptr, fp) == WEOF)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
left -= 1; bp += 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int n = fwprintf (fp, L"%.*s", left, bp);
|
|
|
|
if (n < 0 || n > left) return -1;
|
|
|
|
left -= n; bp += n;
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
|
|
|
|
/* so the scheme has been changed to the following */
|
|
|
|
char* mbp = unicode_to_multibyte (bp, len, &left);
|
|
|
|
if (mbp == NULL) return -1;
|
|
|
|
|
|
|
|
char* ptr = mbp;
|
|
|
|
while (left > 0)
|
|
|
|
{
|
|
|
|
if (*ptr == '\0')
|
|
|
|
{
|
|
|
|
if (fputwc (*ptr, fp) == WEOF)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
left -= 1; ptr += 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int n = fprintf (fp, "%.*s", left, ptr);
|
|
|
|
if (n < 0 || n > left) return -1;
|
|
|
|
left -= n; ptr += n;
|
|
|
|
}
|
2007-07-21 01:21:00 +00:00
|
|
|
}
|
2007-07-25 18:53:00 +00:00
|
|
|
|
|
|
|
::free (mbp);
|
|
|
|
return len;
|
2007-07-21 01:21:00 +00:00
|
|
|
}
|
|
|
|
|
2007-07-25 18:53:00 +00:00
|
|
|
int StdAwk::FlushPipe (Pipe^ pipe)
|
|
|
|
{
|
|
|
|
IntPtr ip = (IntPtr)pipe->Handle;
|
|
|
|
FILE* fp = (FILE*)ip.ToPointer();
|
|
|
|
return (::fflush (fp) == EOF)? -1: 0;
|
|
|
|
}
|
2007-07-16 20:16:00 +00:00
|
|
|
}
|
2007-07-21 01:21:00 +00:00
|
|
|
}
|