1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the GNU Library General Public License. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
external format_int: string -> int -> string = "format_int"
|
|
|
|
external format_float: string -> float -> string = "format_float"
|
|
|
|
|
|
|
|
let fprintf outchan format =
|
|
|
|
let format = (Obj.magic format : string) in
|
|
|
|
let rec doprn i =
|
|
|
|
if i >= String.length format then
|
|
|
|
Obj.magic ()
|
1995-05-05 03:05:18 -07:00
|
|
|
else begin
|
|
|
|
let c = String.unsafe_get format i in
|
|
|
|
if c <> '%' then begin
|
|
|
|
output_char outchan c;
|
|
|
|
doprn (succ i)
|
|
|
|
end else begin
|
|
|
|
let j = skip_args (succ i) in
|
|
|
|
match String.unsafe_get format j with
|
|
|
|
'%' ->
|
|
|
|
output_char outchan '%';
|
|
|
|
doprn (succ j)
|
|
|
|
| 's' ->
|
|
|
|
Obj.magic(fun s ->
|
|
|
|
if j <= i+1 then
|
|
|
|
output_string outchan s
|
|
|
|
else begin
|
|
|
|
let p =
|
|
|
|
try
|
|
|
|
int_of_string (String.sub format (i+1) (j-i-1))
|
|
|
|
with _ ->
|
|
|
|
invalid_arg "fprintf: bad %s format" in
|
1999-02-17 10:49:17 -08:00
|
|
|
if p > 0 && String.length s < p then begin
|
1995-05-05 03:05:18 -07:00
|
|
|
output_string outchan
|
|
|
|
(String.make (p - String.length s) ' ');
|
1995-05-04 03:15:53 -07:00
|
|
|
output_string outchan s
|
1999-02-17 10:49:17 -08:00
|
|
|
end else if p < 0 && String.length s < -p then begin
|
1995-05-05 03:05:18 -07:00
|
|
|
output_string outchan s;
|
|
|
|
output_string outchan
|
|
|
|
(String.make (-p - String.length s) ' ')
|
|
|
|
end else
|
|
|
|
output_string outchan s
|
|
|
|
end;
|
|
|
|
doprn (succ j))
|
|
|
|
| 'c' ->
|
|
|
|
Obj.magic(fun c ->
|
|
|
|
output_char outchan c;
|
|
|
|
doprn (succ j))
|
1999-02-12 04:34:32 -08:00
|
|
|
| 'd' | 'i' | 'o' | 'x' | 'X' | 'u' ->
|
1995-05-05 03:05:18 -07:00
|
|
|
Obj.magic(fun n ->
|
|
|
|
output_string outchan
|
|
|
|
(format_int (String.sub format i (j-i+1)) n);
|
|
|
|
doprn (succ j))
|
|
|
|
| 'f' | 'e' | 'E' | 'g' | 'G' ->
|
|
|
|
Obj.magic(fun f ->
|
|
|
|
output_string outchan
|
|
|
|
(format_float (String.sub format i (j-i+1)) f);
|
|
|
|
doprn (succ j))
|
|
|
|
| 'b' ->
|
|
|
|
Obj.magic(fun b ->
|
|
|
|
output_string outchan (string_of_bool b);
|
|
|
|
doprn (succ j))
|
|
|
|
| 'a' ->
|
|
|
|
Obj.magic(fun printer arg ->
|
|
|
|
printer outchan arg;
|
|
|
|
doprn(succ j))
|
|
|
|
| 't' ->
|
|
|
|
Obj.magic(fun printer ->
|
|
|
|
printer outchan;
|
|
|
|
doprn(succ j))
|
|
|
|
| c ->
|
|
|
|
invalid_arg ("fprintf: unknown format")
|
|
|
|
end
|
|
|
|
end
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
and skip_args j =
|
1995-05-05 03:05:18 -07:00
|
|
|
match String.unsafe_get format j with
|
1995-05-04 03:15:53 -07:00
|
|
|
'0' .. '9' | ' ' | '.' | '-' -> skip_args (succ j)
|
|
|
|
| c -> j
|
|
|
|
|
|
|
|
in doprn 0
|
|
|
|
|
|
|
|
let printf fmt = fprintf stdout fmt
|
|
|
|
and eprintf fmt = fprintf stderr fmt
|
|
|
|
|
1999-02-25 02:26:38 -08:00
|
|
|
let bprintf_internal tostring buf format =
|
1995-05-05 03:05:18 -07:00
|
|
|
let format = (Obj.magic format : string) in
|
1999-02-25 02:26:38 -08:00
|
|
|
let rec doprn i =
|
|
|
|
if i >= String.length format then
|
1999-05-15 10:06:40 -07:00
|
|
|
if tostring then begin
|
|
|
|
let res = Obj.magic (Buffer.contents buf) in
|
|
|
|
Buffer.clear buf; (* just in case [bs]printf is partially applied *)
|
|
|
|
res
|
|
|
|
end else
|
|
|
|
Obj.magic ()
|
1999-02-25 02:26:38 -08:00
|
|
|
else begin
|
|
|
|
let c = String.unsafe_get format i in
|
|
|
|
if c <> '%' then begin
|
|
|
|
Buffer.add_char buf c;
|
|
|
|
doprn (succ i)
|
|
|
|
end else begin
|
1995-05-05 03:05:18 -07:00
|
|
|
let j = skip_args (succ i) in
|
|
|
|
match String.unsafe_get format j with
|
|
|
|
'%' ->
|
1999-02-25 02:26:38 -08:00
|
|
|
Buffer.add_char buf '%';
|
|
|
|
doprn (succ j)
|
1995-05-05 03:05:18 -07:00
|
|
|
| 's' ->
|
|
|
|
Obj.magic(fun s ->
|
1999-02-25 02:26:38 -08:00
|
|
|
if j <= i+1 then
|
|
|
|
Buffer.add_string buf s
|
|
|
|
else begin
|
|
|
|
let p =
|
|
|
|
try
|
|
|
|
int_of_string (String.sub format (i+1) (j-i-1))
|
|
|
|
with _ ->
|
|
|
|
invalid_arg "fprintf: bad %s format" in
|
|
|
|
if p > 0 && String.length s < p then begin
|
|
|
|
Buffer.add_string buf
|
|
|
|
(String.make (p - String.length s) ' ');
|
|
|
|
Buffer.add_string buf s
|
|
|
|
end else if p < 0 && String.length s < -p then begin
|
|
|
|
Buffer.add_string buf s;
|
|
|
|
Buffer.add_string buf
|
|
|
|
(String.make (-p - String.length s) ' ')
|
|
|
|
end else
|
|
|
|
Buffer.add_string buf s
|
|
|
|
end;
|
|
|
|
doprn (succ j))
|
1995-05-05 03:05:18 -07:00
|
|
|
| 'c' ->
|
|
|
|
Obj.magic(fun c ->
|
1999-02-25 02:26:38 -08:00
|
|
|
Buffer.add_char buf c;
|
|
|
|
doprn (succ j))
|
1999-02-17 10:49:17 -08:00
|
|
|
| 'd' | 'i' | 'o' | 'x' | 'X' | 'u' ->
|
1995-05-05 03:05:18 -07:00
|
|
|
Obj.magic(fun n ->
|
1999-02-25 02:26:38 -08:00
|
|
|
Buffer.add_string buf
|
|
|
|
(format_int (String.sub format i (j-i+1)) n);
|
|
|
|
doprn (succ j))
|
1995-05-05 03:05:18 -07:00
|
|
|
| 'f' | 'e' | 'E' | 'g' | 'G' ->
|
|
|
|
Obj.magic(fun f ->
|
1999-02-25 02:26:38 -08:00
|
|
|
Buffer.add_string buf
|
|
|
|
(format_float (String.sub format i (j-i+1)) f);
|
|
|
|
doprn (succ j))
|
1995-05-05 03:05:18 -07:00
|
|
|
| 'b' ->
|
|
|
|
Obj.magic(fun b ->
|
1999-02-25 02:26:38 -08:00
|
|
|
Buffer.add_string buf (string_of_bool b);
|
|
|
|
doprn (succ j))
|
1995-05-05 03:05:18 -07:00
|
|
|
| 'a' ->
|
1999-02-25 02:26:38 -08:00
|
|
|
if tostring then
|
|
|
|
Obj.magic(fun printer arg ->
|
|
|
|
Buffer.add_string buf (printer () arg);
|
|
|
|
doprn(succ j))
|
|
|
|
else
|
|
|
|
Obj.magic(fun printer arg ->
|
|
|
|
printer buf arg;
|
|
|
|
doprn(succ j))
|
1995-05-05 03:05:18 -07:00
|
|
|
| 't' ->
|
1999-02-25 02:26:38 -08:00
|
|
|
if tostring then
|
|
|
|
Obj.magic(fun printer ->
|
|
|
|
Buffer.add_string buf (printer ());
|
|
|
|
doprn(succ j))
|
|
|
|
else
|
|
|
|
Obj.magic(fun printer ->
|
|
|
|
printer buf;
|
|
|
|
doprn(succ j))
|
1995-05-05 03:05:18 -07:00
|
|
|
| c ->
|
|
|
|
invalid_arg ("sprintf: unknown format")
|
|
|
|
end
|
1999-02-25 02:26:38 -08:00
|
|
|
end
|
1995-05-05 03:05:18 -07:00
|
|
|
|
|
|
|
and skip_args j =
|
|
|
|
match String.unsafe_get format j with
|
|
|
|
'0' .. '9' | ' ' | '.' | '-' -> skip_args (succ j)
|
|
|
|
| c -> j
|
|
|
|
|
1999-02-25 02:26:38 -08:00
|
|
|
in doprn 0
|
|
|
|
|
|
|
|
let bprintf buf fmt = bprintf_internal false buf fmt
|
|
|
|
|
|
|
|
let sprintf fmt = bprintf_internal true (Buffer.create 16) fmt
|