Plasma GitLab Archive
Projects Blog Knowledge

#! /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.
 *
 * 
 *)

This web site is published by Informatikbüro Gerd Stolpmann
Powered by Caml