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-31 08:03:04 -08:00
|
|
|
let systype = (Sys.get_config()).Sys.os_type
|
1996-10-07 07:04:03 -07:00
|
|
|
|
1996-10-06 09:38:09 -07:00
|
|
|
let current_dir_name =
|
1996-10-07 07:04:03 -07:00
|
|
|
match systype 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-10-06 09:38:09 -07:00
|
|
|
| _ -> failwith "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-10-07 07:04:03 -07:00
|
|
|
match systype 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-10-06 09:38:09 -07:00
|
|
|
| _ -> failwith "Filename.concat: unknown system"
|
1996-02-15 08:25:44 -08:00
|
|
|
|
1996-10-06 09:38:09 -07:00
|
|
|
let unix_is_absolute n =
|
1996-10-31 08:03:04 -08:00
|
|
|
(String.length n >= 1 && String.sub n 0 1 = "/")
|
|
|
|
|| (String.length n >= 2 && String.sub n 0 2 = "./")
|
|
|
|
|| (String.length n >= 3 && String.sub n 0 3 = "../")
|
|
|
|
|
1996-10-06 09:38:09 -07:00
|
|
|
let wnt_is_absolute n =
|
1996-10-31 08:03:04 -08:00
|
|
|
(String.length n >= 1 &&
|
|
|
|
(let s = String.sub n 0 1 in s = "/" || s = "\\"))
|
|
|
|
|| (String.length n >= 2 &&
|
|
|
|
(let s = String.sub n 0 2 in s = "./" || s = ".\\"))
|
|
|
|
|| (String.length n >= 3 &&
|
|
|
|
(let s = String.sub n 0 3 in s = "../" || s = "..\\"))
|
|
|
|
|| (String.length n >= 2 && String.get n 1 = ':')
|
|
|
|
|
1996-10-06 09:38:09 -07:00
|
|
|
let mac_is_absolute n =
|
|
|
|
try
|
|
|
|
for i = 0 to String.length n - 1 do
|
|
|
|
if n.[i] = ':' then raise Exit
|
|
|
|
done;
|
|
|
|
false
|
|
|
|
with Exit -> true
|
1996-02-15 08:25:44 -08:00
|
|
|
|
1996-10-06 09:38:09 -07:00
|
|
|
let is_absolute =
|
1996-10-07 07:04:03 -07:00
|
|
|
match systype with
|
1996-10-06 09:38:09 -07:00
|
|
|
| "Unix" -> unix_is_absolute
|
1996-10-07 07:04:03 -07:00
|
|
|
| "Win32" -> wnt_is_absolute
|
1996-10-09 09:21:02 -07:00
|
|
|
| "MacOS" -> mac_is_absolute
|
1996-10-06 09:38:09 -07:00
|
|
|
| _ -> failwith "Filename.is_absolute: unknown system"
|
|
|
|
|
|
|
|
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-10-07 07:04:03 -07:00
|
|
|
match systype 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-10-06 09:38:09 -07:00
|
|
|
| _ -> failwith "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-10-07 07:04:03 -07:00
|
|
|
match systype 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-10-06 09:38:09 -07:00
|
|
|
| _ -> failwith "Filename.basename: unknown system"
|
1996-10-31 08:03:04 -08:00
|
|
|
|
1996-10-06 09:38:09 -07:00
|
|
|
let dirname =
|
1996-10-07 07:04:03 -07:00
|
|
|
match systype 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-10-06 09:38:09 -07:00
|
|
|
| _ -> failwith "Filename.dirname: unknown system"
|
1996-02-15 08:25:44 -08:00
|
|
|
|
|
|
|
let temporary_directory =
|
1996-10-07 07:04:03 -07:00
|
|
|
match systype 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-10-06 09:38:09 -07:00
|
|
|
| _ -> failwith "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
|
|
|
|