2002-05-07 00:41:12 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Objective Caml *)
|
|
|
|
(* *)
|
|
|
|
(* Pierre Weis, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
|
|
|
(* Copyright 2002 Institut National de Recherche en Informatique et *)
|
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the GNU Library General Public License, with *)
|
|
|
|
(* the special exception on linking described in file ../LICENSE. *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
2002-05-08 06:51:09 -07:00
|
|
|
(* $Id$ *)
|
2002-05-07 00:41:12 -07:00
|
|
|
|
|
|
|
(** Formatted input functions. *)
|
|
|
|
|
2002-07-25 07:06:19 -07:00
|
|
|
(** Scanning buffers. *)
|
2002-05-27 15:00:09 -07:00
|
|
|
module Scanning : sig
|
|
|
|
|
|
|
|
type scanbuf;;
|
|
|
|
(** The type of scanning buffers. A scanning buffer is the argument passed
|
|
|
|
to the scanning functions used by the [scanf] family of functions.
|
|
|
|
The scanning buffer holds the current state of the scan, plus
|
|
|
|
a function to get the next char from the input, and a token buffer
|
|
|
|
to store the string matched so far. *)
|
|
|
|
|
|
|
|
val from_string : string -> scanbuf;;
|
|
|
|
(** [Scanning.from_string s] returns a scanning buffer which reads
|
|
|
|
from the given string.
|
|
|
|
Reading starts from the first character in the string.
|
|
|
|
The end-of-input condition is set when the end of the string is reached. *)
|
|
|
|
|
|
|
|
val from_channel : in_channel -> scanbuf;;
|
|
|
|
(** [Scanning.from_channel inchan] returns a scanning buffer which reads
|
|
|
|
from the input channel [inchan], at the current reading position. *)
|
|
|
|
|
|
|
|
val from_function : (unit -> char) -> scanbuf;;
|
|
|
|
(** [Scanning.from_function f] returns a scanning buffer with
|
|
|
|
the given function as its reading method.
|
|
|
|
When scanning needs one more character, the given function is called.
|
|
|
|
When the function has no more character to provide, it must set
|
2002-07-28 14:29:42 -07:00
|
|
|
an end-of-input condition by raising the exception [End_of_file]. *)
|
2002-05-27 15:00:09 -07:00
|
|
|
|
|
|
|
end;;
|
|
|
|
|
2002-06-27 02:18:11 -07:00
|
|
|
exception Scan_failure of string;;
|
2002-06-12 01:31:21 -07:00
|
|
|
(** The exception that formatted input functions raise when the input
|
|
|
|
cannot be read according to the given format. *)
|
|
|
|
|
|
|
|
val bscanf :
|
|
|
|
Scanning.scanbuf -> ('a, Scanning.scanbuf, 'b) format -> 'a -> 'b;;
|
|
|
|
(** [bscanf ib format f] reads tokens from the scanning buffer [ib] according
|
2002-05-07 00:41:12 -07:00
|
|
|
to the format string [format], converts these tokens to values, and
|
2002-05-28 10:51:49 -07:00
|
|
|
applies the function [f] to these values.
|
2002-05-07 00:41:12 -07:00
|
|
|
The result of this application of [f] is the result of the whole construct.
|
2002-05-15 13:28:57 -07:00
|
|
|
|
2002-06-12 01:31:21 -07:00
|
|
|
Raise [Scanf.Scan_failure] if the given input does not match the format.
|
|
|
|
|
2002-07-28 14:29:42 -07:00
|
|
|
Raise [Failure] if a conversion to a number is not possible.
|
|
|
|
|
2002-07-11 15:39:26 -07:00
|
|
|
Raise [End_of_file] if the end of input is encountered while scanning
|
|
|
|
and the input matches the given format so far.
|
|
|
|
|
2002-05-27 00:05:36 -07:00
|
|
|
The format is a character string which contains three types of
|
|
|
|
objects:
|
|
|
|
- plain characters, which are simply matched with the
|
2002-07-11 15:39:26 -07:00
|
|
|
characters of the input,
|
2002-05-27 00:05:36 -07:00
|
|
|
- conversion specifications, each of which causes reading and
|
|
|
|
conversion of one argument for [f],
|
2002-07-28 14:29:42 -07:00
|
|
|
- scanning indications to specify boundaries of tokens.
|
2002-05-27 00:05:36 -07:00
|
|
|
|
2002-07-25 05:11:29 -07:00
|
|
|
Among plain characters the space character (ASCII code 32) has a
|
|
|
|
special meaning: it matches ``whitespace'', that is any number of tab,
|
|
|
|
space, newline and carriage return characters. Hence, a space in the format
|
|
|
|
matches any amount of whitespace in the input.
|
2002-05-15 13:28:57 -07:00
|
|
|
|
2002-05-07 00:41:12 -07:00
|
|
|
Conversion specifications consist in the [%] character, followed
|
|
|
|
by optional field width, followed by one or two conversion
|
|
|
|
characters. The conversion characters and their meanings are:
|
2002-05-15 13:28:57 -07:00
|
|
|
- [d]: reads an optionally signed decimal integer.
|
2002-05-07 00:41:12 -07:00
|
|
|
- [i]: reads an optionally signed integer
|
2002-07-25 08:24:58 -07:00
|
|
|
(usual input formats for hexadecimal ([0x[d]+] and [0X[d]+]),
|
|
|
|
octal ([0o[d]+]), and binary [0b[d]+] notations are understood).
|
2002-05-17 01:17:52 -07:00
|
|
|
- [u]: reads an unsigned decimal integer.
|
2002-07-25 08:24:58 -07:00
|
|
|
- [x] or [X]: reads an unsigned hexadecimal integer.
|
2002-05-07 00:41:12 -07:00
|
|
|
- [o]: reads an unsigned octal integer.
|
2002-05-27 00:05:36 -07:00
|
|
|
- [s]: reads a string argument (by default strings end with a space).
|
2002-06-07 03:05:52 -07:00
|
|
|
- [S]: reads a delimited string argument (delimiters and special
|
2002-07-28 14:29:42 -07:00
|
|
|
escaped characters follow the lexical conventions of Caml).
|
2002-05-07 00:41:12 -07:00
|
|
|
- [c]: reads a single character.
|
2002-06-07 03:05:52 -07:00
|
|
|
- [C]: reads a single delimited character (delimiters and special
|
2002-07-28 14:29:42 -07:00
|
|
|
escaped characters follow the lexical conventions of Caml).
|
2002-05-27 13:41:19 -07:00
|
|
|
- [f], [e], [E], [g], [G]: reads an optionally signed floating-point number
|
|
|
|
in decimal notation, in the style [dddd.ddd e/E+-dd].
|
2002-05-07 00:41:12 -07:00
|
|
|
- [b]: reads a boolean argument ([true] or [false]).
|
2002-05-08 06:51:09 -07:00
|
|
|
- [ld], [li], [lu], [lx], [lX], [lo]: reads an [int32] argument to
|
|
|
|
the format specified by the second letter (decimal, hexadecimal, etc).
|
|
|
|
- [nd], [ni], [nu], [nx], [nX], [no]: reads a [nativeint] argument to
|
|
|
|
the format specified by the second letter.
|
|
|
|
- [Ld], [Li], [Lu], [Lx], [LX], [Lo]: reads an [int64] argument to
|
|
|
|
the format specified by the second letter.
|
2002-06-07 03:05:52 -07:00
|
|
|
- [\[ range \]]: reads characters that matches one of the characters
|
|
|
|
mentioned in the range of characters [range] (or not mentioned in
|
2002-07-25 08:24:58 -07:00
|
|
|
it, if the range starts with [^]). Returns a [string] that can be
|
2002-05-27 00:05:36 -07:00
|
|
|
empty, if no character in the input matches the range.
|
2002-05-17 01:17:52 -07:00
|
|
|
- [N]: applies [f] to the number of characters read so far.
|
2002-05-07 00:41:12 -07:00
|
|
|
- [%]: matches one [%] character in the input.
|
|
|
|
|
|
|
|
The field widths are composed of an optional integer literal
|
2002-05-27 00:05:36 -07:00
|
|
|
indicating the maximal width of the token to read.
|
2002-05-07 00:41:12 -07:00
|
|
|
For instance, [%6d] reads an integer, having at most 6 decimal digits;
|
|
|
|
and [%4f] reads a float with 4 characters.
|
|
|
|
|
2002-07-28 14:29:42 -07:00
|
|
|
Scanning indications appear just after string conversions [s] and
|
|
|
|
[\[ range \]] to delimit the end of the token. A scanning
|
|
|
|
indication is introduced by a [@] character, followed by some
|
|
|
|
constant character [c]. It means that the string token should end
|
|
|
|
just before the next matching [c]. If no [c] character is
|
|
|
|
encountered, the string token spreads as much as possible.
|
|
|
|
For instance, ["%s@\t"] reads a string up to the next tabulation
|
2002-07-28 14:51:51 -07:00
|
|
|
character. If a scanning indication [\@c] does not follow a
|
2002-07-28 14:29:42 -07:00
|
|
|
string conversion, it is ignored and treated as a plain [c]
|
2002-05-27 00:05:36 -07:00
|
|
|
character.
|
2002-05-07 00:41:12 -07:00
|
|
|
|
|
|
|
Note: the [scanf] facility is not intended for heavy duty
|
2002-07-28 14:29:42 -07:00
|
|
|
lexical analysis and parsing. If it appears not expressive
|
2002-07-25 05:11:29 -07:00
|
|
|
enough for your needs, several alternative exists: regular expressions
|
|
|
|
(module [Str]), stream parsers, [ocamllex]-generated lexers,
|
|
|
|
[ocamlyacc]-generated parsers. *)
|
2002-05-07 00:41:12 -07:00
|
|
|
|
2002-06-12 01:31:21 -07:00
|
|
|
val fscanf : in_channel -> ('a, Scanning.scanbuf, 'b) format -> 'a -> 'b;;
|
2002-07-25 05:11:29 -07:00
|
|
|
(** Same as {!Scanf.bscanf}, but inputs from the given channel. *)
|
2002-05-07 00:41:12 -07:00
|
|
|
|
2002-05-12 11:40:15 -07:00
|
|
|
val sscanf : string -> ('a, Scanning.scanbuf, 'b) format -> 'a -> 'b;;
|
2002-07-25 05:11:29 -07:00
|
|
|
(** Same as {!Scanf.bscanf}, but inputs from the given string. *)
|
2002-06-12 01:31:21 -07:00
|
|
|
|
|
|
|
val scanf : ('a, Scanning.scanbuf, 'b) format -> 'a -> 'b;;
|
2002-07-25 05:11:29 -07:00
|
|
|
(** Same as {!Scanf.bscanf}, but inputs from [stdin]
|
|
|
|
(the standard input channel). *)
|
2002-06-26 02:31:02 -07:00
|
|
|
|
2002-06-27 02:18:11 -07:00
|
|
|
val kscanf :
|
2002-07-28 14:29:42 -07:00
|
|
|
Scanning.scanbuf -> (Scanning.scanbuf -> exn -> 'a) ->
|
|
|
|
('b, Scanning.scanbuf, 'a) format -> 'b -> 'a;;
|
2002-06-27 02:18:11 -07:00
|
|
|
(** Same as {!Scanf.bscanf}, but takes an additional function argument
|
|
|
|
[ef] that is called in case of error: if the scanning process or
|
2002-07-28 14:51:51 -07:00
|
|
|
some conversion fails, the scanning function aborts and applies the
|
2002-07-25 05:11:29 -07:00
|
|
|
error handling function [ef] to the scanning buffer and the
|
|
|
|
exception that aborted evaluation. *)
|