summaryrefslogtreecommitdiffstats
path: root/stdlib/scanf.mli
blob: cfa1ef6587089b8b73fd39a44f9581da2d43591e (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
(***********************************************************************)
(*                                                                     *)
(*                           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. *)

(** Scanning buffers. *)
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 stdib : scanbuf;;
(** The scanning buffer reading from [stdin].
    [stdib] is equivalent to [Scanning.from_channel stdin]. *)

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_file : string -> scanbuf;;
(** Bufferized file reading in text mode. The efficient and usual
    way to scan text mode files (in effect, [from_file] returns a
    buffer that reads characters in large chunks, rather than one
    character at a time as buffers returned by [from_channel] do).
    [Scanning.from_file fname] returns a scanning buffer which reads
    from the given file [fname] in text mode. *)

val from_file_bin : string -> scanbuf;;
(** Bufferized file reading in binary mode. *)

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 signal
    an end-of-input condition by raising the exception [End_of_file]. *)

val from_channel : in_channel -> scanbuf;;
(** [Scanning.from_channel ic] returns a scanning buffer which reads
    one character at a time from the input channel [ic], starting at the
    current reading position. *)

val end_of_input : scanbuf -> bool;;
(** [Scanning.end_of_input ib] tests the end-of-input condition
    of the given buffer. *)
val beginning_of_input : scanbuf -> bool;;
(** [Scanning.beginning_of_input ib] tests the beginning of input
    condition of the given buffer. *)

val name_of_input : scanbuf -> string;;
(** [Scanning.file_name_of_input ib] returns the name of the character
    source for the input buffer [ib]. *)

end;;

exception Scan_failure of string;;
(** 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
   to the format string [format], converts these tokens to values, and
   applies the function [f] to these values.
   The result of this application of [f] is the result of the whole construct.

   For instance, if [p] is the function [fun s i -> i + 1], then
   [Scanf.sscanf "x = 1" "%s = %i" p] returns [2].

   The format is a character string which contains three types of
   objects:
   - plain characters, which are simply matched with the
   characters of the input,
   - conversion specifications, each of which causes reading and
   conversion of one argument for [f],
   - scanning indications to specify boundaries of tokens.

   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.

   Conversion specifications consist in the [%] character, followed by
   an optional flag, an optional field width, and 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] or [X]: reads an unsigned hexadecimal integer.
   - [o]: reads an unsigned octal integer.
   - [s]: reads a string argument that spreads as much as possible,
     until the next white space, the next scanning indication, or the
     end-of-input is reached. Hence, this conversion always succeeds:
     it returns an empty string if the bounding condition holds
     when the scan begins.
   - [S]: reads a delimited string argument (delimiters and special
     escaped characters follow the lexical conventions of Caml).
   - [c]: reads a single character. To test the current input character
     without reading it, specify a null field width, i.e. use
     specification [%0c]. Raise [Invalid_argument], if the field width
     specification is greater than 1.
   - [C]: reads a single delimited character (delimiters and special
     escaped characters follow the lexical conventions of Caml).
   - [f], [e], [E], [g], [G]: reads an optionally signed
     floating-point number in decimal notation, in the style [dddd.ddd
     e/E+-dd].
   - [F]: reads a floating point number according to the lexical
     conventions of Caml (hence the decimal point is mandatory if the
     exponent part is not mentioned).
   - [B]: reads a boolean argument ([true] or [false]).
   - [b]: reads a boolean argument (for backward compatibility; do not use
     in new programs).
   - [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 matches one of the characters
     mentioned in the range of characters [range] (or not mentioned in
     it, if the range starts with [^]). Returns a [string] that can be
     empty, if no character in the input matches the range. Hence,
     [\[0-9\]] returns a string representing a decimal number or an empty
     string if no decimal digit is found.
     If a closing bracket appears in a range, it must occur as the
     first character of the range (or just after the [^] in case of
     range negation); hence [\[\]\]] matches a [\]] character and
     [\[^\]\]] matches any character that is not [\]].
   - [\{ fmt %\}]: reads a format string argument that matches
     the internal format string specification [fmt].
     The [fmt] character string defines the conversion
     specification sequence that in turn states the format type [t] of the
     argument returned.
     The argument is read according to the lexical conventions of Caml
     for format strings, and must have a type compatible with [t],
     otherwise the exception [Scan_failure] is raised.
     For instance, "%\{%i%\}" reads any format string that can read a value of
     type [int]; hence [Scanf.sscanf "fmt:\\\"number is %u\\\"" "fmt:%\{%i%\}"]
     succeeds and returns the format string ["number is %u"].
   - [\( fmt %\)]: scanning format insertion.
     This conversion reads a format string specified by [fmt] with
     the same conventions as the [\{ fmt %\}] conversion above.
     After successful reading of a format string compatible with
     [fmt], the scanning process goes on, first scanning according to
     the very format string read, then resuming as usual to the
     following conversions.
   - [l]: applies [f] to the number of lines read so far.
   - [n]: applies [f] to the number of characters read so far.
   - [N] or [L]: applies [f] to the number of tokens read so far.
   - [!]: matches the end of input condition.
   - [%]: matches one [%] character in the input.

   Following the [%] character introducing a conversion, there may be
   the special flag [_]: the conversion that follows occurs as usual,
   but the resulting value is discarded.

   The field widths are composed of an optional integer literal
   indicating the maximal width of the token to read.
   For instance, [%6d] reads an integer, having at most 6 decimal digits;
   and [%4f] reads a float with at most 4 characters.

   Scanning indications appear just after the 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] (which is skipped). 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 character. If a scanning indication [\@c] does not
   follow a string conversion, it is ignored and treated as a plain
   [c] character.

   Raise [Scanf.Scan_failure] if the given input does not match the format.

   Raise [Failure] if a conversion to a number is not possible.

   Raise [End_of_file] if the end of input is encountered while some
   more characters are needed to read the current conversion
   specification (this means in particular that scanning a [%s]
   conversion never raises exception [End_of_file]: if the end of
   input is reached the conversion succeeds and simply returns [""]).

   Notes:

   - the scanning indications introduce slight differences in the
   syntax of [Scanf] format strings compared to those used by the
   [Printf] module. However, scanning indications are similar to those
   of the [Format] module; hence, when producing formatted text to be
   scanned by [!Scanf.bscanf], it is wise to use printing functions
   from [Format] (or, if you need to use functions from [Printf],
   banish or carefully double check the format strings that contain
   ['\@'] characters).

   - in addition to relevant digits, ['_'] characters may appear
   inside numbers (this is reminiscent to the usual Caml
   conventions). If stricter scanning is desired, use the range
   conversion facility instead of the number conversions.

   - the [scanf] facility is not intended for heavy duty lexical
   analysis and parsing. If it appears not expressive enough for your
   needs, several alternative exists: regular expressions (module
   [Str]), stream parsers, [ocamllex]-generated lexers,
   [ocamlyacc]-generated parsers.
*)

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

    Warning: since all scanning functions operate from a scanning
    buffer, be aware that each [fscanf] invocation must allocate a new
    fresh scanning buffer (unless careful use of partial evaluation in
    the program). Hence, there are chances that some characters seem
    to be skipped (in fact they are pending in the previously used
    buffer). This happens in particular when calling [fscanf] again
    after a scan involving a format that necessitates some look ahead
    (such as a format that ends by skipping whitespace in the input).

    To avoid confusion, consider using [bscanf] with an explicitly
    created scanning buffer. Use for instance [Scanning.from_file f]
    to allocate the scanning buffer reading from file [f].

    This method is not only clearer it is also faster, since scanning
    buffers to files are optimized for fast bufferized reading. *)

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

val scanf : ('a, Scanning.scanbuf, 'b) format -> 'a -> 'b;;
(** Same as {!Scanf.bscanf}, but reads from the predefined scanning
    buffer {!Scanf.Scanning.stdib} that is connected to [stdin]. *)

val kscanf :
  Scanning.scanbuf -> (Scanning.scanbuf -> exn -> 'a) ->
  ('b, Scanning.scanbuf, 'a) format -> 'b -> 'a;;
(** Same as {!Scanf.bscanf}, but takes an additional function argument
  [ef] that is called in case of error: if the scanning process or
  some conversion fails, the scanning function aborts and applies the
  error handling function [ef] to the scanning buffer and the
  exception that aborted the scanning process. *)