<commit_before>require "fiddle"

module RubyInstaller
  # :nodoc:
  class DllDirectory
    class WinApiError < RuntimeError
    end

    KERNEL32 = Fiddle.dlopen('kernel32.dll')
    begin
      SetDefaultDllDirectories = Fiddle::Function.new(
        KERNEL32['SetDefaultDllDirectories'], [Fiddle::TYPE_LONG], Fiddle::TYPE_INT
      )

      AddDllDirectory = Fiddle::Function.new(
        KERNEL32['AddDllDirectory'], [Fiddle::TYPE_VOIDP], Fiddle::TYPE_VOIDP
      )

      RemoveDllDirectory = Fiddle::Function.new(
        KERNEL32['RemoveDllDirectory'], [Fiddle::TYPE_VOIDP], Fiddle::TYPE_INT
      )
    rescue Fiddle::DLError
      @@add_dll_dir_available = false
    else
      @@add_dll_dir_available = ENV['RI_FORCE_PATH_FOR_DLL'] == '1' ? false : true
    end

    attr_reader :path

    # Set default search paths to the application directory (where ruby.exe resides) and to paths that are added per DllDirectory.new().
    # It disables the PATH environment variable for DLL search.
    #
    # This methos is usually called while RubyInstaller startup.
    def self.set_defaults
      if @@add_dll_dir_available
        set_default_dll_directories_winapi
      end
    end

    def initialize(path)
      path = File.expand_path(path)

      if @@add_dll_dir_available
        # Prefer Winapi function AddDllDirectory(), which requires Windows 7 with KB2533623 or newer.
        self.class.set_default_dll_directories_winapi
        @handle = self.class.add_dll_directory_winapi(path)
        @path = path
      else
        # For older systems fall back to the legacy method of using PATH environment variable.
        if ENV['PATH'].include?(path)
          @handle = nil
          @path = nil
        else
          puts "Temporarily enhancing PATH by #{path}..." #if $DEBUG
          ENV['PATH'] = path + ";" + ENV['PATH']
          @handle = nil
          @path = path
        end
      end
      return unless block_given?
      begin
        yield self
      ensure
        remove
      end
    end

    # Set default search paths to LOAD_LIBRARY_SEARCH_DEFAULT_DIRS to include path added by add_dll_directory_winapi() and exclude paths set per PATH environment variable.
    def self.set_default_dll_directories_winapi
      raise WinApiError, "SetDefaultDllDirectories failed" if SetDefaultDllDirectories.call(0x00001000)==0<commit_msg>Log to stderr and only if DEBUG is enabled. [ci skip]<commit_after>require "fiddle"

module RubyInstaller
  # :nodoc:
  class DllDirectory
    class WinApiError < RuntimeError
    end

    KERNEL32 = Fiddle.dlopen('kernel32.dll')
    begin
      SetDefaultDllDirectories = Fiddle::Function.new(
        KERNEL32['SetDefaultDllDirectories'], [Fiddle::TYPE_LONG], Fiddle::TYPE_INT
      )

      AddDllDirectory = Fiddle::Function.new(
        KERNEL32['AddDllDirectory'], [Fiddle::TYPE_VOIDP], Fiddle::TYPE_VOIDP
      )

      RemoveDllDirectory = Fiddle::Function.new(
        KERNEL32['RemoveDllDirectory'], [Fiddle::TYPE_VOIDP], Fiddle::TYPE_INT
      )
    rescue Fiddle::DLError
      @@add_dll_dir_available = false
    else
      @@add_dll_dir_available = ENV['RI_FORCE_PATH_FOR_DLL'] == '1' ? false : true
    end

    attr_reader :path

    # Set default search paths to the application directory (where ruby.exe resides) and to paths that are added per DllDirectory.new().
    # It disables the PATH environment variable for DLL search.
    #
    # This methos is usually called while RubyInstaller startup.
    def self.set_defaults
      if @@add_dll_dir_available
        set_default_dll_directories_winapi
      end
    end

    def initialize(path)
      path = File.expand_path(path)

      if @@add_dll_dir_available
        # Prefer Winapi function AddDllDirectory(), which requires Windows 7 with KB2533623 or newer.
        self.class.set_default_dll_directories_winapi
        @handle = self.class.add_dll_directory_winapi(path)
        @path = path
      else
        # For older systems fall back to the legacy method of using PATH environment variable.
        if ENV['PATH'].include?(path)
          @handle = nil
          @path = nil
        else
          $stderr.puts "Temporarily enhancing PATH by #{path}..." if $DEBUG
          ENV['PATH'] = path + ";" + ENV['PATH']
          @handle = nil
          @path = path
        end
      end
      return unless block_given?
      begin
        yield self
      ensure
        remove
      end
    end

    # Set default search paths to LOAD_LIBRARY_SEARCH_DEFAULT_DIRS to include path added by add_dll_directory_winapi() and exclude paths set per PATH environment variable.
    def self.set_default_dll_directories_winapi
      raise WinApiError, "SetDefaultDllDirectories failed" if SetDefaultDllDirectories.call(0x00001000)==0
{'fraction_non_alphanumeric': 0.07810203653159774, 'fraction_numerical': 0.01217719924417384, 'mean_word_length': 2.85125303152789, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '3500477', 'n_tokens_mistral': 1505, 'n_tokens_neox': 1385, 'n_words': 379}