File: dirTree.ml

package info (click to toggle)
ocamlgraph 2.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,624 kB
  • sloc: ml: 19,995; xml: 151; makefile: 14; sh: 1
file content (72 lines) | stat: -rw-r--r-- 2,612 bytes parent folder | download | duplicates (3)
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
(**************************************************************************)
(*                                                                        *)
(*  Ocamlgraph: a generic graph library for OCaml                         *)
(*  Copyright (C) 2004-2010                                               *)
(*  Sylvain Conchon, Jean-Christophe Filliatre and Julien Signoles        *)
(*                                                                        *)
(*  This software is free software; you can redistribute it and/or        *)
(*  modify it under the terms of the GNU Library General Public           *)
(*  License version 2.1, with the special exception on linking            *)
(*  described in file LICENSE.                                            *)
(*                                                                        *)
(*  This software 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.                  *)
(*                                                                        *)
(**************************************************************************)

open Unix ;;

type t = {
  id : int;
  name : string ;
  children : t list Lazy.t
} ;;
let id t = t.id

let newid = let r = ref 0 in fun () -> incr r; !r

type label = string ;;

let children t = Lazy.force t.children ;;
let label t = t.name ;;
let string_of_label s = s ;;

let is_dir path =
  try (lstat path).st_kind = S_DIR
  with Unix_error _ -> false ;;

let less t1 t2 = t1.name <= t2.name ;;

let rec tree_list_from_path path =
  let opt_handle =
    try Some(opendir path)
    with Unix_error _ -> None in
  match opt_handle with
  | None -> []
  | Some handle ->
    let list = Sort.list less (tree_list_from_handle path handle) in
    closedir handle ; list

and tree_list_from_handle path handle =
  let opt_name =
    try Some(readdir handle)
    with End_of_file -> None in
  match opt_name with
  | None -> []
  | Some name ->
    let path' = path ^ "/" ^ name in
    if name <> "." && name <> ".." && is_dir path' then
      { id = newid(); name = name ; children = lazy (tree_list_from_path path') } ::
      tree_list_from_handle path handle
    else
      tree_list_from_handle path handle ;;

let from_dir path name =
  try
    let path' = path ^ "/" ^ name in
    if is_dir path' then
      { id = newid(); name = name ;
        children = lazy (tree_list_from_path path') }
    else invalid_arg "DirTree.from_dir"
  with _ -> failwith "DirTree.from_dir" ;;