registryuserpreferences.pas 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. unit registryuserpreferences;
  2. {
  3. $Id: registryuserpreferences.pas,v 1.1 2004/09/30 22:35:47 savage Exp $
  4. }
  5. {******************************************************************************}
  6. { }
  7. { JEDI-SDL : Pascal units for SDL - Simple DirectMedia Layer }
  8. { Wrapper class for Windows Register and INI Files }
  9. { }
  10. { The initial developer of this Pascal code was : }
  11. { Dominqiue Louis <[email protected]> }
  12. { }
  13. { Portions created by Dominqiue Louis are }
  14. { Copyright (C) 2000 - 2001 Dominqiue Louis. }
  15. { }
  16. { }
  17. { Contributor(s) }
  18. { -------------- }
  19. { }
  20. { }
  21. { Obtained through: }
  22. { Joint Endeavour of Delphi Innovators ( Project JEDI ) }
  23. { }
  24. { You may retrieve the latest version of this file at the Project }
  25. { JEDI home page, located at http://delphi-jedi.org }
  26. { }
  27. { The contents of this file are used with permission, subject to }
  28. { the Mozilla Public License Version 1.1 (the "License"); you may }
  29. { not use this file except in compliance with the License. You may }
  30. { obtain a copy of the License at }
  31. { http://www.mozilla.org/MPL/MPL-1.1.html }
  32. { }
  33. { Software distributed under the License is distributed on an }
  34. { "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or }
  35. { implied. See the License for the specific language governing }
  36. { rights and limitations under the License. }
  37. { }
  38. { Description }
  39. { ----------- }
  40. { }
  41. { }
  42. { }
  43. { }
  44. { }
  45. { }
  46. { }
  47. { Requires }
  48. { -------- }
  49. { The SDL Runtime libraris on Win32 : SDL.dll on Linux : libSDL.so }
  50. { They are available from... }
  51. { http://www.libsdl.org . }
  52. { }
  53. { Programming Notes }
  54. { ----------------- }
  55. { }
  56. { }
  57. { }
  58. { }
  59. { Revision History }
  60. { ---------------- }
  61. { September 23 2004 - DL : Initial Creation }
  62. {
  63. $Log: registryuserpreferences.pas,v $
  64. Revision 1.1 2004/09/30 22:35:47 savage
  65. Changes, enhancements and additions as required to get SoAoS working.
  66. }
  67. {******************************************************************************}
  68. interface
  69. uses
  70. {$IFDEF REG}
  71. Registry,
  72. {$ELSE}
  73. IniFiles,
  74. {$ENDIF}
  75. Classes,
  76. userpreferences;
  77. type
  78. TRegistryUserPreferences = class( TUserPreferences )
  79. private
  80. protected
  81. function GetSection( const Index : Integer ) : string; virtual; abstract;
  82. function GetIdentifier( const Index : Integer ) : string; virtual; abstract;
  83. function GetDefaultBoolean( const Index : Integer ) : Boolean; override;
  84. function GetBoolean( const Index : Integer ) : Boolean; override;
  85. procedure SetBoolean( const Index : Integer; const Value : Boolean ); override;
  86. function GetDefaultDateTime( const Index : Integer ) : TDateTime; override;
  87. function GetDateTime( const Index : Integer ) : TDateTime; override;
  88. procedure SetDateTime( const Index : Integer; const Value : TDateTime ); override;
  89. function GetDefaultInteger( const Index : Integer ) : Integer; override;
  90. function GetInteger( const Index : Integer ) : Integer; override;
  91. procedure SetInteger( const Index : Integer; const Value : Integer ); override;
  92. function GetDefaultFloat( const Index : Integer ) : single; override;
  93. function GetFloat( const Index : Integer ) : single; override;
  94. procedure SetFloat( const Index : Integer; const Value : single ); override;
  95. function GetDefaultString( const Index : Integer ) : string; override;
  96. function GetString( const Index : Integer ) : string; override;
  97. procedure SetString( const Index : Integer; const Value : string ); override;
  98. public
  99. Registry : {$IFDEF REG}TRegIniFile{$ELSE}TIniFile{$ENDIF};
  100. constructor Create( const FileName : string = '' ); reintroduce;
  101. destructor Destroy; override;
  102. procedure Update; override;
  103. end;
  104. implementation
  105. uses
  106. SysUtils;
  107. { TRegistryUserPreferences }
  108. constructor TRegistryUserPreferences.Create( const FileName : string );
  109. var
  110. defFileName : string;
  111. begin
  112. inherited Create;
  113. if FileName <> '' then
  114. defFileName := FileName
  115. else
  116. defFileName := ChangeFileExt( ParamStr( 0 ), '.ini' );
  117. Registry := {$IFDEF REG}TRegIniFile{$ELSE}TIniFile{$ENDIF}.Create( defFileName );
  118. end;
  119. destructor TRegistryUserPreferences.Destroy;
  120. begin
  121. Update;
  122. Registry.Free;
  123. Registry := nil;
  124. inherited;
  125. end;
  126. function TRegistryUserPreferences.GetBoolean( const Index : Integer ) : Boolean;
  127. begin
  128. Result := Registry.ReadBool( GetSection( Index ), GetIdentifier( Index ), GetDefaultBoolean( Index ) );
  129. end;
  130. function TRegistryUserPreferences.GetDateTime( const Index : Integer ): TDateTime;
  131. begin
  132. Result := Registry.ReadDateTime( GetSection( Index ){$IFNDEF REG}, GetIdentifier( Index ), GetDefaultDateTime( Index ){$ENDIF} );
  133. end;
  134. function TRegistryUserPreferences.GetDefaultBoolean( const Index : Integer ) : Boolean;
  135. begin
  136. result := false;
  137. end;
  138. function TRegistryUserPreferences.GetDefaultDateTime( const Index: Integer ) : TDateTime;
  139. begin
  140. result := Now;
  141. end;
  142. function TRegistryUserPreferences.GetDefaultFloat( const Index: Integer ) : single;
  143. begin
  144. result := 0.0;
  145. end;
  146. function TRegistryUserPreferences.GetDefaultInteger(const Index : Integer ) : Integer;
  147. begin
  148. result := 0;
  149. end;
  150. function TRegistryUserPreferences.GetDefaultString( const Index : Integer ) : string;
  151. begin
  152. result := '';
  153. end;
  154. function TRegistryUserPreferences.GetFloat( const Index : Integer ): single;
  155. begin
  156. Result := Registry.ReadFloat( GetSection( Index ){$IFNDEF REG}, GetIdentifier( Index ), GetDefaultFloat( Index ){$ENDIF} );
  157. end;
  158. function TRegistryUserPreferences.GetInteger( const Index : Integer ) : Integer;
  159. begin
  160. Result := Registry.ReadInteger( GetSection( Index ), GetIdentifier( Index ), GetDefaultInteger( Index ) );
  161. end;
  162. function TRegistryUserPreferences.GetString( const Index : Integer ): string;
  163. begin
  164. Result := Registry.ReadString( GetSection( Index ), GetIdentifier( Index ), GetDefaultString( Index ) );
  165. end;
  166. procedure TRegistryUserPreferences.SetBoolean( const Index : Integer; const Value : Boolean );
  167. begin
  168. Registry.WriteBool( GetSection( Index ), GetIdentifier( Index ), Value );
  169. inherited;
  170. end;
  171. procedure TRegistryUserPreferences.SetDateTime( const Index: Integer; const Value: TDateTime );
  172. begin
  173. Registry.WriteDateTime( GetSection( Index ){$IFNDEF REG}, GetIdentifier( Index ){$ENDIF}, Value );
  174. inherited;
  175. end;
  176. procedure TRegistryUserPreferences.SetFloat(const Index: Integer; const Value: single);
  177. begin
  178. Registry.WriteFloat( GetSection( Index ){$IFNDEF REG}, GetIdentifier( Index ){$ENDIF}, Value );
  179. inherited;
  180. end;
  181. procedure TRegistryUserPreferences.SetInteger( const Index, Value : Integer );
  182. begin
  183. Registry.WriteInteger( GetSection( Index ), GetIdentifier( Index ), Value );
  184. inherited;
  185. end;
  186. procedure TRegistryUserPreferences.SetString( const Index : Integer; const Value : string );
  187. begin
  188. Registry.WriteString( GetSection( Index ), GetIdentifier( Index ), Value );
  189. inherited;
  190. end;
  191. procedure TRegistryUserPreferences.Update;
  192. begin
  193. {$IFDEF REG}
  194. Registry.CloseKey;
  195. {$ELSE}
  196. Registry.UpdateFile;
  197. {$ENDIF}
  198. end;
  199. end.