1. [GEN] Re: multiple pointers
On Sun, 15 Oct 2000, Bernie wrote:
>> On Sun, 15 Oct 2000 02:11:49 -0600, cense <cense at MAIL.RU> wrote:
>>
>> cense:
>>
>> I don't know exactly what you are trying to do, could you
>> show me a little more of the "C" function you are trying
>> to use and I will help you. Is "C" returning you a pointer to
>> a pointer of a string ? I need to see a little code.
>>
>> Bernie
Well i did not ask for help yet, but sure! Thanks for offer Bernie :)
The function im trying to wrap right now is `gethostbyname` :
// from the man page:
stuct hostent *gethostbyname( const char *name );
`gethostbyname` returns a pointer to a struct of type hostent, my first
thought was to access this struct like so:
-- Eu code
pointer hostent_ptr
pointer fake_hostent_ptr
hostent = get_hostent( ) -- this function does the `struc` call and sets it up
fake_hostent_ptr = c_func( gethostbyname_, { char_ptr } )
mem_copy( hostent_ptr, fake_hostent_ptr, sizeof( fake_hostent_ptr ) )
blah = Gets( hostent_ptr, "this" ) -- etc
-- end Eu code
Is this the correct way to get to the struct returned by `gethostbyname` ?
There probably is a more effecient way to do it.
OK, another problem:
Here is what the struct returned by `gethostbyname` looks like:
//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
};
So here is my second problem, How do i get a pointer out of a mixedlib.e
struc? I know to use Gets( ) for values. Would loc( ) be what i need to use
to get a pointer out of the struc and into a euphoria variable?
eg.
pointer my_ptr
my_ptr = loc( hostent_ptr, "h_name" )
Now would my_ptr point to the same memory that `char *h_name` in the
struc does?
Ok i know this post is getting long and disorganized but i have one more
thing to address, its related to teh above question. How do i get `char
**h_aliases` and `char **h_addr_list` into euphoria variables?
I just repeated myself basically because it should be the same as just
getting a single pointer from the struc, No?
Anyway, if you can make any sense of my rantings, than i would greatly
appreciate any feedback.
Thanks for all the help along the way Bernie. Your name is already on my
CONTRIB list for both mixedlib.e and all the help you have offered me :)
--
evil, corruption and bad taste
^cense
2. [GEN] Re: multiple pointers
On Sun, 15 Oct 2000, Mark Brown wrote:
>> Hi Cense
hello mark
>> I replied but perhaps what I suggested was dumb.
>>
>> You can't get pointers to your Euphoria atoms. You need to allocate memory
>> to hold the values you require. You then save a pointer to the memory (which
>> will need to be a certain size to hold the value(s) you need).
>>
>> (You probably knew all this anyway!)
ya i knew this ;)
>> Perhaps you should try peek4u instead to get the whole pointer value.
>>
>> -- to get at the list using your pointer to the list
>> ptr = peek4u(ptr_to_list) -- should give us the pointer to the list
>> all_values_in_list = peek({ptr, 50}) -- read all values in the list into a
>> sequence
thanks! This is the kinda response i was looking for.
>> Could you let us know what you are working on?
I am working on a GNU/Linux Interprocess Communication wrapper for Euphoria.
It started out as just a socket wrapper but im goin to expand it further. Lots
of C code to deal with.
Right now i have a working socket wrapper but it needs more features. So that
is what im working on.
>> If I have got this totally wrong please let me know.
Actually this is what i was looking for. I will go and try what you suggested
now. Hopefully it will work.
>> All the best
thanks :)
>> Mark
--
evil, corruption and bad taste
^cense
3. [GEN] Re: multiple pointers
Well Mark, your explanations did help me out, but it did not solve my
problem. It seems (im 99% sure) that my problem lies deeper in my code than
just what i explained. Probably has something to do with actually *getting*
this pointer to a pointer.
I think my problem is with mixedlib.e by Bernie Ryan. Im not suggesting that
anything is wrong with the lib, actually its quite good, i like it alot, but
it seems that i dont know how to use it properly or am just missing something
subtle. I will contact Bernie himself sometime about it if i dont make any
progress.
Thanks again Mark!
--
evil, corruption and bad taste
^cense
4. Re: [GEN] Re: multiple pointers
On Sun, 15 Oct 2000 02:11:49 -0600, cense <cense at MAIL.RU> wrote:
cense:
I don't know exactly what you are trying to do, could you
show me a little more of the "C" function you are trying
to use and I will help you. Is "C" returning you a pointer to
a pointer of a string ? I need to see a little code.
Bernie
5. Re: [GEN] Re: multiple pointers
On Sun, 15 Oct 2000 12:36:08 -0600, cense <cense at MAIL.RU> wrote:
cense :
This code is untested but it just to give an idea of what
you need to do. Add the procedure below to mixedlib.e to
make working with structure pointers easier.
Bernie
-----------------------------------------------------------------<>---------
--
-- Associates pointer to a structure that was returned by a "C"
--
global
procedure AssociatePtr( pointer address, sequence structure )
-- declare
pointer ptr
---| ptr | ST | name | offset | type | size | ... | total bytes |---
ptr = struc(structure,HIGH)
MastList[length(MastList)][1] = address
free(ptr)
--
end procedure -- end of AssociatePtr procedure
-----------------------------------------------------------------<>---------
I am assuming that you are using Linux.
Add the above procedure to the mixedlib.e at the end of the code.
-- "C" structure definition
-- 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
--};
-- stuct hostent *gethostbyname( const char *name );
When "C" returns the pointer to hostent structure use the above
procedure to associate the pointer to the hostent structure
like this.
AssociatePtr(hostent_ptr, " h_name : pointer : 1 "&
" h_aliases : pointer : 1 "&
" h_addrtype : int : 1 "&
" h_length : int : 200 "&
" h_addr_list : pointer : 1 ",
HIGH) -- allocate in high memory
Now the hostent_ptr can be used to access the structure that
"C" placed in memory.
pointer host_name_ptr
-- this would return the host name
host_name_ptr = Gets( hostent_ptr, "h_name" )
-- this would return the host name but as a sequence
pointer host_name_str
sequence host_name_seq
host_name_str = string(255) -- create an empty string
-- copy out the string
host_name_str = strcpy(host_name_str, host_name_ptr)
-- you can convert to sequence for euphoria use
host_name_seq = str2seq(host_name_str)
-- or you can use the "C" string functions to manipulate it.
-- heres how to get the pointer to a pointer
pointer h_aliases_ptr_2_ptr
h_aliases_ptr_2_ptr = peek4u(Gets( hostent_ptr, "h_aliases" ))
pointer h_addr_list_ptr_2_ptr
h_addr_list_ptr_2_ptr = peek4u(Gets( hostent_ptr, "h_addr_list" ))
6. Re: [GEN] Re: multiple pointers
- Posted by Bernie <xotron at PCOM.NET>
Oct 15, 2000
-
Last edited Oct 16, 2000
On Sun, 15 Oct 2000 19:46:20 -0400, Bernie <xotron at PCOM.NET> wrote:
cense : I made a mistake the procedure code should be
this:
-----------------------------------------------------------------<>---------
--
-- Associates pointer to a structure that was returned by a "C"
--
global
procedure AssociatePtr( pointer address, sequence structure )
-- declare
pointer ptr
---| ptr | ST | name | offset | type | size | ... | total bytes |---
ptr = struc(structure)
MastList[length(MastList)][1] = address
free(ptr)
--
end procedure -- end of AssociatePtr procedure
-----------------------------------------------------------------<>---------
Bernie
7. Re: [GEN] Re: multiple pointers
- Posted by Bernie <xotron at PCOM.NET>
Oct 15, 2000
-
Last edited Oct 16, 2000
On Sun, 15 Oct 2000 19:46:20 -0400, Bernie <xotron at PCOM.NET> wrote:
cense :
Whoops another typing ERROR this should have been.
AssociatePtr(hostent_ptr, " h_name : pointer : 1 "&
" h_aliases : pointer : 1 "&
" h_addrtype : int : 1 "&
" h_length : int : 1 "&
" h_addr_list : pointer : 1 ",
HIGH) -- allocate in high memory
I guess I'am just tired out and sleepy. Sorry about the mistakes.
Bernie
8. [GEN] Re: multiple pointers
- Posted by cense <cense at MAIL.RU>
Oct 15, 2000
-
Last edited Oct 16, 2000
Thanks alot Bernie and Derek. I dont know what i would do without this help
of this list. Now, i have not tried any of the solutions *yet* but im sure by
reading them that i will easily be able to adapt them if they are not 100%
correct already :)
--
evil, corruption and bad taste
^cense