xmlcfg.pp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999 Sebastian Guenther, [email protected]
  5. Implementation of TXMLConfig class
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {
  13. TXMLConfig enables applications to use XML files for storing their
  14. configuration data
  15. }
  16. {$MODE objfpc}
  17. unit xmlcfg;
  18. interface
  19. uses DOM, xmlread, xmlwrite;
  20. type
  21. TXMLConfig = class
  22. protected
  23. doc: TXMLDocument;
  24. FileName: String;
  25. public
  26. constructor Create(AFileName: String);
  27. destructor Destroy; override;
  28. procedure Flush;
  29. function GetValue(APath, ADefault: String): String;
  30. function GetValue(APath: String; ADefault: Integer): Integer;
  31. function GetValue(APath: String; ADefault: Boolean): Boolean;
  32. procedure SetValue(APath, AValue: String);
  33. procedure SetValue(APath: String; AValue: Integer);
  34. procedure SetValue(APath: String; AValue: Boolean);
  35. end;
  36. implementation
  37. uses sysutils;
  38. constructor TXMLConfig.Create(AFileName: String);
  39. var
  40. f: File;
  41. cfg: TDOMElement;
  42. begin
  43. FileName := AFileName;
  44. Assign(f, AFileName);
  45. {$I-}
  46. Reset(f, 1);
  47. {$I+}
  48. if IOResult = 0 then begin
  49. doc := ReadXMLFile(f);
  50. Close(f);
  51. doc.SetDocumentElement(TDOMElement(doc.FindNode('CONFIG')));
  52. end else begin
  53. doc := TXMLDocument.Create;
  54. cfg := doc.CreateElement('CONFIG');
  55. doc.AppendChild(cfg);
  56. doc.SetDocumentElement(cfg);
  57. end;
  58. end;
  59. destructor TXMLConfig.Destroy;
  60. begin
  61. Flush;
  62. inherited Destroy;
  63. end;
  64. procedure TXMLConfig.Flush;
  65. var
  66. f: Text;
  67. begin
  68. Assign(f, FileName);
  69. Rewrite(f);
  70. WriteXMLFile(doc, f);
  71. Close(f);
  72. end;
  73. function TXMLConfig.GetValue(APath, ADefault: String): String;
  74. var
  75. node, subnode, attr: TDOMNode;
  76. i: Integer;
  77. name: String;
  78. begin
  79. node := doc.DocumentElement;
  80. while True do begin
  81. i := Pos('/', APath);
  82. if i = 0 then break;
  83. name := Copy(APath, 1, i - 1);
  84. APath := Copy(APath, i + 1, Length(APath));
  85. subnode := node.FindNode(name);
  86. if subnode = nil then begin
  87. Result := ADefault;
  88. exit;
  89. end;
  90. node := subnode;
  91. end;
  92. attr := node.Attributes.GetNamedItem(APath);
  93. if attr = nil then
  94. Result := ADefault
  95. else
  96. Result := attr.NodeValue;
  97. end;
  98. function TXMLConfig.GetValue(APath: String; ADefault: Integer): Integer;
  99. begin
  100. Result := StrToInt(GetValue(APath, IntToStr(ADefault)));
  101. end;
  102. function TXMLConfig.GetValue(APath: String; ADefault: Boolean): Boolean;
  103. var
  104. s: String;
  105. begin
  106. if ADefault then s := 'True'
  107. else s := 'False';
  108. s := GetValue(APath, s);
  109. if UpperCase(s) = 'TRUE' then Result := True
  110. else if UpperCase(s) = 'FALSE' then Result := False
  111. else Result := ADefault;
  112. end;
  113. procedure TXMLConfig.SetValue(APath, AValue: String);
  114. var
  115. node, subnode, attr: TDOMNode;
  116. i: Integer;
  117. name: String;
  118. begin
  119. node := doc.DocumentElement;
  120. while True do begin
  121. i := Pos('/', APath);
  122. if i = 0 then break;
  123. name := Copy(APath, 1, i - 1);
  124. APath := Copy(APath, i + 1, Length(APath));
  125. subnode := node.FindNode(name);
  126. if subnode = nil then begin
  127. subnode := doc.CreateElement(name);
  128. node.AppendChild(subnode);
  129. end;
  130. node := subnode;
  131. end;
  132. attr := node.Attributes.GetNamedItem(APath);
  133. if attr = nil then begin
  134. attr := doc.CreateAttribute(APath);
  135. node.Attributes.SetNamedItem(attr);
  136. end;
  137. attr.NodeValue := AValue;
  138. end;
  139. procedure TXMLConfig.SetValue(APath: String; AValue: Integer);
  140. begin
  141. SetValue(APath, IntToStr(AValue));
  142. end;
  143. procedure TXMLConfig.SetValue(APath: String; AValue: Boolean);
  144. begin
  145. if AValue then SetValue(APath, 'True')
  146. else SetValue(APath, 'False');
  147. end;
  148. end.
  149. {
  150. $Log$
  151. Revision 1.1 1999-07-09 08:35:09 michael
  152. + Initial implementation by Sebastian Guenther
  153. }