X-Git-Url: http://git.annexia.org/?p=ocaml-ancient.git;a=blobdiff_plain;f=test_ancient_dict_read.ml;fp=test_ancient_dict_read.ml;h=7269a1f3355a5349baddde3bf641d9a85cb297a1;hp=0000000000000000000000000000000000000000;hb=690b40de4026658d385caf2cdbfa13837a1e679e;hpb=d5c4a7be65fa03b8d77d8b73d3d2f250b11b255a diff --git a/test_ancient_dict_read.ml b/test_ancient_dict_read.ml new file mode 100644 index 0000000..7269a1f --- /dev/null +++ b/test_ancient_dict_read.ml @@ -0,0 +1,67 @@ +(* Read shared dictionary. + * $Id: test_ancient_dict_read.ml,v 1.1 2006-10-06 15:03:47 rich Exp $ + *) + +open Printf +open Unix + +let argv = Array.to_list Sys.argv + +let datafile = + match argv with + | [_; datafile] -> + datafile + | _ -> + failwith (sprintf "usage: %s datafile" + Sys.executable_name) + +let md = + let fd = openfile datafile [O_RDWR] 0o644 in + Ancient.attach fd 0n + +let arraysize = 256 (* one element for each character *) + +type t = Not_Found | Exists of t array | Not_Exists of t array;; +let tree : t array Ancient.ancient = Ancient.get md 0 +let tree = Ancient.follow tree + +let word_exists word = + try + let tree = ref tree in + let len = String.length word in + for i = 0 to len-2; do + let c = word.[i] in + let c = Char.code c in + match (!tree).(c) with + | Not_Found -> raise Not_found + | Exists tree' + | Not_Exists tree' -> tree := tree' + done; + + (* Final character. *) + let c = word.[len-1] in + let c = Char.code c in + match (!tree).(c) with + | Not_Found + | Not_Exists _ -> false + | Exists _ -> true + with + Not_found -> false + +let () = + let rec loop () = + printf "Enter a word to check (q = quit program): "; + let word = read_line () in + if word <> "q" then ( + printf "'%s' exists? %B\n%!" word (word_exists word); + loop () + ) + in + loop (); + + Ancient.detach md; + + (* Garbage collect - good way to check we haven't broken anything. *) + Gc.compact (); + + printf "Program finished.\n"