Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > ffi, struct pointer question

Reply
Thread Tools

ffi, struct pointer question

 
 
Daniel Berger
Guest
Posts: n/a
 
      08-02-2009
Hi,

I've recently been trying to port some C code over to FFI. I'm trying
to figure out how to unravel the gr_mem group struct member. Any
suggestions?

require 'ffi'

module Sys
class Admin
extend FFI::Library

class GroupStruct < FFI::Struct
layout(
:gr_name, :string,
:gr_passwd, :string,
:gr_gid, :int,
:gr_mem, ointer
)
end

attach_function :getgrgid, [:int], ointer

def self.get_group(gid)
GroupStruct.new(getgrgid(gid))
end
end
end

# How do I unravel the gr_mem struct member?
struct = Sys::Admin.get_group(84)
p struct[:gr_mem]

Regards,

Dan

 
Reply With Quote
 
 
 
 
Thomas Chust
Guest
Posts: n/a
 
      08-02-2009
2009/8/2 Daniel Berger <>:
> [...]
> =A0 =A0 =A0class GroupStruct < FFI::Struct
> =A0 =A0 =A0 =A0 layout(
> [...]
> =A0 =A0 =A0 =A0 =A0 =A0:gr_mem, =A0 =A0ointer
> =A0 =A0 =A0 =A0 )
> =A0 =A0 =A0end
>
> =A0 =A0 =A0attach_function :getgrgid, [:int], ointer
>
> =A0 =A0 =A0def self.get_group(gid)
> =A0 =A0 =A0 =A0 GroupStruct.new(getgrgid(gid))
> =A0 =A0 =A0end
> [...]
> # How do I unravel the gr_mem struct member?
> struct =3D Sys::Admin.get_group(84)
> p struct[:gr_mem]


Hello,

the gr_mem member of the group structure is a NULL terminated array of
zero terminated strings, so you need to read all the string pointers
from the array until you encounter a null pointer and then read the
string data from each string pointer.

I would suggest the use of an auxiliary method, which could actually
be added to the FFI:ointer class for convenience:

class FFI:ointer
def read_string_array_to_null()
elements =3D []

psz =3D self.class.size
loc =3D self
until ((element =3D loc.read_pointer).null?)
elements << element.read_string_to_null
loc +=3D psz
end

elements
end
end

Using this method, you can unravel the mysterious pointer member as follows=
:

p struct[:gr_mem].read_string_array_to_null




I hope that helps,
Thomas


--=20
When C++ is your hammer, every problem looks like your thumb.

 
Reply With Quote
 
 
 
 
Daniel Berger
Guest
Posts: n/a
 
      08-02-2009


On Aug 2, 6:13=A0am, Thomas Chust <ch...@web.de> wrote:
> 2009/8/2 Daniel Berger <djber...@gmail.com>:
>
>
>
> > [...]
> > =A0 =A0 =A0class GroupStruct < FFI::Struct
> > =A0 =A0 =A0 =A0 layout(
> > [...]
> > =A0 =A0 =A0 =A0 =A0 =A0:gr_mem, =A0 =A0ointer
> > =A0 =A0 =A0 =A0 )
> > =A0 =A0 =A0end

>
> > =A0 =A0 =A0attach_function :getgrgid, [:int], ointer

>
> > =A0 =A0 =A0def self.get_group(gid)
> > =A0 =A0 =A0 =A0 GroupStruct.new(getgrgid(gid))
> > =A0 =A0 =A0end
> > [...]
> > # How do I unravel the gr_mem struct member?
> > struct =3D Sys::Admin.get_group(84)
> > p struct[:gr_mem]

>
> Hello,
>
> the gr_mem member of the group structure is a NULL terminated array of
> zero terminated strings, so you need to read all the string pointers
> from the array until you encounter a null pointer and then read the
> string data from each string pointer.
>
> I would suggest the use of an auxiliary method, which could actually
> be added to the FFI:ointer class for convenience:
>
> =A0 =A0 =A0 =A0 class FFI:ointer
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 def read_string_array_to_null()
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 elements =3D []
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 psz =3D self.class.size
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 loc =3D self
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 until ((element =3D loc.r=

ead_pointer).null?)
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 elements =

<< element.read_string_to_null
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 loc +=3D =

psz
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 end
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 elements
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 end
> =A0 =A0 =A0 =A0 end
>
> Using this method, you can unravel the mysterious pointer member as follo=

ws:
>
> =A0 =A0 =A0 =A0 p struct[:gr_mem].read_string_array_to_null
>
>
>
> I hope that helps,
> Thomas


Thank you for that Thomas, it worked great.

I also found nice-ffi, and I suspect it may have some convenient way
of doing what you're doing there, only I don't see it.

http://github.com/jacius/nice-ffi/tree/master

Regards,

Dan

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Struct pointer vs. struct array pointer aleksa C Programming 16 02-20-2013 08:20 PM
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
(: Pointer to struct withing pointer to struct :) Zero C Programming 16 11-19-2005 01:27 AM
passing pointer->struct->pointer->struct to function. .. ?? beetle C Programming 2 01-25-2005 06:08 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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 56 57