caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Run-time evaluation of a Printf format string
@ 2008-08-28 13:37 Jan Kybic
  2008-08-28 14:10 ` [Caml-list] " David Teller
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Jan Kybic @ 2008-08-28 13:37 UTC (permalink / raw)
  To: caml-list

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

-- 
-------------------------------------------------------------------------
Jan Kybic <kybic@fel.cvut.cz>                       tel. +420 2 2435 5721
http://cmp.felk.cvut.cz/~kybic                      ICQ 200569450
D


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Run-time evaluation of a Printf format string
  2008-08-28 13:37 Run-time evaluation of a Printf format string Jan Kybic
@ 2008-08-28 14:10 ` David Teller
  2008-08-28 14:58 ` Edgar Friendly
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: David Teller @ 2008-08-28 14:10 UTC (permalink / raw)
  To: Jan Kybic; +Cc: caml-list

As you may have seen in Pervasives.ml, format6 and string are actually
the same thing. Which means that you can Obj.magic your way around the
problem (gasp!) -- provided you have already checked manually that the
string actually represents the format you're interested in.

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.

[1]
https://forge.ocamlcore.org/plugins/scmsvn/viewcvs.php/trunk/extlib/IO.mli?rev=23&root=batteries&view=auto 



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. 


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Run-time evaluation of a Printf format string
  2008-08-28 13:37 Run-time evaluation of a Printf format string Jan Kybic
  2008-08-28 14:10 ` [Caml-list] " David Teller
@ 2008-08-28 14:58 ` Edgar Friendly
  2008-08-28 15:01 ` Dave Benjamin
  2008-08-28 16:01 ` Stéphane Glondu
  3 siblings, 0 replies; 6+ messages in thread
From: Edgar Friendly @ 2008-08-28 14:58 UTC (permalink / raw)
  To: Jan Kybic, caml-list

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Run-time evaluation of a Printf format string
  2008-08-28 13:37 Run-time evaluation of a Printf format string Jan Kybic
  2008-08-28 14:10 ` [Caml-list] " David Teller
  2008-08-28 14:58 ` Edgar Friendly
@ 2008-08-28 15:01 ` Dave Benjamin
  2008-08-28 16:01 ` Stéphane Glondu
  3 siblings, 0 replies; 6+ messages in thread
From: Dave Benjamin @ 2008-08-28 15:01 UTC (permalink / raw)
  To: Jan Kybic; +Cc: caml-list

Jan Kybic wrote:
>         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? 

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Run-time evaluation of a Printf format string
  2008-08-28 13:37 Run-time evaluation of a Printf format string Jan Kybic
                   ` (2 preceding siblings ...)
  2008-08-28 15:01 ` Dave Benjamin
@ 2008-08-28 16:01 ` Stéphane Glondu
  2008-08-29  7:34   ` Jan Kybic
  3 siblings, 1 reply; 6+ messages in thread
From: Stéphane Glondu @ 2008-08-28 16:01 UTC (permalink / raw)
  To: Jan Kybic; +Cc: caml-list

Jan Kybic wrote:
>         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?

What about Scanf.format_from_string?


Cheers,

-- 
Stéphane


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Run-time evaluation of a Printf format string
  2008-08-28 16:01 ` Stéphane Glondu
@ 2008-08-29  7:34   ` Jan Kybic
  0 siblings, 0 replies; 6+ messages in thread
From: Jan Kybic @ 2008-08-29  7:34 UTC (permalink / raw)
  To: caml-list

> Jan Kybic wrote:
>>         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?
>

> Stéphane Glondu <steph@glondu.net> writes:
> What about Scanf.format_from_string?

> Dave Benjamin <dave@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 <kybic@fel.cvut.cz>                       tel. +420 2 2435 5721
http://cmp.felk.cvut.cz/~kybic                      ICQ 200569450


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2008-08-29  7:35 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-08-28 13:37 Run-time evaluation of a Printf format string Jan Kybic
2008-08-28 14:10 ` [Caml-list] " David Teller
2008-08-28 14:58 ` Edgar Friendly
2008-08-28 15:01 ` Dave Benjamin
2008-08-28 16:01 ` Stéphane Glondu
2008-08-29  7:34   ` Jan Kybic

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).