Plasma GitLab Archive
Projects Blog Knowledge

(* $Id: netdb.mlp 2007 2014-08-24 22:31:13Z gerd $
 * ----------------------------------------------------------------------
 *
 *)


let net_db_dir = ref "@NET_DB_DIR@" ;;

let net_db_hash = Hashtbl.create 20 ;;

let file_db_is_enabled = ref true ;;

let read_db name =
  try
    Marshal.from_string (Hashtbl.find net_db_hash name) 0
  with
      Not_found ->
	if !file_db_is_enabled then begin
	  let filename = Filename.concat !net_db_dir name ^ ".netdb" in
	  if Sys.file_exists filename then begin
	    let ch = open_in_bin filename in
	    try
	      let v = input_value ch in
	      close_in ch;
	      v
	    with exn ->
	      close_in ch;
	      raise exn
	  end
	  else
	    failwith ("Ocamlnet: Cannot find the lookup table `" ^ name ^ 
		      "' which is supposed to be available as file " ^ 
		      filename)

	end
	else 
	  failwith ("Ocamlnet: The lookup table `" ^ name ^
		    "' is not compiled into the program, and access to " ^
		    "the external file database is disabled")
;;


let exists_db name =
  Hashtbl.mem net_db_hash name || (
    !file_db_is_enabled && (
      let filename = Filename.concat !net_db_dir name ^ ".netdb" in
      Sys.file_exists filename
    ))
;;


let set_db name value =
  Hashtbl.replace net_db_hash name value
;;


let disable_file_db () =
  file_db_is_enabled := false
;;

let override_file_db dir =
  net_db_dir := dir
;;

This web site is published by Informatikbüro Gerd Stolpmann
Powered by Caml