Thread overview
Q: char[][] args to C char**argv
Sep 28, 2003
Ant
Re: char[][] args to C char**argv
Sep 28, 2003
Walter
Sep 28, 2003
Ant
Sep 28, 2003
Helmut Leitner
September 28, 2003
How do we do that?

D source:

extern(C) c_parse_args((int argc,char **argv);

void main(char[][] args)
{
     char** argv = ?;

     int argc = args.length

     c_parse_args(argc, argv);
}

Ant


September 28, 2003
char** argv = new char*[args.length];
int i = 0;
foreach (char[] p; args)
{
    argv[i++] = cast(char*)p;
}

"Ant" <Ant_member@pathlink.com> wrote in message news:bl5s9q$140q$1@digitaldaemon.com...
> How do we do that?
>
> D source:
>
> extern(C) c_parse_args((int argc,char **argv);
>
> void main(char[][] args)
> {
>      char** argv = ?;
>
>      int argc = args.length
>
>      c_parse_args(argc, argv);
> }
>
> Ant
>
>


September 28, 2003

Ant wrote:
> 
> How do we do that?
> 
> D source:
> 
> extern(C) c_parse_args((int argc,char **argv);
> 
> void main(char[][] args)
> {
>      char** argv = ?;
> 
>      int argc = args.length
> 
>      c_parse_args(argc, argv);
> }

This should do the job:

    import string;

    alias char [] String;

    char** ArgsRetArgv(String [] args)
    {
      char* [] argv;
      argv.length=args.length+1;

      for(int i; i<args.length; i++) {
        argv[i] = (char *)args[i];
      }
      return argv;
    }

    int main (String [] args)
    {
      char** argv=ArgsRetArgv(args);
//      c_parse_args(args.length,argv);
      return 0;
    }

Its based on the (tested) assumption, that args consists of slices on the existing '\0'-terminated argv-buffer.

-- 
Helmut Leitner    leitner@hls.via.at
Graz, Austria   www.hls-software.com
September 28, 2003
In article <bl62sm$1dq0$1@digitaldaemon.com>, Walter says...
>
>char** argv = new char*[args.length];
>int i = 0;
>foreach (char[] p; args)
>{
>    argv[i++] = cast(char*)p;
>}
>

Thanks.
Maybe this couldl be on one of the C to D pages.

Thanks also to Helmut.

Ant