unit uDemoReadConfig;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes,
  ZaphodsMap;

const
  cDemoBranch = 'ACompany' + PathDelim + 'AProduct';

function ReadZMEnvVar(out Caption: string): string;
function ReadConfig1(out Caption, ErrorText: string): string;
function ReadConfig2(out Caption, ErrorText: string): string;
function ReadConfig3(out Caption, ErrorText: string): string;
function ReadConfig4(out Caption, ErrorText: string): string;

implementation

function ReadZMEnvVar(out Caption: string): string;
begin
  Caption := 'ZaphodsMap env var:';
  Result := GetEnvironmentVariable('ZaphodsMap');
end;

function ReadConfig1(out Caption, ErrorText: string): string;
var
  KeyedDetail: TKeyedDetail;
begin
  Caption := 'Retrieve Key2Value:';
  {load an arbitrary keyed value}
  Result := ZaphodKeyedValue(
    cDemoBranch,         // branch
    'AProductArea',      // key group name
    'FtpHost',           // key2value name
    cxOptional,          // use of context is optional
    usrNone,             // no need for username
    '',                  // default result
    KeyedDetail);

  if KeyedDetail.ErrorMessage <> '' then
  begin
    ErrorText := KeyedDetail.ErrorMessage;
    Result := '(error)';
  end;
end;

function ReadConfig2(out Caption, ErrorText: string): string;
var
  KeyedDetail: TKeyedDetail;
begin
  Caption := 'Retrieve Key2Folder:';

  {load a folder name}
  Result := ZaphodKeyedFolder(
    cDemoBranch,         // branch
    'AProductArea',      // key group name
    'Database',          // key2folder name
    cxOptional,          // use of context is optional
    usrNone,             // no need for username
    '',                  // default result
    KeyedDetail);

  if KeyedDetail.ErrorMessage <> '' then
  begin
    ErrorText := KeyedDetail.ErrorMessage;
    Result := '(error)';
  end;
end;

function ReadConfig3(out Caption, ErrorText: string): string;
var
  KeyedDetail: TKeyedDetail;
begin
  Caption := 'Retrieve Key2File:';

  {load an arbitrary filename}
  Result := ZaphodKeyedFilename(
    cDemoBranch,         // branch
    'AProductArea',      // key group name
    'Security',          // key2file name
    cxOptional,          // use of context is optional
    usrNone,             // no need for username
    '',                  // default result
    KeyedDetail);

  if KeyedDetail.ErrorMessage <> '' then
  begin
    ErrorText := KeyedDetail.ErrorMessage;
    Result := '(error)';
  end;
end;

function ReadConfig4(out Caption, ErrorText: string): string;
begin
  Caption := 'Retrieve attribute from keyed file:';

  {go into a keyed file (XML config file), follow an xpath, load an attribute}

  Result := ZaphodKeyedFileZNodeAttr(
    cDemoBranch,
    'AProductArea',      // key group name
    'Master',            // key2file name
    'AnyConfig',         // root tag in xml of loaded xml config file
    ['AA', 'BB', 'CC'],  // xpath to the node we want to find
    cxOptional,          // use of context is optional
    usrNone,             // no need for username
    'setting',           // name of the attribute to retrieve
    '',                  // default result
    ErrorText);

  if ErrorText <> '' then
  begin
    Result := '(error)';
  end;

end;

end.
