69441
|
1 |
(* Title: Pure/General/bytes.ML
|
|
2 |
Author: Makarius
|
|
3 |
|
69446
|
4 |
Byte-vector messages.
|
69441
|
5 |
*)
|
|
6 |
|
|
7 |
signature BYTES =
|
|
8 |
sig
|
|
9 |
val read_line: BinIO.instream -> string option
|
|
10 |
val read_block: BinIO.instream -> int -> string
|
|
11 |
end;
|
|
12 |
|
|
13 |
structure Bytes: BYTES =
|
|
14 |
struct
|
|
15 |
|
|
16 |
fun read_line stream =
|
|
17 |
let
|
|
18 |
val result = trim_line o String.implode o rev;
|
|
19 |
fun read cs =
|
|
20 |
(case BinIO.input1 stream of
|
|
21 |
NONE => if null cs then NONE else SOME (result cs)
|
|
22 |
| SOME b =>
|
|
23 |
(case Byte.byteToChar b of
|
|
24 |
#"\n" => SOME (result cs)
|
|
25 |
| c => read (c :: cs)));
|
|
26 |
in read [] end;
|
|
27 |
|
|
28 |
fun read_block stream n =
|
|
29 |
Byte.bytesToString (BinIO.inputN (stream, n));
|
|
30 |
|
|
31 |
end;
|