Ignore:
Timestamp:
Apr 3, 2025, 10:49:00 PM (13 days ago)
Author:
chronos
Message:
  • Modified: Updated Common package.
File:
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/Packages/Common/RegistryEx.pas

    r20 r21  
    1 unit URegistry;
    2 
    3 {$MODE Delphi}
     1unit RegistryEx;
    42
    53interface
     
    97
    108type
    11   TRegistryRoot = (rrKeyClassesRoot = HKEY($80000000),
    12     rrKeyCurrentUser = HKEY($80000001),
    13     rrKeyLocalMachine = HKEY($80000002),
    14     rrKeyUsers = HKEY($80000003),
    15     rrKeyPerformanceData = HKEY($80000004),
    16     rrKeyCurrentConfig = HKEY($80000005),
    17     rrKeyDynData = HKEY($80000006));
     9  TRegistryRoot = (rrKeyClassesRoot, rrKeyCurrentUser, rrKeyLocalMachine,
     10    rrKeyUsers, rrKeyPerformanceData, rrKeyCurrentConfig, rrKeyDynData);
    1811
    1912  { TRegistryContext }
     
    2215    RootKey: HKEY;
    2316    Key: string;
     17    class function Create(RootKey: TRegistryRoot; Key: string): TRegistryContext; static; overload;
     18    class function Create(RootKey: HKEY; Key: string): TRegistryContext; static; overload;
    2419    class operator Equal(A, B: TRegistryContext): Boolean;
    2520  end;
     
    3227    procedure SetCurrentContext(AValue: TRegistryContext);
    3328  public
     29    function ReadChar(const Name: string): Char;
     30    procedure WriteChar(const Name: string; Value: Char);
    3431    function ReadBoolWithDefault(const Name: string;
    3532      DefaultValue: Boolean): Boolean;
    3633    function ReadIntegerWithDefault(const Name: string; DefaultValue: Integer): Integer;
    3734    function ReadStringWithDefault(const Name: string; DefaultValue: string): string;
     35    function ReadCharWithDefault(const Name: string; DefaultValue: Char): Char;
    3836    function ReadFloatWithDefault(const Name: string;
    3937      DefaultValue: Double): Double;
     38    function ReadDateTimeWithDefault(const Name: string; DefaultValue: TDateTime): TDateTime;
    4039    function DeleteKeyRecursive(const Key: string): Boolean;
    4140    function OpenKey(const Key: string; CanCreate: Boolean): Boolean;
     
    4342  end;
    4443
    45 function RegContext(RootKey: HKEY; Key: string): TRegistryContext;
     44const
     45  RegistryRootHKEY: array[TRegistryRoot] of HKEY = (HKEY_CLASSES_ROOT,
     46    HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_PERFORMANCE_DATA,
     47    HKEY_CURRENT_CONFIG, HKEY_DYN_DATA);
    4648
    4749
    4850implementation
    49 
    50 function RegContext(RootKey: HKEY; Key: string): TRegistryContext;
    51 begin
    52   Result.RootKey := RootKey;
    53   Result.Key := Key;
    54 end;
    5551
    5652{ TRegistryContext }
     
    5955begin
    6056  Result := (A.Key = B.Key) and (A.RootKey = B.RootKey);
     57end;
     58
     59class function TRegistryContext.Create(RootKey: TRegistryRoot; Key: string): TRegistryContext;
     60begin
     61  Result.RootKey := RegistryRootHKEY[RootKey];
     62  Result.Key := Key;
     63end;
     64
     65class function TRegistryContext.Create(RootKey: HKEY; Key: string): TRegistryContext;
     66begin
     67  Result.RootKey := RootKey;
     68  Result.Key := Key;
    6169end;
    6270
     
    8391end;
    8492
     93function TRegistryEx.ReadCharWithDefault(const Name: string; DefaultValue: Char
     94  ): Char;
     95begin
     96  if ValueExists(Name) then Result := ReadChar(Name)
     97    else begin
     98      WriteChar(Name, DefaultValue);
     99      Result := DefaultValue;
     100    end;
     101end;
     102
    85103function TRegistryEx.ReadFloatWithDefault(const Name: string;
    86104  DefaultValue: Double): Double;
     
    89107    else begin
    90108      WriteFloat(Name, DefaultValue);
     109      Result := DefaultValue;
     110    end;
     111end;
     112
     113function TRegistryEx.ReadDateTimeWithDefault(const Name: string;
     114  DefaultValue: TDateTime): TDateTime;
     115begin
     116  if ValueExists(Name) then Result := ReadDateTime(Name)
     117    else begin
     118      WriteDateTime(Name, DefaultValue);
    91119      Result := DefaultValue;
    92120    end;
     
    113141function TRegistryEx.OpenKey(const Key: string; CanCreate: Boolean): Boolean;
    114142begin
    115   {$IFDEF Linux}
    116   CloseKey;
     143  {$IFDEF UNIX}
     144  //CloseKey;
    117145  {$ENDIF}
    118   Result := inherited OpenKey(Key, CanCreate);
     146  Result := inherited;
    119147end;
    120148
    121149function TRegistryEx.GetCurrentContext: TRegistryContext;
    122150begin
    123   Result.Key := CurrentPath;
     151  Result.Key := String(CurrentPath);
    124152  Result.RootKey := RootKey;
    125153end;
     
    129157  RootKey := AValue.RootKey;
    130158  OpenKey(AValue.Key, True);
     159end;
     160
     161function TRegistryEx.ReadChar(const Name: string): Char;
     162var
     163  S: string;
     164begin
     165  S := ReadString(Name);
     166  if Length(S) > 0 then Result := S[1]
     167    else Result := #0;
     168end;
     169
     170procedure TRegistryEx.WriteChar(const Name: string; Value: Char);
     171begin
     172  WriteString(Name, Value);
    131173end;
    132174
Note: See TracChangeset for help on using the changeset viewer.