diff options
author | Jacques Garrigue <garrigue at math.nagoya-u.ac.jp> | 1999-12-21 13:58:12 +0000 |
---|---|---|
committer | Jacques Garrigue <garrigue at math.nagoya-u.ac.jp> | 1999-12-21 13:58:12 +0000 |
commit | 4f9a5ee37b2024e7ba1a2d5002be179a9340e379 (patch) | |
tree | 8236c9dd9ebb6a8e8869979fa29a718faa707981 | |
parent | 4c16e2165274ff208ba1b2a5ed188dca487def69 (diff) |
clean-up error handling code
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@2706 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
-rw-r--r-- | otherlibs/labltk/browser/useunix.ml | 38 | ||||
-rw-r--r-- | otherlibs/labltk/jpf/fileselect.ml | 54 |
2 files changed, 47 insertions, 45 deletions
diff --git a/otherlibs/labltk/browser/useunix.ml b/otherlibs/labltk/browser/useunix.ml index f65f78c0d..9d29cc050 100644 --- a/otherlibs/labltk/browser/useunix.ml +++ b/otherlibs/labltk/browser/useunix.ml @@ -16,17 +16,21 @@ open Unix let get_files_in_directory dir = - try - let dirh = opendir dir in - let rec get_them () = - try - let x = readdir dirh in - x :: get_them () - with - _ -> closedir dirh; [] - in - Sort.list order:(<) (get_them ()) - with Unix_error _ -> [] + match + try Some(opendir dir) with Unix_error _ -> None + with + None -> [] + | Some dirh -> + let rec get_them l = + match + try Some(readdir dirh) with _ -> None + with + | Some x -> + get_them (x::l) + | None -> + closedir dirh; l + in + Sort.list order:(<=) (get_them []) let is_directory name = try @@ -39,11 +43,13 @@ let get_directories_in_files :path = (************************************************** Subshell call *) let subshell :cmd = let rc = open_process_in cmd in - let rec it () = - try - let x = input_line rc in x :: it () - with _ -> [] + let rec it l = + match + try Some(input_line rc) with _ -> None + with + Some x -> it (x::l) + | None -> List.rev l in - let answer = it () in + let answer = it [] in ignore (close_process_in rc); answer diff --git a/otherlibs/labltk/jpf/fileselect.ml b/otherlibs/labltk/jpf/fileselect.ml index 9974bc3c1..bf981eea6 100644 --- a/otherlibs/labltk/jpf/fileselect.ml +++ b/otherlibs/labltk/jpf/fileselect.ml @@ -54,13 +54,16 @@ let subshell cmd = | id -> close w; let rc = in_channel_of_descr r in - let rec it () = try - let x = input_line rc in x:: it () - with _ -> [] + let rec it l = + match + try Some(input_line rc) with _ -> None + with + Some x -> it (x::l) + | None -> List.rev l in - let answer = it() in - close_in rc; (* because of finalize_channel *) - let p, st = waitpid mode:[] id in answer + let answer = it [] in + close_in rc; (* because of finalize_channel *) + let p, st = waitpid mode:[] id in answer (***************************************************************** Path name *) @@ -96,31 +99,24 @@ let ls dir pattern = let get_files_in_directory dir = let dirh = opendir dir in - let rec get_them () = - try - let x = readdir dirh in (* no let cause Out of memory *) - x::(get_them ()) - with - End_of_file -> closedir dirh; [] + let rec get_them l = + match + try Some(Unix.readdir dirh) with _ -> None + with + | None -> + Unix.closedir dirh; l + | Some x -> + get_them (x::l) in - Sort.list order:(<) (get_them ()) + Sort.list order:(<=) (get_them []) -let rec get_directories_in_files path = function - [] -> [] - | x::xs -> - if try (stat (path ^ x)).st_kind = S_DIR with _ -> false then - x::(get_directories_in_files path xs) - else get_directories_in_files path xs - -let remove_directories dirname = - let rec remove = function - [] -> [] - | x :: xs -> - if try (stat (dirname ^ x)).st_kind = S_DIR with _ -> true then - remove xs - else - x :: (remove xs) - in remove +let rec get_directories_in_files path = + List.filter + pred:(fun x -> try (stat (path ^ x)).st_kind = S_DIR with _ -> false) + +let remove_directories path = + List.filter + pred:(fun x -> try (stat (path ^ x)).st_kind <> S_DIR with _ -> false) (************************* a nice interface to listbox - from frx_listbox.ml *) |