File: fileutil.ml

package info (click to toggle)
missinglib 0.4.10.debian-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 504 kB
  • ctags: 329
  • sloc: ml: 1,726; sh: 233; makefile: 163
file content (54 lines) | stat: -rw-r--r-- 1,294 bytes parent folder | download | duplicates (2)
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));;