(** mutt_query.ml - find an e-mail address in 'doc/addresses.added' matching arg 1. Copyright 2005 (c) by Peter Stuifzand This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *) (* The format of the addresses.added file is: name;email;comment Peter;peter@stuifzand.com;Author of mutt_query Another;noone@noone.unk; Please keep in mind, that this is my first OCaml program. I can compile my version using: ocamlc str.cma mutt_query.ml -o mutt_query More info can be found at http://peter.wijvervelenons.nl/mutt.html. Or mail me at: mailto:peter@stuifzand.com. *) let (=~) s re = try let pos = Str.search_forward (Str.regexp_case_fold re) s 0 in true with Not_found -> false ;; let read_addresses_matching text = let inputfile = String.concat "/" [ (Sys.getenv "HOME") ; "doc/addresses.added" ] in let channel = open_in inputfile in let split_line = Str.split (Str.regexp ";") in let rec read_file () = try let line = (input_line channel) in if line =~ text then let data = Array.of_list (split_line line) in data :: (read_file ()) else (read_file ()) with End_of_file -> close_in channel ; [] in read_file() ;; let reorder ary = if Array.length ary = 3 then [| Array.get ary 1; Array.get ary 0; Array.get ary 2 |] else [| Array.get ary 1; Array.get ary 0; "" |] let show_found_addresses text = let found = read_addresses_matching text in Printf.printf "%d matches found\n" (List.length found) ; let sorted = List.sort (fun a b -> String.compare (Array.get a 0) (Array.get b 0)) found in let show_one ary = String.concat "\t" (Array.to_list ary) in List.iter (fun item -> print_endline (show_one (reorder item)) ) sorted ;; try if Array.length Sys.argv = 2 then show_found_addresses (Array.get Sys.argv 1) else begin print_string "usage: mutt_query "; print_newline () end with Sys_error s -> print_string "Error: "; print_string s; print_newline () ;;