]> matita.cs.unibo.it Git - helm.git/blobdiff - components/extlib/hExtlib.ml
fixed two preblems in matitadep, one coming from the dep-parser and
[helm.git] / components / extlib / hExtlib.ml
index 2fba7f651497a532af8db1445cdc12d25dcea302..3ee270e4e00b42fd0b012a512b11a92feef48c82 100644 (file)
@@ -425,32 +425,56 @@ let estimate_size x =
 let normalize_path s = 
   let s = Str.global_replace (Str.regexp "//") "/" s in
   let l = Str.split (Str.regexp "/") s in
-  let rec aux = function
-    | [] -> []
-    | he::".."::tl -> aux tl
-    | he::"."::tl -> aux (he::tl)
-    | he::tl -> he :: aux tl
+  let rec aux acc = function
+    | [] -> acc
+    | he::"."::tl -> aux acc (he::tl)
+    | he::".."::tl when he <> ".." -> aux [] (acc @ tl)
+    | he::tl -> aux (acc@[he]) tl
   in
   (if Str.string_match (Str.regexp "^/") s 0 then "/" else "") ^
-  String.concat "/" (aux l)
+  String.concat "/" (aux [] l)
   ^ (if Str.string_match (Str.regexp "/$") s 0 then "/" else "")
 ;;
 
 let find_in paths path =
    let rec aux = function
-   | [] -> raise (Failure "not found")
+   | [] -> raise (Failure "find_in")
    | p :: tl ->
       let path = normalize_path (p ^ "/" ^ path) in
        try
          if (Unix.stat path).Unix.st_kind = Unix.S_REG then path
          else aux tl
-       with Unix.Unix_error _ -> aux tl
+       with Unix.Unix_error _ as exn -> 
+               aux tl
    in
    try
      aux paths
    with Unix.Unix_error _ | Failure _ -> 
      raise 
-       (Failure ("File " ^ path ^ " not found (or not readable) in: " ^
-         String.concat ":" paths))
+       (Failure "find_in")
+;;
+
+let is_prefix_of_aux d1 d2 = 
+  let len1 = String.length d1 in
+  let len2 = String.length d2 in
+  if len2 < len1 then 
+    false, len1, len2
+  else
+    let pref = String.sub d2 0 len1 in
+    pref = d1 && (len1 = len2 || d1.[len1-1] = '/' || d2.[len1] = '/'), len1, len2
+
+let is_prefix_of d1 d2 =
+  let b,_,_ = is_prefix_of_aux d1 d2 in b
+;;
+
+let chop_prefix prefix s =
+  let b,lp,ls = is_prefix_of_aux prefix s in
+  if b then
+    String.sub s lp (ls - lp)
+  else 
+    s
+;;
+
+let touch s =
+  try close_out(open_out s) with Sys_error _ -> ()
 ;;
-