<commit_before>// Inno Setup utilities
//
// Copyright (c) 2009 <NAME>
// Revision: 11/03/2009 8:04:17 PM
// License: MIT

// forward declarations
function MungePathish(const SrcList: TStringList; NewData: Array of String; RegValue: String; IsUninstalling: Boolean): Boolean; forward;
procedure ModifyPathish(NewData: Array of String; RegValue, Delim: String); forward;
function StrToList(const SrcString: String; Delim: String): TStringList; forward;
function ListToStr(const SrcList: TStringList; Delim: String): String; forward;

function IsAdmin(): Boolean;
begin
  Result := IsAdminLoggedOn or IsPowerUserLoggedOn;
end;

function IsNotAdmin(): Boolean;
begin
  Result := not (IsAdminLoggedOn or IsPowerUserLoggedOn);
end;

function GetUserHive(): Integer;
begin
  if IsAdminLoggedOn or IsPowerUserLoggedOn then
    Result :=  HKLM
  else
    Result := HKCU;
end;

function GetEnvironmentKey(): String;
begin
  if IsAdmin then
    Result := 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
  else
    Result := 'Environment';
end;

procedure ModifyFileExts(Exts: Array of String);
begin
  ModifyPathish(Exts, 'PATHEXT', ';');
end;

procedure ModifyPath(Exts: Array of String);
begin
  ModifyPathish(Exts, 'PATH', ';');
end;

// Modifies path-like registry keys such as PATH, PATHEXT
procedure ModifyPathish(NewData: Array of String; RegValue, Delim: String);
var
  NeedRegChange: Boolean;
  RootKey: Integer;
  SubKey, OrigData, NewPathish, Tmp: String;
  PathishList: TStringList;
begin
  RootKey := GetUserHive;
  SubKey := GetEnvironmentKey;

  try
    RegQueryStringValue(RootKey, SubKey, RegValue, OrigData);
    Log('Original ' + AnsiUppercase(RegValue) + ': ' + OrigData);

    PathishList := StrToList(OrigData, Delim);

    NeedRegChange := MungePathish(PathishList, NewData, RegValue, IsUninstaller);

    if NeedRegChange then
    begin
      NewPathish := ListToStr(PathishList, ';');

      case AnsiUppercase(RegValue) of
        'PATH': RegWriteExpandStringValue(RootKey, SubKey, 'Path', NewPathish);
        'PATHEXT': RegWriteExpandStringValue(RootKey, SubKey, 'PATHEXT', NewPathish);
      end;
      Log(AnsiUppercase(RegValue) + ' updated to: ' + NewPathish);
      
      // remove values if empty after uninstaller reverts its mods
      if IsUninstaller then
      begin
        if RegQueryStringValue(RootKey, SubKey, RegValue, Tmp) then
        begin
          if Tmp = '' then<commit_msg>Mod and cleanup file associations for non-admins<commit_after>// Inno Setup utilities
//
// Copyright (c) 2009 <NAME>
// Revision: 11/03/2009 8:04:17 PM
// License: MIT

// forward declarations
function MungePathish(const SrcList: TStringList; NewData: Array of String; RegValue: String; IsUninstalling: Boolean): Boolean; forward;
procedure ModifyPathish(NewData: Array of String; RegValue, Delim: String); forward;
function StrToList(const SrcString: String; Delim: String): TStringList; forward;
function ListToStr(const SrcList: TStringList; Delim: String): String; forward;

function IsAdmin(): Boolean;
begin
  Result := IsAdminLoggedOn or IsPowerUserLoggedOn;
end;

function IsNotAdmin(): Boolean;
begin
  Result := not (IsAdminLoggedOn or IsPowerUserLoggedOn);
end;

function GetUserHive(): Integer;
begin
  if IsAdminLoggedOn or IsPowerUserLoggedOn then
    Result :=  HKLM
  else
    Result := HKCU;
end;

function GetEnvironmentKey(): String;
begin
  if IsAdmin then
    Result := 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
  else
    Result := 'Environment';
end;

procedure ModifyFileExts(Exts: Array of String);
begin
  ModifyPathish(Exts, 'PATHEXT', ';');
end;

procedure ModifyPath(Exts: Array of String);
begin
  ModifyPathish(Exts, 'PATH', ';');
end;

// Modifies path-like registry keys such as PATH, PATHEXT
procedure ModifyPathish(NewData: Array of String; RegValue, Delim: String);
var
  NeedRegChange: Boolean;
  RootKey: Integer;
  SubKey, OrigData, NewPathish, Tmp, TmpExpandable: String;
  PathishList: TStringList;
begin
  RootKey := GetUserHive;
  SubKey := GetEnvironmentKey;

  try
    RegQueryStringValue(RootKey, SubKey, RegValue, OrigData);
    Log('Original ' + AnsiUppercase(RegValue) + ': ' + OrigData);

    // ensure originally empty users PATHEXT also contains system values
    if (RootKey = HKCU) and (AnsiUppercase(RegValue) = 'PATHEXT') and (OrigData = '') then
    begin
      Log('Empty HKCU ' + AnsiUppercase(RegValue) + ', prepending %PATHEXT% to new value');
      OrigData := ('%' + RegValue + '%');
    end;

    PathishList := StrToList(OrigData, Delim);

    NeedRegChange := MungePathish(PathishList, NewData, RegValue, IsUninstaller);

    if NeedRegChange then
    begin
      NewPathish := ListToStr(PathishList, ';');

      case AnsiUppercase(RegValue) of
        'PATH': RegWriteExpandStringValue(RootKey, SubKey, 'Path', NewPathish);
        'PATHEXT': RegWriteExpandStringValue(RootKey, SubKey, 'PATHEXT', NewPathish);
      end;
      Log(AnsiUppercase(RegValue) + ' updated to: ' + NewPathish);
      
      // remove values if empty after uninstaller reverts its mods
      if IsUninstaller then
      begin
        if RegQueryStringValue(RootKey, SubKey, RegValue, Tmp) then
        begin
          // If the key is empty or expandable version (%RegValue%), remove it.
          TmpExpandable := '%' + RegValue + '%';
          if (Tmp = '') or (Tmp = TmpExpandable) then
{'fraction_non_alphanumeric': 0.10671863400148478, 'fraction_numerical': 0.006310319227913883, 'mean_word_length': 4.2626953125, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4012182', 'n_tokens_mistral': 1775, 'n_tokens_neox': 1697, 'n_words': 486}