home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / adav313.zip / gnat-3_13p-os2-bin-20010916.zip / emx / gnatlib / i-cstrea.ads < prev    next >
Text File  |  2000-07-19  |  14KB  |  349 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT COMPILER COMPONENTS                         --
  4. --                                                                          --
  5. --                 I N T E R F A C E S . C _ S T R E A M S                  --
  6. --                                                                          --
  7. --                                 S p e c                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.23 $
  10. --                                                                          --
  11. --          Copyright (C) 1995-1999 Free Software Foundation, Inc.          --
  12. --                                                                          --
  13. -- GNAT is free software;  you can  redistribute it  and/or modify it under --
  14. -- terms of the  GNU General Public License as published  by the Free Soft- --
  15. -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
  16. -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  17. -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  18. -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
  19. -- for  more details.  You should have  received  a copy of the GNU General --
  20. -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
  21. -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
  22. -- MA 02111-1307, USA.                                                      --
  23. --                                                                          --
  24. -- As a special exception,  if other files  instantiate  generics from this --
  25. -- unit, or you link  this unit with other files  to produce an executable, --
  26. -- this  unit  does not  by itself cause  the resulting  executable  to  be --
  27. -- covered  by the  GNU  General  Public  License.  This exception does not --
  28. -- however invalidate  any other reasons why  the executable file  might be --
  29. -- covered by the  GNU Public License.                                      --
  30. --                                                                          --
  31. -- GNAT was originally developed  by the GNAT team at  New York University. --
  32. -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
  33. --                                                                          --
  34. ------------------------------------------------------------------------------
  35.  
  36.  
  37.  
  38. --  This package is a thin binding to selected functions in the C
  39. --  library that provide a complete interface for handling C streams.
  40.  
  41. with Unchecked_Conversion;
  42. with System;
  43. with System.Parameters;
  44.  
  45. package Interfaces.C_Streams is
  46. pragma Elaborate_Body (C_Streams);
  47.  
  48.    --  Note: the reason we do not use the types that are in Interfaces.C is
  49.    --  that we want to avoid dragging in the code in this unit if possible.
  50.  
  51.    lbits1 : constant := System.Parameters.long_bits - 1;
  52.  
  53.    subtype chars is System.Address;
  54.    --  Pointer to null-terminated array of characters
  55.  
  56.    subtype FILEs is System.Address;
  57.    --  Corresponds to the C type FILE*
  58.  
  59.    subtype voids is System.Address;
  60.    --  Corresponds to the C type void*
  61.  
  62.    subtype int is Integer;
  63.    --  Note: the above type is a subtype deliberately, and it is part of
  64.    --  this spec that the above correspondence is guaranteed. This means
  65.    --  that it is legitimate to, for example, use Integer instead of int.
  66.    --  We provide this synonym for clarity, but in some cases it may be
  67.    --  convenient to use the underlying types (for example to avoid an
  68.    --  unnecessary dependency of a spec on the spec of this unit).
  69.  
  70.    type long is range -(2 ** lbits1) .. +(2 ** lbits1) - 1;
  71.    --  Note: the above type also used to be a subtype, but the correspondence
  72.    --  was unused so it was made into a parameterized type to avoid having
  73.    --  multiple versions of this spec for systems where long /= Long_Integer.
  74.  
  75.    type size_t is mod 2 ** Standard'Address_Size;
  76.  
  77.    NULL_Stream : constant FILEs;
  78.    --  Value returned (NULL in C) to indicate an fdopen/fopen/tmpfile error
  79.  
  80.    ----------------------------------
  81.    -- Constants Defined in stdio.h --
  82.    ----------------------------------
  83.  
  84.    EOF : constant int;
  85.    --  Used by a number of routines to indicate error or end of file
  86.  
  87.    IOFBF : constant int;
  88.    IOLBF : constant int;
  89.    IONBF : constant int;
  90.    --  Used to indicate buffering mode for setvbuf call
  91.  
  92.    L_tmpnam : constant int;
  93.    --  Maximum length of file name that can be returned by tmpnam
  94.  
  95.    SEEK_CUR : constant int;
  96.    SEEK_END : constant int;
  97.    SEEK_SET : constant int;
  98.    --  Used to indicate origin for fseek call
  99.  
  100.    function stdin  return FILEs;
  101.    function stdout return FILEs;
  102.    function stderr return FILEs;
  103.    --  Streams associated with standard files
  104.  
  105.    --------------------------
  106.    -- Standard C functions --
  107.    --------------------------
  108.  
  109.    --  The functions selected below are ones that are available in DOS,
  110.    --  OS/2, UNIX and Xenix (but not necessarily in ANSI C). These are
  111.    --  very thin interfaces which copy exactly the C headers. For more
  112.    --  documentation on these functions, see the Microsoft C "Run-Time
  113.    --  Library Reference" (Microsoft Press, 1990, ISBN 1-55615-225-6),
  114.    --  which includes useful information on system compatibility.
  115.  
  116.    procedure clearerr (stream : FILEs);
  117.  
  118.    function fclose (stream : FILEs) return int;
  119.  
  120.    function fdopen (handle : int; mode : chars) return FILEs;
  121.  
  122.    function feof (stream : FILEs) return int;
  123.  
  124.    function ferror (stream : FILEs) return int;
  125.  
  126.    function fflush (stream : FILEs) return int;
  127.  
  128.    function fgetc (stream : FILEs) return int;
  129.  
  130.    function fgets (strng : chars; n : int; stream : FILEs) return chars;
  131.  
  132.    function fileno (stream : FILEs) return int;
  133.  
  134.    function fopen (filename : chars; Mode : chars) return FILEs;
  135.    --  Note: to maintain target independence, use text_translation_required,
  136.    --  a boolean variable defined in a-sysdep.c to deal with the target
  137.    --  dependent text translation requirement. If this variable is set,
  138.    --  then b/t should be appended to the standard mode argument to set
  139.    --  the text translation mode off or on as required.
  140.  
  141.    function fputc (C : int; stream : FILEs) return int;
  142.  
  143.    function fputs (Strng : chars; Stream : FILEs) return int;
  144.  
  145.    function fread
  146.      (buffer : voids;
  147.       size   : size_t;
  148.       count  : size_t;
  149.       stream : FILEs)
  150.       return   size_t;
  151.  
  152.    function fread
  153.      (buffer : voids;
  154.       index  : size_t;
  155.       size   : size_t;
  156.       count  : size_t;
  157.       stream : FILEs)
  158.       return   size_t;
  159.    --  Same as normal fread, but has a parameter 'index' that indicates
  160.    --  the starting index for the read within 'buffer' (which must be the
  161.    --  address of the beginning of a whole array object with an assumed
  162.    --  zero base). This is needed for systems that do not support taking
  163.    --  the address of an element within an array.
  164.  
  165.    function freopen
  166.      (filename : chars;
  167.       mode     : chars;
  168.       stream   : FILEs)
  169.       return     FILEs;
  170.  
  171.    function fseek
  172.      (stream : FILEs;
  173.       offset : long;
  174.       origin : int)
  175.       return   int;
  176.  
  177.    function ftell (stream : FILEs) return long;
  178.  
  179.    function fwrite
  180.      (buffer : voids;
  181.       size   : size_t;
  182.       count  : size_t;
  183.       stream : FILEs)
  184.       return   size_t;
  185.  
  186.    function isatty (handle : int) return int;
  187.  
  188.    procedure mktemp (template : chars);
  189.    --  The return value (which is just a pointer to template) is discarded
  190.  
  191.    procedure rewind (stream : FILEs);
  192.  
  193.    function setvbuf
  194.      (stream : FILEs;
  195.       buffer : chars;
  196.       mode   : int;
  197.       size   : size_t)
  198.       return   int;
  199.  
  200.    procedure tmpnam (string : chars);
  201.    --  The parameter must be a pointer to a string buffer of at least L_tmpnam
  202.    --  bytes (the call with a null parameter is not supported). The returned
  203.    --  value, which is just a copy of the input argument, is discarded.
  204.  
  205.    function tmpfile return FILEs;
  206.  
  207.    function ungetc (c : int; stream : FILEs) return int;
  208.  
  209.    function unlink (filename : chars) return int;
  210.  
  211.    ---------------------
  212.    -- Extra functions --
  213.    ---------------------
  214.  
  215.    --  These functions supply slightly thicker bindings than those above.
  216.    --  They are derived from functions in the C Run-Time Library, but may
  217.    --  do a bit more work than just directly calling one of the Library
  218.    --  functions.
  219.  
  220.    function file_exists (name : chars) return int;
  221.    --  Tests if given name corresponds to an existing file.
  222.  
  223.    function is_regular_file (handle : int) return int;
  224.    --  Tests if given handle is for a regular file (result 1) or for
  225.    --  a non-regular file (pipe or device, result 0).
  226.  
  227.    ---------------------------------
  228.    -- Control of Text/Binary Mode --
  229.    ---------------------------------
  230.  
  231.    --  If text_translation_required is true, then the following functions may
  232.    --  be used to dynamically switch a file from binary to text mode or vice
  233.    --  versa. These functions have no effect if text_translation_required is
  234.    --  false (i.e. in normal unix mode). Use fileno to get a stream handle.
  235.  
  236.    procedure set_binary_mode (handle : int);
  237.    procedure set_text_mode   (handle : int);
  238.  
  239.    ----------------------------
  240.    -- Full Path Name support --
  241.    ----------------------------
  242.  
  243.    procedure full_name (nam : chars; buffer : chars);
  244.    --  Given a NUL terminated string representing a file name, returns in
  245.    --  buffer a NUL terminated string representing the full path name for
  246.    --  the file name. On systems where it is relevant the drive is also part
  247.    --  of the full path name. It is the responsibility of the caller to
  248.    --  pass an actual parameter for buffer that is big enough for any full
  249.    --  path name. Use max_path_len given below as the size of buffer.
  250.  
  251.    max_path_len : Integer;
  252.    --  Maximum length of an allowable full path name on the system,
  253.    --  including a terminating NUL character.
  254.  
  255. private
  256.    --  The following functions are specialized in the body depending on the
  257.    --  operating system.
  258.  
  259.    pragma Inline (fread);
  260.    pragma Inline (fwrite);
  261.    pragma Inline (setvbuf);
  262.  
  263.    --  The following routines are always functions in C, and thus can be
  264.    --  imported directly into Ada without any intermediate C needed
  265.  
  266.    pragma Import (C, clearerr);
  267.    pragma Import (C, fclose);
  268.    pragma Import (C, fdopen);
  269.    pragma Import (C, fflush);
  270.    pragma Import (C, fgetc);
  271.    pragma Import (C, fgets);
  272.    pragma Import (C, fopen);
  273.    pragma Import (C, fputc);
  274.    pragma Import (C, fputs);
  275.    pragma Import (C, freopen);
  276.    pragma Import (C, fseek);
  277.    pragma Import (C, ftell);
  278.    pragma Import (C, isatty);
  279.    pragma Import (C, mktemp);
  280.    pragma Import (C, rewind);
  281.    pragma Import (C, tmpnam);
  282.    pragma Import (C, tmpfile);
  283.    pragma Import (C, ungetc);
  284.    pragma Import (C, unlink);
  285.  
  286.    pragma Import (C, file_exists, "__gnat_file_exists");
  287.    pragma Import (C, is_regular_file, "is_regular_file_fd");
  288.  
  289.    pragma Import (C, set_binary_mode, "set_binary_mode");
  290.    pragma Import (C, set_text_mode, "set_text_mode");
  291.  
  292.    pragma Import (C, max_path_len, "max_path_len");
  293.    pragma Import (C, full_name, "full_name");
  294.  
  295.    --  The following may be implemented as macros, and so are supported
  296.    --  via an interface function in the a-stdio.c file.
  297.  
  298.    pragma Import (C, feof,   "feof__");
  299.    pragma Import (C, ferror, "ferror__");
  300.    pragma Import (C, fileno, "fileno__");
  301.  
  302.    --  Constants in stdio are provided via imported variables that are
  303.    --  defined in a-cstrea.c using the stdio.h header. It would be cleaner
  304.    --  if we could import constant directly, but GNAT does not support
  305.    --  pragma Import for constants ???
  306.  
  307.    c_constant_EOF      : int;
  308.  
  309.    c_constant_IOFBF    : int;
  310.    c_constant_IOLBF    : int;
  311.    c_constant_IONBF    : int;
  312.  
  313.    c_constant_SEEK_CUR : int;
  314.    c_constant_SEEK_END : int;
  315.    c_constant_SEEK_SET : int;
  316.  
  317.    c_constant_L_tmpnam : int;
  318.  
  319.    pragma Import (C, c_constant_EOF, "c_constant_eof");
  320.    pragma Import (C, c_constant_IOFBF, "c_constant_iofbf");
  321.    pragma Import (C, c_constant_IOLBF, "c_constant_iolbf");
  322.    pragma Import (C, c_constant_IONBF, "c_constant_ionbf");
  323.    pragma Import (C, c_constant_SEEK_CUR, "c_constant_seek_cur");
  324.    pragma Import (C, c_constant_SEEK_END, "c_constant_seek_end");
  325.    pragma Import (C, c_constant_SEEK_SET, "c_constant_seek_set");
  326.    pragma Import (C, c_constant_L_tmpnam, "c_constant_l_tmpnam");
  327.  
  328.    pragma Import (C, stderr, "c_constant_stderr");
  329.    pragma Import (C, stdin,  "c_constant_stdin");
  330.    pragma Import (C, stdout, "c_constant_stdout");
  331.  
  332.    EOF      : constant int := c_constant_EOF;
  333.    IOFBF    : constant int := c_constant_IOFBF;
  334.    IOLBF    : constant int := c_constant_IOLBF;
  335.    IONBF    : constant int := c_constant_IONBF;
  336.    SEEK_CUR : constant int := c_constant_SEEK_CUR;
  337.    SEEK_END : constant int := c_constant_SEEK_END;
  338.    SEEK_SET : constant int := c_constant_SEEK_SET;
  339.    L_tmpnam : constant int := c_constant_L_tmpnam;
  340.  
  341.    type Dummy is access Integer;
  342.    function To_Address is new Unchecked_Conversion (Dummy, System.Address);
  343.    --  Used to concoct the null address below
  344.  
  345.    NULL_Stream : constant FILEs := To_Address (Dummy'(null));
  346.    --  Value returned (NULL in C) to indicate an fdopen/fopen/tmpfile error
  347.  
  348. end Interfaces.C_Streams;
  349.