0.0
No commit activity in last 3 years
No release in over 3 years
Convert from/to C native `wchar_t` string to used for FFI binding libraries.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 1.7
~> 5.0
>= 10.0
~> 0.41

Runtime

~> 1.0
 Project Readme

FfiWideChar Build Status

A Ruby gem helper for FFI binding libraries to convert from/to C native wchar_t wide-string.

It provides 2 methods: read_wide_string and to_wide_string for converting.

Installation

Add this line to your application's Gemfile:

gem 'ffi_wide_char'

And then execute:

$ bundle

Or install it yourself as:

$ gem install ffi_wide_char

Usage

Example of calling native library functions using wide-string type:

require 'ffi_wide_char'

# Unicode Popup Dialog
module Win
  extend FFI::Library

  ffi_lib 'user32'
  ffi_convention :stdcall

  attach_function :message_box, :MessageBoxW, %i[pointer buffer_in buffer_in int], :int
end

msg = FfiWideChar.to_wide_string('a中Я')
Win.message_box(nil, msg, msg, 0)

# Convert multibyte to wide-string
ptr = FFI::MemoryPointer.new(:char, 16)
FfiWideChar::LibC.mbstowcs(ptr, '©Ðõ'.encode('filesystem'), 4)

str = FfiWideChar.read_wide_string ptr
puts str.encode('UTF-8')

Development

After checking out the repo, run bundle install to install dependencies. Then, run bundle exec rake to run the tests. You can also run irb -r bundler/setup -r ffi_wide_char for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/nthachus/ffi_wide_char.

License

The gem is available as open source under the terms of the MIT License.