0x2a

Nov 14, 2009

0x2a - Smoothly installing the "charguess" gem

One of the solutions to guessing a text's encoding in rails is the charguess gem. Sadly, though, this isn't well prepared to a "gem install charguess" session. The following commands will gladly download, compile and install the charguess gem on your machine:

1
2
3
4
5
6
7
8
git clone git://github.com/fnando/charguess.git
cd charguess/libcharguess
./configure
make
cd ../gem
ruby extconf.rb --with-charguess-include=../libcharguess --with-charguess-lib=../libcharguess
make
sudo make install

If you are on Linux you might see an error like this:

1
2
3
4
/usr/bin/ld: ../libcharguess/libcharguess.a(charguess.o): relocation R_X86_64_32 against
   `__gxx_personality_v0@@CXXABI_1.3' can not be used when making a shared object; 
   recompile with -fPIC
../libcharguess/libcharguess.a: could not read symbols: Bad value

in that case you change the ./configure line into

1
CXXFLAGS=-fPIC ./configure

And if you like my style: the following gives you String#iconv, with guessing:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
require 'iconv'

begin
  require 'charguess' # not necessary if input encoding is known
rescue MissingSourceFile
  STDERR.puts "install 'charguess', see http://radiospiel.org/0x2a-smooth-charguess-install"
end

class String
  def iconv(output_encoding, input_encoding=nil)
    input_encoding ||= CharGuess::guess(self)                 # => "windows-1252"
    Iconv.new(output_encoding.to_s, input_encoding.to_s).iconv(self)
  end
end

#
# "some string".iconv("utf-8")
Toggle Comments