Project

General

Profile

Bug #9016 ยป encoding-lying.rb

renatosilva (Renato Silva), 10/12/2013 04:02 PM

 
1
# Encoding: UTF-8
2

    
3
expected_encoding = Encoding::CP850
4
path = ARGV[0]
5

    
6
if path == nil or path.encoding != expected_encoding
7
    puts "Please pass me a #{expected_encoding.name} file path to an existing file."
8
    exit
9
end
10

    
11
puts "Path encoding is #{path.encoding} and is #{path.valid_encoding? ? "valid" : "invalid"}."
12

    
13
begin
14
    puts "Here is content of #{path}:"
15
    puts File.readlines(path)
16
rescue Exception => e
17
    puts "I could not open #{path}"
18
end
19

    
20
puts
21
puts "So... we must encode the path into its very own encoding, and from a different encoding???"
22
puts "It's just like path.encoding is lying and we need to fix it!"
23
puts "    new_path = path.encode(path.encoding, \"ISO-8859-1\")"
24
puts
25

    
26
begin
27
    new_path = path.encode(path.encoding, "ISO-8859-1")
28
    puts "Here is content of #{new_path}:"
29
    puts File.readlines(new_path)
30
rescue Exception => e
31
    puts "I could not open #{new_path}, this should not have happened."
32
    puts "Please replace 'ISO-8859-1' in source file with your best bet for the 'actual' encoding."
33
end