Module: ICU::Lib

Extended by:
FFI::Library
Defined in:
lib/ffi-icu/lib.rb,
lib/ffi-icu/lib/util.rb

Defined Under Namespace

Modules: Util Classes: UParseError, UTransPosition, VersionInfo

Class Method Summary collapse

Class Method Details

.attach_optional_functionObject



195
196
197
198
199
# File 'lib/ffi-icu/lib.rb', line 195

def self.attach_optional_function(*)
  attach_function(*)
rescue FFI::NotFoundError
  # ignore
end

.check_errorObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/ffi-icu/lib.rb', line 121

def self.check_error
  ptr = FFI::MemoryPointer.new(:int)
  ret = yield(ptr)
  error_code = ptr.read_int

  if error_code.positive?
    name = Lib.u_errorName(error_code)
    if name == 'U_BUFFER_OVERFLOW_ERROR'
      raise(BufferOverflowError)
    elsif name == 'U_MISSING_RESOURCE_ERROR'
      raise(MissingResourceError)
    else
      raise(Error, name)
    end
  elsif error_code.negative?
    warn("ffi-icu: #{Lib.u_errorName(error_code)}") if $DEBUG || $VERBOSE
  end

  ret
end

.cldr_versionObject



185
186
187
188
189
# File 'lib/ffi-icu/lib.rb', line 185

def self.cldr_version
  @cldr_version ||= VersionInfo.new.tap do |version|
    check_error { |status| ulocdata_getCLDRVersion(version, status) }
  end
end

.enum_ptr_to_array(enum_ptr) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/ffi-icu/lib.rb', line 142

def self.enum_ptr_to_array(enum_ptr)
  length = check_error do |status|
    uenum_count(enum_ptr, status)
  end

  len = FFI::MemoryPointer.new(:int)

  (0...length).map do |_idx|
    check_error { |st| uenum_next(enum_ptr, len, st) }
  end
end

.figure_suffix(version) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ffi-icu/lib.rb', line 91

def self.figure_suffix(version)
  # For some reason libicu prepends its exported functions with version information,
  # which differs across all platforms.  Some examples:
  #
  # OSX:
  #   u_errorName
  #
  # CentOS 5
  #   u_errorName_3_6
  #
  # Fedora 14 and Windows (using mingw)
  #   u_errorName_44
  #
  # So we need to figure out which one it is.

  # Here are the possible suffixes
  suffixes = ['']
  suffixes << "_#{version}" << "_#{version[0].chr}_#{version[1].chr}" << "_#{version.split('.')[0]}" if version

  # Try to find the u_errorName function using the possible suffixes
  suffixes.find do |suffix|
    function_name = "u_errorName#{suffix}"
    function_names(function_name, nil).find do |fname|
      ffi_libraries.find do |lib|
        lib.find_function(fname)
      end
    end
  end
end

.find_lib(lib) ⇒ Object



30
31
32
33
34
# File 'lib/ffi-icu/lib.rb', line 30

def self.find_lib(lib)
  Dir.glob(search_paths.map do |path|
    File.expand_path(File.join(path, lib))
  end).first
end

.icu_version(libs) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ffi-icu/lib.rb', line 76

def self.icu_version(libs)
  version = nil

  libs.find do |lib|
    # Get the version - sure would be nice if libicu exported this in a function
    # we could just call cause this is super fugly!
    match = lib.name.match(/(\d\d)\.#{FFI::Platform::LIBSUFFIX}/) ||
            lib.name.match(/#{FFI::Platform::LIBSUFFIX}\.(\d\d)/)
    version = match[1] if match
  end

  # Note this may return nil, like on OSX
  version
end

.load_icuObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ffi-icu/lib.rb', line 36

def self.load_icu
  lib_names =
    case ICU.platform
    when :linux, :bsd
      [find_lib("libicui18n.#{FFI::Platform::LIBSUFFIX}.??"),
       find_lib("libicutu.#{FFI::Platform::LIBSUFFIX}.??")]
    when :osx
      if ENV.key?('FFI_ICU_LIB')
        # Ensure we look in the user-supplied override dir for a user-compiled libicu
        [find_lib("libicui18n.??.#{FFI::Platform::LIBSUFFIX}"),
         find_lib("libicutu.??.#{FFI::Platform::LIBSUFFIX}")]
      elsif Gem::Version.new(`sw_vers -productVersion`) >= Gem::Version.new('11')
        ["libicucore.#{FFI::Platform::LIBSUFFIX}"]
      else
        [find_lib("libicucore.#{FFI::Platform::LIBSUFFIX}")]
      end
    when :windows
      [find_lib("{lib,}icuuc??.#{FFI::Platform::LIBSUFFIX}"),
       find_lib("{lib,}icuin??.#{FFI::Platform::LIBSUFFIX}")]
    end

  lib_names&.compact!

  if !lib_names || lib_names.empty?
    raise(LoadError,
          "Could not find ICU on #{ICU.platform.inspect}. " \
          'Patches welcome, or you can add the containing directory yourself: ' \
          "#{self}.search_paths << '/path/to/lib'")
  end

  # And now try to load the library
  begin
    libs = ffi_lib(*lib_names)
  rescue LoadError => e
    raise(LoadError, "no idea how to load ICU on #{ICU.platform.inspect}, patches appreciated! (#{e.message})")
  end

  icu_version(libs)
end

.not_available(func_name) ⇒ Object



154
155
156
157
158
# File 'lib/ffi-icu/lib.rb', line 154

def self.not_available(func_name)
  self.class.send(:define_method, func_name) do |*_args|
    raise(Error, "#{func_name} not available on platform #{ICU.platform.inspect}")
  end
end

.resource_bundle_name(type) ⇒ Object



494
495
496
497
# File 'lib/ffi-icu/lib.rb', line 494

def self.resource_bundle_name(type)
  stem = "icudt#{version.read_array_of_char(4)[0]}l-"
  stem + type.to_s
end

.search_pathsObject



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ffi-icu/lib.rb', line 16

def self.search_paths
  @search_paths ||= if ENV['FFI_ICU_LIB']
                      [ENV['FFI_ICU_LIB']]
                    elsif FFI::Platform::IS_WINDOWS
                      ENV['PATH'].split(File::PATH_SEPARATOR)
                    else
                      [
                        '/usr/local/{lib64,lib}',
                        '/opt/local/{lib64,lib}',
                        '/usr/{lib64,lib}'
                      ] + Dir['/usr/lib/*-linux-gnu'] # for Debian Multiarch http://wiki.debian.org/Multiarch
                    end
end

.versionObject



191
192
193
# File 'lib/ffi-icu/lib.rb', line 191

def self.version
  @version ||= VersionInfo.new.tap { |version| u_getVersion(version) }
end