For the moment I have started to implement it myself for the limited
set of format specifications I will need but if there is some more
elegant solution I would be interested to hear about it.
Thanks,
Jan
--
-------------------------------------------------------------------------
Jan Kybic <ky...@fel.cvut.cz> tel. +420 2 2435 5721
http://cmp.felk.cvut.cz/~kybic ICQ 200569450
D
_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
Of course, that's a tad risky. If I were you, I'd rather re-implement a
small unparsing language with a format comparable to printf's and a tiny
parser to go with it.
Does this help?
Cheers,
David
P.S.:
If you're interested, I've put together a little bit of documentation
on format* which I think can't be found in OCaml's doc [1]. Look for
"{7 Format4}" in the comments.
On Thu, 2008-08-28 at 15:37 +0200, Jan Kybic wrote:
> Hello,
> I would need an equivalent of Printf.sprintf where the
> format string is not constant, it is read from the command line.
> The motivation is to let the user specify a template for file names,
> such as "img%03d.png". Can this be achieved in Ocaml? It seems not, as
> Pervasives.string_of_format only accepts constant strings.
> Or is there some external library useful for this task?
>
> For the moment I have started to implement it myself for the limited
> set of format specifications I will need but if there is some more
> elegant solution I would be interested to hear about it.
>
> Thanks,
>
> Jan
>
--
David Teller-Rajchenbach
Security of Distributed Systems
http://www.univ-orleans.fr/lifo/Members/David.Teller
Angry researcher: French Universities need reforms, but the LRU act brings liquidations.
What arguments will you pass to your sprintf command? Just a single
integer? Or do you have multiple values that can be inserted into the
template.
For templating like this, you probably want the different %x's to refer
to different values in your program (i.e. %d - count number, %w - width
of image, %h - height of image, etc). For this use, you'll have to
write your own handler of the input "format" strings, and you won't rely
on sprintf to do the work.
E
Does it have to be printf-style formatting? If you can switch to a
syntax like $(foo), you can use Buffer.add_substitute:
# Buffer.add_substitute;;
- : Buffer.t -> (string -> string) -> string -> unit = <fun>
# let buffer = Buffer.create 0;;
val buffer : Buffer.t = <abstr>
# let vars = ["who", "world"];;
val vars : (string * string) list = [("who", "world")]
# Buffer.add_substitute buffer
(fun name -> List.assoc name vars)
"hello $(who)";;
- : unit = ()
# Buffer.contents buffer;;
- : string = "hello world"
Dave
What about Scanf.format_from_string?
Cheers,
--
Stéphane
> Stéphane Glondu <st...@glondu.net> writes:
> What about Scanf.format_from_string?
> Dave Benjamin <da...@ramenlabs.com> writes:
> Does it have to be printf-style formatting? If you can switch to a
> syntax like $(foo), you can use Buffer.add_substitute:
Thank you for all your suggestions. Here is my summary:
- If I only want to insert a suitably formatted sequence number,
the simplest solution is using Scanf.format_from_string, such as
let format = "img%03d.png" in
Printf.sprintf (Scanf.format_from_string format "%d") num
- If I want to be able to insert several different variables
(e.g. sequence number, image height and width) but do not
have to specify formatting (like padding zeroes), then
Buffer.add_substitute is easy enough to use.
- If I want both to insert several different variables and to
specify the formatting, I will have to implement my own parser and
interpreter of format strings.
Jan
--
-------------------------------------------------------------------------
Jan Kybic <ky...@fel.cvut.cz> tel. +420 2 2435 5721
http://cmp.felk.cvut.cz/~kybic ICQ 200569450
_______________________________________________