English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] unix.chop_extension
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-05-27 (09:47)
From: skaller <skaller@u...>
Subject: Re: [Caml-list] unix.chop_extension
On Thu, 2004-05-27 at 18:15, YANG Shouxun wrote:

> +  let bn = Filename.basename name in
> +    try
> +      String.sub bn 0 (String.rindex bn '.')
> +    with Not_found ->
> +      invalid_arg "Filename.chop_extension"

But this code returns only the basename with extension chopped!

You actually need this:

let xchop_extension f =
  concat (dirname f) (chop_extension (basename f))

but the problem with that is it doesn't just
chop the extension .. it also adds a './'
to the start of the filename if it didn't
have a directory prefix.

So I actually wrote that and then stripped
any leading ./ off .. which is not generally
useful, because one might wish to preserve
that info.

It can be fixed by "even worse hackery" calling
is_implicit and is_relative before and after
the concatenation, and adjusting the result
to preserve those properties.

The right way to fix this is to rindex
for "/" first, and limit the scan for "."
by the result.

BTW: many Win98 apps including IE gets this totally wrong :(
Grrrr.... It scans for the FIRST "." and discards
the rest of the filename after the second one.
When I save 'x.y.jpg' I actually save 'x.y'
and the filetype is lost.

John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net

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