|
Packit |
9ff65e |
(******************************************************************************)
|
|
Packit |
9ff65e |
(* ocaml-fileutils: files and filenames common operations *)
|
|
Packit |
9ff65e |
(* *)
|
|
Packit |
9ff65e |
(* Copyright (C) 2003-2014, Sylvain Le Gall *)
|
|
Packit |
9ff65e |
(* *)
|
|
Packit |
9ff65e |
(* This library is free software; you can redistribute it and/or modify it *)
|
|
Packit |
9ff65e |
(* under the terms of the GNU Lesser General Public License as published by *)
|
|
Packit |
9ff65e |
(* the Free Software Foundation; either version 2.1 of the License, or (at *)
|
|
Packit |
9ff65e |
(* your option) any later version, with the OCaml static compilation *)
|
|
Packit |
9ff65e |
(* exception. *)
|
|
Packit |
9ff65e |
(* *)
|
|
Packit |
9ff65e |
(* This library is distributed in the hope that it will be useful, but *)
|
|
Packit |
9ff65e |
(* WITHOUT ANY WARRANTY; without even the implied warranty of *)
|
|
Packit |
9ff65e |
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file *)
|
|
Packit |
9ff65e |
(* COPYING for more details. *)
|
|
Packit |
9ff65e |
(* *)
|
|
Packit |
9ff65e |
(* You should have received a copy of the GNU Lesser General Public License *)
|
|
Packit |
9ff65e |
(* along with this library; if not, write to the Free Software Foundation, *)
|
|
Packit |
9ff65e |
(* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *)
|
|
Packit |
9ff65e |
(******************************************************************************)
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Operations on abstract filenames.
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
This module allow to manipulate string or abstract representation of a
|
|
Packit |
9ff65e |
filename.
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
Abstract representation of a filename allow to decode it only once, and
|
|
Packit |
9ff65e |
should speed up further operation on it (comparison in particular). If you
|
|
Packit |
9ff65e |
intend to do a lot of processing on filename, you should consider using its
|
|
Packit |
9ff65e |
abstract representation.
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
This module manipulate abstract path that are not bound to a real
|
|
Packit |
9ff65e |
filesystem. In particular, it makes the assumption that there is no
|
|
Packit |
9ff65e |
symbolic link that should modify the meaning of a path. If you intend to use
|
|
Packit |
9ff65e |
this module against a real set of filename, the best solution is to apply to
|
|
Packit |
9ff65e |
every filename to solve symbolic link through {!FileUtil.readlink}.
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
@author Sylvain Le Gall
|
|
Packit |
9ff65e |
*)
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Filename type. *)
|
|
Packit |
9ff65e |
type filename = string
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Extension type. *)
|
|
Packit |
9ff65e |
type extension = string
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** {2 Exceptions and types} *)
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Cannot pass a base filename which is relative. *)
|
|
Packit |
9ff65e |
exception BaseFilenameRelative of filename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** We do not have recognized any OS, please contact upstream. *)
|
|
Packit |
9ff65e |
exception UnrecognizedOS of string
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** The filename use was empty. *)
|
|
Packit |
9ff65e |
exception EmptyFilename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** The last component of the filename does not support extension (Root,
|
|
Packit |
9ff65e |
ParentDir...)
|
|
Packit |
9ff65e |
*)
|
|
Packit |
9ff65e |
exception NoExtension of filename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** The filename used is invalid. *)
|
|
Packit |
9ff65e |
exception InvalidFilename of filename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** {2 Ordering} *)
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** [is_subdir fl1 fl2] Is [fl2] a sub directory of [fl1] *)
|
|
Packit |
9ff65e |
val is_subdir: filename -> filename -> bool
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** [is_updir fl1 fl2] Is [fl1] a sub directory of [fl2] *)
|
|
Packit |
9ff65e |
val is_updir: filename -> filename -> bool
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** [compare fl1 fl2] Give an order between the two filename. The
|
|
Packit |
9ff65e |
classification is done by sub directory relation, [fl1] < [fl2] iff [fl1] is
|
|
Packit |
9ff65e |
a subdirectory of [fl2], and lexicographical order of each part of the
|
|
Packit |
9ff65e |
reduce filename when [fl1] and [fl2] has no hierarchical relation
|
|
Packit |
9ff65e |
*)
|
|
Packit |
9ff65e |
val compare: filename -> filename -> int
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** {2 Standard operations } *)
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Current dir. *)
|
|
Packit |
9ff65e |
val current_dir: filename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Upper dir. *)
|
|
Packit |
9ff65e |
val parent_dir: filename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Make a filename from a set of strings. *)
|
|
Packit |
9ff65e |
val make_filename: string list -> filename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Extract only the file name of a filename. *)
|
|
Packit |
9ff65e |
val basename: filename -> filename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Extract the directory name of a filename. *)
|
|
Packit |
9ff65e |
val dirname: filename -> filename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Append a filename to a filename. *)
|
|
Packit |
9ff65e |
val concat: filename -> filename -> filename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Return the shortest filename which is equal to the filename given. It remove
|
|
Packit |
9ff65e |
the "." in Unix filename, for example.
|
|
Packit |
9ff65e |
If [no_symlink] flag is set, consider that the path doesn't contain symlink
|
|
Packit |
9ff65e |
and in this case ".." for Unix filename are also reduced.
|
|
Packit |
9ff65e |
*)
|
|
Packit |
9ff65e |
val reduce: ?no_symlink:bool -> filename -> filename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Create an absolute filename from a filename relative and an absolute base
|
|
Packit |
9ff65e |
filename.
|
|
Packit |
9ff65e |
*)
|
|
Packit |
9ff65e |
val make_absolute: filename -> filename -> filename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Create a filename which is relative to the base filename. *)
|
|
Packit |
9ff65e |
val make_relative: filename -> filename -> filename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** [reparent fln_src fln_dst fln] Return the same filename as [fln]
|
|
Packit |
9ff65e |
but the root is no more [fln_src] but [fln_dst]. It replaces the
|
|
Packit |
9ff65e |
[fln_src] prefix by [fln_dst].
|
|
Packit |
9ff65e |
*)
|
|
Packit |
9ff65e |
val reparent: filename -> filename -> filename -> filename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Identity for testing the stability of implode/explode. *)
|
|
Packit |
9ff65e |
val identity: filename -> filename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Test if the filename is a valid one. *)
|
|
Packit |
9ff65e |
val is_valid: filename -> bool
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Check if the filename is relative to a dir or not.
|
|
Packit |
9ff65e |
*)
|
|
Packit |
9ff65e |
val is_relative: filename -> bool
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Check if the filename is the current directory.
|
|
Packit |
9ff65e |
*)
|
|
Packit |
9ff65e |
val is_current: filename -> bool
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Check if the filename is the parent directory.
|
|
Packit |
9ff65e |
*)
|
|
Packit |
9ff65e |
val is_parent: filename -> bool
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** {2 Extension}*)
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Extension is define as the suffix of a filename, just after the last ".".
|
|
Packit |
9ff65e |
*)
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Remove extension and the trailing ".". *)
|
|
Packit |
9ff65e |
val chop_extension: filename -> filename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Extract the extension. *)
|
|
Packit |
9ff65e |
val get_extension: filename -> extension
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Check the extension. *)
|
|
Packit |
9ff65e |
val check_extension: filename -> extension -> bool
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Add an extension with a "." before. *)
|
|
Packit |
9ff65e |
val add_extension: filename -> extension -> filename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Replace extension. *)
|
|
Packit |
9ff65e |
val replace_extension: filename -> extension -> filename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** {2 PATH-like operation}*)
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** PATH-like refers the environment variable PATH. This variable holds a list
|
|
Packit |
9ff65e |
of filename. The functions [string_of_path] and [path_of_string] allow to
|
|
Packit |
9ff65e |
convert this kind of list by using the good separator between filename.
|
|
Packit |
9ff65e |
*)
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Create a PATH-like string. *)
|
|
Packit |
9ff65e |
val string_of_path: filename list -> string
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Extract filenames from a PATH-like string. *)
|
|
Packit |
9ff65e |
val path_of_string: string -> filename list
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** {2 Filename specifications} *)
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Definition of operations for path manipulation. *)
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Generic operations. *)
|
|
Packit |
9ff65e |
module type PATH_SPECIFICATION =
|
|
Packit |
9ff65e |
sig
|
|
Packit |
9ff65e |
type filename
|
|
Packit |
9ff65e |
type extension
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** {3 Converting abstract type from/to string } *)
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Create a filename from a string. *)
|
|
Packit |
9ff65e |
val string_of_filename: filename -> string
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Create a string from a filename. *)
|
|
Packit |
9ff65e |
val filename_of_string: string -> filename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Create an extension from a string. *)
|
|
Packit |
9ff65e |
val extension_of_string: string -> extension
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Return string representation of an extension. *)
|
|
Packit |
9ff65e |
val string_of_extension: extension -> string
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** {3 Standard operations} *)
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** See {!FilePath.make_filename} *)
|
|
Packit |
9ff65e |
val make_filename: string list -> filename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** See {!FilePath.is_subdir} *)
|
|
Packit |
9ff65e |
val is_subdir: filename -> filename -> bool
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** See {!FilePath.is_updir} *)
|
|
Packit |
9ff65e |
val is_updir: filename -> filename -> bool
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** See {!FilePath.compare} *)
|
|
Packit |
9ff65e |
val compare: filename -> filename -> int
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** See {!FilePath.basename} *)
|
|
Packit |
9ff65e |
val basename: filename -> filename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** See {!FilePath.dirname} *)
|
|
Packit |
9ff65e |
val dirname: filename -> filename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** See {!FilePath.concat} *)
|
|
Packit |
9ff65e |
val concat: filename -> filename -> filename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** See {!FilePath.reduce} *)
|
|
Packit |
9ff65e |
val reduce: ?no_symlink:bool -> filename -> filename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** See {!FilePath.make_absolute} *)
|
|
Packit |
9ff65e |
val make_absolute: filename -> filename -> filename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** See {!FilePath.make_relative} *)
|
|
Packit |
9ff65e |
val make_relative: filename -> filename -> filename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** See {!FilePath.reparent} *)
|
|
Packit |
9ff65e |
val reparent: filename -> filename -> filename -> filename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** See {!FilePath.identity} *)
|
|
Packit |
9ff65e |
val identity: filename -> filename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** See {!FilePath.is_valid} *)
|
|
Packit |
9ff65e |
val is_valid: filename -> bool
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** See {!FilePath.is_relative} *)
|
|
Packit |
9ff65e |
val is_relative: filename -> bool
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** See {!FilePath.is_current} *)
|
|
Packit |
9ff65e |
val is_current: filename -> bool
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** See {!FilePath.is_parent} *)
|
|
Packit |
9ff65e |
val is_parent: filename -> bool
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** See {!FilePath.chop_extension} *)
|
|
Packit |
9ff65e |
val chop_extension: filename -> filename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** See {!FilePath.get_extension} *)
|
|
Packit |
9ff65e |
val get_extension: filename -> extension
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** See {!FilePath.check_extension} *)
|
|
Packit |
9ff65e |
val check_extension: filename -> extension -> bool
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** See {!FilePath.add_extension} *)
|
|
Packit |
9ff65e |
val add_extension: filename -> extension -> filename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** See {!FilePath.replace_extension} *)
|
|
Packit |
9ff65e |
val replace_extension: filename -> extension -> filename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** See {!FilePath.string_of_path} *)
|
|
Packit |
9ff65e |
val string_of_path: filename list -> string
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** See {!FilePath.path_of_string} *)
|
|
Packit |
9ff65e |
val path_of_string: string -> filename list
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** See {!FilePath.current_dir} *)
|
|
Packit |
9ff65e |
val current_dir: filename
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** See {!FilePath.parent_dir} *)
|
|
Packit |
9ff65e |
val parent_dir: filename
|
|
Packit |
9ff65e |
end
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Generic operations, with type filename and extension as strings. *)
|
|
Packit |
9ff65e |
module type PATH_STRING_SPECIFICATION =
|
|
Packit |
9ff65e |
sig
|
|
Packit |
9ff65e |
module Abstract: PATH_SPECIFICATION
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
include PATH_SPECIFICATION with
|
|
Packit |
9ff65e |
type filename = string and
|
|
Packit |
9ff65e |
type extension = string
|
|
Packit |
9ff65e |
end
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Operations on filenames for other OS. The {!DefaultPath} always match the
|
|
Packit |
9ff65e |
current OS.
|
|
Packit |
9ff65e |
*)
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Default operating system. *)
|
|
Packit |
9ff65e |
module DefaultPath: PATH_STRING_SPECIFICATION
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Unix operating system. *)
|
|
Packit |
9ff65e |
module UnixPath: PATH_STRING_SPECIFICATION
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** MacOS operating system. *)
|
|
Packit |
9ff65e |
module MacOSPath: PATH_STRING_SPECIFICATION
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Win32 operating system. *)
|
|
Packit |
9ff65e |
module Win32Path: PATH_STRING_SPECIFICATION
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
(** Cygwin operating system. *)
|
|
Packit |
9ff65e |
module CygwinPath: PATH_STRING_SPECIFICATION
|