(* virt-dmesg * (C) Copyright 2008-2011 Red Hat Inc. * * 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., 675 Mass Ave, Cambridge, MA 02139, USA. *) open Printf module StringMap = Map.Make (String) type symbol_table = int64 StringMap.t let ( +^ ) = Int64.add let ( -^ ) = Int64.sub let ( *^ ) = Int64.mul let ( /^ ) = Int64.div let ( &^ ) = Int64.logand let frequency xs = let xs = List.sort compare xs in let rec loop = function | [] -> [] | [x] -> [1, x] | x :: y :: xs when x = y -> let rest = loop (y :: xs) in let (count, _), rest = List.hd rest, List.tl rest in (count+1, y) :: rest | x :: xs -> (1, x) :: loop xs in let xs = loop xs in List.rev (List.sort compare xs) let rec filter_map f = function | [] -> [] | x :: xs -> match f x with | None -> filter_map f xs | Some y -> y :: filter_map f xs let replace_chars f s = let len = String.length s in let rec loop i acc = if i < len then ( let c = String.unsafe_get s i in let acc = f c :: acc in loop (i+1) acc ) else String.concat "" (List.rev acc) in loop 0 [] let program_name = ref "virt-dmesg" let set_program_name str = program_name := str let get_program_name () = !program_name let debug_mode = ref false let set_debug () = debug_mode := true let debug fs = ksprintf (fun str -> if !debug_mode then print_endline str) fs let error fs = ksprintf ( fun str -> prerr_string !program_name; prerr_string ": "; prerr_endline str ) fs