ATBTariffWebInterface/atbwebinterface.pas

140 lines
4.2 KiB
ObjectPascal

unit ATBWebInterface;
{$mode ObjFPC}{$H+}
interface
uses SysUtils, CTypes, fpjson, jsonparser, ATBUtils;
type
PTJSONObject = ^TJSONObject;
//
// public interface
//
function GetFileMenu(const localRepository: UTF8String;
jObj: PTJSONObject): Boolean;
function GetFile(const localRepository: UTF8String;
const fileId: UTF8String;
jObj: PTJSONObject): Boolean;
function SetFile(const localRepository: UTF8String;
const fileId: UTF8String;
const jsonData: TJSONData): Boolean;
function GetFileName(const localRepository: UTF8String;
const fileId: UTF8String): UTF8String;
// private c-interface
function GetFileMenuInternal(const localRepository: PChar): PChar; stdcall;
function GetFileInternal(const localRepository: PChar; const fileId: PChar): PChar; stdcall;
function SetFileInternal(const localRepository: PChar; const fileId: PChar;
const json: PChar; size: cint32): cbool; stdcall;
function GetFileNameInternal(const localRepository: PChar;
const fileId: PChar): PChar; stdcall;
implementation
const DLLName = 'CalculatorCInterface.dll';
///
function GetFileMenu(const localRepository: UTF8String; jObj: PTJSONObject): Boolean;
var
p: PChar;
fileMenuContent: UTF8String;
begin
Result := false;
try
p := GetFileMenuInternal(PChar(Utf8ToAnsi(localRepository)));
if p <> nil then begin
fileMenuContent := UTF8String(p);
jObj^ := GetJSON(fileMenuContent) as TJSONObject;
Result := true;
end;
finally
DeleteMem(p);
end;
end;
function GetFileName(const localRepository: UTF8String;
const fileId: UTF8String): UTF8String;
var
p: PChar;
begin
Result := '';
try
p := GetFileNameInternal(PChar(Utf8ToAnsi(localRepository)),
PChar(Utf8ToAnsi(fileId)));
if p <> nil then begin
Result := UTF8String(p);
end;
finally
DeleteMem(p);
end;
end;
function GetFile(const localRepository: UTF8String; const fileId: UTF8String;
jObj: PTJSONObject): Boolean;
var
p: PChar;
fileContent: UTF8String;
begin
Result := false;
try
p := GetFileInternal(PChar(Utf8ToAnsi(localRepository)),
PChar(Utf8ToAnsi(fileId)));
if p <> nil then begin
fileContent := UTF8String(p);
jObj^ := GetJSON(fileContent) as TJSONObject;
Result := true;
end;
finally
DeleteMem(p);
end;
end;
///
function SetFile(const localRepository: UTF8String; const fileId: UTF8String;
const jsonData: TJSONData): Boolean;
var
P: PChar;
Len: Integer;
S: UTF8String;
begin
try
writeLn(jsonData.FormatJSON);
S := jsonData.AsJSON;
P := PChar(S);
Len := System.Length(Utf8ToAnsi(P));
Result := SetFileInternal(PChar(Utf8ToAnsi(localRepository)),
PChar(Utf8ToAnsi(fileId)), P, Len);
finally
end;
end;
//
// external c-interface
//
function GetFileInternal(const localRepository: PChar;
const fileId: PChar): PChar; stdcall;
external DLLName;
function GetFileNameInternal(const localRepository: PChar;
const fileId: PChar): PChar; stdcall;
external DLLName;
function GetFileMenuInternal(const localRepository: PChar): PChar; stdcall;
external DLLName;
function SetFileInternal(const localRepository: PChar;
const fileId: PChar;
const json: PChar;
size: cint32): cbool; stdcall;
external DLLName;
end.