Version française
Home     About     Download     Resources     Contact us    
Browse thread
Run-time evaluation of a Printf format string
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Edgar Friendly <thelema314@g...>
Subject: Re: [Caml-list] Run-time evaluation of a Printf format string
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
> 

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