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: John Prevost <visigoth@c...>
Subject: Re: [Caml-list] Unix-Filekind => codexample with questions
>>>>> "ob" == Oliver Bandel <oliver@first.in-berlin.de> writes:

    ob> Hello, I need a function, which tells me if a file is a
    ob> regular file or not.

    ob> 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;;


    ob> This example works: It tells me, if a file is of kind/type
    ob> regular file with true. If it is not a regular file or an
    ob> error occured, it tells me this by passing me the result
    ob> false.

How about:

let is_regularfile name =
  try (match Unix.stat name with
         | {Unix.st_kind = Unix.S_REG} -> true
         | _ -> false)
  with
    _ -> false

I'm not sure why you're using so many intermediate values here--it
just seems to make the code harder to figure out.  Better to just wrap
a try around the whole check, and use a small simple match on the stat
result.

John.
-------------------
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