summaryrefslogtreecommitdiff
path: root/stdlib/scanf.mli
blob: 5b24a1f559c4b4c2fca3c2f6fd1da7136724c7a7 (plain)
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
(***********************************************************************)
(*                                                                     *)
(*                           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.     *)
(*                                                                     *)
(***********************************************************************)

(* $Id$ *)

(** Formatted input functions. *)

val fscanf : in_channel -> ('a, Scanning.scanbuf, 'b) format -> 'a -> 'b;;
(** [fscanf inchan format f] reads tokens from the channel [inchan] according
   to the format string [format], converts these tokens to values, and
   applies these values to the function [f].
   The result of this application of [f] is the result of the whole construct.

   The format is a character string which contains two types of
   objects:  plain  characters, which are simply matched with the
   input channel, and conversion specifications, each of which
   causes  conversion and reading of one argument for [f].

   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:
   - [d]: reads an optionally signed decimal integer.
   - [i]: reads an optionally signed integer
     (usual input formats for hexadecimal ([0x\[d\]+] and [0X\[d+\]]),
      octal ([0o\[d\]+]), and binary [0b\[d\]+] notations are understood).
   - [u]: reads an unsigned decimal integer.
   - [x]: reads an unsigned hexadecimal integer with lowercase letters.
   - [X]: reads an unsigned hexadecimal integer with uppercase letters.
   - [o]: reads an unsigned octal integer.
   - [s]: reads a string argument (string ends with a space).
   - [S]: reads a delimited string argument (delimitors and special
     escaped characters follow the lexical conventions of Objective Caml).
   - [c]: reads a single character.
   - [C]: reads a single delimited character (delimitors and special
     escaped characters follow the lexical conventions of Objective Caml).
   - [f]: reads a floating-point number in decimal notation,
     in the style [dddd.ddd].
   - [e] or [E]: reads a floating-point argument in decimal notation,
     in the style [d.ddd e+-dd] (mantissa and exponent).
   - [g] or [G]: reads a floating-point argument in decimal notation,
     in style [f] or [e], [E].
   - [b]: reads a boolean argument ([true] or [false]).
   - [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.
   - [\[ range \]]: reads characters that maches one of the characters
     mentioned in the range of characters [range] (or not mentionned in
     it, if the range starts by [^]).
   - [N]: applies [f] to the number of characters read so far.
   - [%]: matches one [%] character in the input.

   The field widths are composed of an optional integer literal
   indicating the maximal width of the token to read, possibly followed by
   a dot [.] and another integer literal indicating how many digits
   follow the decimal point in the [%f], [%e], and [%E] conversions.
   For instance, [%6d] reads an integer, having at most 6 decimal digits;
   and [%4f] reads a float with 4 characters.

   Each conversion starts by skiping space (except the [c]
   conversion). Space is any number of tab, white space, newline and
   return. Hence, a space in the format matches any number of white
   spaces in the input.

   Note: the [scanf] facility is not intended for heavy duty
   lexing and parsing; if you need efficient language syntactic analysis,
   use the corresponding devoted libraries. *)

val scanf : ('a, Scanning.scanbuf, 'b) format -> 'a -> 'b;;
(** Same as {!Scanf.fscanf}, but inputs from [stdin]. *)

val bscanf :
  Scanning.scanbuf -> ('a, Scanning.scanbuf, 'b) format -> 'a -> 'b;;
(** Same as {!Scanf.fscanf}, but inputs from the buffer argument. *)

val sscanf : string -> ('a, Scanning.scanbuf, 'b) format -> 'a -> 'b;;
(** Same as {!Scanf.fscanf}, but inputs from the string argument. *)