This commit is contained in:
1 parent
7fb9935770
commit
741a56552e
8 files changed
+397
-1
No files matched your search
@@ -0,0 +1,277 @@
|
||||
unit KM_IoXML;
|
||||
{$I KaM_Remake.inc}
|
||||
interface
|
||||
uses
|
||||
Classes, SysUtils,
|
||||
DOM, XMLRead, XMLWrite;
|
||||
|
||||
type
|
||||
TKMXmlDomDocument = TXMLDocument;
|
||||
|
||||
TKMXmlNode = class;
|
||||
|
||||
TKMSimpleVariant = record
|
||||
private
|
||||
// Internal representation is always a string
|
||||
fValue: string;
|
||||
public
|
||||
class operator Implicit(const A: Boolean): TKMSimpleVariant;
|
||||
class operator Implicit(const A: Cardinal): TKMSimpleVariant;
|
||||
class operator Implicit(const A: Integer): TKMSimpleVariant;
|
||||
class operator Implicit(const A: Single): TKMSimpleVariant;
|
||||
class operator Implicit(const A: string): TKMSimpleVariant;
|
||||
class operator Implicit(const A: TDateTime): TKMSimpleVariant;
|
||||
function AsBoolean: Boolean; overload;
|
||||
function AsBoolean(aDefault: Boolean): Boolean; overload;
|
||||
function AsCardinal: Cardinal; overload;
|
||||
function AsCardinal(aDefault: Cardinal): Cardinal; overload;
|
||||
function AsDateTime: TDateTime;
|
||||
function AsFloat: Single; overload;
|
||||
function AsFloat(aDefault: Single): Single; overload;
|
||||
function AsInteger: Integer; overload;
|
||||
function AsInteger(aDefault: Integer): Integer; overload;
|
||||
function AsString: string; overload;
|
||||
function AsString(aDefault: string): string; overload;
|
||||
end;
|
||||
|
||||
TKMXmlDocument = class
|
||||
private
|
||||
fDocument: TKMXmlDomDocument;
|
||||
fRoot: TKMXmlNode;
|
||||
function GetText: String;
|
||||
procedure SetText(const aText: string);
|
||||
procedure ApplyDefaultSettings;
|
||||
class constructor Create;
|
||||
public
|
||||
class var FDateFormat: TFormatSettings;
|
||||
constructor Create(const aRoot: string = 'Root');
|
||||
destructor Destroy; override;
|
||||
property Root: TKMXmlNode read fRoot;
|
||||
procedure LoadFromFile(const aFilename: string; aRoot: string = 'Root'; aReadOnly: Boolean = True);
|
||||
procedure SaveToFile(const aFilename: string; aCompressed: Boolean = False);
|
||||
property Text: string read GetText write SetText;
|
||||
end;
|
||||
|
||||
TKMXmlNode = class(TDOMElement)
|
||||
protected
|
||||
function GetAttrib(const AttrName: String): TKMSimpleVariant;
|
||||
procedure SetAttrib(const AttrName: String; const AttrValue: TKMSimpleVariant);
|
||||
function GetChildsCount: Integer;
|
||||
function GetChild(const aIndex: Integer): TKMXmlNode;
|
||||
public
|
||||
function AddChild(const Name: String): TKMXmlNode;
|
||||
function FindNode(const Name: String): TKMXmlNode; reintroduce;
|
||||
function AddOrFindChild(const aChildNodeName: string): TKMXmlNode;
|
||||
function HasAttribute(const AttrName: String): Boolean;
|
||||
function HasChild(const Name: String): Boolean;
|
||||
property Attributes[const AttrName: String]: TKMSimpleVariant read GetAttrib write SetAttrib;
|
||||
property ChildsCount: Integer read GetChildsCount;
|
||||
property Childs[const aIndex: Integer]: TKMXmlNode read GetChild;
|
||||
end;
|
||||
|
||||
implementation
|
||||
uses
|
||||
Variants, DateUtils, StrUtils;
|
||||
|
||||
class constructor TKMXmlDocument.Create;
|
||||
begin
|
||||
inherited;
|
||||
end;
|
||||
|
||||
constructor TKMXmlDocument.Create(const aRoot: string = 'Root');
|
||||
begin
|
||||
inherited Create;
|
||||
fDocument := TKMXmlDomDocument.Create;
|
||||
ApplyDefaultSettings;
|
||||
if aRoot <> '' then
|
||||
begin
|
||||
fRoot := TKMXmlNode(fDocument.CreateElement(aRoot));
|
||||
fDocument.AppendChild(fRoot);
|
||||
end;
|
||||
end;
|
||||
|
||||
destructor TKMXmlDocument.Destroy;
|
||||
begin
|
||||
fDocument.Free;
|
||||
inherited;
|
||||
end;
|
||||
|
||||
procedure TKMXmlDocument.ApplyDefaultSettings;
|
||||
begin
|
||||
fDocument.XMLVersion := '1.0';
|
||||
end;
|
||||
|
||||
procedure TKMXmlDocument.LoadFromFile(const aFilename: string; aRoot: string = 'Root'; aReadOnly: Boolean = True);
|
||||
begin
|
||||
if FileExists(aFilename) then
|
||||
ReadXMLFile(fDocument, aFilename);
|
||||
|
||||
ApplyDefaultSettings;
|
||||
fRoot := TKMXmlNode(fDocument.DocumentElement.FindNode(aRoot));
|
||||
if fRoot = nil then
|
||||
begin
|
||||
fRoot := TKMXmlNode(fDocument.CreateElement(aRoot));
|
||||
fDocument.AppendChild(fRoot);
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TKMXmlDocument.SaveToFile(const aFilename: string; aCompressed: Boolean = False);
|
||||
begin
|
||||
ForceDirectories(ExtractFilePath(ExpandFileName(aFilename)));
|
||||
WriteXMLFile(fDocument, aFilename);
|
||||
end;
|
||||
|
||||
function TKMXmlDocument.GetText: String;
|
||||
begin
|
||||
// Not implemented
|
||||
end;
|
||||
|
||||
procedure TKMXmlDocument.SetText(const aText: string);
|
||||
begin
|
||||
// Not implemented
|
||||
end;
|
||||
|
||||
function TKMXmlNode.AddChild(const Name: String): TKMXmlNode;
|
||||
begin
|
||||
end;
|
||||
|
||||
function TKMXmlNode.FindNode(const Name: String): TKMXmlNode;
|
||||
begin
|
||||
end;
|
||||
|
||||
function TKMXmlNode.AddOrFindChild(const aChildNodeName: string): TKMXmlNode;
|
||||
begin
|
||||
if not HasChild(aChildNodeName) then
|
||||
Result := AddChild(aChildNodeName)
|
||||
else
|
||||
Result := FindNode(aChildNodeName);
|
||||
end;
|
||||
|
||||
function TKMXmlNode.HasAttribute(const AttrName: String): Boolean;
|
||||
begin
|
||||
end;
|
||||
|
||||
function TKMXmlNode.HasChild(const Name: String): Boolean;
|
||||
begin
|
||||
end;
|
||||
|
||||
function TKMXmlNode.GetAttrib(const AttrName: String): TKMSimpleVariant;
|
||||
begin
|
||||
end;
|
||||
|
||||
procedure TKMXmlNode.SetAttrib(const AttrName: String; const AttrValue: TKMSimpleVariant);
|
||||
begin
|
||||
end;
|
||||
|
||||
function TKMXmlNode.GetChildsCount: Integer;
|
||||
begin
|
||||
end;
|
||||
|
||||
function TKMXmlNode.GetChild(const aIndex: Integer): TKMXmlNode;
|
||||
begin
|
||||
end;
|
||||
|
||||
function TKMSimpleVariant.AsBoolean: Boolean;
|
||||
begin
|
||||
Result := StrToBool(fValue);
|
||||
end;
|
||||
|
||||
function TKMSimpleVariant.AsBoolean(aDefault: Boolean): Boolean;
|
||||
begin
|
||||
Result := StrToBoolDef(fValue, aDefault);
|
||||
end;
|
||||
|
||||
function TKMSimpleVariant.AsCardinal: Cardinal;
|
||||
begin
|
||||
Result := StrToInt64(fValue);
|
||||
end;
|
||||
|
||||
function TKMSimpleVariant.AsCardinal(aDefault: Cardinal): Cardinal;
|
||||
begin
|
||||
Result := StrToInt64Def(fValue, aDefault);
|
||||
end;
|
||||
|
||||
function TKMSimpleVariant.AsDateTime: TDateTime;
|
||||
begin
|
||||
Result := StrToDateTimeDef(fValue, 0, TKMXmlDocument.FDateFormat);
|
||||
end;
|
||||
|
||||
function TKMSimpleVariant.AsFloat: Single;
|
||||
var
|
||||
str: string;
|
||||
begin
|
||||
str := StringReplace(fValue, '.', FormatSettings.DecimalSeparator, [rfReplaceAll]);
|
||||
str := StringReplace(str, ',', FormatSettings.DecimalSeparator, [rfReplaceAll]);
|
||||
Result := StrToFloat(str);
|
||||
end;
|
||||
|
||||
function TKMSimpleVariant.AsFloat(aDefault: Single): Single;
|
||||
var
|
||||
str: string;
|
||||
begin
|
||||
str := StringReplace(fValue, '.', FormatSettings.DecimalSeparator, [rfReplaceAll]);
|
||||
str := StringReplace(str, ',', FormatSettings.DecimalSeparator, [rfReplaceAll]);
|
||||
Result := StrToFloatDef(str, aDefault);
|
||||
end;
|
||||
|
||||
function TKMSimpleVariant.AsInteger: Integer;
|
||||
begin
|
||||
Result := StrToInt(fValue);
|
||||
end;
|
||||
|
||||
function TKMSimpleVariant.AsInteger(aDefault: Integer): Integer;
|
||||
begin
|
||||
Result := StrToIntDef(fValue, aDefault);
|
||||
end;
|
||||
|
||||
function TKMSimpleVariant.AsString: string;
|
||||
begin
|
||||
Result := fValue;
|
||||
end;
|
||||
|
||||
function TKMSimpleVariant.AsString(aDefault: string): string;
|
||||
begin
|
||||
if fValue <> '' then
|
||||
Result := fValue
|
||||
else
|
||||
Result := aDefault;
|
||||
end;
|
||||
|
||||
class operator TKMSimpleVariant.Implicit(const A: Boolean): TKMSimpleVariant;
|
||||
const
|
||||
BOOL_STR: array [Boolean] of string = ('False', 'True');
|
||||
begin
|
||||
Result.fValue := BOOL_STR[A];
|
||||
end;
|
||||
|
||||
class operator TKMSimpleVariant.Implicit(const A: Cardinal): TKMSimpleVariant;
|
||||
begin
|
||||
Result.fValue := '$' + IntToHex(A, 8);
|
||||
end;
|
||||
|
||||
class operator TKMSimpleVariant.Implicit(const A: Integer): TKMSimpleVariant;
|
||||
begin
|
||||
Result.fValue := IntToStr(A);
|
||||
end;
|
||||
|
||||
class operator TKMSimpleVariant.Implicit(const A: Single): TKMSimpleVariant;
|
||||
var
|
||||
str: string;
|
||||
fs: TFormatSettings;
|
||||
begin
|
||||
fs.DecimalSeparator := '.';
|
||||
str := FormatFloat('0.0000', A, fs);
|
||||
Result.fValue := str;
|
||||
end;
|
||||
|
||||
class operator TKMSimpleVariant.Implicit(const A: string): TKMSimpleVariant;
|
||||
begin
|
||||
Result.fValue := A;
|
||||
end;
|
||||
|
||||
class operator TKMSimpleVariant.Implicit(const A: TDateTime): TKMSimpleVariant;
|
||||
begin
|
||||
Result.fValue := DateTimeToStr(A, TKMXmlDocument.FDateFormat);
|
||||
end;
|
||||
|
||||
end.
|
||||
Reference in new issue
Block a user