tash_8.7.2_4c588c12/src/cargv.ads

  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
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
--------------------------------------------------------------------
--
--  cargv.ads -- Create C-style "argv" vectors from strings and
--              Ada.Command_Line.
--
--  Copyright (c) 1995-2000 Terry J. Westley
--
--  See the file "license.htm" for information on usage and
--  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
--
--  This package provides the data type Chars_Ptr_Ptr which corresponds
--  to the char** of C and subprograms for creating and manipulating
--  arrays of C strings.
--
--------------------------------------------------------------------

with Interfaces.C.Pointers;
with Interfaces.C.Strings;

package CArgv is

   package C renames Interfaces.C;

   subtype CNatural is C.int range 0 .. C.int'Last;

   type Vector is array (CNatural range <>) of aliased C.Strings.chars_ptr;
   --  This is a C-style "argv" vector.

   package Argv_Pointer
   is new C.Pointers (Index => CNatural,
                      Element => C.Strings.chars_ptr,
                      Element_Array => Vector,
                      Default_Terminator => C.Strings.Null_Ptr);

   subtype Chars_Ptr_Ptr is Argv_Pointer.Pointer;
   --  This is C char **

   ---------------------------------------------------------------------
   --
   --  The following subprograms support converting command line
   --  arguments to C-style argc/argv command line arguments.
   --
   ---------------------------------------------------------------------

   procedure Create (Argc : out CNatural; Argv : out Chars_Ptr_Ptr);
   --  Create returns the command line arguments from Ada.Command_Line
   --  and converts them to a C-style, null-terminated argument vector.

   procedure Show (Argc : CNatural; Argv : Chars_Ptr_Ptr);
   --  Prints Argc and Argv to standard out.

   procedure Free (Argv : in out Chars_Ptr_Ptr);
   --  Free all space used by Argv.

   --  Example of getting Ada command line arguments and passing them
   --  to a C function requiring argc/argv arguments:
   --
   --    declare
   --       Argc : C.Int;
   --       Argv : CArgv.Chars_Ptr_Ptr;
   --    begin
   --       CArgv.Create (Argc, Argv);
   --       Tcl.Tcl_Concat (Argc, Argv);
   --       CArgv.Free (Argv);
   --    end;

   ---------------------------------------------------------------------
   --
   --  The following subprogram supports retrieving a command line
   --  argument from C-style argv command line arguments.
   --
   ---------------------------------------------------------------------

   function Arg (Argv : Chars_Ptr_Ptr; N : CNatural) return String;
   --  Returns the Nth argument from Argv.

   ---------------------------------------------------------------------
   --
   --  The following subprograms support creating C-style argc/argv
   --  argument vectors from strings.
   --
   ---------------------------------------------------------------------

   function Empty return Chars_Ptr_Ptr;
   --  An empty Chars_Ptr_Ptr, used for constructors.

   function "&" (Argv : Chars_Ptr_Ptr; Arg : String) return Chars_Ptr_Ptr;
   --  Construct a Chars_Ptr_Ptr using concat operation.

   function Argc (Argv : Chars_Ptr_Ptr) return CNatural;
   --  Returns the number of arguments in a Chars_Ptr_Ptr.

   --  Example of creating a Chars_Ptr_Ptr to pass to a C function requiring
   --  argc/argv arguments:
   --
   --    declare
   --       Argv : CArgv.Chars_Ptr_Ptr :=
   --          Empty & "this" & "is" & "four" & "arguments";
   --    begin
   --       Tcl.Tcl_Concat (CArgv.Argc (Argv), Argv);
   --       CArgv.Free (Argv);
   --    end;

end CArgv;