49
by Mehdi Dogguy
Rename Stm to Ben |
1 |
(**************************************************************************)
|
175
by Iain Lane
Sync with U |
2 |
(* Copyright © 2009-2013 Stéphane Glondu <steph@glondu.net> *)
|
3 |
(* © 2010-2013 Mehdi Dogguy <mehdi@dogguy.org> *)
|
|
49
by Mehdi Dogguy
Rename Stm to Ben |
4 |
(* *)
|
5 |
(* This program is free software: you can redistribute it and/or modify *)
|
|
6 |
(* it under the terms of the GNU Affero General Public License as *)
|
|
7 |
(* published by the Free Software Foundation, either version 3 of the *)
|
|
8 |
(* License, or (at your option) any later version, with the additional *)
|
|
9 |
(* exemption that compiling, linking, and/or using OpenSSL is allowed. *)
|
|
10 |
(* *)
|
|
11 |
(* This program is distributed in the hope that it will be useful, but *)
|
|
12 |
(* WITHOUT ANY WARRANTY; without even the implied warranty of *)
|
|
13 |
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *)
|
|
14 |
(* Affero General Public License for more details. *)
|
|
15 |
(* *)
|
|
16 |
(* You should have received a copy of the GNU Affero General Public *)
|
|
17 |
(* License along with this program. If not, see *)
|
|
18 |
(* <http://www.gnu.org/licenses/>. *)
|
|
19 |
(**************************************************************************)
|
|
20 |
||
176
by Gianfranco Costamagna
Upgrade ben to 0.7.7ubuntu1 |
21 |
module StringMap = struct |
22 |
include Map.Make(String) |
|
23 |
||
24 |
let from_list = |
|
25 |
List.fold_left |
|
26 |
(fun map (key, value) -> add key value map) |
|
27 |
empty
|
|
28 |
||
29 |
let fusion m1 m2 = |
|
30 |
let smerge _ v1 v2 = match v1, v2 with |
|
178
by Gianfranco Costamagna
Merge 0.8.0ubuntu1 from Debian |
31 |
| Some v1, Some _ -> Some v1 |
176
by Gianfranco Costamagna
Upgrade ben to 0.7.7ubuntu1 |
32 |
| Some v1, None -> Some v1 |
33 |
| None , Some v2 -> Some v2 |
|
34 |
| _ -> None |
|
35 |
in
|
|
36 |
merge smerge m1 m2 |
|
37 |
end
|
|
38 |
||
39 |
module StringSet = struct |
|
40 |
include Set.Make(String) |
|
41 |
let from_list = |
|
42 |
List.fold_left |
|
43 |
(fun set elt -> add elt set) |
|
44 |
empty
|
|
45 |
end
|
|
46 |
||
47 |
module IntMap = Map.Make(struct |
|
48 |
type t = int |
|
49 |
let compare : t -> t -> int = compare |
|
50 |
end) |
|
51 |
||
153.1.28
by Mehdi Dogguy
Add support for stdin, .gz and .bz2 files to 'ben query' |
52 |
let with_in_channel chan f = |
53 |
try
|
|
54 |
let res = f chan in |
|
55 |
close_in chan; res |
|
56 |
with e -> close_in chan; raise e |
|
57 |
||
49
by Mehdi Dogguy
Rename Stm to Ben |
58 |
let with_in_file file f = |
59 |
let chan = open_in_bin file in |
|
60 |
try
|
|
61 |
let res = f chan in |
|
62 |
close_in chan; res |
|
63 |
with e -> close_in chan; raise e |
|
64 |
||
65 |
let with_out_file file f = |
|
66 |
let chan = open_out_bin file in |
|
67 |
try
|
|
68 |
let res = f chan in |
|
69 |
close_out chan; res |
|
70 |
with e -> close_out chan; raise e |
|
71 |
||
72 |
let escape_for_shell str = |
|
73 |
let buf = Buffer.create (2 * String.length str) in |
|
74 |
Buffer.add_char buf '\''; |
|
75 |
String.iter |
|
76 |
(function |
|
77 |
| '\'' -> Buffer.add_string buf "'\\''" |
|
78 |
| c -> Buffer.add_char buf c) |
|
79 |
str; |
|
80 |
Buffer.add_char buf '\''; |
|
81 |
Buffer.contents buf |
|
82 |
||
83 |
let get_rfc2822_date () = |
|
84 |
let chan = Unix.open_process_in "date -R" in |
|
85 |
let r = input_line chan in |
|
86 |
match Unix.close_process_in chan with |
|
87 |
| Unix.WEXITED 0 -> r |
|
88 |
| _ -> failwith "unexpected return of date" |
|
89 |
||
90 |
let list_rev_mapi f xs = |
|
91 |
let rec aux i accu = function |
|
92 |
| [] -> accu |
|
93 |
| x::xs -> aux (i+1) ((f i x)::accu) xs |
|
94 |
in aux 0 [] xs |
|
95 |
||
176
by Gianfranco Costamagna
Upgrade ben to 0.7.7ubuntu1 |
96 |
let rec uniq = function |
97 |
| [] -> [] |
|
98 |
| h::l -> |
|
99 |
if List.mem h l |
|
100 |
then uniq l |
|
101 |
else h :: (uniq l) |
|
102 |
||
49
by Mehdi Dogguy
Rename Stm to Ben |
103 |
let simple_split delim str = |
104 |
let n = String.length str in |
|
105 |
let rec aux i accu = |
|
106 |
if i < n then |
|
107 |
let j = try String.index_from str i delim with Not_found -> n in |
|
108 |
aux (j+1) (String.sub str i (j-i) :: accu) |
|
109 |
else List.rev accu |
|
110 |
in aux 0 [] |
|
111 |
||
175
by Iain Lane
Sync with U |
112 |
let capitalize ?(sep = '-') s = |
113 |
let l = simple_split sep s in |
|
178
by Gianfranco Costamagna
Merge 0.8.0ubuntu1 from Debian |
114 |
let l = List.map String.capitalize_ascii l in |
175
by Iain Lane
Sync with U |
115 |
String.concat (String.make 1 sep) l |
116 |
||
49
by Mehdi Dogguy
Rename Stm to Ben |
117 |
let starts_with str x = |
118 |
let n = String.length str and p = String.length x in |
|
119 |
n > p && String.sub str 0 p = x |
|
120 |
||
121 |
let ends_with str x = |
|
122 |
let n = String.length str and p = String.length x in |
|
123 |
n > p && String.sub str (n-p) p = x |