Module: Tins::FileBinary

Defined in:
lib/tins/file_binary.rb

Defined Under Namespace

Modules: ClassMethods, Constants

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.default_optionsObject

Default options can be queried/set via this hash.



17
18
19
# File 'lib/tins/file_binary.rb', line 17

def default_options
  @default_options
end

Class Method Details

.included(modul) ⇒ Object



60
61
62
63
64
65
# File 'lib/tins/file_binary.rb', line 60

def self.included(modul)
  modul.instance_eval do
    extend ClassMethods
  end
  super
end

Instance Method Details

#ascii?(options = {}) ⇒ Boolean

Returns true if FileBinary#binary? returns false, false if FileBinary#binary? returns true, and nil otherwise. For an explanation of options, see FileBinary#binary?.

Returns:

  • (Boolean)


53
54
55
56
57
58
# File 'lib/tins/file_binary.rb', line 53

def ascii?(options = {})
  case binary?(options)
  when true   then false
  when false  then true
  end
end

#binary?(options = {}) ⇒ Boolean

Returns true if this file is considered to be binary, false if it is not considered to be binary, and nil if it was empty.

A file is considered to be binary if the percentage of zeros exceeds options[:percentage_zeros] or the percentage of binary bytes (8-th bit is 1) exceeds options[:percentage_binary] in the buffer of size options[:buffer_size] that is checked (beginning from offset options[:offset]). If an option isn’t given the one from FileBinary.default_options is used instead.

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tins/file_binary.rb', line 35

def binary?(options = {})
  options = FileBinary.default_options.merge(options)
  old_pos = tell
  seek options[:offset], Constants::SEEK_SET
  data = read options[:buffer_size]
  !data or data.empty? and return nil
  data_size = data.size
  data.count(Constants::ZERO).to_f / data_size >
    options[:percentage_zeros] / 100.0 and return true
  data.count(Constants::BINARY).to_f / data_size >
    options[:percentage_binary] / 100.0
ensure
  old_pos and seek old_pos, Constants::SEEK_SET
end