Ignore:
Timestamp:
Jun 4, 2024, 12:22:49 AM (5 months ago)
Author:
chronos
Message:
  • Modified: Removed U prefix from unit names.
  • Modified: Updated Common package.
File:
1 moved

Legend:

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

    r74 r75  
    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;
     
    4341  end;
    4442
    45 function RegContext(RootKey: HKEY; Key: string): TRegistryContext;
     43const
     44  RegistryRootHKEY: array[TRegistryRoot] of HKEY = (HKEY_CLASSES_ROOT,
     45    HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_PERFORMANCE_DATA,
     46    HKEY_CURRENT_CONFIG, HKEY_DYN_DATA);
    4647
    4748
    4849implementation
    49 
    50 function RegContext(RootKey: HKEY; Key: string): TRegistryContext;
    51 begin
    52   Result.RootKey := RootKey;
    53   Result.Key := Key;
    54 end;
    5550
    5651{ TRegistryContext }
     
    5954begin
    6055  Result := (A.Key = B.Key) and (A.RootKey = B.RootKey);
     56end;
     57
     58class function TRegistryContext.Create(RootKey: TRegistryRoot; Key: string): TRegistryContext;
     59begin
     60  Result.RootKey := RegistryRootHKEY[RootKey];
     61  Result.Key := Key;
     62end;
     63
     64class function TRegistryContext.Create(RootKey: HKEY; Key: string): TRegistryContext;
     65begin
     66  Result.RootKey := RootKey;
     67  Result.Key := Key;
    6168end;
    6269
     
    7986    else begin
    8087      WriteString(Name, DefaultValue);
     88      Result := DefaultValue;
     89    end;
     90end;
     91
     92function TRegistryEx.ReadCharWithDefault(const Name: string; DefaultValue: Char
     93  ): Char;
     94begin
     95  if ValueExists(Name) then Result := ReadChar(Name)
     96    else begin
     97      WriteChar(Name, DefaultValue);
    8198      Result := DefaultValue;
    8299    end;
     
    113130function TRegistryEx.OpenKey(const Key: string; CanCreate: Boolean): Boolean;
    114131begin
    115   {$IFDEF Linux}
    116   CloseKey;
     132  {$IFDEF UNIX}
     133  //CloseKey;
    117134  {$ENDIF}
    118135  Result := inherited OpenKey(Key, CanCreate);
     
    121138function TRegistryEx.GetCurrentContext: TRegistryContext;
    122139begin
    123   Result.Key := CurrentPath;
     140  Result.Key := String(CurrentPath);
    124141  Result.RootKey := RootKey;
    125142end;
     
    129146  RootKey := AValue.RootKey;
    130147  OpenKey(AValue.Key, True);
     148end;
     149
     150function TRegistryEx.ReadChar(const Name: string): Char;
     151var
     152  S: string;
     153begin
     154  S := ReadString(Name);
     155  if Length(S) > 0 then Result := S[1]
     156    else Result := #0;
     157end;
     158
     159procedure TRegistryEx.WriteChar(const Name: string; Value: Char);
     160begin
     161  WriteString(Name, Value);
    131162end;
    132163
Note: See TracChangeset for help on using the changeset viewer.