File: insert_variant

package info (click to toggle)
pxp 1.1.4-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 6,944 kB
  • ctags: 1,728
  • sloc: ml: 15,005; xml: 2,583; sh: 972; makefile: 677
file content (105 lines) | stat: -rwxr-xr-x 2,527 bytes parent folder | download
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
#! /bin/sh
# (*
exec ocaml "$0" "$@"
*) directory ".";;

(* $Id: insert_variant,v 1.3 2001/04/21 17:41:57 gerd Exp $
 * ----------------------------------------------------------------------
 *
 *)

let get_arg variant insert_line =
  (* returns the argument of an "#insert" line *)
  let s = ref "" in
  for i = 8 to String.length insert_line - 1 do
    match insert_line.[i] with
	' ' -> ()
      | '*' ->
	  (* replace '*' with 'variant' *)
	  s := !s ^ variant
      | c ->
	  s := !s ^ String.make 1 c
  done;
  !s
;;


let edit_file variant name =
  let basename = Filename.chop_suffix name ".src" in
  let mllname = basename ^ "_" ^ variant ^ ".mll" in
  let chin = open_in name in
  let chout = open_out mllname in
  output_string chout "(* File generated by insert_variant; DO NOT EDIT! *)\n";
  begin try
    while true do
      let line = input_line chin in
      (* We do not have Str here. *)
      if String.length line >= 8 & String.sub line 0 8 = "#insert " then begin
	let insname = get_arg variant line in
	(* Copy the file 'insname' to chout *)
	let chcopy = open_in_bin insname in
	let n = in_channel_length chcopy in
	let s = String.create n in
	really_input chcopy s 0 n;
	close_in chcopy;
	output_string chout s;
      end
      else begin
	output_string chout line;
	output_char chout '\n';
      end
    done
  with
      End_of_file -> ()
  end;
  close_in chin;
  close_out chout
;;


let main() =
  let variant = ref "" in
  let files = ref [] in
  Arg.current := 0;          (* Because of a OCaml-3.00 bug *)
  Arg.parse
      [ "-variant", Arg.String (fun s -> variant := s),
	        "<name>  Set the variant (character encoding)";
      ]
      (fun s -> files := !files @ [s])
      "insert_variant [ options ] file.src ...

Reads the files, replaces the #insert lines by the referred files, and 
writes the file file_variant.mll. 

The #insert lines include the specified file into the source. The
asterisk (*) is replaced by the name of the variant.

Options:
";
  
  if !variant = "" then 
    failwith "No variant specified!";

  List.iter 
    (fun name -> edit_file !variant name)
    !files
;;


main();;

(* ======================================================================
 * History:
 * 
 * $Log: insert_variant,v $
 * Revision 1.3  2001/04/21 17:41:57  gerd
 * 	Hope this makes insert_variant working under Cygwin.
 *
 * Revision 1.2  2000/05/20 21:14:33  gerd
 * 	Workaround for an OCaml 3.00 bug.
 *
 * Revision 1.1  2000/05/20 20:30:15  gerd
 * 	Initial revision.
 *
 * 
 *)