Module: Kakasi::Lib

Extended by:
Importer, Library
Includes:
DL
Defined in:
lib/kakasi/dl.rb,
lib/kakasi/ffi.rb,
lib/kakasi/fiddle.rb,
lib/kakasi/platform.rb

Constant Summary collapse

Fiddle =
DL
Pointer =
DL::CPtr
FFI =
Rubinius::FFI
INTERNAL_ENCODING =
Encoding::CP932
LIBPREFIX =
case RbConfig::CONFIG['host_os']
when /mingw|mswin/i
  ''.freeze
when /cygwin/i
  'cyg'.freeze
else
  'lib'.freeze
end
LIBSUFFIX =
case RbConfig::CONFIG['host_os']
when /darwin/i
  'dylib'.freeze
when /mingw|mswin|cygwin/i
  'dll'.freeze
else
  'so'.freeze
end
DEFLIBPATH =
begin
  [RbConfig::CONFIG['libir']] |
    case RbConfig::CONFIG['host_os']
    when /mingw|mswin/i
      []
    else
      %w[/lib /usr/lib]
    end
end.freeze

Class Method Summary collapse

Class Method Details

.kakasi(options, string) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/kakasi/ffi.rb', line 26

def kakasi(options, string)
  @mutex.synchronize {
    if options != @options
      kakasi_close_kanwadict() if @options

      args = ['kakasi', *options.split]

      argc = args.size
      argv = FFI::MemoryPointer.new(:pointer, argc).
        write_array_of_pointer(args.map { |arg|
          FFI::MemoryPointer.from_string(arg)
        })

      kakasi_getopt_argv(argc, argv).zero? or
        raise "failed to initialize kakasi"

      @options = options.dup
    end

    encoding = string.encoding
    result = ''.force_encoding(INTERNAL_ENCODING)
    string.encode(INTERNAL_ENCODING).split(/(\0+)/).each { |str, nul|
      buf = kakasi_do(str)
      result << buf.read_string.force_encoding(INTERNAL_ENCODING)
      kakasi_free(buf)
      result << nul if nul
    }
    result.encode(encoding)
  }
end

.map_library_name(name) ⇒ Object



39
40
41
# File 'lib/kakasi/platform.rb', line 39

def self.map_library_name(name)
  "#{LIBPREFIX}#{name}.#{LIBSUFFIX}"
end

.try_load(libpath = LIBPATH) {|basename| ... } ⇒ Object

Yields:

  • (basename)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/kakasi/platform.rb', line 43

def self.try_load(libpath = LIBPATH)
  basename = map_library_name('kakasi')
  (libpath | DEFLIBPATH).each { |dir|
    begin
      filename = File.join(dir, basename)
      yield filename
      return filename
    rescue LoadError, StandardError
      next
    end
  }

  yield basename
  return basename
end