-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathocpChecker.ml
More file actions
81 lines (73 loc) · 2 KB
/
ocpChecker.ml
File metadata and controls
81 lines (73 loc) · 2 KB
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
let remove_trailing_whitespaces line =
let len = String.length line in
let new_len = ref len in
let loop = ref (!new_len > 0) in
while !loop do
let c = line.[!new_len - 1] in
if c = ' ' || c = '\t' || c = '\r' then
begin
decr new_len;
loop := !new_len > 0
end
else loop := false
done;
if !new_len <> len then Some (String.sub line 0 !new_len)
else None
let check_buffer file cin =
let spacesRemoved = ref false in
let longLines = ref false in
let lines = Queue.create () in
let cpt = ref 0 in
try
while true do
incr cpt;
let line = input_line cin in
let line =
match remove_trailing_whitespaces line with
| None -> Queue.push line lines; line
| Some line2 -> Queue.push line2 lines; spacesRemoved := true; line2
in
if String.length line > 80 then begin
Format.eprintf "File %s: line %d too long@." file !cpt;
longLines := true;
end
done;
assert false
with End_of_file -> lines, !spacesRemoved, !longLines
let update_file file lines =
let cout =
try open_out file
with e ->
Format.eprintf "Error while opening (out) file: %s@.%s@."
file (Printexc.to_string e);
exit 2
in
Queue.iter (fun line ->
Pervasives.output_string cout line;
Pervasives.output_string cout "\n"
)lines;
Pervasives.flush cout;
close_out cout
let check_file file =
let cin =
try open_in file
with e ->
Format.eprintf "Error while opening (in) file: %s@.%s@."
file (Printexc.to_string e);
exit 2
in
let lines, spacesRemoved, longLines = check_buffer file cin in
close_in cin;
if spacesRemoved then begin
update_file file lines;
exit 10
end;
if longLines then exit 11
let () =
match Array.length Sys.argv with
| 0 | 1 ->
Format.eprintf "%s: Too few arguments!@." Sys.argv.(0); exit 3
| 2 ->
check_file Sys.argv.(1)
| _ ->
Format.eprintf "%s: Too many arguments!@." Sys.argv.(0); exit 4