-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.ml
100 lines (81 loc) · 2.21 KB
/
read.ml
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
open Printf
open Error
type position = {
line_start : int;
line_end : int;
char_start : int;
char_end : int
}
let pos_zero = {line_start=0;line_end=0;char_start=0;char_end=0}
let prev_line = ref 1
let curr_line = ref 1
let prev_start_line_char = ref 1
let curr_start_line_char = ref 1
let start_line_char = ref 0
let prev_char = ref 1
let curr_char = ref 1
let filename =
match Array.to_list Sys.argv with
| _ :: filename :: _ -> filename
| _ -> error "Please provide a filename as an argument\n"; exit 1
let channel =
try open_in filename
with Sys_error(str) -> error "%s\n" str; exit 1
let lexbuf = Lexing.from_channel channel
let update_status () =
prev_start_line_char := !curr_start_line_char;
curr_start_line_char := !start_line_char;
prev_char := !curr_char;
curr_char := lexbuf.lex_curr_p.pos_cnum;
prev_line := !curr_line;
curr_line := lexbuf.lex_curr_p.pos_lnum
let rec count_digits n =
if n < 10 then
1
else
1 + count_digits (n / 10)
let print_spaces n =
let rec print_spaces_helper count =
if count > 0 then (
print_char ' ';
print_spaces_helper (count - 1)
)
in
print_spaces_helper n
let print_file_line filename n =
let rec print_lines ch count =
if count = n then
match input_line ch with
| line -> print_endline line
| exception End_of_file -> ()
else
match input_line ch with
| _ -> print_lines ch (count + 1)
| exception End_of_file -> ()
in
let ch = open_in filename in
print_lines ch 1;
close_in ch
let print_file_lines filename n m =
let rec print_lines ch count =
if count >= n && count <= m then
match input_line ch with
| line -> print_spaces ((count_digits m) - (count_digits count)); printf " %d | " count; print_endline line; print_lines ch (count + 1)
| exception End_of_file -> ()
else if count < n then
(ignore (input_line ch); print_lines ch (count + 1))
else
()
in
let ch = open_in filename in
print_lines ch 1;
close_in ch
let print_carat_with_spaces n =
let rec print_spaces count =
if count > 0 then
(print_string " "; print_spaces (count - 1))
else
()
in
print_spaces n;
print_endline "^"