unit CMLxPlugServ;

interface

uses
  Windows, ActiveX, Classes, ComObj, CM2CMLXInterface, CM2MMRB, SysUtils,
   CM2AutoUpdateInterface, Filter;

type
  TCM2CMLX_CMLInclude = class(TComObject, ICM2CMLXPlug2, ICM2AutoUpdate)
  private
   lasterrorstring: String;
   sFile: String;
  protected
   Function Name(OUT Name: Pchar): HResult; stdcall;
   Function Version(OUT Version: word): HResult; stdcall; // Uses MMRB Notation
   Function RequiredCoreVersion(OUT RCV: Word): HResult; stdcall; // Uses MMRB Notation - Please refer to CM2Plugtypes for details
   Function SetParentHandle(Handle: THandle): HResult; stdcall;
   // Declare Error commands
   Function GetLastErrorMsg(OUT Msg: PChar): HResult; stdcall;
   // Declare Info system
   Function GetInfoCount(OUT InfoCount: Integer): HResult; stdcall;
   Function GetInfo(Index: Integer; OUT InfoName: PChar; OUT InfoVar: PChar): HResult; stdcall;
  // Declare "Event" types
   Function Init: HResult; stdcall;
   Function Terminating: HResult; stdcall;
   Function Setup: HResult; stdcall;
   // Plugin Speficic
   Function TagName(OUT Tag: PChar): HResult; stdcall;
   Function Perform(Attribute: PChar; Parameters: PChar; OUT ResultString: WideString; OUT ResultType: Byte): HResult; stdcall;
   // Update API
   Function UpdateFileURL(OUT URL: Pchar): HResult; stdcall;
   // Parese API
   procedure Parser(Param: String);
   end;
const
  Class_CM2CMLX_CMLInclude: TGUID = '{035BE8D0-9E76-4A81-89FC-B5E9998C0CEC}';
  AutoUpdateFile = '';
  PlugInVersion: Word = 0200;

implementation

uses ComServ;

{
Todo:
- check if parameter exist

v 100:
- First Release
}

function TCM2CMLX_CMLInclude.GetLastErrorMsg(OUT Msg: PChar): HResult;
begin
 If LastErrorString = '' then LastErrorString := 'No error';
 Msg := PChar(LastErrorString);
 Result := S_OK;
end;

function TCM2CMLX_CMLInclude.GetInfo(Index: Integer; out InfoName,
  InfoVar: PChar): HResult;
begin
 Result := S_OK;
 Case Index of
  0: Begin
      InfoName := PChar('Programmed By');
      InfoVar := PChar('Jorge Schrauwen');
     End;
  1: Begin
      InfoName := PChar('Created');
      InfoVar := Pchar('13th September 2004');
     End;
  2: Begin
      Infoname := PChar('Updated');
      Infovar := PChar('22th January 2005');
     End;
  else begin
        Result := E_FAIL;
        LastErrorString := 'Index out of bounds';
       End;
 End;
end;

function TCM2CMLX_CMLInclude.GetInfoCount(out InfoCount: Integer): HResult;
begin
 Result := S_OK;
 InfoCount := 3; // The infolist contains 3 items
end;

function TCM2CMLX_CMLInclude.Init: HResult;
begin
 Result := S_OK;
end;

function TCM2CMLX_CMLInclude.Terminating: HResult;
begin
 Result := S_OK; // We dont need to free any memory
end;

function TCM2CMLX_CMLInclude.Setup: HResult;
begin
 Result := E_NOTIMPL; // We dont need to run a setup
end;


function TCM2CMLX_CMLInclude.Name(OUT Name: Pchar): HResult;
begin
 Name := PChar('CMLInclude');
 Result := S_OK;
end;

function TCM2CMLX_CMLInclude.Version(OUT Version: word): HResult;
begin
 Version := PlugInVersion;
 Result := S_OK;
end;

function TCM2CMLX_CMLInclude.RequiredCoreVersion(out RCV: Word): HResult;
Var
 MMRB: TMMRB;
begin
 MMRB.Major := 0;
 MMRB.Minor := 7;
 MMRB.Release := 1;
 MMRB.Built := 0;
 RCV := MMRBToWord(MMRB);
 Result := S_OK;
end;

Function TCM2CMLX_CMLInclude.TagName(OUT Tag: PChar): HResult;
begin
 Tag := PChar('Include');
 Result := S_OK;
end;

Function TCM2CMLX_CMLInclude.Perform(Attribute: PChar; Parameters: PChar; OUT ResultString: Widestring; OUT ResultType: Byte): HResult;
var
 srcFile: TextFile;
 sBuffer, sData: string;
begin
  sFile := '';
  sData := '';
  Parser(Attribute);
  ResultType := 3;
  AssignFile(srcFile, sFile);
  Reset(srcFile);
  while not EOF(srcFile) do begin
    ReadLn(srcFile, sBuffer);
    sData := sData + sBuffer + #13#10;
  end;
  CloseFile(srcFile);
  sData := '<cml>' + sData + '</cml>';
  ResultString := PChar(sData);
  Result := S_OK;
end;

function TCM2CMLX_CMLInclude.SetParentHandle(Handle: THandle): HResult;
begin
 // We don't need this handle so do nothing
 Result := S_OK;
end;

function TCM2CMLX_CMLInclude.UpdateFileURL(out URL: Pchar): HResult;
begin
 URL := PChar(AutoUpdateFile);
 Result := S_OK;
end;

procedure TCM2CMLX_CMLInclude.Parser(Param: String);
var
 i: Integer;
 n, v: string;
begin
 for i := 1 to countfilt(Param, '|') do
   begin
     n := GetFilt(Param, i, '|');
     v := GetFilt(n, 2, '=');
     n := Uppercase(GetFilt(n, 1, '='));
     if n = 'FILE' then sFile := PChar(LowerCase(v));
   end;
end;

initialization
  TComObjectFactory.Create(ComServer, TCM2CMLX_CMLInclude, Class_CM2CMLX_CMLInclude,
    'CM2CMLX_CMLInclude', 'CM2 CMLx Plugin - CMLInclude', ciMultiInstance, tmApartment);
end.                    
