//
// Copyright 2010 Jacob Gladish. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY Jacob Gladish ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are those of the
// authors and should not be interpreted as representing official policies, either expressed
// or implied, of <copyright holder>.
//
#include "StdAfx.h"
#using <mscorlib.dll>
#include "clientapi_m.h"
using namespace System::Runtime::InteropServices;
p4dn::ClientApi::ClientApi()
{
_clientApi = new ::ClientApi();
_keepAliveDelegate = NULL;
}
p4dn::ClientApi::~ClientApi()
{
delete _clientApi;
delete _keepAliveDelegate;
}
void p4dn::ClientApi::SetTrans( int output, int content, int fnames, int dialog )
{
_clientApi->SetTrans( output, content, fnames, dialog );
}
void p4dn::ClientApi::SetProtocol( String *p, String *v )
{
char* str_p = (char *)(void *) Marshal::StringToHGlobalAnsi( p );
char* str_v = (char *)(void *) Marshal::StringToHGlobalAnsi( v );
_clientApi->SetProtocol( str_p, str_v );
Marshal::FreeHGlobal( str_p );
Marshal::FreeHGlobal( str_v );
}
void p4dn::ClientApi::SetArgv( String* args[] )
{
char** argsArray = new char* [ args->Length ];
for (int i = 0; i < args->Length; ++i) {
argsArray[i] = (char *)(void *) Marshal::StringToHGlobalAnsi( args[i] );
}
_clientApi->SetArgv( args->Length, argsArray );
for (int i = 0; i < args->Length; ++i) {
Marshal::FreeHGlobal( argsArray[i] );
}
}
void p4dn::ClientApi::SetProtocolV( String* p )
{
char* str_p = (char *)(void *) Marshal::StringToHGlobalAnsi( p );
_clientApi->SetProtocolV( str_p );
Marshal::FreeHGlobal( str_p );
}
String* p4dn::ClientApi::GetProtocol( String *v )
{
char* str_p = (char *)(void *) Marshal::StringToHGlobalAnsi( v );
StrPtr* ptr = _clientApi->GetProtocol( str_p );
String* s = ptr->Text();
delete ptr;
Marshal::FreeHGlobal( str_p );
return s;
}
void p4dn::ClientApi::Init( p4dn::Error* e )
{
_clientApi->Init( e->get_InternalError() );
}
void p4dn::ClientApi::Run( String* func, p4dn::ClientUser *ui )
{
// System::Console::WriteLine( "Running command {0}", func );
char* f = (char *)(void *) Marshal::StringToHGlobalAnsi( func );
_clientApi->Run( f, ui->_clientUserDelegate );
Marshal::FreeHGlobal( f );
}
int p4dn::ClientApi::Final( p4dn::Error *e )
{
return _clientApi->Final( e->get_InternalError() );
}
int p4dn::ClientApi::Dropped()
{
return _clientApi->Dropped();
}
void p4dn::ClientApi::RunTag( String* func, p4dn::ClientUser *ui )
{
char* f = (char *)(void *) Marshal::StringToHGlobalAnsi( func );
_clientApi->RunTag( f, ui->_clientUserDelegate );
Marshal::FreeHGlobal( f );
}
void p4dn::ClientApi::WaitTag( p4dn::ClientUser *ui )
{
_clientApi->WaitTag( ui->_clientUserDelegate );
}
void p4dn::ClientApi::SetCharset( String* c )
{
char* f = (char *)(void *) Marshal::StringToHGlobalAnsi( c );
_clientApi->SetCharset( f );
Marshal::FreeHGlobal( f );
}
void p4dn::ClientApi::SetClient( String* c )
{
char* f = (char *)(void *) Marshal::StringToHGlobalAnsi( c );
_clientApi->SetClient( f );
Marshal::FreeHGlobal( f );
}
void p4dn::ClientApi::SetCwd( String* c )
{
char* f = (char *)(void *) Marshal::StringToHGlobalAnsi( c );
_clientApi->SetCwd( f );
Marshal::FreeHGlobal( f );
}
void p4dn::ClientApi::SetHost( String* c )
{
char* f = (char *)(void *) Marshal::StringToHGlobalAnsi( c );
_clientApi->SetHost( f );
Marshal::FreeHGlobal( f );
}
void p4dn::ClientApi::SetLanguage( String* c )
{
char* f = (char *)(void *) Marshal::StringToHGlobalAnsi( c );
_clientApi->SetLanguage( f );
Marshal::FreeHGlobal( f );
}
void p4dn::ClientApi::SetPassword( String* c )
{
char* f = (char *)(void *) Marshal::StringToHGlobalAnsi( c );
_clientApi->SetPassword( f );
Marshal::FreeHGlobal( f );
}
void p4dn::ClientApi::SetPort( String* c )
{
char* f = (char *)(void *) Marshal::StringToHGlobalAnsi( c );
_clientApi->SetPort( f );
Marshal::FreeHGlobal( f );
}
void p4dn::ClientApi::SetUser( String* c )
{
char* f = (char *)(void *) Marshal::StringToHGlobalAnsi( c );
_clientApi->SetUser( f );
Marshal::FreeHGlobal( f );
}
void p4dn::ClientApi::SetBreak( p4dn::KeepAlive *keepAlive )
{
if (_keepAliveDelegate != NULL) {
return;
}
// get pinned handle and stach it away.
GCHandle h =GCHandle::Alloc( keepAlive );
IntPtr ptr = GCHandle::op_Explicit( h );
int handle = ptr.ToInt32();
_keepAliveDelegate = new KeepAliveDelegate( handle );
}
void p4dn::ClientApi::DefineCharset( String* c, p4dn::Error* e )
{
char* f = (char *)(void *) Marshal::StringToHGlobalAnsi( c );
_clientApi->DefineCharset( f, e->get_InternalError() );
Marshal::FreeHGlobal( f );
}
void p4dn::ClientApi::DefineClient( String* c, p4dn::Error* e )
{
char* f = (char *)(void *) Marshal::StringToHGlobalAnsi( c );
_clientApi->DefineClient( f, e->get_InternalError() );
Marshal::FreeHGlobal( f );
}
void p4dn::ClientApi::DefineHost( String* c, p4dn::Error* e )
{
char* f = (char *)(void *) Marshal::StringToHGlobalAnsi( c );
_clientApi->DefineHost( f, e->get_InternalError() );
Marshal::FreeHGlobal( f );
}
void p4dn::ClientApi::DefineLanguage( String* c, p4dn::Error* e )
{
char* f = (char *)(void *) Marshal::StringToHGlobalAnsi( c );
_clientApi->DefineLanguage( f, e->get_InternalError() );
Marshal::FreeHGlobal( f );
}
void p4dn::ClientApi::DefinePassword( String* c, p4dn::Error* e )
{
char* f = (char *)(void *) Marshal::StringToHGlobalAnsi( c );
_clientApi->DefinePassword( f, e->get_InternalError() );
Marshal::FreeHGlobal( f );
}
void p4dn::ClientApi::DefinePort( String* c, p4dn::Error* e )
{
char* f = (char *)(void *) Marshal::StringToHGlobalAnsi( c );
_clientApi->DefinePort( f, e->get_InternalError() );
Marshal::FreeHGlobal( f );
}
void p4dn::ClientApi::DefineUser( String* c, p4dn::Error* e )
{
char* f = (char *)(void *) Marshal::StringToHGlobalAnsi( c );
_clientApi->DefineUser( f, e->get_InternalError() );
Marshal::FreeHGlobal( f );
}