|
@@ -1,4 +1,3 @@
|
|
|
-open Printf
|
|
|
|
|
open Ast
|
|
open Ast
|
|
|
|
|
|
|
|
let input_all ic =
|
|
let input_all ic =
|
|
@@ -8,6 +7,19 @@ let input_all ic =
|
|
|
close_in ic;
|
|
close_in ic;
|
|
|
buf
|
|
buf
|
|
|
|
|
|
|
|
|
|
+let input_buffered ic chunksize =
|
|
|
|
|
+ let rec read_all buf bufsize pos =
|
|
|
|
|
+ match input ic buf pos (bufsize - pos) with
|
|
|
|
|
+ | 0 -> (close_in ic; buf)
|
|
|
|
|
+ | nread when nread = bufsize - pos ->
|
|
|
|
|
+ let bufsize = bufsize + chunksize in
|
|
|
|
|
+ let pos = pos + nread in
|
|
|
|
|
+ read_all (buf ^ String.create chunksize) bufsize pos
|
|
|
|
|
+ | nread ->
|
|
|
|
|
+ read_all buf bufsize (pos + nread)
|
|
|
|
|
+ in
|
|
|
|
|
+ read_all (String.create chunksize) chunksize 0
|
|
|
|
|
+
|
|
|
let phase ir =
|
|
let phase ir =
|
|
|
prerr_endline "- Load input file";
|
|
prerr_endline "- Load input file";
|
|
|
match ir with
|
|
match ir with
|
|
@@ -16,36 +28,29 @@ let phase ir =
|
|
|
| Some filename -> filename
|
|
| Some filename -> filename
|
|
|
| None -> "<stdin>"
|
|
| None -> "<stdin>"
|
|
|
in
|
|
in
|
|
|
|
|
+ let bufsize = 512 in
|
|
|
|
|
|
|
|
if args.cpp then
|
|
if args.cpp then
|
|
|
- let _ = prerr_endline "- Run C preprocessor" in
|
|
|
|
|
-
|
|
|
|
|
let cpp_out = match args.filename with
|
|
let cpp_out = match args.filename with
|
|
|
| Some filename ->
|
|
| Some filename ->
|
|
|
Unix.open_process_in ("cpp " ^ filename)
|
|
Unix.open_process_in ("cpp " ^ filename)
|
|
|
| None ->
|
|
| None ->
|
|
|
- let content = input_all stdin in
|
|
|
|
|
|
|
+ let content = input_buffered stdin bufsize in
|
|
|
let (cpp_out, cpp_in) = Unix.open_process "cpp" in
|
|
let (cpp_out, cpp_in) = Unix.open_process "cpp" in
|
|
|
output_string cpp_in content;
|
|
output_string cpp_in content;
|
|
|
close_out cpp_in;
|
|
close_out cpp_in;
|
|
|
cpp_out
|
|
cpp_out
|
|
|
in
|
|
in
|
|
|
|
|
|
|
|
|
|
+ let _ = prerr_endline "- Run C preprocessor" in
|
|
|
|
|
+
|
|
|
(* Read preprocessed code from cpp's stdout *)
|
|
(* Read preprocessed code from cpp's stdout *)
|
|
|
- let bufsize = 1024 in
|
|
|
|
|
- let newbuf () = String.create bufsize in
|
|
|
|
|
- let rec read_all buf pos =
|
|
|
|
|
- let nread = input cpp_out buf pos bufsize in
|
|
|
|
|
- if nread = 0
|
|
|
|
|
- then (close_in cpp_out; buf)
|
|
|
|
|
- else read_all (String.concat "" [buf; newbuf ()]) nread
|
|
|
|
|
- in
|
|
|
|
|
- let preprocessed = read_all (newbuf ()) 0 in
|
|
|
|
|
|
|
+ let preprocessed = input_buffered cpp_out bufsize in
|
|
|
FileContent (display_name, preprocessed, args)
|
|
FileContent (display_name, preprocessed, args)
|
|
|
else
|
|
else
|
|
|
- let infile = match args.filename with
|
|
|
|
|
- | Some filename -> open_in filename
|
|
|
|
|
- | None -> stdin
|
|
|
|
|
|
|
+ let content = match args.filename with
|
|
|
|
|
+ | Some filename -> input_all (open_in filename)
|
|
|
|
|
+ | None -> input_buffered stdin bufsize
|
|
|
in
|
|
in
|
|
|
- FileContent (display_name, input_all infile, args)
|
|
|
|
|
|
|
+ FileContent (display_name, content, args)
|
|
|
| _ -> raise (InvalidInput "load")
|
|
| _ -> raise (InvalidInput "load")
|