<commit_before>module Blizzardry
  class MPQ
    require 'blizzardry/mpq/file'
    require 'blizzardry/mpq/storm'

    def initialize(handle)
      @handle = handle
    end

    def patch(archive)
      Storm.SFileOpenPatchArchive(@handle, archive, nil, 0)
    end

    def patched?
      Storm.SFileIsPatchedArchive(@handle)
    end

    def close
      Storm.SFileCloseArchive(@handle)
    end

    def has?(filename)
      Storm.SFileHasFile(@handle, filename)
    end

    def find(query)
      result = Storm::SearchResult.new

      handle = Storm.SFileFindFirstFile(@handle, query, result, nil)
      unless handle.null?
        yield result[:filename].to_s

        while Storm.SFileFindNextFile(handle, result)
          yield result[:filename].to_s
        end
      end
    ensure
      Storm.SFileFindClose(handle)
    end

    def get(filename)
      handle = FFI::MemoryPointer.new :pointer
      if Storm.SFileOpenFileEx(@handle, filename, 0, handle)
        File.new(handle.read_pointer)
      end
    end

    def extract(filename, local)
      Storm.SFileExtractFile(@handle, filename, local, 0)
    end

    class << self

      private :new

      def open(archive)
        handle = FFI::MemoryPointer.new :pointer
        Storm.SFileOpenArchive(archive, 0, 0, handle)

        mpq = new(handle.read_pointer)
        if block_given?
          yield mpq
          mpq.close
          nil
        else
          mpq<commit_msg>Return nil in case of MPQ open failure<commit_after>module Blizzardry
  class MPQ
    require 'blizzardry/mpq/file'
    require 'blizzardry/mpq/storm'

    def initialize(handle)
      @handle = handle
    end

    def patch(archive)
      Storm.SFileOpenPatchArchive(@handle, archive, nil, 0)
    end

    def patched?
      Storm.SFileIsPatchedArchive(@handle)
    end

    def close
      Storm.SFileCloseArchive(@handle)
    end

    def has?(filename)
      Storm.SFileHasFile(@handle, filename)
    end

    def find(query)
      result = Storm::SearchResult.new

      handle = Storm.SFileFindFirstFile(@handle, query, result, nil)
      unless handle.null?
        yield result[:filename].to_s

        while Storm.SFileFindNextFile(handle, result)
          yield result[:filename].to_s
        end
      end
    ensure
      Storm.SFileFindClose(handle)
    end

    def get(filename)
      handle = FFI::MemoryPointer.new :pointer
      if Storm.SFileOpenFileEx(@handle, filename, 0, handle)
        File.new(handle.read_pointer)
      end
    end

    def extract(filename, local)
      Storm.SFileExtractFile(@handle, filename, local, 0)
    end

    class << self

      private :new

      def open(archive)
        handle = FFI::MemoryPointer.new :pointer
        return unless Storm.SFileOpenArchive(archive, 0, 0, handle)

        mpq = new(handle.read_pointer)
        if block_given?
          yield mpq
          mpq.close
          nil
        else
          mpq
{'fraction_non_alphanumeric': 0.08330488221235917, 'fraction_numerical': 0.003414134516899966, 'mean_word_length': 2.2628062360801784, 'pattern_counts': {'":': 0, '<': 7, '<?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': '473529', 'n_tokens_mistral': 917, 'n_tokens_neox': 879, 'n_words': 208}