#using <mscorlib.dll> #include "StdAfx.h" #include "ClientUserDelegate.h" using namespace System::Runtime::InteropServices; ClientUserDelegate::ClientUserDelegate( gcroot<p4dn::ClientUser*> ManagedClientUser ) { mcu = ManagedClientUser; m_SaveForm = false; } ClientUserDelegate::~ClientUserDelegate() { delete mcu; } void ClientUserDelegate::SaveForm() { m_SaveForm = true; } void ClientUserDelegate::InputData( StrBuf *strbuf, ::Error* err ) { //Build a System::String to pass to DotNet System::String* s = strbuf->Text(); p4dn::Error* e = new p4dn::Error( err ); if (m_SaveForm) { // Managed type declarations StringDictionary* sd; System::String* mSpecDef, *mSpecData; System::IntPtr pKey, pVal, pSpecDef,pSpecData; // Unmanaged types const char *sKey, *sVal, *sSpecDef, *sSpecData; //p4api types StrDict* varList; StrBuf specText; ::Error parseErr; ::Spec* cspec; ::SpecDataTable* specData; mcu->InputForm(&sd, e); // Convert the StringDictionary to a StrDict, build a spec // and save if (!sd->ContainsKey("parseforms-specdef")) { //Throw an error... invalid form } if (!sd->ContainsKey("parseforms-specdata")) { //Throw an error... invalid form } //Fetch the specdata and specdef fields and convert to c-strings mSpecDef = sd->get_Item("parseforms-specdef"); sd->Remove("parseforms-specdef"); pSpecDef = Marshal::StringToHGlobalAnsi(mSpecDef); sSpecDef = static_cast<const char *>( pSpecDef.ToPointer() ); mSpecData = sd->get_Item("parseforms-specdata"); sd->Remove("parseforms-specdata"); pSpecData = Marshal::StringToHGlobalAnsi(mSpecData); sSpecData = static_cast<const char *>( pSpecData.ToPointer() ); //Now, build the spec object and get the dictionary cspec = new Spec(sSpecDef, ""); specData = new SpecDataTable(); cspec->Parse(sSpecData, specData, &parseErr); varList = specData->Dict(); int i = 0; ::StrRef var, val; while (varList->GetVar(i,var,val) != -0) { pVal = Marshal::StringToHGlobalAnsi( sd->get_Item(var.Text())); sVal = static_cast<const char *>( pVal.ToPointer() ); if(sd->ContainsKey(var.Text())) { varList->ReplaceVar(var.Text(), sVal); } Marshal::FreeHGlobal(pVal); i++; } cspec->Format(specData, &specText); strbuf->Set(specText.Text()); delete cspec; delete specData; Marshal::FreeHGlobal(pSpecDef); Marshal::FreeHGlobal(pSpecData); } else { mcu->InputData(&s, e ); //Convert System::String back to a StrBuf System::IntPtr ptr = Marshal::StringToHGlobalAnsi( s ); const char* str = static_cast< const char * >( ptr.ToPointer() ); strbuf->Set( str ); Marshal::FreeHGlobal( ptr ); } e->Dispose(); } void ClientUserDelegate::HandleError( ::Error *err ) { p4dn::Error* e = new p4dn::Error( err); mcu->HandleError( e ); e->Dispose(); } void ClientUserDelegate::Message( ::Error *err ) { p4dn::Error* e = new p4dn::Error( err ); mcu->Message( e ); e->Dispose(); } void ClientUserDelegate::OutputError( const_char *errBuf ) { System::String* s = errBuf; mcu->OutputError( s ); } void ClientUserDelegate::OutputInfo( char level, const_char *data ) { System::String* s = data; mcu->OutputInfo( level, s ); } void ClientUserDelegate::OutputBinary( const_char *data, int length ) { System::Byte b[] = new System::Byte[length]; Marshal::Copy(IntPtr(data), b, 0, length); mcu->OutputBinary( b ); } void ClientUserDelegate::OutputText( const_char *data, int length ) { System::String *s = new System::String(data); mcu->OutputText( s ); } /* In this method, I deviate from the C++ api in form. If this is a form, we add the 2 strings (specdata and specdef) to the dictionary to denote it is a form */ void ClientUserDelegate::OutputStat( StrDict *varList ) { //Managed Types System::Collections::Specialized::StringDictionary* dict; System::String* key; System::String* value; StrDict* Dict; //p4api types ::Spec* cspec; ::SpecDataTable* specData; StrPtr* data = varList->GetVar("data"); StrPtr* specdef = varList->GetVar("specdef"); ::StrRef var, val; ::Error e; int i = 0; dict = new System::Collections::Specialized::StringDictionary(); if (specdef && data) { // We have a form, parse it out cspec = new Spec(specdef->Text(), ""); specData = new SpecDataTable(); cspec->Parse(data->Text(), specData, &e); Dict = specData->Dict(); //since this is a spec, we will append the specdef string //This will be needed when the dictionary is passed back to save the form dict->Add( "parseforms-specdef", specdef->Text()); dict->Add( "parseforms-specdata", data->Text()); } else { // No form, just use the raw dictionary Dict = varList; } while (Dict->GetVar(i,var,val) != -0) { System::String* key = var.Text(); System::String* value = val.Text(); if (!dict->ContainsKey(key)) { dict->Add( key, value ); } i++; } delete cspec; delete specData; mcu->OutputStat( dict ); } void ClientUserDelegate::Prompt( const StrPtr& msg, StrBuf& rsp, int noEcho, ::Error *err ) { String* response; String* message = msg.Text(); bool bEcho = ( noEcho != 0 ); p4dn::Error* e = new p4dn::Error( err ); mcu->Prompt( message, response, bEcho, e ); char* str = (char *)(void *) Marshal::StringToHGlobalAnsi( response ); rsp.Set( str ); e->Dispose(); Marshal::FreeHGlobal( str ); } void ClientUserDelegate::ErrorPause( char *errBuf, ::Error *err ) { System::String* s = errBuf; p4dn::Error* e = new p4dn::Error( err ); mcu->ErrorPause( s, e ); e->Dispose(); } void ClientUserDelegate::Edit( FileSys *f1, ::Error *err ) { p4dn::Error* e = new p4dn::Error( err ); System::String* name = f1->Name(); System::IO::FileInfo* info = new System::IO::FileInfo( name ); mcu->Edit( info, e ); e->Dispose(); } void ClientUserDelegate::Diff( FileSys *f1, FileSys *f2, int doPage, char *diffFlags, ::Error *e ) { // TODO: System::Console::WriteLine( "10" ); } void ClientUserDelegate::Merge( FileSys *base, FileSys *leg1, FileSys *leg2, FileSys *result, ::Error *e ) { // TODO: } void ClientUserDelegate::Help( const_char *const *help ) { System::String* s = *help; mcu->Help( s ); } FileSys* ClientUserDelegate::File( FileSysType type ) { return FileSys::Create( type ); } void ClientUserDelegate::Finished() { mcu->Finished(); }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#5 | 5826 | Shawn Hladky | PerforceDotNet is now obsolete | ||
#4 | 5349 | Shawn Hladky |
Initial check-in for the new API interface. Nothing works yet, but it should compile at least. |
||
#3 | 5064 | Shawn Hladky |
WIP -- Added ability to parse forms. Added new tester application. ** not ready for human consumption, needs more review and testing. |
||
#2 | 4643 | Shawn Hladky | Clean-up some more memory issues | ||
#1 | 4545 | Shawn Hladky |
Several Fixes: Updated solution to Visual Studio 2003 Implemented InputData Implemented OutPutBinary Changed ClientAPI and Error objects to implement the IDisposable interface to improve Garbage Collection Changed header files so you don't need to add #pragma once to the p4 api headers. Refactored the ClientUserDelagate class Fixed a few minor memory leaks |