File: model.ml

package info (click to toggle)
headache 1.03-27
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 556 kB
  • ctags: 190
  • sloc: ml: 621; xml: 218; makefile: 70
file content (207 lines) | stat: -rw-r--r-- 5,889 bytes parent folder | download | duplicates (5)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
(**************************************************************************)
(*                                                                        *)
(*                               Headache                                 *)
(*                                                                        *)
(*          Vincent Simonet, Projet Cristal, INRIA Rocquencourt           *)
(*                                                                        *)
(*  Copyright 2002                                                        *)
(*  Institut National de Recherche en Informatique et en Automatique.     *)
(*  All rights reserved.  This file is distributed under the terms of     *)
(*  the GNU Library General Public License.                               *)
(*                                                                        *)
(*  Vincent.Simonet@inria.fr           http://cristal.inria.fr/~simonet/  *)
(*                                                                        *)
(**************************************************************************)

(* $Id: model.ml,v 1.2 2003/11/13 16:08:44 simonet Exp $ *)

open Printf

exception Error of string



(***************************************************************************)
(** {2 Headers generators} *)

type generator =
    { remove: in_channel -> string;
      create: out_channel -> string list -> int -> unit;
    } 

(***************************************************************************)
(** {2 Models} *)

type model = (string * string) list -> generator

let models : (string, model) Hashtbl.t = 
  Hashtbl.create 3

let add name m =
  Hashtbl.add models name m

let find name =
  Hashtbl.find models name



(***************************************************************************)

let arg_string args ?default name =
  try
    List.assoc name args
  with
    Not_found ->
      match default with
	None -> raise (Error (sprintf "parameter %s is missing" name))
      |	Some s -> s

let arg_int args ?default name =
  try
    int_of_string (arg_string args ?default name)
  with
    Failure "int_of_string" -> 
      raise (Error (sprintf "parameter %s expects an integer" name))

let arg_char args ?default name =
  let s = arg_string args ?default name in
  if UTF8.length s = 1 then s.[0]
  else raise (Error (sprintf "parameter %s expects a character" name))

    

let make_frame ~open_comment ~close_comment ~line_char ~margin ~width =

  let regexp_header = 
    Str.regexp_string (sprintf "%s%s" open_comment (String.make 10 line_char))
  in
  let regexp_blank = Str.regexp "^[ ]*$" in

  let remove ic =
    try
      let line = input_line ic in
      if Str.string_match regexp_header line 0
      then begin
	while not (Str.string_match regexp_blank (input_line ic) 0) do () done;
	""
      end
      else if Str.string_match regexp_blank line 0 
      then ""
      else (line ^ "\n")
    with End_of_file -> 
      ""
  in

  let create oc header header_width =
    let real_width = max width header_width in
    let width' = real_width + 2 * String.length margin in
    let white = String.make width' ' ' in
    let line = String.make width' line_char in
    Printf.fprintf oc "%s%s%s\n" open_comment line close_comment;
    
    List.iter (function string ->
      output_string oc open_comment;
      output_string oc margin;
      output_string oc string;
      output oc white 0 (max 0 (real_width - UTF8.length string));
      output_string oc margin;
      output_string oc close_comment;
      output_char oc '\n'
    ) header;

    Printf.fprintf oc "%s%s%s\n\n" open_comment line close_comment
  in

  { remove = remove;
    create = create
  } 

let _ =
  add "frame" begin function args -> 
	make_frame 
	  ~open_comment:(arg_string args "open")
	  ~close_comment:(arg_string args "close")
	  ~line_char:(arg_char args "line")
	  ~margin:(arg_string args ~default:"  " "margin")
	  ~width:(arg_int args ~default:"68" "width")
  end



let make_lines ~open_comment ~close_comment ~line_char ~begin_line
    ~begin_last ~width =

  let regexp_begin = 
    Str.regexp_string (sprintf "%s%s" open_comment (String.make 10 line_char))
  in
  let regexp_end = 
    Str.regexp_string (sprintf "%s%s" (String.make 10 line_char) close_comment)
  in
  let end_length = 10 + String.length close_comment
  in

  let regexp_blank = Str.regexp "^[ ]*$" in

  let remove ic =
    try
      let line = input_line ic in
      if Str.string_match regexp_begin line 0
      then begin
	while
          let s = input_line ic in
            not (Str.string_match regexp_end s
                   (max 0 (UTF8.length s - end_length)))
        do () done;
	""
      end
      else if Str.string_match regexp_blank line 0 
      then ""
      else (line ^ "\n")
    with End_of_file -> 
      ""
  in

  let create oc header header_width =
    let real_width = max width header_width in
    Printf.fprintf oc "%s%s\n" open_comment 
      (String.make (max 0 (real_width - String.length open_comment)) line_char);
    
    List.iter (function string ->
      output_string oc begin_line;
      output_string oc string;
      output_char oc '\n'
    ) header;

    Printf.fprintf oc "%s%s%s\n\n" 
      begin_last
      (String.make (max 0 (real_width - String.length begin_last
			     - String.length close_comment)) line_char)
      close_comment;

  in

  { remove = remove;
    create = create
  } 

let _ =
  add "lines" begin function args -> 
	make_lines 
	  ~open_comment:(arg_string args "open")
	  ~close_comment:(arg_string args "close")
	  ~line_char:(arg_char args "line")
	  ~begin_line:(arg_string args ~default:"  " "begin")
	  ~begin_last:(arg_string args ~default:"" "last")
	  ~width:(arg_int args ~default:"70" "width")
  end
      


let make_no () =

  { remove = (fun _ -> "");
    create = (fun _ _ _ -> ())
  } 

let _ =
  add "no" (function _ -> make_no ())