UNSAFE MODULE; IMPORT FS, File, OSError, Text; CONST N = 16_1000000; (* == 2^24 == 16MBytes *) TYPE Ptr = UNTRACED REF ARRAY [0..N-1] OF File.Byte; PROCEDURE Buf FromFile (path: TEXT; src: File.T; pad : CARDINAL): T RAISES {OSError.E} = VAR f: File.T; len, next, got: INTEGER; t: T; p: Ptr; BEGIN IF (src = NIL) THEN f := FS.OpenFileReadonly (path); ELSE f := src; END; len := VAL(f.status().size, INTEGER); t := NEW (T, len + pad); next := 0; WHILE (len > N) DO p := LOOPHOLE (ADR (t[next]), Ptr); got := f.read (p^, mayBlock := TRUE); INC (next, got); DEC (len, got); END; WHILE (len > 0) DO p := LOOPHOLE (ADR (t[next]), Ptr); got := f.read (SUBARRAY (p^, 0, len), mayBlock := TRUE); INC (next, got); DEC (len, got); END; IF (src = NIL) THEN f.close (); END; RETURN t; END FromFile; PROCEDUREFromText (txt: TEXT): T = VAR t := NEW (T, Text.Length (txt)); BEGIN Text.SetChars (t^, txt); RETURN t; END FromText; BEGIN <*ASSERT BYTESIZE (File.Byte) = BYTESIZE (CHAR) *> END Buf.