Module: Magic

Extended by:
FFI::Library
Defined in:
lib/libmagic.rb,
lib/libmagic.rb

Overview

Just to make things neater, split out the FFI part here

Constant Summary collapse

VERSION =
'0.5.11'
ASCII_CHARSET =
"us-ascii"
EXTENDED_ASCII_CHARSET =

currently libmagic doesn’t distinguish the various extended ASCII charsets except ISO-8859-1

"unknown"
PROBLEMATIC_EXTENDED_ASCII_CHAR =

windows-1252 ellipsis

133
REGEX =
/charset=(.+)$/
CHUNK_SIZE =
2 ** 15

Class Method Summary collapse

Class Method Details

.file_charset(filename) ⇒ Object



13
14
15
# File 'lib/libmagic.rb', line 13

def file_charset(filename)
  mime_type_to_charset(file_mime_type(filename))
end

.file_charset!(filename) ⇒ Object

Exhaustively checks file contents. The plan is you should always be able to trust the answer this method returns.



18
19
20
21
22
23
24
25
26
# File 'lib/libmagic.rb', line 18

def file_charset!(filename)
  quick_answer = file_charset(filename)

  if quick_answer == ASCII_CHARSET
    return File.open(filename) { |io| io_charset(io) } # try harder
  else
    return quick_answer
  end
end

.file_mime_type(filename) ⇒ Object



158
159
160
161
# File 'lib/libmagic.rb', line 158

def file_mime_type(filename)
  cookie = load_cookie
  return process_result(cookie, magic_file(cookie, filename))
end

.io_charset(io) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/libmagic.rb', line 28

def io_charset(io)
  io.rewind
  special_characters = collect_special_characters(io)
  return special_characters.empty? ? ASCII_CHARSET : string_charset(special_characters)
ensure
  io.rewind
end

.string_charset(text) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/libmagic.rb', line 36

def string_charset(text)
  quick_answer = mime_type_to_charset(string_mime_type(text))
  if quick_answer == ASCII_CHARSET
    text.each_byte { |byte| return EXTENDED_ASCII_CHARSET if byte == PROBLEMATIC_EXTENDED_ASCII_CHAR }
  end
  return quick_answer
end

.string_mime_type(string) ⇒ Object



153
154
155
156
# File 'lib/libmagic.rb', line 153

def string_mime_type(string)
  cookie = load_cookie
  return process_result(cookie, magic_buffer(cookie, string, string.size))
end