-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfileExt.ml
81 lines (73 loc) · 2.22 KB
/
fileExt.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
(*
Atari Jaguar Removers' Linker
Copyright (C) 2014-2017 Seb/The Removers ([email protected])
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*)
let exists filename =
try
close_in (open_in_bin filename);
true
with _ -> false
let has_extension filename =
try
ignore (Filename.chop_extension filename : string);
true
with Invalid_argument _ -> false
let find ?(path = []) ?(ext = []) filename =
let has_ext = has_extension filename in
let find_aux filename =
if exists filename then Some filename
else if not has_ext then
let rec aux = function
| [] -> None
| ext :: others ->
let filename_ext = filename ^ ext in
if exists filename_ext then Some filename_ext else aux others
in
aux ext
else None
in
match find_aux filename with
| None ->
if Filename.is_implicit filename then
let rec aux = function
| [] -> None
| dir :: others -> (
match find_aux (Filename.concat dir filename) with
| None -> aux others
| Some filename -> Some filename )
in
aux path
else None
| Some filename -> Some filename
let load filename =
let ic = open_in_bin filename in
let buf = Buffer.create 256 in
try
while true do
Buffer.add_char buf (input_char ic)
done;
raise End_of_file
with End_of_file ->
close_in ic;
Buffer.contents buf
let all_lines filename =
let ic = open_in filename in
let rec aux accu =
try
let l = input_line ic in
aux (l :: accu)
with End_of_file -> List.rev accu
in
let lines = aux [] in
close_in ic;
lines