1996-09-08 08:41:59 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Objective Caml *)
|
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
|
|
|
(* 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. *)
|
1996-09-08 08:41:59 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
|
|
|
(* Same as ../../stdlib/pervasives.ml, except that I/O functions have
|
|
|
|
been redefined to not block the whole process, but only the calling
|
|
|
|
thread. *)
|
|
|
|
|
1999-11-30 08:07:38 -08:00
|
|
|
(* type 'a option = None | Some of 'a *)
|
1996-09-08 08:41:59 -07:00
|
|
|
|
|
|
|
(* Exceptions *)
|
|
|
|
|
|
|
|
external raise : exn -> 'a = "%raise"
|
|
|
|
|
|
|
|
let failwith s = raise(Failure s)
|
|
|
|
let invalid_arg s = raise(Invalid_argument s)
|
|
|
|
|
|
|
|
exception Exit
|
1997-05-15 06:26:56 -07:00
|
|
|
exception Assert_failure of (string * int * int)
|
1996-09-08 08:41:59 -07:00
|
|
|
|
|
|
|
(* Comparisons *)
|
|
|
|
|
|
|
|
external (=) : 'a -> 'a -> bool = "%equal"
|
|
|
|
external (<>) : 'a -> 'a -> bool = "%notequal"
|
|
|
|
external (<) : 'a -> 'a -> bool = "%lessthan"
|
|
|
|
external (>) : 'a -> 'a -> bool = "%greaterthan"
|
|
|
|
external (<=) : 'a -> 'a -> bool = "%lessequal"
|
|
|
|
external (>=) : 'a -> 'a -> bool = "%greaterequal"
|
1998-11-11 07:35:48 -08:00
|
|
|
external compare: 'a -> 'a -> int = "compare"
|
1996-09-08 08:41:59 -07:00
|
|
|
|
|
|
|
let min x y = if x <= y then x else y
|
|
|
|
let max x y = if x >= y then x else y
|
|
|
|
|
|
|
|
external (==) : 'a -> 'a -> bool = "%eq"
|
|
|
|
external (!=) : 'a -> 'a -> bool = "%noteq"
|
|
|
|
|
|
|
|
(* Boolean operations *)
|
|
|
|
|
|
|
|
external not : bool -> bool = "%boolnot"
|
|
|
|
external (&) : bool -> bool -> bool = "%sequand"
|
|
|
|
external (&&) : bool -> bool -> bool = "%sequand"
|
|
|
|
external (or) : bool -> bool -> bool = "%sequor"
|
|
|
|
external (||) : bool -> bool -> bool = "%sequor"
|
|
|
|
|
|
|
|
(* Integer operations *)
|
|
|
|
|
|
|
|
external (~-) : int -> int = "%negint"
|
|
|
|
external succ : int -> int = "%succint"
|
|
|
|
external pred : int -> int = "%predint"
|
|
|
|
external (+) : int -> int -> int = "%addint"
|
|
|
|
external (-) : int -> int -> int = "%subint"
|
|
|
|
external ( * ) : int -> int -> int = "%mulint"
|
|
|
|
external (/) : int -> int -> int = "%divint"
|
|
|
|
external (mod) : int -> int -> int = "%modint"
|
|
|
|
|
|
|
|
let abs x = if x >= 0 then x else -x
|
|
|
|
|
|
|
|
external (land) : int -> int -> int = "%andint"
|
|
|
|
external (lor) : int -> int -> int = "%orint"
|
|
|
|
external (lxor) : int -> int -> int = "%xorint"
|
|
|
|
|
|
|
|
let lnot x = x lxor (-1)
|
|
|
|
|
|
|
|
external (lsl) : int -> int -> int = "%lslint"
|
|
|
|
external (lsr) : int -> int -> int = "%lsrint"
|
|
|
|
external (asr) : int -> int -> int = "%asrint"
|
|
|
|
|
|
|
|
let min_int = 1 lsl (if 1 lsl 31 = 0 then 30 else 62)
|
|
|
|
let max_int = min_int - 1
|
|
|
|
|
|
|
|
(* Floating-point operations *)
|
|
|
|
|
|
|
|
external (~-.) : float -> float = "%negfloat"
|
|
|
|
external (+.) : float -> float -> float = "%addfloat"
|
|
|
|
external (-.) : float -> float -> float = "%subfloat"
|
|
|
|
external ( *. ) : float -> float -> float = "%mulfloat"
|
|
|
|
external (/.) : float -> float -> float = "%divfloat"
|
|
|
|
external ( ** ) : float -> float -> float = "power_float" "pow" "float"
|
|
|
|
external exp : float -> float = "exp_float" "exp" "float"
|
|
|
|
external acos : float -> float = "acos_float" "acos" "float"
|
1997-03-04 06:55:17 -08:00
|
|
|
external asin : float -> float = "asin_float" "asin" "float"
|
1996-09-08 08:41:59 -07:00
|
|
|
external atan : float -> float = "atan_float" "atan" "float"
|
|
|
|
external atan2 : float -> float -> float = "atan2_float" "atan2" "float"
|
1997-03-04 06:55:17 -08:00
|
|
|
external cos : float -> float = "cos_float" "cos" "float"
|
|
|
|
external cosh : float -> float = "cosh_float" "cosh" "float"
|
|
|
|
external log : float -> float = "log_float" "log" "float"
|
|
|
|
external log10 : float -> float = "log10_float" "log10" "float"
|
|
|
|
external sin : float -> float = "sin_float" "sin" "float"
|
|
|
|
external sinh : float -> float = "sinh_float" "sinh" "float"
|
|
|
|
external sqrt : float -> float = "sqrt_float" "sqrt" "float"
|
|
|
|
external tan : float -> float = "tan_float" "tan" "float"
|
|
|
|
external tanh : float -> float = "tanh_float" "tanh" "float"
|
|
|
|
external ceil : float -> float = "ceil_float" "ceil" "float"
|
|
|
|
external floor : float -> float = "floor_float" "floor" "float"
|
1996-09-08 08:41:59 -07:00
|
|
|
external abs_float : float -> float = "%absfloat"
|
1997-03-04 06:55:17 -08:00
|
|
|
external mod_float : float -> float -> float = "fmod_float" "fmod" "float"
|
|
|
|
external frexp : float -> float * int = "frexp_float"
|
|
|
|
external ldexp : float -> int -> float = "ldexp_float"
|
1998-12-09 01:24:59 -08:00
|
|
|
external modf : float -> float * float = "modf_float"
|
1996-09-08 08:41:59 -07:00
|
|
|
external float : int -> float = "%floatofint"
|
1998-12-02 02:39:36 -08:00
|
|
|
external float_of_int : int -> float = "%floatofint"
|
1996-09-08 08:41:59 -07:00
|
|
|
external truncate : float -> int = "%intoffloat"
|
1998-12-02 02:39:36 -08:00
|
|
|
external int_of_float : float -> int = "%intoffloat"
|
2001-02-09 01:40:12 -08:00
|
|
|
external float_of_bytes : string -> float = "float_of_bytes"
|
|
|
|
let infinity =
|
|
|
|
float_of_bytes "\127\240\000\000\000\000\000\000"
|
|
|
|
(* 0x7F F0 00 00 00 00 00 00 *)
|
|
|
|
let neg_infinity =
|
|
|
|
float_of_bytes "\255\240\000\000\000\000\000\000"
|
|
|
|
(* 0xFF F0 00 00 00 00 00 00 *)
|
|
|
|
let nan =
|
|
|
|
float_of_bytes "\127\240\000\000\000\000\000\001"
|
|
|
|
(* 0x7F F0 00 00 00 00 00 01 *)
|
2001-10-30 06:29:29 -08:00
|
|
|
let max_float =
|
|
|
|
float_of_bytes "\127\239\255\255\255\255\255\255"
|
|
|
|
(* 0x7f ef ff ff ff ff ff ff *)
|
|
|
|
let min_float =
|
|
|
|
float_of_bytes "\000\016\000\000\000\000\000\000"
|
|
|
|
(* 0x00 10 00 00 00 00 00 00 *)
|
|
|
|
let epsilon_float =
|
|
|
|
float_of_bytes "\060\176\000\000\000\000\000\000"
|
|
|
|
(* 0x3c b0 00 00 00 00 00 00 *)
|
2001-02-09 01:40:12 -08:00
|
|
|
type fpclass =
|
|
|
|
FP_normal
|
|
|
|
| FP_subnormal
|
|
|
|
| FP_zero
|
|
|
|
| FP_infinite
|
|
|
|
| FP_nan
|
|
|
|
external classify_float: float -> fpclass = "classify_float"
|
1996-09-08 08:41:59 -07:00
|
|
|
|
|
|
|
(* String operations -- more in module String *)
|
|
|
|
|
1998-10-01 05:33:55 -07:00
|
|
|
external string_length : string -> int = "%string_length"
|
1996-09-08 08:41:59 -07:00
|
|
|
external string_create: int -> string = "create_string"
|
|
|
|
external string_blit : string -> int -> string -> int -> int -> unit
|
1998-10-01 05:33:55 -07:00
|
|
|
= "blit_string" "noalloc"
|
1996-09-08 08:41:59 -07:00
|
|
|
|
|
|
|
let (^) s1 s2 =
|
|
|
|
let l1 = string_length s1 and l2 = string_length s2 in
|
|
|
|
let s = string_create (l1 + l2) in
|
|
|
|
string_blit s1 0 s 0 l1;
|
|
|
|
string_blit s2 0 s l1 l2;
|
|
|
|
s
|
|
|
|
|
1998-12-08 06:53:55 -08:00
|
|
|
(* Character operations -- more in module Char *)
|
|
|
|
|
|
|
|
external int_of_char : char -> int = "%identity"
|
|
|
|
external unsafe_char_of_int : int -> char = "%identity"
|
|
|
|
let char_of_int n =
|
2000-12-28 05:07:42 -08:00
|
|
|
if n < 0 || n > 255 then invalid_arg "char_of_int" else unsafe_char_of_int n
|
1998-12-08 06:53:55 -08:00
|
|
|
|
1999-02-24 07:21:50 -08:00
|
|
|
(* Unit operations *)
|
|
|
|
|
|
|
|
external ignore : 'a -> unit = "%ignore"
|
|
|
|
|
1996-09-08 08:41:59 -07:00
|
|
|
(* Pair operations *)
|
|
|
|
|
|
|
|
external fst : 'a * 'b -> 'a = "%field0"
|
|
|
|
external snd : 'a * 'b -> 'b = "%field1"
|
|
|
|
|
2000-04-03 05:07:36 -07:00
|
|
|
(* References *)
|
|
|
|
|
|
|
|
type 'a ref = { mutable contents: 'a }
|
|
|
|
external ref: 'a -> 'a ref = "%makemutable"
|
|
|
|
external (!): 'a ref -> 'a = "%field0"
|
|
|
|
external (:=): 'a ref -> 'a -> unit = "%setfield0"
|
|
|
|
external incr: int ref -> unit = "%incr"
|
|
|
|
external decr: int ref -> unit = "%decr"
|
|
|
|
|
1996-09-08 08:41:59 -07:00
|
|
|
(* String conversion functions *)
|
|
|
|
|
|
|
|
external format_int: string -> int -> string = "format_int"
|
|
|
|
external format_float: string -> float -> string = "format_float"
|
|
|
|
|
|
|
|
let string_of_bool b =
|
|
|
|
if b then "true" else "false"
|
1998-12-02 02:39:36 -08:00
|
|
|
let bool_of_string = function
|
|
|
|
| "true" -> true
|
|
|
|
| "false" -> false
|
2001-10-09 08:14:01 -07:00
|
|
|
| _ -> invalid_arg "bool_of_string"
|
1996-09-08 08:41:59 -07:00
|
|
|
|
|
|
|
let string_of_int n =
|
|
|
|
format_int "%d" n
|
|
|
|
|
|
|
|
external int_of_string : string -> int = "int_of_string"
|
|
|
|
|
|
|
|
let string_of_float f =
|
|
|
|
format_float "%.12g" f
|
|
|
|
|
|
|
|
external float_of_string : string -> float = "float_of_string"
|
|
|
|
|
|
|
|
(* List operations -- more in module List *)
|
|
|
|
|
|
|
|
let rec (@) l1 l2 =
|
|
|
|
match l1 with
|
|
|
|
[] -> l2
|
|
|
|
| hd :: tl -> hd :: (tl @ l2)
|
|
|
|
|
|
|
|
(* I/O operations *)
|
|
|
|
|
|
|
|
type in_channel
|
|
|
|
type out_channel
|
|
|
|
|
2001-10-09 08:14:01 -07:00
|
|
|
external open_descriptor_out: int -> out_channel = "caml_open_descriptor_out"
|
|
|
|
external open_descriptor_in: int -> in_channel = "caml_open_descriptor_in"
|
1996-09-08 08:41:59 -07:00
|
|
|
|
1999-10-25 01:41:12 -07:00
|
|
|
let stdin = open_descriptor_in 0
|
|
|
|
let stdout = open_descriptor_out 1
|
|
|
|
let stderr = open_descriptor_out 2
|
1996-09-08 08:41:59 -07:00
|
|
|
|
|
|
|
(* Non-blocking stuff *)
|
|
|
|
|
1998-11-20 07:37:44 -08:00
|
|
|
external thread_wait_read_prim : Unix.file_descr -> unit = "thread_wait_read"
|
|
|
|
external thread_wait_write_prim : Unix.file_descr -> unit = "thread_wait_write"
|
1996-09-09 05:35:55 -07:00
|
|
|
|
1998-11-20 07:37:44 -08:00
|
|
|
let thread_wait_read fd = thread_wait_read_prim fd
|
|
|
|
let thread_wait_write fd = thread_wait_write_prim fd
|
1996-09-09 05:35:55 -07:00
|
|
|
|
1996-09-08 08:41:59 -07:00
|
|
|
external inchan_ready : in_channel -> bool = "thread_inchan_ready"
|
|
|
|
external outchan_ready : out_channel -> int -> bool = "thread_outchan_ready"
|
|
|
|
external descr_inchan : in_channel -> Unix.file_descr = "channel_descriptor"
|
|
|
|
external descr_outchan : out_channel -> Unix.file_descr = "channel_descriptor"
|
|
|
|
|
|
|
|
let wait_inchan ic =
|
1998-11-20 07:37:44 -08:00
|
|
|
if not (inchan_ready ic) then thread_wait_read(descr_inchan ic)
|
|
|
|
|
1996-09-08 08:41:59 -07:00
|
|
|
let wait_outchan oc len =
|
1998-11-20 07:37:44 -08:00
|
|
|
if not (outchan_ready oc len) then thread_wait_write(descr_outchan oc)
|
1996-09-08 08:41:59 -07:00
|
|
|
|
|
|
|
(* General output functions *)
|
|
|
|
|
|
|
|
type open_flag =
|
|
|
|
Open_rdonly | Open_wronly | Open_append
|
|
|
|
| Open_creat | Open_trunc | Open_excl
|
|
|
|
| Open_binary | Open_text | Open_nonblock
|
|
|
|
|
|
|
|
external open_desc: string -> open_flag list -> int -> int = "sys_open"
|
|
|
|
|
|
|
|
let open_out_gen mode perm name =
|
|
|
|
open_descriptor_out(open_desc name mode perm)
|
|
|
|
|
|
|
|
let open_out name =
|
|
|
|
open_out_gen [Open_wronly; Open_creat; Open_trunc; Open_text] 0o666 name
|
|
|
|
|
|
|
|
let open_out_bin name =
|
|
|
|
open_out_gen [Open_wronly; Open_creat; Open_trunc; Open_binary] 0o666 name
|
|
|
|
|
1997-08-29 08:05:51 -07:00
|
|
|
external flush_partial : out_channel -> bool = "caml_flush_partial"
|
1996-09-08 08:41:59 -07:00
|
|
|
|
1999-11-17 11:11:51 -08:00
|
|
|
let rec flush oc =
|
1999-02-25 02:25:25 -08:00
|
|
|
let success =
|
|
|
|
try
|
|
|
|
flush_partial oc
|
|
|
|
with Sys_blocked_io ->
|
|
|
|
wait_outchan oc (-1); false in
|
1999-11-17 11:11:51 -08:00
|
|
|
if success then () else flush oc
|
1996-09-08 08:41:59 -07:00
|
|
|
|
2001-10-09 08:14:01 -07:00
|
|
|
external out_channels_list : unit -> out_channel list
|
|
|
|
= "caml_out_channels_list"
|
|
|
|
|
|
|
|
let flush_all () =
|
|
|
|
let rec iter = function
|
|
|
|
[] -> ()
|
|
|
|
| a::l ->
|
|
|
|
begin try
|
|
|
|
flush a
|
|
|
|
with Sys_error _ ->
|
|
|
|
() (* ignore channels closed during a preceding flush. *)
|
|
|
|
end;
|
|
|
|
iter l
|
2001-10-12 04:13:29 -07:00
|
|
|
in iter (out_channels_list ())
|
2001-10-09 08:14:01 -07:00
|
|
|
|
1996-09-08 08:41:59 -07:00
|
|
|
external unsafe_output_partial : out_channel -> string -> int -> int -> int
|
1997-08-29 08:05:51 -07:00
|
|
|
= "caml_output_partial"
|
1996-09-08 08:41:59 -07:00
|
|
|
|
|
|
|
let rec unsafe_output oc buf pos len =
|
|
|
|
if len > 0 then begin
|
1999-02-25 02:25:25 -08:00
|
|
|
let written =
|
|
|
|
try
|
|
|
|
unsafe_output_partial oc buf pos len
|
|
|
|
with Sys_blocked_io ->
|
|
|
|
wait_outchan oc len; 0 in
|
1996-09-08 08:41:59 -07:00
|
|
|
unsafe_output oc buf (pos + written) (len - written)
|
|
|
|
end
|
|
|
|
|
1997-08-29 08:05:51 -07:00
|
|
|
external output_char_blocking : out_channel -> char -> unit
|
|
|
|
= "caml_output_char"
|
|
|
|
external output_byte_blocking : out_channel -> int -> unit = "caml_output_char"
|
1996-09-08 08:41:59 -07:00
|
|
|
|
1998-11-20 07:37:44 -08:00
|
|
|
let rec output_char oc c =
|
1999-02-25 02:25:25 -08:00
|
|
|
try
|
|
|
|
output_char_blocking oc c
|
|
|
|
with Sys_blocked_io ->
|
|
|
|
wait_outchan oc 1; output_char oc c
|
1996-09-08 08:41:59 -07:00
|
|
|
|
|
|
|
let output_string oc s =
|
|
|
|
unsafe_output oc s 0 (string_length s)
|
|
|
|
|
|
|
|
let output oc s ofs len =
|
2000-05-15 04:50:42 -07:00
|
|
|
if ofs < 0 || len < 0 || ofs + len > string_length s
|
1996-09-08 08:41:59 -07:00
|
|
|
then invalid_arg "output"
|
|
|
|
else unsafe_output oc s ofs len
|
|
|
|
|
2001-09-06 01:52:32 -07:00
|
|
|
let output' oc ~buf ~pos ~len = output oc buf pos len
|
|
|
|
|
1998-11-20 07:37:44 -08:00
|
|
|
let rec output_byte oc b =
|
1999-02-25 02:25:25 -08:00
|
|
|
try
|
|
|
|
output_byte_blocking oc b
|
|
|
|
with Sys_blocked_io ->
|
|
|
|
wait_outchan oc 1; output_byte oc b
|
1996-09-08 08:41:59 -07:00
|
|
|
|
|
|
|
let output_binary_int oc n =
|
|
|
|
output_byte oc (n asr 24);
|
|
|
|
output_byte oc (n asr 16);
|
|
|
|
output_byte oc (n asr 8);
|
|
|
|
output_byte oc n
|
|
|
|
|
1997-08-29 08:05:51 -07:00
|
|
|
external marshal_to_string : 'a -> unit list -> string
|
|
|
|
= "output_value_to_string"
|
|
|
|
|
1997-07-02 11:16:15 -07:00
|
|
|
let output_value oc v = output_string oc (marshal_to_string v [])
|
1996-09-08 08:41:59 -07:00
|
|
|
|
1997-08-29 08:05:51 -07:00
|
|
|
external seek_out_blocking : out_channel -> int -> unit = "caml_seek_out"
|
1996-09-08 08:41:59 -07:00
|
|
|
|
|
|
|
let seek_out oc pos = flush oc; seek_out_blocking oc pos
|
|
|
|
|
1997-08-29 08:05:51 -07:00
|
|
|
external pos_out : out_channel -> int = "caml_pos_out"
|
|
|
|
external out_channel_length : out_channel -> int = "caml_channel_size"
|
|
|
|
external close_out_channel : out_channel -> unit = "caml_close_channel"
|
1996-09-08 08:41:59 -07:00
|
|
|
|
|
|
|
let close_out oc = flush oc; close_out_channel oc
|
1998-07-02 02:51:50 -07:00
|
|
|
external set_binary_mode_out : out_channel -> bool -> unit
|
|
|
|
= "caml_set_binary_mode"
|
1996-09-08 08:41:59 -07:00
|
|
|
|
|
|
|
(* General input functions *)
|
|
|
|
|
|
|
|
let open_in_gen mode perm name =
|
|
|
|
open_descriptor_in(open_desc name mode perm)
|
|
|
|
|
|
|
|
let open_in name =
|
|
|
|
open_in_gen [Open_rdonly; Open_text] 0 name
|
|
|
|
|
|
|
|
let open_in_bin name =
|
|
|
|
open_in_gen [Open_rdonly; Open_binary] 0 name
|
|
|
|
|
1997-08-29 08:05:51 -07:00
|
|
|
external input_char_blocking : in_channel -> char = "caml_input_char"
|
|
|
|
external input_byte_blocking : in_channel -> int = "caml_input_char"
|
1996-09-08 08:41:59 -07:00
|
|
|
|
1998-11-20 07:37:44 -08:00
|
|
|
let rec input_char ic =
|
1999-02-25 02:25:25 -08:00
|
|
|
try
|
|
|
|
input_char_blocking ic
|
|
|
|
with Sys_blocked_io ->
|
|
|
|
wait_inchan ic; input_char ic
|
1996-09-08 08:41:59 -07:00
|
|
|
|
|
|
|
external unsafe_input_blocking : in_channel -> string -> int -> int -> int
|
1997-08-29 08:05:51 -07:00
|
|
|
= "caml_input"
|
1996-09-08 08:41:59 -07:00
|
|
|
|
1998-11-20 07:37:44 -08:00
|
|
|
let rec unsafe_input ic s ofs len =
|
1999-02-25 02:25:25 -08:00
|
|
|
try
|
|
|
|
unsafe_input_blocking ic s ofs len
|
|
|
|
with Sys_blocked_io ->
|
|
|
|
wait_inchan ic; unsafe_input ic s ofs len
|
1996-09-08 08:41:59 -07:00
|
|
|
|
|
|
|
let input ic s ofs len =
|
2000-05-15 04:50:42 -07:00
|
|
|
if ofs < 0 || len < 0 || ofs + len > string_length s
|
1996-09-08 08:41:59 -07:00
|
|
|
then invalid_arg "input"
|
|
|
|
else unsafe_input ic s ofs len
|
|
|
|
|
2001-09-06 01:52:32 -07:00
|
|
|
let input' ic ~buf ~pos ~len = input ic buf pos len
|
|
|
|
|
1996-09-08 08:41:59 -07:00
|
|
|
let rec unsafe_really_input ic s ofs len =
|
|
|
|
if len <= 0 then () else begin
|
|
|
|
let r = unsafe_input ic s ofs len in
|
|
|
|
if r = 0
|
|
|
|
then raise End_of_file
|
|
|
|
else unsafe_really_input ic s (ofs+r) (len-r)
|
|
|
|
end
|
|
|
|
|
|
|
|
let really_input ic s ofs len =
|
2000-05-15 04:50:42 -07:00
|
|
|
if ofs < 0 || len < 0 || ofs + len > string_length s
|
1996-09-08 08:41:59 -07:00
|
|
|
then invalid_arg "really_input"
|
|
|
|
else unsafe_really_input ic s ofs len
|
|
|
|
|
|
|
|
let input_line ic =
|
2000-04-03 05:07:36 -07:00
|
|
|
let buf = ref (string_create 128) in
|
|
|
|
let pos = ref 0 in
|
|
|
|
begin try
|
|
|
|
while true do
|
|
|
|
if !pos = string_length !buf then begin
|
|
|
|
let newbuf = string_create (2 * !pos) in
|
|
|
|
string_blit !buf 0 newbuf 0 !pos;
|
|
|
|
buf := newbuf
|
|
|
|
end;
|
1996-09-08 08:41:59 -07:00
|
|
|
let c = input_char ic in
|
2000-04-03 05:07:36 -07:00
|
|
|
if c = '\n' then raise Exit;
|
|
|
|
!buf.[!pos] <- c;
|
|
|
|
incr pos
|
|
|
|
done
|
|
|
|
with Exit -> ()
|
|
|
|
| End_of_file -> if !pos = 0 then raise End_of_file
|
|
|
|
end;
|
|
|
|
let res = string_create !pos in
|
|
|
|
string_blit !buf 0 res 0 !pos;
|
|
|
|
res
|
1996-09-08 08:41:59 -07:00
|
|
|
|
1998-11-20 07:37:44 -08:00
|
|
|
let rec input_byte ic =
|
1999-02-25 02:25:25 -08:00
|
|
|
try
|
|
|
|
input_byte_blocking ic
|
|
|
|
with Sys_blocked_io ->
|
|
|
|
wait_inchan ic; input_byte ic
|
1996-09-08 08:41:59 -07:00
|
|
|
|
|
|
|
let input_binary_int ic =
|
|
|
|
let b1 = input_byte ic in
|
|
|
|
let n1 = if b1 >= 128 then b1 - 256 else b1 in
|
|
|
|
let b2 = input_byte ic in
|
|
|
|
let b3 = input_byte ic in
|
|
|
|
let b4 = input_byte ic in
|
|
|
|
(n1 lsl 24) + (b2 lsl 16) + (b3 lsl 8) + b4
|
|
|
|
|
1997-07-02 11:16:15 -07:00
|
|
|
external unmarshal : string -> int -> 'a = "input_value_from_string"
|
|
|
|
external marshal_data_size : string -> int -> int = "marshal_data_size"
|
1996-09-09 05:35:55 -07:00
|
|
|
|
1996-09-08 08:41:59 -07:00
|
|
|
let input_value ic =
|
1996-09-09 05:35:55 -07:00
|
|
|
let header = string_create 20 in
|
1996-09-08 08:41:59 -07:00
|
|
|
really_input ic header 0 20;
|
1997-07-02 11:16:15 -07:00
|
|
|
let bsize = marshal_data_size header 0 in
|
1996-09-09 05:35:55 -07:00
|
|
|
let buffer = string_create (20 + bsize) in
|
|
|
|
string_blit header 0 buffer 0 20;
|
1996-09-08 08:41:59 -07:00
|
|
|
really_input ic buffer 20 bsize;
|
1997-07-02 11:16:15 -07:00
|
|
|
unmarshal buffer 0
|
1996-09-08 08:41:59 -07:00
|
|
|
|
1997-08-29 08:05:51 -07:00
|
|
|
external seek_in : in_channel -> int -> unit = "caml_seek_in"
|
|
|
|
external pos_in : in_channel -> int = "caml_pos_in"
|
|
|
|
external in_channel_length : in_channel -> int = "caml_channel_size"
|
|
|
|
external close_in : in_channel -> unit = "caml_close_channel"
|
1998-07-02 02:51:50 -07:00
|
|
|
external set_binary_mode_in : in_channel -> bool -> unit
|
|
|
|
= "caml_set_binary_mode"
|
1996-09-08 08:41:59 -07:00
|
|
|
|
|
|
|
(* Output functions on standard output *)
|
|
|
|
|
|
|
|
let print_char c = output_char stdout c
|
|
|
|
let print_string s = output_string stdout s
|
|
|
|
let print_int i = output_string stdout (string_of_int i)
|
|
|
|
let print_float f = output_string stdout (string_of_float f)
|
|
|
|
let print_endline s = output_string stdout s; output_char stdout '\n'
|
|
|
|
let print_newline () = output_char stdout '\n'; flush stdout
|
|
|
|
|
|
|
|
(* Output functions on standard error *)
|
|
|
|
|
|
|
|
let prerr_char c = output_char stderr c
|
|
|
|
let prerr_string s = output_string stderr s
|
|
|
|
let prerr_int i = output_string stderr (string_of_int i)
|
|
|
|
let prerr_float f = output_string stderr (string_of_float f)
|
|
|
|
let prerr_endline s =
|
|
|
|
output_string stderr s; output_char stderr '\n'; flush stderr
|
|
|
|
let prerr_newline () = output_char stderr '\n'; flush stderr
|
|
|
|
|
|
|
|
(* Input functions on standard input *)
|
|
|
|
|
|
|
|
let read_line () = flush stdout; input_line stdin
|
|
|
|
let read_int () = int_of_string(read_line())
|
|
|
|
let read_float () = float_of_string(read_line())
|
|
|
|
|
|
|
|
(* Miscellaneous *)
|
|
|
|
|
|
|
|
external sys_exit : int -> 'a = "sys_exit"
|
|
|
|
|
2001-10-09 08:14:01 -07:00
|
|
|
let exit_function = ref flush_all
|
1996-09-08 08:41:59 -07:00
|
|
|
|
|
|
|
let at_exit f =
|
|
|
|
let g = !exit_function in
|
|
|
|
exit_function := (fun () -> f(); g())
|
1996-10-09 04:14:50 -07:00
|
|
|
|
|
|
|
let do_at_exit () = (!exit_function) ()
|
|
|
|
|
|
|
|
let exit retcode =
|
|
|
|
do_at_exit ();
|
|
|
|
sys_exit retcode
|
2001-10-30 01:25:09 -08:00
|
|
|
|
|
|
|
external register_named_value: string -> 'a -> unit = "register_named_value"
|
|
|
|
|
|
|
|
let _ = register_named_value "Pervasives.do_at_exit" do_at_exit
|