Sha256: f24e35db8537283da803654810e6bee76f345decbc4c6d3be484c1afd9368e70

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

module Mapi
	# This is a helper class for {Pst} and {Msg}.
	class Helper
		# @return [String, nil] Encoding name of ANSI string we assume
		attr_reader :ansi_encoding

		# @return [Boolean] Convert all ANSI string to UTF-8
		attr_reader :to_unicode

		# @param ansi_encoding [String]
		# @param to_unicode [Boolean]
		def initialize ansi_encoding=nil, to_unicode=false
			@ansi_encoding = ansi_encoding || "BINARY"
			@to_unicode = to_unicode
		end

		# 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}
		#
		# @param str [String]
		# @return [Object]
		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
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
libis-mapi-0.3.1 lib/mapi/helper.rb