summaryrefslogtreecommitdiffstats
path: root/stdlib/filename.ml
blob: af63af08fc4ccbf344400c3154bae11a32b95b0b (plain)
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
let check_suffix name suff =
 String.length name >= String.length suff &
 String.sub name (String.length name - String.length suff) (String.length suff)
    = suff

let chop_suffix name suff =
  let n = String.length name - String.length suff in
  if n < 0 then invalid_arg "chop_suffix" else String.sub name 0 n

let current_dir_name = "."

let concat dirname filename =
  let l = String.length dirname - 1 in
  if l < 0 or String.get dirname l = '/'
  then dirname ^ filename
  else dirname ^ "/" ^ filename

let is_absolute n =
     (String.length n >= 1 & String.sub n 0 1 = "/")
  or (String.length n >= 2 & String.sub n 0 2 = "./")
  or (String.length n >= 3 & String.sub n 0 3 = "../")

let slash_pos s =
  let rec pos i =
    if i < 0 then raise Not_found
    else if String.get s i = '/' then i
    else pos (i - 1)
  in pos (String.length s - 1)

let basename name =
  try
    let p = slash_pos name + 1 in
      String.sub name p (String.length name - p)
  with Not_found ->
    name

let dirname name =
  try
    match slash_pos name with
      0 -> "/"
    | n -> String.sub name 0 (slash_pos name)
  with Not_found ->
    "."