-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilesystem-win32.ml
More file actions
63 lines (49 loc) · 1.98 KB
/
Filesystem-win32.ml
File metadata and controls
63 lines (49 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
(* Invariant: pathnames are strings that start with a drive letter and
':\'. There are no backslashes in the string. *)
type pathname = string
external get_full_path_name : string -> string
= "win_caml_GetFullPathName"
let replace_forwardslashes_with_backwardslashes str =
let str = String.copy str in
for i = 0 to String.length str - 1 do
if str.[i] = '/' then str.[i] <- '\\'
done;
str
let pathname_of_relative_path relative_path =
"\\\\?\\" ^ get_full_path_name (replace_forwardslashes_with_backwardslashes relative_path)
let append pathname relative_path =
pathname ^ "\\" ^ (replace_forwardslashes_with_backwardslashes relative_path)
let string_of_pathname pathname =
pathname
(**********************************************************************)
(* File opening *)
external file_exists : pathname -> bool = "file_exists_unicode"
external openfile : pathname -> Unix.open_flag list -> Unix.file_perm -> Unix.file_descr
= "open_unicode"
(**********************************************************************)
(* Directories *)
external findfirst_unicode : string -> string * int = "win_findfirst_unicode"
external findnext_unicode : int -> string = "win_findnext_unicode"
external win_findclose : int -> unit = "win_findclose"
external is_directory : pathname -> bool = "isdir_unicode"
type dir_entry =
Dir_empty
| Dir_read of string
| Dir_toread
type dir_handle =
{ dirname: string; mutable handle: int; mutable entry_read: dir_entry }
let opendir dirname =
try
let (first_entry, handle) = findfirst_unicode (dirname ^ "\\*.*") in
{ dirname = dirname; handle = handle; entry_read = Dir_read first_entry }
with End_of_file ->
{ dirname = dirname; handle = 0; entry_read = Dir_empty }
let readdir d =
match d.entry_read with
Dir_empty -> raise End_of_file
| Dir_read name -> d.entry_read <- Dir_toread; name
| Dir_toread -> findnext_unicode d.handle
let closedir d =
match d.entry_read with
Dir_empty -> ()
| _ -> win_findclose d.handle