Debian Codegraph @

inifiles.ml

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 3 — not the installed (fully patched) source.
3 Debian patches touch this file
1 (* A small library to read and write .ini files
2
3 Copyright (C) 2004 Eric Stokes, and The California State University
4 at Northridge
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *)
20
21
22 open Pcre
23 open Parseini
24 open Inilexer
25
26 exception Invalid_section of string
27 exception Invalid_element of string
28 exception Missing_section of string
29 exception Missing_element of string
30 exception Ini_parse_error of (int * string)
31
32 type attribute_specification = {
33 atr_name: string;
34 atr_required: bool;
35 atr_default: (string list) option;
36 atr_validator: Pcre.regexp option;
37 }
38
39 type section_specification = {
40 sec_name: string;
41 sec_required: bool;
42 sec_attributes: attribute_specification list;
43 }
44
45 type specification = section_specification list
46
47 let comment = regexp "^#.*$"
48
49 module Ordstr =
50 struct
51 type t = string
52 let compare (x:t) (y:t) =
53 String.compare (String.lowercase x) (String.lowercase y)
54 end
55
56 module Strset = Set.Make(Ordstr)
57
58 let setOfList list =
59 let rec dosetOfList list set =
60 match list with
61 a :: tail -> dosetOfList tail (Strset.add a set)
62 | [] -> set
63 in
64 dosetOfList list Strset.empty
65
66 let rec filterfile ?(buf=Buffer.create 500) f fd =
67 try
68 let theline = input_line fd in
69 if f theline then begin
70 Buffer.add_string buf (theline ^ "\n");
71 filterfile ~buf f fd
72 end
73 else
74 filterfile ~buf f fd
75 with End_of_file -> Buffer.contents buf
76
77 let rec read_inifile file fd tbl =
78 let lxbuf =
79 Lexing.from_string
80 (filterfile
81 (fun line -> not (Pcre.pmatch ~rex:comment line))
82 fd)
83 in
84 try
85 let parsed_file = inifile lexini lxbuf in
86 List.iter
87 (fun (section, values) ->
88 Hashtbl.add tbl section
89 (List.fold_left
90 (fun tbl (key, value) -> Hashtbl.add tbl key value;tbl)
91 (Hashtbl.create 10)
92 values))
93 parsed_file
94 with Parsing.Parse_error | Failure "lexing: empty token" ->
95 raise (Ini_parse_error (lxbuf.Lexing.lex_curr_p.Lexing.pos_lnum, file))
96
97 let write_inifile fd tbl =
98 Hashtbl.iter
99 (fun k v ->
100 output_string fd "[";output_string fd k;output_string fd "]\n";
101 (Hashtbl.iter
102 (fun k v ->
103 output_string fd k;output_string fd " = ";output_string fd v;
104 output_string fd "\n") v);
105 output_string fd "\n")
106 tbl
107
108 class inifile ?(spec=[]) file =
109 object (self)
110 val file = file
111 val data = Hashtbl.create 50
112
113 initializer
114 let inch = open_in file in
115 (try read_inifile file inch data
116 with exn -> close_in inch;raise exn);
117 close_in inch;
118 self#validate
119
120 method private validate =
121 match spec with
122 [] -> ()
123 | spec ->
124 List.iter
125 (fun {sec_name=name;sec_required=required;
126 sec_attributes=attrs} ->
127 try
128 let sec =
129 try Hashtbl.find data name
130 with Not_found -> raise (Missing_section name)
131 in
132 List.iter
133 (fun {atr_name=name;atr_required=req;
134 atr_default=def;atr_validator=validator} ->
135 try
136 let attr_val =
137 try Hashtbl.find sec name
138 with Not_found -> raise (Missing_element name)
139 in
140 (match validator with
141 Some rex ->
142 if not (Pcre.pmatch ~rex:rex attr_val) then
143 raise
144 (Invalid_element
145 (name ^ ": validation failed"))
146 | None -> ())
147 with Missing_element elt ->
148 if req then raise (Missing_element elt)
149 else match def with
150 Some def -> List.iter (Hashtbl.add sec name) def
151 | None -> ())
152 attrs
153 with Missing_section s ->
154 if required then raise (Missing_section s))
155 spec
156
157 method getval sec elt =
158 try Hashtbl.find
159 (try (Hashtbl.find data sec)
160 with Not_found -> raise (Invalid_section sec))
161 elt
162 with Not_found -> raise (Invalid_element elt)
163
164 method getaval sec elt =
165 try Hashtbl.find_all
166 (try (Hashtbl.find data sec)
167 with Not_found -> raise (Invalid_section sec))
168 elt
169 with Not_found -> raise (Invalid_element elt)
170
171 method setval sec elt v =
172 (Hashtbl.add
173 (try Hashtbl.find data sec
174 with Not_found ->
175 let h = Hashtbl.create 10 in
176 Hashtbl.add data sec h;h)
177 elt v);
178 try self#validate
179 with exn -> Hashtbl.remove data elt;raise exn
180
181 method delval sec elt =
182 let valu =
183 try
184 Some
185 (Hashtbl.find
186 (try Hashtbl.find data sec
187 with Not_found -> raise (Invalid_section sec))
188 elt)
189 with Not_found -> None
190 in
191 match valu with
192 Some v ->
193 ((Hashtbl.remove
194 (try Hashtbl.find data sec
195 with Not_found -> raise (Invalid_section sec))
196 elt);
197 try self#validate
198 with exn ->
199 (Hashtbl.add
200 (try Hashtbl.find data sec
201 with Not_found -> raise (Invalid_section sec))
202 elt v);
203 raise exn)
204 | None -> ()
205
206
207 method sects =
208 (Hashtbl.fold
209 (fun k v keys -> k :: keys)
210 data [])
211
212 method iter func sec =
213 (Hashtbl.iter func
214 (try Hashtbl.find data sec
215 with Not_found -> raise (Invalid_section sec)))
216
217 method attrs sec =
218 (Strset.elements
219 (setOfList
220 (Hashtbl.fold
221 (fun k v attrs -> k :: attrs)
222 (try Hashtbl.find data sec
223 with Not_found -> raise (Invalid_section sec))
224 [])))
225
226 method save ?(file = file) () =
227 let outch = open_out file in
228 write_inifile outch data;
229 flush outch;
230 end
231
232 let readdir path =
233 let dir = Unix.handle_unix_error Unix.opendir path in
234 let rec do_read dir =
235 try
236 let current = Unix.readdir dir in
237 current :: (do_read dir)
238 with End_of_file -> []
239 in
240 let lst = do_read dir in
241 Unix.closedir dir;
242 lst
243
244 let fold func path initial =
245 let check_file path =
246 match
247 (path,
248 (try (Unix.stat path).Unix.st_kind
249 with Unix.Unix_error (_,_,_) -> Unix.S_DIR))
250 with
251 (name, Unix.S_REG) when
252 (Pcre.pmatch ~rex:(Pcre.regexp "^.*\\.ini$") path) ->
253 true
254 | _ -> false
255 in
256 (List.fold_left
257 func
258 initial
259 (List.rev_map
260 (new inifile)
261 (List.filter
262 check_file
263 (List.rev_map
264 (fun p -> (path ^ "/" ^ p))
265 (readdir path)))))