From: Stefano Zacchiroli Date: Mon, 4 Jul 2005 12:08:55 +0000 (+0000) Subject: added some utility functions on filename suffixes X-Git-Tag: PRE_GETTER_STORAGE~19 X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=commitdiff_plain;h=9577234cdce1fea3f0090f954f15f897efd2394d;p=helm.git added some utility functions on filename suffixes --- diff --git a/helm/ocaml/getter/http_getter_misc.ml b/helm/ocaml/getter/http_getter_misc.ml index c1f21512e..4149d4603 100644 --- a/helm/ocaml/getter/http_getter_misc.ml +++ b/helm/ocaml/getter/http_getter_misc.ml @@ -258,3 +258,37 @@ let is_blank_line = fun line -> Pcre.pmatch ~rex:blank_line_RE line +let normalize_dir s = (* append "/" if missing *) + let len = String.length s in + try + if s.[len - 1] = '/' then s + else s ^ "/" + with Invalid_argument _ -> (* string is empty *) "/" + +let strip_trailing_slash s = + try + let len = String.length s in + if s.[len - 1] = '/' then String.sub s 0 (len - 1) + else s + with Invalid_argument _ -> s + +let strip_suffix ~suffix s = + try + let s_len = String.length s in + let suffix_len = String.length suffix in + let suffix_sub = String.sub s (s_len - suffix_len) suffix_len in + if suffix_sub <> suffix then raise (Invalid_argument ""); + String.sub s 0 (s_len - suffix_len) + with Invalid_argument _ -> + raise (Invalid_argument "Http_getter_misc.strip_suffix") + +let rec list_uniq = function + | [] -> [] + | h::[] -> [h] + | h1::h2::tl when h1 = h2 -> list_uniq (h2 :: tl) + | h1::tl (* when h1 <> h2 *) -> h1 :: list_uniq tl + +let extension s = + let idx = String.rindex s '.' in + String.sub s idx (String.length s - idx) + diff --git a/helm/ocaml/getter/http_getter_misc.mli b/helm/ocaml/getter/http_getter_misc.mli index bc2f72a31..0e0a3471f 100644 --- a/helm/ocaml/getter/http_getter_misc.mli +++ b/helm/ocaml/getter/http_getter_misc.mli @@ -54,6 +54,8 @@ val hashtbl_sorted_fold : (** like Hashtbl.iter but keys are processed ordered *) val hashtbl_sorted_iter : ('a -> 'b -> unit) -> ('a, 'b) Hashtbl.t -> unit +val list_uniq: 'a list -> 'a list (* uniq unix filter on lists *) + (** cp frontend *) val cp: string -> string -> unit (** wget frontend, if output is given it is the destination file, otherwise @@ -72,8 +74,9 @@ val gunzip: ?keep: bool -> ?output: string -> string -> unit suffix is used (actually "_http_getter" *) val tempfile: unit -> string (** mkdir frontend, if parents = true also parent directories will be created. - If the given directory already exists doesn't act *) -val mkdir: ?parents: bool -> string -> unit + If the given directory already exists doesn't act. + parents defaults to false *) +val mkdir: ?parents:bool -> string -> unit (** pretty printer for Unix.process_status values *) val string_of_proc_status : Unix.process_status -> string @@ -86,3 +89,9 @@ val http_get: string -> string option (** true on blanks-only and #-commented lines, false otherwise *) val is_blank_line: string -> bool +val normalize_dir: string -> string (** add trailing "/" if missing *) +val strip_trailing_slash: string -> string +val strip_suffix: suffix:string -> string -> string + +val extension: string -> string (** @return string part after rightmost "." *) +