File: config_file.ml

package info (click to toggle)
approx 5.10-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 340 kB
  • sloc: ml: 2,220; sh: 42; makefile: 32
file content (66 lines) | stat: -rw-r--r-- 1,672 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
(* approx: proxy server for Debian archive files
   Copyright (C) 2017  Eric C. Cooper <ecc@cmu.edu>
   Released under the GNU General Public License *)

open Util

let lines_of_channel chan =
  let next () =
    try Some (input_line chan)
    with End_of_file -> None
  in
  let rec loop list =
    match next () with
    | Some line -> loop (line :: list)
    | None -> List.rev list
  in
  loop []

let comment_re = Pcre.regexp "\\s*#.*$"

let remove_comment str = Pcre.qreplace ~rex: comment_re str ~templ: ""

let words_of_line line = Pcre.split (remove_comment line)

let map = ref []

let reset () = map := []

let get_generic convert ?default k =
  try convert (List.assoc k !map)
  with Not_found ->
    (match default with
    | Some v -> v
    | None -> raise Not_found)

let get = get_generic (fun x -> x)

let get_int = get_generic int_of_string

let bool_of_string str =
  match String.lowercase str with
  | "true"  | "yes" | "on"  | "1" -> true
  | "false" | "no"  | "off" | "0" -> false
  | _ -> failwith ("not a boolean value: " ^ str)

let get_bool = get_generic bool_of_string

let set key value = map := (key, value) :: !map

let fold f init = List.fold_left (fun x (k, v) -> f k v x) init !map

let iter f = fold (fun k v () -> f k v) ()

let read filename =
  let read_file chan =
    let lines = List.map words_of_line (lines_of_channel chan) in
    close_in chan;
    let enter = function
      | [key; value] -> set key value
      | [] -> ()
      | words -> failwith ("malformed line in " ^ filename ^ ": " ^
                           String.concat " " words)
    in
    List.iter enter lines
  in
  with_in_channel open_in filename read_file