[Win32utils-devel] Fwd: FFI::MemoryPointer.read_wide_string?

Heesob Park phasis at gmail.com
Tue Nov 12 02:56:45 UTC 2013


2013/11/12 Daniel Berger <djberg96 at gmail.com>:
> I take it that method does not currently exist (I didn't see it in the
> source).
> What about modifying the existing Pointer#read_string method so that it
> accepts an encoding as an optional second argument?
> I'm not sure how to implement that, though. It appears that
> Pointer#read_string is calling get_string, which is defined in
> AbstractMemory.c (in the FFI source). Is it a simple matter of using wmemchr
> instead of memchr? Or is it more complex than that?
I think it would be preferable to implement in pointer.rb, not AbstractMemory.c.

> Or maybe this is better implemented as its own method. I defer to you. :)
Here is a draft implementation

module FFI
  class Pointer
    def read_string_with_enc(len=nil,enc=nil,trail=false)
        len = self.size unless len
        enc = Encoding.default_external unless enc
        null_char = "\x00".encode(enc)
        str = self.read_string(len).force_encoding(enc)
        str = str[0,str.index(null_char)||str.bytesize]
        str += null_char if trail
    def read_wide_string(len=nil,trail=false)

The line 64 of file.rb
ext = PathFindExtensionW(wfile).read_string(suffix.length * 2).delete(0.chr)
can be changed to
ext = PathFindExtensionW(wfile).read_wide_string(MAX_PATH*2,true)

Park Heesob

More information about the win32utils-devel mailing list