-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.sml
83 lines (73 loc) · 2.26 KB
/
reader.sml
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
structure Reader : sig
val read : string -> (bool Array2.array * real * real * real)
end =
struct
fun gettriple s = let
fun realFromString s =
case Real.fromString s of
NONE => 0.0
| SOME x => x
val ts = String.tokens (fn x => x = #" ") s
in
(realFromString (List.nth (ts, 0)),
realFromString (List.nth (ts, 1)),
realFromString (List.nth (ts, 2)))
end
fun getdata f = let
val so = TextIO.inputLine f
in
case so of
NONE => raise Fail "End of file reached"
| SOME s => if (String.isPrefix ";" s) then
getdata f
else
gettriple s
end
(* Skipping ;; *)
fun getline f = let
val so = TextIO.inputLine f
in
case so of
NONE => NONE
| SOME s => if (String.isPrefix ";;" s) then
getline f
else
so
end
(* Spaces are walls, everything else isn't. We are actually recording more information than we need, but
* it's more trouble than it's worth to come up with a more compact representation. Also, strip off the
* trailing newline
*)
fun toWalls s = map (fn c => c <> #" ") ((rev o tl o rev o explode) s)
fun read fname = let
val f = TextIO.openIn fname
fun getWalls f =
let
fun getlines f = let
fun getlines' acc =
case getline f of
NONE => acc
| SOME s => getlines' (s::acc)
in
rev (getlines' [])
end
val (wheight, wthick, thresh) = getdata f
(* Tweak the input data. We want walls on the top and bottom. The easiest way to do this is to
* insert blank data at the beginning and end. This actually requires two lines at both ends
*)
val lines = let
val lines' = getlines f
val spaces = implode (List.tabulate (size (hd lines'), fn _ => #" "))
in
(spaces::spaces::lines')@[spaces, spaces]
end
in
(Array2.fromList (Misc.transposeList (map (fn x => toWalls x) lines)),
wheight,
wthick,
thresh)
end
in
getWalls f
end
end