Re: multiple pointers [GEN]
Hi,
I'll have a go at this, but I can't test it on my machine at work.
>//in C
>
>struct hostent {
> char *h_name; // official name of host
> char **h_aliases; // alias list
> int h_addrtype; // host address type
> int h_length; // length of address
> char **h_addr_list; // list of addresses
>};
-- Assuming that Linux is being used and that 'int' is a 32-bit signed
integer.
constant
h_name = 0,
h_aliases = 4,
h_addrtype = 8,
h_length = 12,
h_addr_list = 16
atom hostent_p,
name_p,
aliases_p,
alias_p,
addr_list_p,
addr_p,
addrtype,
length
-- Get address of the hostent structure
hostent_p = get_hostent()
-- Get the address of the first character in the name.
name_p = peek4u(hostent_p + h_name)
-- Get the address of the first alias pointer
aliases_p = peek4u(hostent_p + h_aliases)
-- Get the address of the first character in the first alias
alias_p = peek4u( aliases_p )
-- Get the '32-bit int' address type
addrtype = peek4s(hostent_p + h_addrtype)
-- Get the '32-bit int' length
length = peek4s(hostent_p + h_length)
-- Get the address of the first addr in the list
addr_list_p = peek4u(hostent_p + h_addr_list)
-- Get the address of the first char in the first addr in the list
addr_p = peek4u(addr_list_p)
With the name_p address, you can use the earlier reply to convert a C-string
into a Euphoria sequence.
theName = CstrToSeq(name_p)
With the aliases and the addr_list, you also need to know how many entries
are in these lists. From memory, I think that these lists are terminated by
a zero address. If so you can do something like ...
-- initialize my alias sequence to empty
aliases = {}
-- Copy the Pointer-to-pointer address
memaddress = aliases_p
-- collect aliases until I find a zero memory address
while 1 do
-- pick up the next pointer to an alias C-string
alias_p = peek4u( memaddress )
if alias_p = 0 then
exit -- Got them all.
end if
-- convert to a sequence and add to the my alias list
aliases = append(aliases, CstrToSeq( alias_p ) )
-- calculate where the next alias pointer is.
memaddress += 4
end while
-----
cheers,
Derek Parnell
derekp at solace.com.au
Solace Limited ( http://www.solace.com.au )
Melbourne, Australia
+61 3 9291 7557
|
Not Categorized, Please Help
|
|