Skip to content

Commit

Permalink
Encoding updates.
Browse files Browse the repository at this point in the history
- Add magic encoding comments (forcing ASCII-8BIT) to most files.
  This fixes ruby-1.9 problems where if internal encoding was set
  to UTF-8 then various compares fail (eg MAGIC test).
- Add test for using unicode filenames in ole objects.
- Add deprecation warning to ole/file_system require.

git-svn-id: https://ruby-ole.googlecode.com/svn/trunk@73 f297d60c-f930-0410-b1c6-9ffd80c20a0c
  • Loading branch information
aquasync committed Apr 8, 2010
1 parent 156dc99 commit 4f734f7
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/ole/base.rb
@@ -1,3 +1,5 @@
# encoding: ASCII-8BIT


require 'ole/support'

Expand Down
7 changes: 6 additions & 1 deletion lib/ole/file_system.rb
@@ -1,2 +1,7 @@
# keeping this file around for now, but will delete later on...
warn <<-end
Use of ole/file_system is deprecated. Use ole/storage/file_system
or better just ole/storage (file_system is recommended api and is
enabled by default).
end

require 'ole/storage/file_system'
2 changes: 2 additions & 0 deletions lib/ole/ranges_io.rb
@@ -1,3 +1,5 @@
# encoding: ASCII-8BIT

# need IO::Mode
require 'ole/support'

Expand Down
2 changes: 2 additions & 0 deletions lib/ole/storage/base.rb 100755 → 100644
@@ -1,3 +1,5 @@
# encoding: ASCII-8BIT

require 'tempfile'

require 'ole/base'
Expand Down
2 changes: 2 additions & 0 deletions lib/ole/storage/file_system.rb
@@ -1,3 +1,5 @@
# encoding: ASCII-8BIT

#
# = Introduction
#
Expand Down
2 changes: 2 additions & 0 deletions lib/ole/storage/meta_data.rb
@@ -1,3 +1,5 @@
# encoding: ASCII-8BIT

require 'ole/types/property_set'

module Ole
Expand Down
2 changes: 2 additions & 0 deletions lib/ole/support.rb
@@ -1,3 +1,5 @@
# encoding: ASCII-8BIT

#
# A file with general support functions used by most files in the project.
#
Expand Down
4 changes: 3 additions & 1 deletion lib/ole/types/base.rb
@@ -1,3 +1,5 @@
# encoding: ASCII-8BIT

require 'iconv'
require 'date'

Expand Down Expand Up @@ -49,7 +51,7 @@ def self.dump str
data = TO_UTF16.iconv str
# not sure if this is the recommended way to do it, but I want to treat
# the resulting utf16 data as regular bytes, not characters.
data.force_encoding Encoding::US_ASCII if data.respond_to? :encoding
data.force_encoding Encoding::ASCII_8BIT if data.respond_to? :encoding
data
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/ole/types/property_set.rb
@@ -1,3 +1,5 @@
# encoding: ASCII-8BIT

require 'ole/types'
require 'yaml'

Expand Down
23 changes: 23 additions & 0 deletions test/test_filesystem.rb
Expand Up @@ -873,7 +873,30 @@ def test_tell_seek

end

class OleUnicodeTest < Test::Unit::TestCase
def setup
@io = StringIO.new ''
end

def test_unicode
# in ruby-1.8, encoding is assumed to be UTF-8 (and converted with iconv).
# in ruby-1.9, UTF-8 works also, but probably shouldn't be using fixed
# TO_UTF16 iconv for other encodings.
resume = "R\xc3\xa9sum\xc3\xa9"
resume.force_encoding Encoding::UTF_8 if resume.respond_to? :encoding
Ole::Storage.open @io do |ole|
ole.file.open(resume, 'w') { |f| f.write 'Skills: writing bad unit tests' }
end
Ole::Storage.open @io do |ole|
assert_equal ['.', '..', resume], ole.dir.entries('.')
# use internal api to verify utf16 encoding
assert_equal "R\x00\xE9\x00s\x00u\x00m\x00\xE9\x00", ole.root.children[0].name_utf16[0, 6 * 2]
assert_equal 'Skills', ole.file.read(resume).split(': ', 2).first
end
end
end

# Copyright (C) 2002, 2003 Thomas Sondergaard
# rubyzip is free software; you can redistribute it and/or
# modify it under the terms of the ruby license.

0 comments on commit 4f734f7

Please sign in to comment.