{$code appl,LDCD,code,100}
program CPDBdemo;
type iBasFunType=function(S:string):string;
var iBasCallPP:iBasFunType;

{$i PalmAPI.pas}
{$i CPDBSTD4PP.pas}

function Max(i,j:integer):integer;
begin
 if i>j then 
  Max:=i
 else
  max:=j;  
end;

function IntToString(i:integer):string;
var
 sResult:string;
begin
 StrIToA(sResult,i);
 IntToString:=sResult;
end;

function CutString(const sMyString:string;iSub:integer):string;
var
 sResult:string;
 i,j:integer;
begin
 sResult:='';
 i:=3;
 j:=1;
 while i<Length(sMyString)+1 do begin
  if sMyString[i]=chr(10) then begin
   j:=j+1;   
   i:=i+1;
  end; 
  if j=iSub then
   sResult:=sResult+sMyString[i];
  i:=i+1;
 end;
 CutString:=sResult;   
end;

function StringToCreatorID(const sMyString:string):integer;
begin
 StringToCreatorID:=
  Ord(sMyString[4])+
  256*Ord(sMyString[3])+
  65536*Ord(sMyString[2])+    
  16777216*Ord(sMyString[1]); 
end;


function CallPP(S:string):string;
var
 Error:err;
 iWhat,iRecord,i:integer;
 sWhat:string;
 iHandle:UInt8;
 iI8:Int8;
 iI16:Int16;
 iUI16:UInt16;
begin
 Error:=0;
 iRecord:=-1;
 sWhat:=S[1]+S[2];
 iWhat:=StrAToI(sWhat);
 case iWhat of
  // Open CPDB
  1: CPDB_OPENLIB;
  // Close CPDB
  2: CPDB_CLOSELIB;
  // DB exists ? Returns Yes=0 or No=1
  3: Error:=Ord(CPDB_DatabaseExist(CutString(S,1))=CPDB_ERR_NOEXIST);
  // Create DB   
  4: Error:=CPDB_CreateDatabase(0,CutString(S,1),StringToCreatorID(CutString(S,2)),CutString(S,3));  
  // Write Record DB
  5: begin
      Error:=CPDB_Open(0,CutString(S,1),dmModeReadWrite,iHandle);
      if Error=0 then begin
       Error:=Max(Error,CPDB_AddRecord(iHandle));
       i:=2;
       while (CutString(S,i)<>'') and (Error=0) do begin
        if CutString(S,i)='sI' then 
         Error:=Max(Error,CPDB_WriteShortInt(iHandle,CutString(S,i+1),StrAToI(CutString(S,i+2))))      
        else if CutString(S,i)='mI' then
         Error:=Max(Error,CPDB_WriteInt(iHandle,CutString(S,i+1),StrAToI(CutString(S,i+2))))
        else
         Error:=1; 
        i:=i+3;
       end;
       Error:=Max(Error,CPDB_UpdateRecord(iHandle));
      end;
      Error:=Max(Error,CPDB_Close(iHandle));
     end;
  // Count number of records   
  6: begin
      Error:=CPDB_Open(0,CutString(S,1),dmModeReadWrite,iHandle);
      Error:=Max(Error,CPDB_CountRecord(iHandle,iUI16));
      iRecord:=iUI16;     
      Error:=Max(Error,CPDB_Close(iHandle));  
     end;
  // Calc Sum of records on one field 
  7: begin
      Error:=CPDB_Open(0,CutString(S,1),dmModeReadOnly,iHandle);
      if Error=0 then begin
       Error:=CPDB_ReadFirst(iHandle);
       if Error=0 then
        iRecord:=0;
       while Error=0 do begin
        if CutString(S,2)='sI' then begin 
         Error:=CPDB_ReadShortInt(iHandle,CutString(S,3),iI8);      
         iRecord:=iRecord+iI8;
        end
        else if CutString(S,2)='mI' then begin
         Error:=CPDB_ReadInt(iHandle,CutString(S,3),iI16);
         iRecord:=iRecord+iI16;
        end
        else
         Error:=1;
        Error:=Max(Error,CPDB_ReadNext(iHandle));        
       end;
      end;
      Error:=CPDB_Close(iHandle);  
     end;   
 end;
 if (Error=0) and (iRecord>-1) then
  CallPP:=IntToString(iRecord)
 else 
  CallPP:=IntToString(-1*Ord(Error>0));
end;

begin
 iBasCallPP:=CallPP;
end.
