home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Caml Light 0.61 / Source / src / lib / io.mli < prev    next >
Encoding:
Text File  |  1993-09-24  |  12.4 KB  |  242 lines  |  [TEXT/MPS ]

  1. (* Buffered input and output *)
  2.  
  3. type in_channel;;
  4. type out_channel;;
  5.         (* The abstract types of input channels and output channels. *)
  6.  
  7. exception End_of_file
  8.         (* Raised when an operation cannot complete, because the end
  9.            of the file has been reached. *)
  10. ;;
  11. value stdin : in_channel
  12.   and std_in : in_channel
  13.   and stdout : out_channel
  14.   and std_out : out_channel
  15.   and stderr : out_channel
  16.   and std_err : out_channel
  17.         (* The standard input, standard output, and standard error output
  18.            for the process. [std_in], [std_out] and [std_err] are respectively
  19.        synonymous with [stdin], [stdout] and [stderr]. *)
  20. ;;
  21. value exit : int -> 'a
  22.         (* Flush all pending writes on [std_out] and [std_err],
  23.            and terminate the process, returning the given status code
  24.        to the operating system
  25.            (usually 0 to indicate no errors, and a small positive integer
  26.            to indicate failure.) This function should be called at
  27.            the end of all standalone programs that output results on
  28.            [std_out] or [std_err]; otherwise, the program may appear
  29.            to produce no output, or its output may be truncated. *)
  30. ;;
  31.  
  32. (*** Output functions on standard output *)
  33.  
  34. value print_char : char -> unit
  35.         (* Print the character on standard output. *)
  36.   and print_string : string -> unit
  37.         (* Print the string on standard output. *)
  38.   and print_int : int -> unit
  39.         (* Print the integer, in decimal, on standard output. *)
  40.   and print_float : float -> unit
  41.         (* Print the floating-point number, in decimal, on standard output. *)
  42.   and print_endline : string -> unit
  43.         (* Print the string, followed by a newline character, on
  44.            standard output. *)
  45.   and print_newline : unit -> unit
  46.         (* Print a newline character on standard output, and flush
  47.            standard output. This can be used to simulate line
  48.            buffering of standard output. *)
  49. ;;
  50.  
  51. (*** Output functions on standard error *)
  52.  
  53. value prerr_char : char -> unit
  54.         (* Print the character on standard error. *)
  55.   and prerr_string : string -> unit
  56.         (* Print the string on standard error. *)
  57.   and prerr_int : int -> unit
  58.         (* Print the integer, in decimal, on standard error. *)
  59.   and prerr_float : float -> unit
  60.         (* Print the floating-point number, in decimal, on standard error. *)
  61.   and prerr_endline : string -> unit
  62.         (* Print the string, followed by a newline character on standard error
  63.        and flush standard error. *)
  64. ;;
  65.  
  66. (*** Input functions on standard input *)
  67.  
  68. value read_line : unit -> string
  69.         (* Flush standard output, then read characters from standard input
  70.        until a newline character is encountered. Return the string of
  71.            all characters read, without the newline character at the end. *)
  72.   and read_int : unit -> int
  73.         (* Flush standard output, then read one line from standard input
  74.            and convert it to an integer. Raise [Failure "int_of_string"]
  75.            if the line read is not a valid representation of an integer. *)
  76.   and read_float : unit -> float
  77.         (* Flush standard output, then read one line from standard input
  78.            and convert it to a floating-point number.
  79.            The result is unspecified if the line read is not a valid
  80.            representation of a floating-point number. *)
  81. ;;
  82.  
  83. (*** General output functions *)
  84.  
  85. value open_out : string -> out_channel
  86.         (* Open the named file for writing, and return a new output channel
  87.            on that file, positionned at the beginning of the file. The
  88.            file is truncated to zero length if it already exists. It
  89.            is created if it does not already exists.
  90.            Raise [sys__Sys_error] if the file could not be opened. *)
  91.   and open_out_bin : string -> out_channel
  92.         (* Same as [open_out], but the file is opened in binary mode,
  93.            so that no translation takes place during writes. On operating
  94.            systems that do not distinguish between text mode and binary
  95.            mode, this function behaves like [open_out]. *)
  96.   and open_out_gen : sys__open_flag list -> int -> string -> out_channel
  97.         (* [open_out_gen mode rights filename] opens the file named
  98.            [filename] for writing, as above. The extra argument [mode]
  99.            specify the opening mode (see [sys__open]). The extra
  100.            argument [rights] specifies the file permissions, in case the
  101.            file must be created (see [sys__open]).
  102.            [open_out] and [open_out_bin] are special cases of this function. *)
  103.   and open_descriptor_out : int -> out_channel = 1 "open_descriptor"
  104.         (* [open_descriptor_out fd] returns a buffered output channel
  105.            writing to the file descriptor [fd]. The file descriptor [fd]
  106.            must have been previously opened for writing, else the behavior is
  107.        undefined. *)
  108.   and flush : out_channel -> unit = 1 "flush"
  109.         (* Flush the buffer associated with the given output channel, 
  110.            performing all pending writes on that channel.
  111.            Interactive programs must be careful about flushing [std_out]
  112.            at the right times. *)
  113.   and output_char : out_channel -> char -> unit = 2 "output_char"
  114.         (* Write the character on the given output channel. *)
  115.   and output_string : out_channel -> string -> unit
  116.         (* Write the string on the given output channel. *)
  117.   and output : out_channel -> string -> int -> int -> unit
  118.         (* [output chan buff ofs len] writes [len] characters from string 
  119.            [buff], starting at offset [ofs], to the output channel [chan].
  120.            Raise [Invalid_argument "output"] if [ofs] and [len] do not
  121.            designate a valid substring of [buff]. *)          
  122.   and output_byte : out_channel -> int -> unit = 2 "output_char"
  123.         (* Write one 8-bit integer (as the single character with that code)
  124.            on the given output channel. The given integer is taken modulo
  125.            256. *)
  126.   and output_binary_int : out_channel -> int -> unit = 2 "output_int"
  127.         (* Write one integer in binary format on the given output channel.
  128.            The only reliable way to read it back is through the
  129.            [input_binary_int] function. The format is compatible across
  130.        all machines for a given version of Caml Light. *)
  131.   and output_value : out_channel -> 'a -> unit = 2 "extern_val"
  132.         (* Write the representation of a structured value of any type
  133.            to a channel. Circularities and sharing inside the value
  134.            are detected and preserved. The object can be read back,
  135.            by the function [input_value]. The format is compatible across
  136.        all machines for a given version of Caml Light. *)
  137.   and seek_out : out_channel -> int -> unit = 2 "seek_out"
  138.         (* [seek_out chan pos] sets the current writing position to [pos]
  139.            for channel [chan]. This works only for regular files. On
  140.            files of other kinds (such as terminals, pipes and sockets,)
  141.        the behavior is unspecified. *)
  142.   and pos_out : out_channel -> int = 1 "pos_out"
  143.         (* Return the current writing position for the given channel. *)
  144.   and out_channel_length : out_channel -> int = 1 "channel_size"
  145.         (* Return the total length (number of characters) of the
  146.            given channel. *)
  147.   and close_out : out_channel -> unit = 1 "close_out"
  148.         (* Close the given channel, flushing all buffered write operations.
  149.        The behavior is unspecified if any of the above functions is
  150.        called on a closed channel. *)
  151. ;;
  152.  
  153. (*** General input functions *)
  154.  
  155. value open_in : string -> in_channel
  156.         (* Open the named file for reading, and return a new input channel
  157.            on that file, positionned at the beginning of the file.
  158.            Raise [sys__Sys_error] if the file could not be opened. *)
  159.   and open_in_bin : string -> in_channel
  160.         (* Same as [open_in], but the file is opened in binary mode,
  161.            so that no translation takes place during reads. On operating
  162.            systems that do not distinguish between text mode and binary
  163.            mode, this function behaves like [open_in]. *)
  164.   and open_in_gen : sys__open_flag list -> int -> string -> in_channel
  165.         (* [open_in_gen mode rights filename] opens the file named
  166.            [filename] for reading, as above. The extra arguments
  167.            [mode] and [rights] specify the opening mode and file permissions
  168.            (see [sys__open]). [open_in] and [open_in_bin] are special cases
  169.            of this function. *)
  170.   and open_descriptor_in : int -> in_channel = 1 "open_descriptor"
  171.         (* [open_descriptor_in fd] returns a buffered input channel
  172.            reading from the file descriptor [fd]. The file descriptor [fd]
  173.            must have been previously opened for reading, else the behavior is
  174.        undefined. *)
  175.   and input_char : in_channel -> char = 1 "input_char"
  176.         (* Read one character from the given input channel.
  177.            Raise [End_of_file] if there are no more characters to read. *)
  178.   and input_line : in_channel -> string = 1 "input_line"
  179.         (* Read characters from the given input channel, until a
  180.            newline character is encountered. Return the string of
  181.            all characters read, without the newline character at the end.
  182.            Raise [End_of_file] if the end of the file is reached
  183.            before the line is complete. *)
  184.   and input : in_channel -> string -> int -> int -> int
  185.         (* [input chan buff ofs len] attempts to read [len] characters
  186.            from channel [chan], storing them in string [buff], starting at
  187.            character number [ofs]. It returns the actual number of characters
  188.            read, between 0 and [len] (inclusive).
  189.            A return value of 0 means that the end of file was reached.
  190.            A return value between 0 and [len] exclusive means that
  191.            no more characters were available at that time; [input] must be
  192.            called again to read the remaining characters, if desired.
  193.            Exception [Invalid_argument "input"] is raised if [ofs] and [len]
  194.            do not designate a valid substring of [buff]. *)          
  195.   and really_input : in_channel -> string -> int -> int -> unit
  196.         (* [input chan buff ofs len] reads [len] characters
  197.            from channel [chan], storing them in string [buff], starting at
  198.            character number [ofs]. Raise [End_of_file] if
  199.            the end of file is reached before [len] characters have been read.
  200.            Raise [Invalid_argument "really_input"] if
  201.            [ofs] and [len] do not designate a valid substring of [buff]. *)
  202.   and input_byte : in_channel -> int = 1 "input_char"
  203.         (* Same as [input_char], but return the 8-bit integer representing
  204.            the character.
  205.            Raise [End_of_file] if an end of file was reached. *)
  206.   and input_binary_int : in_channel -> int = 1 "input_int"
  207.         (* Read an integer encoded in binary format from the given input
  208.            channel. See [output_binary_int].
  209.            Raise [End_of_file] if an end of file was reached while reading the
  210.        integer. *)
  211.   and input_value : in_channel -> 'a = 1 "intern_val"
  212.         (* Read the representation of a structured value, as produced
  213.            by [output_value], and return the corresponding value.
  214.            This is not type-safe. The type of the returned object is
  215.            not ['a] properly speaking: the returned object has one
  216.            unique type, which cannot be determined at compile-time.
  217.            The programmer should explicitly give the expected type of the
  218.            returned value, using the following syntax:
  219.                      [(input_value chan : type)].
  220.        The behavior is unspecified if the object in the file does not
  221.        belong to the given type. *)
  222.   and seek_in : in_channel -> int -> unit = 2 "seek_in"
  223.         (* [seek_in chan pos] sets the current reading position to [pos]
  224.            for channel [chan]. *)
  225.   and pos_in : in_channel -> int = 1 "pos_in"
  226.         (* Return the current reading position for the given channel. *)
  227.   and in_channel_length : in_channel -> int = 1 "channel_size"
  228.         (* Return the total length (number of characters) of the
  229.            given channel. This works only for regular files. On files of
  230.            other kinds, the result is meaningless. *)
  231.   and close_in : in_channel -> unit = 1 "close_in"
  232.         (* Close the given channel. Anything can happen if any of the
  233.            above functions is called on a closed channel. *)
  234. ;;
  235.  
  236. (*--*)
  237.  
  238. value fast_input : in_channel -> string -> int -> int -> int = 4 "input"
  239.   and fast_really_input : in_channel -> string -> int -> int -> unit
  240.   and fast_output : out_channel -> string -> int -> int -> unit = 4 "output"
  241. ;;
  242.