DCConfiguration.pas 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. unit DCConfiguration;
  2. {$ifdef fpc}
  3. {$mode objfpc}
  4. {$h+}
  5. {$endif}
  6. interface
  7. uses
  8. Classes;
  9. type
  10. {*----------------------------------------------------------------------------
  11. ----------------------------------------------------------------------------}
  12. TAbstractConfiguration = class (TObject)
  13. private
  14. protected
  15. public
  16. procedure AddProperty(AKey : String; AValue : String); Virtual; Abstract;
  17. procedure RemoveProperty(AKey : String); Virtual; Abstract;
  18. procedure Clear(); Virtual; Abstract;
  19. function ContainsKey(AKey : String) : Boolean; Virtual; Abstract;
  20. function GetKeys() : TStrings; Virtual; Abstract;
  21. function IsEmpty() : Boolean; Virtual; Abstract;
  22. function GetProperty(AKey : String) : String; Virtual; Abstract;
  23. end;
  24. {*----------------------------------------------------------------------------
  25. ----------------------------------------------------------------------------}
  26. TBaseConfiguration = class (TAbstractConfiguration)
  27. private
  28. protected
  29. FMap : TStrings;
  30. public
  31. constructor Create; Virtual;
  32. destructor Destroy; Override;
  33. procedure AddProperty(AKey : String; AValue : String); Override;
  34. procedure RemoveProperty(AKey : String); Override;
  35. procedure Clear(); Override;
  36. function ContainsKey(AKey : String) : Boolean; Override;
  37. function GetKeys() : TStrings; Override;
  38. function IsEmpty() : Boolean; Override;
  39. function GetProperty(AKey : String) : String; Override;
  40. end;
  41. {*----------------------------------------------------------------------------
  42. ----------------------------------------------------------------------------}
  43. TFileConfiguration = class (TBaseConfiguration)
  44. private
  45. protected
  46. public
  47. procedure Load(AFilename : String); Overload; Virtual;
  48. procedure Load(AStream : TFileStream); Overload; Virtual;
  49. procedure Save(AFilename : String); Overload; Virtual;
  50. procedure Save(AStream : TFileStream); Overload; Virtual;
  51. end;
  52. implementation
  53. {*----------------------------------------------------------------------------
  54. TBaseConfiguration
  55. ----------------------------------------------------------------------------}
  56. constructor TBaseConfiguration.Create;
  57. begin
  58. inherited Create;
  59. FMap := TStringList.Create;
  60. end;
  61. destructor TBaseConfiguration.Destroy;
  62. begin
  63. FMap.Free;
  64. inherited Destroy;
  65. end;
  66. procedure TBaseConfiguration.AddProperty(AKey : String; AValue : String);
  67. begin
  68. FMap.Add(AKey + '=' + AValue)
  69. end;
  70. procedure TBaseConfiguration.RemoveProperty(AKey : String);
  71. var
  72. index : Integer;
  73. begin
  74. index := FMap.IndexOfName(AKey);
  75. if (index >= 0) then
  76. FMap.Delete(index);
  77. end;
  78. procedure TBaseConfiguration.Clear();
  79. begin
  80. FMap.Clear;
  81. end;
  82. function TBaseConfiguration.ContainsKey(AKey : String) : Boolean;
  83. var
  84. index : Integer;
  85. begin
  86. index := FMap.IndexOfName(AKey);
  87. Result := (index >= 0);
  88. end;
  89. function TBaseConfiguration.GetKeys() : TStrings;
  90. var
  91. res : TStrings;
  92. count : Integer;
  93. begin
  94. res := TStringList.Create;
  95. for count := 0 to FMap.Count-1 do
  96. res.Add(FMap.Names[count]);
  97. Result := res;
  98. end;
  99. function TBaseConfiguration.IsEmpty() : Boolean;
  100. begin
  101. Result := (FMap.Count <= 0);
  102. end;
  103. function TBaseConfiguration.GetProperty(AKey : String) : String;
  104. begin
  105. Result := '';
  106. if (Self.ContainsKey(AKey)) then
  107. Result := FMap.Values[AKey];
  108. end;
  109. {*----------------------------------------------------------------------------
  110. TFileConfiguration
  111. ----------------------------------------------------------------------------}
  112. procedure TFileConfiguration.Load(AFilename : String);
  113. begin
  114. Self.FMap.LoadFromFile(AFilename);
  115. end;
  116. procedure TFileConfiguration.Load(AStream : TFileStream);
  117. begin
  118. Self.FMap.LoadFromStream(AStream);
  119. end;
  120. procedure TFileConfiguration.Save(AFilename : String);
  121. begin
  122. Self.FMap.SaveToFile(AFilename);
  123. end;
  124. procedure TFileConfiguration.Save(AStream : TFileStream);
  125. begin
  126. Self.FMap.SaveToStream(AStream);
  127. end;
  128. end.