xref: /inferno-os/appl/lib/strokes/writestrokes.b (revision 37da2899f40661e3e9631e497da8dc59b971cbd0)
1implement Writestrokes;
2
3#
4# write structures to classifier files
5#
6
7include "sys.m";
8	sys: Sys;
9
10include "bufio.m";
11	bufio: Bufio;
12	Iobuf: import bufio;
13
14include "strokes.m";
15	strokes: Strokes;
16	Penpoint, Stroke: import strokes;
17
18init(s: Strokes)
19{
20	sys = load Sys Sys->PATH;
21	bufio = load Bufio Bufio->PATH;
22	strokes = s;
23}
24
25write_examples(fd: ref Sys->FD, names: array of string, examples: array of list of ref Stroke): string
26{
27	fp := bufio->fopen(fd, Bufio->OWRITE);
28	nclass := len names;
29	fp.puts(sys->sprint("%d\n", nclass));
30	for(i := 0; i < nclass; i++){
31		exl := examples[i];
32		fp.puts(sys->sprint("%d %s\n", len exl, names[i]));
33		for(; exl != nil; exl = tl exl){
34			putpoints(fp, hd exl);
35			fp.putc('\n');
36		}
37	}
38	if(fp.flush() == Bufio->ERROR)
39		return sys->sprint("write error: %r");
40	fp.close();
41	return nil;
42}
43
44write_digest(fd: ref Sys->FD, cnames: array of string, dompts: array of ref Stroke): string
45{
46	fp := bufio->fopen(fd, Bufio->OWRITE);
47	n := len cnames;
48	for(i := 0; i < n; i++){
49		d := dompts[i];
50		npts := d.npts;
51		fp.puts(cnames[i]);
52		putpoints(fp, d);
53		fp.putc('\n');
54	}
55	if(fp.flush() == Bufio->ERROR)
56		return sys->sprint("write error: %r");
57	fp.close();
58	return nil;
59}
60
61putpoints(fp: ref Iobuf, d: ref Stroke)
62{
63	fp.puts(sys->sprint(" %d", d.npts));
64	for(j := 0; j < d.npts; j++){
65		p := d.pts[j];
66		fp.puts(sys->sprint(" %d %d", p.x, p.y));
67	}
68}
69