DocAda(tm) is a productivity tool of KSCE

____ | Up | Next | Back | Forward
About DocAda Light: Preface / Preliminary / Help / TOC / Copyright
DocAda Online at the Ada Home: Complete RM95 / Updates / News

B.3.1 The Package Interfaces.C.Strings

The package Interfaces.C.Strings declares types and subprograms allowing an Ada program to allocate, reference, update, and free C-style strings. In particular, the private type chars_ptr corresponds to a common use of ``char *'' in C programs, and an object of this type can be passed to a subprogram to which pragma Import(C,...) has been applied, and for which ``char *'' is the type of the argument of the C function.

Static Semantics

The library package Interfaces.C.Strings has the following declaration:

   
       package Interfaces.C.Strings is
          pragma Preelaborate(Strings);

          type char_array_access is access all char_array;

          type chars_ptr is private;

          type chars_ptr_array is array (size_t range <>) of chars_ptr;

          Null_Ptr : constant chars_ptr;

          function To_Chars_Ptr (Item      : in char_array_access;
                                 Nul_Check : in Boolean := False)
             return chars_ptr;

          function New_Char_Array (Chars   : in char_array) return chars_ptr;

          function New_String (Str : in String) return chars_ptr;

          procedure Free (Item : in out chars_ptr);

          Dereference_Error : exception;

          function Value (Item : in chars_ptr) return char_array;

          function Value (Item : in chars_ptr; Length : in size_t)
             return char_array;

          function Value (Item : in chars_ptr) return String;

          function Value (Item : in chars_ptr; Length : in size_t)
             return String;

          function Strlen (Item : in chars_ptr) return size_t;

          procedure Update (Item   : in chars_ptr;
                            Offset : in size_t;
                            Chars  : in char_array;
                            Check  : in Boolean := True);

          procedure Update (Item   : in chars_ptr;
                            Offset : in size_t;
                            Str    : in String;
                            Check  : in Boolean := True);

          Update_Error : exception;

       private
          ... -- not specified by the language
       end Interfaces.C.Strings;

The type chars_ptr is C-compatible and corresponds to the use of C's ``char *'' for a pointer to the first char in a char array terminated by nul. When an object of type chars_ptr is declared, its value is by default set to Null_Ptr, unless the object is imported (see B.1).

    
       function To_Chars_Ptr (Item      : in char_array_access;
                              Nul_Check : in Boolean := False)
          return chars_ptr;
    
       function New_Char_Array (Chars   : in char_array) return chars_ptr;

    
       function New_String (Str : in String) return chars_ptr;
    
       procedure Free (Item : in out chars_ptr);
    
       function Value (Item : in chars_ptr) return char_array;
    
       function Value (Item : in chars_ptr; Length : in size_t)
          return char_array;
    
       function Value (Item : in chars_ptr) return String;
    
       function Value (Item : in chars_ptr; Length : in size_t)
          return String;
    
       function Strlen (Item : in chars_ptr) return size_t;
    
       procedure Update (Item   : in chars_ptr;
                         Offset : in size_t;
                         Chars  : in char_array;
                         Check  : Boolean := True);
    
       procedure Update (Item   : in chars_ptr;
                         Offset : in size_t;
                         Str    : in String;
                         Check  : in Boolean := True);

Erroneous Execution

Execution of any of the following is erroneous if the Item parameter is not null_ptr and Item does not point to a nul-terminated array of chars.

Execution of Free(X) is also erroneous if the chars_ptr X was not returned by New_Char_Array or New_String.

Reading or updating a freed char_array is erroneous.

Execution of Update is erroneous if Check is False and a call with Check equal to True would have propagated Update_Error.

About DocAda Light: Preface / Preliminary / Help / TOC / Copyright
DocAda Online at the Ada Home: Complete RM95 / Updates / News
____ | Up | Next | Back | Forward


Copyright © 1994-1997 by Kempe Software Capital Enterprises. All Rights Reserved. For comments on this DocAda(tm) hypertext document, please write to KSCE, at docada@ksce.com