Class: Mapi::Helper

Inherits:
Object
  • Object
show all
Defined in:
lib/mapi/helper.rb

Overview

This is a helper class for Pst and Msg.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ansi_encoding = nil, to_unicode = false) ⇒ Helper

Returns a new instance of Helper.

Parameters:

  • ansi_encoding (String) (defaults to: nil)
  • to_unicode (Boolean) (defaults to: false)


12
13
14
15
# File 'lib/mapi/helper.rb', line 12

def initialize ansi_encoding=nil, to_unicode=false
	@ansi_encoding = ansi_encoding || "BINARY"
	@to_unicode = to_unicode
end

Instance Attribute Details

#ansi_encodingString? (readonly)

Returns Encoding name of ANSI string we assume.

Returns:

  • (String, nil)

    Encoding name of ANSI string we assume



5
6
7
# File 'lib/mapi/helper.rb', line 5

def ansi_encoding
  @ansi_encoding
end

#to_unicodeBoolean (readonly)

Returns Convert all ANSI string to UTF-8.

Returns:

  • (Boolean)

    Convert all ANSI string to UTF-8



8
9
10
# File 'lib/mapi/helper.rb', line 8

def to_unicode
  @to_unicode
end

Instance Method Details

#convert_ansi_str(str) ⇒ Object

Convert ‘ASCII_8BIT` string. Maybe produce UTF_8 string, or arbitrary object

Use cases:

  • Decode PT_STRING8 in Pst

  • Decode ‘0x001e` in Msg

  • Decode body (rtf, text) in PropertySet

Parameters:

  • str (String)

Returns:

  • (Object)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mapi/helper.rb', line 27

def convert_ansi_str str
	return nil unless str
	if @ansi_encoding
		if @to_unicode
			# assume we can convert this text to UTF-8
			begin
				str.force_encoding(@ansi_encoding).encode("UTF-8")
			rescue Encoding::UndefinedConversionError => ex
				# some text are already UTF-8 due to unknown reason
				str.force_encoding("UTF-8").encode("UTF-8")
			end
		else
			str.force_encoding(@ansi_encoding)
		end
	else
		str
	end
end