1996-02-15 08:25:44 -08:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
1996-02-15 08:25:44 -08:00
|
|
|
(* *)
|
1996-10-31 08:03:04 -08:00
|
|
|
(* Xavier Leroy and Damien Doligez, INRIA Rocquencourt *)
|
1996-02-15 08:25:44 -08:00
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1996-02-15 08:25:44 -08:00
|
|
|
(* Automatique. Distributed only by permission. *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1996-10-06 09:38:09 -07:00
|
|
|
let current_dir_name =
|
1996-11-07 03:00:19 -08:00
|
|
|
match Sys.os_type with
|
1996-10-06 09:38:09 -07:00
|
|
|
| "Unix" -> "."
|
1996-10-07 07:04:03 -07:00
|
|
|
| "Win32" -> "."
|
1996-10-09 09:21:02 -07:00
|
|
|
| "MacOS" -> ":"
|
1996-11-07 03:00:19 -08:00
|
|
|
| _ -> invalid_arg "Filename.current_dir_name: unknown system"
|
1996-02-15 08:25:44 -08:00
|
|
|
|
1996-10-06 09:38:09 -07:00
|
|
|
let unix_concat dirname filename =
|
1996-02-15 08:25:44 -08:00
|
|
|
let l = String.length dirname in
|
|
|
|
if l = 0 or dirname.[l-1] = '/'
|
|
|
|
then dirname ^ filename
|
|
|
|
else dirname ^ "/" ^ filename
|
1996-10-31 08:03:04 -08:00
|
|
|
|
1996-10-06 09:38:09 -07:00
|
|
|
let wnt_concat dirname filename =
|
1996-02-15 08:25:44 -08:00
|
|
|
let l = String.length dirname in
|
|
|
|
if l = 0 or (let c = dirname.[l-1] in c = '/' or c = '\\' or c = ':')
|
|
|
|
then dirname ^ filename
|
|
|
|
else dirname ^ "\\" ^ filename
|
1996-10-31 08:03:04 -08:00
|
|
|
|
1996-10-06 09:38:09 -07:00
|
|
|
let mac_concat dirname filename =
|
|
|
|
let l = String.length dirname in
|
|
|
|
if l = 0 or dirname.[l-1] = ':'
|
|
|
|
then dirname ^ filename
|
|
|
|
else dirname ^ ":" ^ filename
|
|
|
|
|
|
|
|
let concat =
|
1996-11-07 03:00:19 -08:00
|
|
|
match Sys.os_type with
|
1996-10-06 09:38:09 -07:00
|
|
|
| "Unix" -> unix_concat
|
1996-10-07 07:04:03 -07:00
|
|
|
| "Win32" -> wnt_concat
|
1996-10-09 09:21:02 -07:00
|
|
|
| "MacOS" -> mac_concat
|
1996-11-07 03:00:19 -08:00
|
|
|
| _ -> invalid_arg "Filename.concat: unknown system"
|
1996-02-15 08:25:44 -08:00
|
|
|
|
1997-06-16 10:07:42 -07:00
|
|
|
let unix_is_relative n = String.length n < 1 || n.[0] <> '/';;
|
|
|
|
|
|
|
|
let unix_is_implicit n =
|
|
|
|
unix_is_relative n
|
|
|
|
&& (String.length n < 2 || String.sub n 0 2 <> "./")
|
|
|
|
&& (String.length n < 3 || String.sub n 0 3 <> "../")
|
|
|
|
;;
|
|
|
|
|
|
|
|
let wnt_is_relative n =
|
|
|
|
(String.length n < 1 || n.[0] <> '/')
|
|
|
|
&& (String.length n < 1 || n.[0] <> '\\')
|
|
|
|
&& (String.length n < 2 || n.[1] <> ':')
|
|
|
|
;;
|
|
|
|
|
|
|
|
let wnt_is_implicit n =
|
|
|
|
wnt_is_relative n
|
|
|
|
&& (String.length n < 2 || String.sub n 0 2 <> "./")
|
|
|
|
&& (String.length n < 2 || String.sub n 0 2 <> ".\\")
|
|
|
|
&& (String.length n < 3 || String.sub n 0 3 <> "../")
|
|
|
|
&& (String.length n < 3 || String.sub n 0 3 <> "..\\")
|
|
|
|
;;
|
|
|
|
|
|
|
|
let contains_colon n =
|
1996-10-06 09:38:09 -07:00
|
|
|
try
|
|
|
|
for i = 0 to String.length n - 1 do
|
|
|
|
if n.[i] = ':' then raise Exit
|
|
|
|
done;
|
|
|
|
false
|
|
|
|
with Exit -> true
|
1997-06-16 10:07:42 -07:00
|
|
|
;;
|
|
|
|
|
|
|
|
let mac_is_relative n =
|
|
|
|
(String.length n >= 1 && n.[0] = ':')
|
|
|
|
|| not (contains_colon n)
|
|
|
|
;;
|
|
|
|
|
|
|
|
let mac_is_implicit n = not (contains_colon n);;
|
1996-02-15 08:25:44 -08:00
|
|
|
|
1997-06-16 10:07:42 -07:00
|
|
|
let (is_relative, is_implicit) =
|
1996-11-07 03:00:19 -08:00
|
|
|
match Sys.os_type with
|
1997-06-16 10:07:42 -07:00
|
|
|
| "Unix" -> (unix_is_relative, unix_is_implicit)
|
|
|
|
| "Win32" -> (wnt_is_relative, wnt_is_implicit)
|
|
|
|
| "MacOS" -> (mac_is_relative, mac_is_implicit)
|
|
|
|
| _ -> invalid_arg "Filename.is_relative: unknown system"
|
1996-10-06 09:38:09 -07:00
|
|
|
|
|
|
|
let unix_check_suffix name suff =
|
1996-10-31 08:03:04 -08:00
|
|
|
String.length name >= String.length suff &&
|
1996-10-06 09:38:09 -07:00
|
|
|
String.sub name (String.length name - String.length suff) (String.length suff)
|
|
|
|
= suff
|
1996-10-31 08:03:04 -08:00
|
|
|
|
1996-10-06 09:38:09 -07:00
|
|
|
let wnt_check_suffix name suff =
|
1996-10-31 08:03:04 -08:00
|
|
|
String.length name >= String.length suff &&
|
1996-02-15 08:25:44 -08:00
|
|
|
(let s = String.sub name (String.length name - String.length suff)
|
|
|
|
(String.length suff) in
|
1996-10-31 08:03:04 -08:00
|
|
|
String.lowercase s = String.lowercase suff)
|
|
|
|
|
|
|
|
let mac_check_suffix = unix_check_suffix
|
1996-10-06 09:38:09 -07:00
|
|
|
|
|
|
|
let check_suffix =
|
1996-11-07 03:00:19 -08:00
|
|
|
match Sys.os_type with
|
1996-10-06 09:38:09 -07:00
|
|
|
| "Unix" -> unix_check_suffix
|
1996-10-07 07:04:03 -07:00
|
|
|
| "Win32" -> wnt_check_suffix
|
1996-10-09 09:21:02 -07:00
|
|
|
| "MacOS" -> mac_check_suffix
|
1996-11-07 03:00:19 -08:00
|
|
|
| _ -> invalid_arg "Filename.check_suffix: unknown system"
|
1996-02-15 08:25:44 -08:00
|
|
|
|
|
|
|
let chop_suffix name suff =
|
|
|
|
let n = String.length name - String.length suff in
|
|
|
|
if n < 0 then invalid_arg "Filename.chop_suffix" else String.sub name 0 n
|
1996-10-06 09:38:09 -07:00
|
|
|
|
|
|
|
let rindex s c =
|
|
|
|
let rec pos i =
|
|
|
|
if i < 0 then raise Not_found
|
|
|
|
else if s.[i] = c then i
|
|
|
|
else pos (i - 1)
|
|
|
|
in pos (String.length s - 1)
|
1996-10-31 08:03:04 -08:00
|
|
|
|
|
|
|
let wnt_rindexsep s =
|
1996-10-06 09:38:09 -07:00
|
|
|
let rec pos i =
|
|
|
|
if i < 0 then raise Not_found
|
1996-10-31 08:03:04 -08:00
|
|
|
else if (let c = s.[i] in c = '/' || c = '\\' || c = ':') then i
|
1996-10-06 09:38:09 -07:00
|
|
|
else pos (i - 1)
|
|
|
|
in pos (String.length s - 1)
|
1996-02-15 08:25:44 -08:00
|
|
|
|
|
|
|
let chop_extension name =
|
|
|
|
try
|
|
|
|
String.sub name 0 (rindex name '.')
|
|
|
|
with Not_found ->
|
|
|
|
invalid_arg "Filename.chop_extension"
|
|
|
|
|
1996-10-06 09:38:09 -07:00
|
|
|
let unix_basename name =
|
1996-02-15 08:25:44 -08:00
|
|
|
try
|
|
|
|
let p = rindex name '/' + 1 in
|
|
|
|
String.sub name p (String.length name - p)
|
|
|
|
with Not_found ->
|
|
|
|
name
|
1996-10-31 08:03:04 -08:00
|
|
|
|
1996-10-06 09:38:09 -07:00
|
|
|
let unix_dirname name =
|
1996-02-15 08:25:44 -08:00
|
|
|
try
|
|
|
|
match rindex name '/' with
|
|
|
|
0 -> "/"
|
|
|
|
| n -> String.sub name 0 n
|
|
|
|
with Not_found ->
|
|
|
|
"."
|
1996-10-31 08:03:04 -08:00
|
|
|
|
1996-10-06 09:38:09 -07:00
|
|
|
let wnt_basename name =
|
1996-02-15 08:25:44 -08:00
|
|
|
try
|
1996-10-31 08:03:04 -08:00
|
|
|
let p = wnt_rindexsep name + 1 in
|
1996-02-15 08:25:44 -08:00
|
|
|
String.sub name p (String.length name - p)
|
|
|
|
with Not_found ->
|
|
|
|
name
|
1996-10-31 08:03:04 -08:00
|
|
|
|
1996-10-06 09:38:09 -07:00
|
|
|
let wnt_dirname name =
|
1996-02-15 08:25:44 -08:00
|
|
|
try
|
1996-10-31 08:03:04 -08:00
|
|
|
match wnt_rindexsep name with
|
1996-02-15 08:25:44 -08:00
|
|
|
0 -> "\\"
|
|
|
|
| n -> String.sub name 0 n
|
|
|
|
with Not_found ->
|
|
|
|
"."
|
1996-10-31 08:03:04 -08:00
|
|
|
|
1996-10-06 09:38:09 -07:00
|
|
|
let mac_basename name =
|
|
|
|
try
|
|
|
|
let p = rindex name ':' + 1 in
|
|
|
|
String.sub name p (String.length name - p)
|
|
|
|
with Not_found -> name
|
1996-10-31 08:03:04 -08:00
|
|
|
|
1996-10-06 09:38:09 -07:00
|
|
|
let mac_dirname name =
|
|
|
|
try match rindex name ':' with
|
|
|
|
| 0 -> ":"
|
|
|
|
| n -> String.sub name 0 n
|
|
|
|
with Not_found -> ":"
|
|
|
|
|
|
|
|
let basename =
|
1996-11-07 03:00:19 -08:00
|
|
|
match Sys.os_type with
|
1996-10-06 09:38:09 -07:00
|
|
|
| "Unix" -> unix_basename
|
1996-10-07 07:04:03 -07:00
|
|
|
| "Win32" -> wnt_basename
|
1996-10-09 09:21:02 -07:00
|
|
|
| "MacOS" -> mac_basename
|
1996-11-07 03:00:19 -08:00
|
|
|
| _ -> invalid_arg "Filename.basename: unknown system"
|
1996-10-31 08:03:04 -08:00
|
|
|
|
1996-10-06 09:38:09 -07:00
|
|
|
let dirname =
|
1996-11-07 03:00:19 -08:00
|
|
|
match Sys.os_type with
|
1996-10-06 09:38:09 -07:00
|
|
|
| "Unix" -> unix_dirname
|
1996-10-07 07:04:03 -07:00
|
|
|
| "Win32" -> wnt_dirname
|
1996-10-09 09:21:02 -07:00
|
|
|
| "MacOS" -> mac_dirname
|
1996-11-07 03:00:19 -08:00
|
|
|
| _ -> invalid_arg "Filename.dirname: unknown system"
|
1996-02-15 08:25:44 -08:00
|
|
|
|
|
|
|
let temporary_directory =
|
1996-11-07 03:00:19 -08:00
|
|
|
match Sys.os_type with
|
1996-10-06 09:38:09 -07:00
|
|
|
| "Unix" -> (try Sys.getenv "TMPDIR" with Not_found -> "/tmp")
|
1996-10-07 07:04:03 -07:00
|
|
|
| "Win32" -> (try Sys.getenv "TEMP" with Not_found -> "C:\\temp")
|
1996-10-09 09:21:02 -07:00
|
|
|
| "MacOS" -> (try Sys.getenv "TempFolder" with Not_found -> ":")
|
1996-11-07 03:00:19 -08:00
|
|
|
| _ -> invalid_arg "Filename.temporary_directory: unknown system"
|
1996-10-31 08:03:04 -08:00
|
|
|
|
|
|
|
external open_desc: string -> open_flag list -> int -> int = "sys_open"
|
|
|
|
external close_desc: int -> unit = "sys_close"
|
1996-02-15 08:25:44 -08:00
|
|
|
|
|
|
|
let temp_file prefix suffix =
|
|
|
|
let rec try_name counter =
|
|
|
|
let name =
|
|
|
|
concat temporary_directory (prefix ^ string_of_int counter ^ suffix) in
|
1996-10-31 08:03:04 -08:00
|
|
|
try
|
|
|
|
close_desc(open_desc name [Open_wronly; Open_creat; Open_excl] 0o666);
|
|
|
|
name
|
|
|
|
with Sys_error _ ->
|
|
|
|
try_name (counter + 1)
|
1996-02-15 08:25:44 -08:00
|
|
|
in try_name 0
|
1996-10-31 08:03:04 -08:00
|
|
|
|