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
|
(* arch-tag: file utilities
Copyright (c) 2004 John Goerzen *)
let getfirstline filename =
let fd = open_in filename in
let line = input_line fd in
close_in fd;
line;;
let getlines filename =
let fd = open_in filename in
let retval = ref [] in
begin
try
while true do
retval := (input_line fd) :: !retval
done
with End_of_file -> ();
end;
close_in fd;
!retval;
;;
let abspath ?startdir filename =
(* if not (Filename.is_relative filename) then
filename
else *)
let s = match startdir with
None -> Sys.getcwd ()
| Some x -> x in
if String.length filename < 1 then
s
else
let rec proclist l =
match l with
[] -> []
| "" :: xs -> proclist xs
| "." :: xs -> proclist xs
| ".." :: ".." :: xs -> ".." :: ".." :: proclist xs
| x :: ".." :: xs -> proclist xs
(* | ".." :: xs -> raise (Failure "proclist: .. at bad place") *)
| x :: xs -> x :: proclist xs
in
let rec modlist l =
let pl = proclist l in
if pl = l then l else modlist pl
in
let components =
(if String.sub filename 0 1 = "/" then [] else (Strutil.split "/" s)) @
Strutil.split "/" filename in
"/" ^ (Strutil.join "/" (modlist components));;
|