File: bomstrip.b

package info (click to toggle)
bomstrip 9-15
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 252 kB
  • sloc: pascal: 41; ansic: 34; cpp: 34; java: 33; python: 31; sh: 24; makefile: 20; perl: 7; php: 6; ruby: 6; haskell: 6; awk: 2; sed: 1
file content (43 lines) | stat: -rwxr-xr-x 808 bytes parent folder | download | duplicates (10)
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
implement Bomstrip;

include "sys.m";
include "draw.m";

Bomstrip: module
{
	init:	fn(ctxt: ref Draw->Context, argv: list of string);
};

init(nil: ref Draw->Context, nil: list of string)
{
	sys := load Sys Sys->PATH;
	buf := array[sys->ATOMICIO] of byte;
	utf8bom := array[3] of {byte 16rEF, byte 16rBB, byte 16rBF};
	stdin := sys->fildes(0);
	stdout := sys->fildes(1);

	n := sys->read(stdin, buf, len buf);
	if(n < 0)
		raise "fail:read";
	if(n == 0)
		return;
	m := 3;
	if(n < m)
		m = n;
	if(m != 3
		|| buf[0] != utf8bom[0]
		|| buf[1] != utf8bom[1]
		|| buf[2] != utf8bom[2])
		sys->write(stdout, buf[0:3], m);
	if(n > 3)
		sys->write(stdout, buf[3:n], n-3);

	for(;;) {
		n = sys->read(stdin, buf, len buf);
		if(n < 0)
			raise "fail:read";
		if(n == 0)
			break;
		sys->write(stdout, buf, n);
	}
}