Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Unix-Filekind => codexample with questions
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Remi VANICAT <vanicat@l...>
Subject: Re: [Caml-list] Unix-Filekind => codexample with questions
Oliver Bandel <oliver@first.in-berlin.de> writes:

> Hello,
> 
> I need a function, which tells me if a file is
> a regular file or not.
> 
> Please look at my code-example:
> 
> let is_regularfile name =
>       let filekind name = let tmp = Unix.stat name in tmp.Unix.st_kind
>       in
>       let fk = try (filekind name ) with
>                   | Unix.Unix_error (Unix.ENOENT,_,_) -> Unix.S_DIR
>                   | _ -> Unix.S_DIR
> 
>        in
>        match fk with
>        | Unix.S_REG -> true
>        | _          -> false;;
> 
> 
> 

[...]


> 
> IMHO it looks a littlebid dirty, to return Unix.S_DIR
> on an error-case, even if it is only internal to the
> function is_regularfile.


it is, i would use :

let is_regularfile name =
  let filekind name = let tmp = Unix.stat name in tmp.Unix.st_kind in
  try 
    match filekind name with
    | Unix.S_REG -> true
    | _          -> false;;
  with
    | Unix.Unix_error (Unix.ENOENT,_,_) -> false
    | _ -> false



-- 
Rémi Vanicat
vanicat@labri.u-bordeaux.fr
http://dept-info.labri.u-bordeaux.fr/~vanicat
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners