char ** equivalent 
Author Message
 char ** equivalent

Hello,

I want to use an external C-function of the pattern
                                    void func (int *argcp, char **argv).

The "int *argcp" is clear. In Eiffel, I hand over "$argument_count".
But I have problems with the "char **argv".

I tried it by creating "argv: ARRAY [POINTER]", who's members contain
the result of "arguments.item (i).to_external" with "arguments" being
an "ARRAY [STRING]" and handing over "argv.to_external" to "func", but
I only got core dumps and segmentation faults.

So, is there a solution for my problem? (Compiler independent, please)

Thanks,
Jan



Tue, 28 Dec 2004 23:40:29 GMT  
 char ** equivalent

Quote:

> Hello,

> I want to use an external C-function of the pattern
>                                     void func (int *argcp, char **argv).

> The "int *argcp" is clear. In Eiffel, I hand over "$argument_count".
> But I have problems with the "char **argv".

> I tried it by creating "argv: ARRAY [POINTER]", who's members contain
> the result of "arguments.item (i).to_external" with "arguments" being
> an "ARRAY [STRING]" and handing over "argv.to_external" to "func", but
> I only got core dumps and segmentation faults.

> So, is there a solution for my problem? (Compiler independent, please)

It always helps to post the compiler, example code, and the errors.
Here's my example code. This works with SmallEiffel but won't work
with ISE 4.5. It might work with newer versions of the ISE compiler.
There's just not enough standardization among the compilers for there
to be a portable solution.

-- Test program to create an array of pointers
-- to C strings that can then be passed to a
-- C function in an ARGV-style array.
--
class TEST creation make
feature make is
      local
         bar: ARRAY[POINTER]
         entry: STRING
         do
            create bar.make (1, 6)
            from count := bar.lower
            until count > bar.upper
            loop
               entry := clone ("foo " + count.out)
               bar.put (entry.to_external, count)
               count := count + 1
            end        
            count := bar.count
            foo ($count, bar.to_external)
         end
feature
   foo (p1, p2: POINTER) is
    external
      "C macro use %"foo.h%""
    alias
       "foo"
    end
    count : INTEGER
end
----------------------------------------------------
// Include file for C function foo
//
void foo (int *argcp, char ** argv);

----------------------------------------------------
// foo.c
#include <stdio.h>
#include "foo.h"

void foo (int *argcp, char **argv)
{

   for (int index = 0; index < *argcp; ++index)
   {
      puts (argv[index]);
   }

Quote:
}

-----------------------------------------------------
Compile and execute:

C:\test\eiffel\argv>compile test foo.c
C:\test\eiffel\argv>test
foo 1
foo 2
foo 3
foo 4
foo 5
foo 6



Wed, 29 Dec 2004 06:44:13 GMT  
 char ** equivalent

Quote:

> It always helps to post the compiler, example code, and the errors.

Ok, next time, I will.

My solution was (almost) exactly the same as yours and now it works.
The problem was, that I handed over "$argument_count", which is the
address of the _routine_ "argument_count" inherited from GENERAL.
That, of course, couldn't work, but now it does. Thanks!

Could you tell me, which part of your solution ISE 4.5 wouldn't accept?



Thu, 30 Dec 2004 01:23:40 GMT  
 char ** equivalent

Quote:
> Could you tell me, which part of your solution ISE 4.5 wouldn't accept?

`to_external' is not a standard way of doing this. In ISE 4.5, you need to
do:

    bar: ARRAY [ANY]
    a: ANY

in the loop you do:

    bar.put (entry.to_c, count)

to call the C external:

    a := bar.to_c
    foo ($count, $a)

The reason it uses ANY instead of POINTER is that if you have a GC that
moves object (which is the case of the ISE GC) the POINTER value is only
valid for the time you get it, but at the next instruction it might point to
an invalid location.

Manu



Fri, 31 Dec 2004 04:58:23 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. awk equivalent for casting char to int?

2. Char data type equivalent

3. Equivalent to c/c++'s (int argc, char *argv[])

4. Object comment do: [:char | char yourself]

5. reversing lines char by char, but not the line order in a file

6. How to read line char by char?

7. CHAR and [CHAR]

8. ASCII vs. CHAR [CHAR]

9. CHAR with ARRAY OF CHAR assignment compatible ?

10. CHAR with ARRAY OF CHAR assignment compatible ?

11. write-char vs. display-char

12. Control chars and special chars

 

 
Powered by phpBB® Forum Software