seanohalpin / ffi-ncurses

Interface to ncurses using Ruby FFI (Foreign Function Interface)
MIT License
82 stars 21 forks source link

wchar_t size #9

Open stronny opened 11 years ago

stronny commented 11 years ago

typedefs.rb: typedef :ushort, :wchar_t

Is there a reason to hardcode wchar_t size?

Here's a trivial C program that on my system produces: wchar_t: 4, wint_t: 4:

#include <stdio.h>
#include <wchar.h>

int main() { printf("wchar_t: %d, wint_t: %d\n", sizeof(wchar_t), sizeof(wint_t)); }

Related to this is the following issue (widechars.rb):

buffer_size = (txt.size + 1) * wchar_t.size
buffer = FFI::Buffer.new(wchar_t, buffer_size)

The second argument to Buffer.new is the number of elements in the buffer (http://rubydoc.info/github/ffi/ffi/master/FFI/Buffer#initialize-instance_method), not the size of the buffer in bytes, so buffer_size is twice what's needed and that's why this code works on my machine, ironically.

I am no expert in FFI and C and all this, but I'm sure there must be a way to determine the size of wchar_t and alias is appropriately.