Blame support/fileevent.ml

Packit bd2e5d
(***********************************************************************)
Packit bd2e5d
(*                                                                     *)
Packit bd2e5d
(*                 MLTk, Tcl/Tk interface of OCaml                     *)
Packit bd2e5d
(*                                                                     *)
Packit bd2e5d
(*    Francois Rouaix, Francois Pessaux, Jun Furuse and Pierre Weis    *)
Packit bd2e5d
(*               projet Cristal, INRIA Rocquencourt                    *)
Packit bd2e5d
(*            Jacques Garrigue, Kyoto University RIMS                  *)
Packit bd2e5d
(*                                                                     *)
Packit bd2e5d
(*  Copyright 2002 Institut National de Recherche en Informatique et   *)
Packit bd2e5d
(*  en Automatique and Kyoto University.  All rights reserved.         *)
Packit bd2e5d
(*  This file is distributed under the terms of the GNU Library        *)
Packit bd2e5d
(*  General Public License, with the special exception on linking      *)
Packit bd2e5d
(*  described in file LICENSE found in the OCaml source tree.          *)
Packit bd2e5d
(*                                                                     *)
Packit bd2e5d
(***********************************************************************)
Packit bd2e5d
Packit bd2e5d
(* $Id$ *)
Packit bd2e5d
Packit bd2e5d
open Unix
Packit bd2e5d
open Protocol
Packit bd2e5d
Packit bd2e5d
external add_file_input : file_descr -> cbid -> unit
Packit bd2e5d
        =  "camltk_add_file_input"
Packit bd2e5d
external rem_file_input : file_descr -> cbid -> unit
Packit bd2e5d
        =  "camltk_rem_file_input"
Packit bd2e5d
external add_file_output : file_descr -> cbid -> unit
Packit bd2e5d
        =  "camltk_add_file_output"
Packit bd2e5d
external rem_file_output : file_descr -> cbid -> unit
Packit bd2e5d
        =  "camltk_rem_file_output"
Packit bd2e5d
Packit bd2e5d
(* File input handlers *)
Packit bd2e5d
Packit bd2e5d
let fd_table = Hashtbl.create 37 (* Avoid space leak in callback table *)
Packit bd2e5d
Packit bd2e5d
let add_fileinput ~fd ~callback:f =
Packit bd2e5d
  let id = new_function_id () in
Packit bd2e5d
  Hashtbl.add callback_naming_table id (fun _ -> f());
Packit bd2e5d
  Hashtbl.add fd_table (fd, 'r') id;
Packit bd2e5d
  if !Protocol.debug then begin
Packit bd2e5d
    Protocol.prerr_cbid id; prerr_endline " for fileinput"
Packit bd2e5d
  end;
Packit bd2e5d
  add_file_input fd id
Packit bd2e5d
Packit bd2e5d
let remove_fileinput ~fd =
Packit bd2e5d
  try
Packit bd2e5d
    let id = Hashtbl.find fd_table (fd, 'r') in
Packit bd2e5d
    clear_callback id;
Packit bd2e5d
    Hashtbl.remove fd_table (fd, 'r');
Packit bd2e5d
    if !Protocol.debug then begin
Packit bd2e5d
      prerr_string "clear ";
Packit bd2e5d
      Protocol.prerr_cbid id;
Packit bd2e5d
      prerr_endline " for fileinput"
Packit bd2e5d
    end;
Packit bd2e5d
    rem_file_input fd id
Packit bd2e5d
  with
Packit bd2e5d
    Not_found -> ()
Packit bd2e5d
Packit bd2e5d
let add_fileoutput ~fd ~callback:f =
Packit bd2e5d
  let id = new_function_id () in
Packit bd2e5d
  Hashtbl.add callback_naming_table id (fun _ -> f());
Packit bd2e5d
  Hashtbl.add fd_table (fd, 'w') id;
Packit bd2e5d
  if !Protocol.debug then begin
Packit bd2e5d
    Protocol.prerr_cbid id; prerr_endline " for fileoutput"
Packit bd2e5d
  end;
Packit bd2e5d
  add_file_output fd id
Packit bd2e5d
Packit bd2e5d
let remove_fileoutput ~fd =
Packit bd2e5d
  try
Packit bd2e5d
    let id = Hashtbl.find fd_table (fd, 'w') in
Packit bd2e5d
    clear_callback id;
Packit bd2e5d
    Hashtbl.remove fd_table (fd, 'w');
Packit bd2e5d
    if !Protocol.debug then begin
Packit bd2e5d
      prerr_string "clear ";
Packit bd2e5d
      Protocol.prerr_cbid id;
Packit bd2e5d
      prerr_endline " for fileoutput"
Packit bd2e5d
    end;
Packit bd2e5d
    rem_file_output fd id
Packit bd2e5d
  with
Packit bd2e5d
    Not_found -> ()