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['libdir']] |
    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

.try_load(libpath = LIBPATH) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/kakasi/platform.rb', line 40

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

  begin
    yield basename
  rescue LoadError
    raise
  rescue
    raise LoadError, $!.message
  end

  return basename
end