interplane.Get Omega
Read in your language

omega-office

Omega office documents: read and write office files in Omega

Brought by drakestapleton

branch main Branches Tags History

omega-office / src / sink.om

198 lines · 4198 bytes Raw Blame History
1note ======== src/sink.om: the output sink the vendored zip engine calls, buffered stdout, refusals ========
2note vendor/omega-zip/src/io.om (unchanged, omega-zip's own seam) checksums win[0 .. n) and then calls
3note wout n. Here those bytes go to the XML reader of the current mode; in mode 0 (no XML reader) they go
4note to woutfd, the zip engine's own file-descriptor sink. Standard output is buffered in obuf and
5note written by oflush; a refusal exits without flushing, so only exit 0 means a complete result.
6
7global obuf uchar 65536
8global on ulong 1
9note fbuf: input buffer for a plain XML file (xmlev without ENTRY)
10global fbuf uchar 65536
11note handler modes (xs[QMODE]); 0 is the zip engine's own file sink
12const MEV ulong 1
13const MRL ulong 2
14const MCT ulong 3
15const MDOC ulong 4
16const MWB ulong 5
17const MSS ulong 6
18const MSH ulong 7
19
20fn quit c int m view uchar * to void
21warn kpre
22warn m
23warn tnl
24exit c
25end
26
27note xfastok: the next byte may take the fast path: UTF-8 input, not inside a multi-byte sequence, no CR
28note waiting for its LF. A fast-path byte (printable ASCII) never changes these three.
29fn xfastok to bool
30if ne get xs QENC 1
31ret false
32end
33ret and eq get xs QU8 0 eq get xs QCR 0
34end
35
36note wout: the zip engine's window win[0 .. n) into the XML reader. Printable ASCII in character data,
37note in attribute values and in names is stored inline here (the same effect as xin, without its calls);
38note every other byte, and any byte at a bound, goes through xin. It returns n: io.om's wflush assigns
39note the result ("w = wout n"), so a build without this function is refused by omc (error 3).
40fn wout n ulong to ulong
41if eq get xs QMODE 0
42woutfd n
43ret n
44end
45ok = xfastok
46i = ulong 0
47while lt i n bound 32769
48c = ulong get win i
49i = add i 1
50f = ulong get ccls c
51if and ok ne f 0
52s = get xs QST
53if and eq s ZTX ne and f 1 0
54t = get xs QTL
55if and ne get xs QDEP 0 lt t XTBUF
56put tbuf t uchar c
57put xs QTL add t 1
58put xs QBR 0
59put xs QPOS add get xs QPOS 1
60if eq add t 1 XTBUF
61tflush
62end
63continue
64end
65end
66if and eq s ZAV ne and f 4 0
67p = get xs QAP
68if lt p XAPOOL
69put apool p uchar c
70put xs QAP add p 1
71put xs QPOS add get xs QPOS 1
72continue
73end
74end
75if and ne and f 8 0 or eq s ZLT eq s ZEN
76if wfirst s c
77continue
78end
79end
80if and ne and f 2 0 eq get xs QNF 1
81if wname s c
82continue
83end
84end
85end
86xin c
87ok = xfastok
88end
89ret n
90end
91
92note wname: an ASCII name character c in state s (start-tag, attribute or end-tag name) stored inline;
93note false when it must take the full path (another state, or the name is at its bound).
94fn wname s ulong c ulong to bool
95if eq s ZSN
96p = get xs QNL
97if lt p XNAME
98put xname p uchar c
99put xs QNL add p 1
100put xs QPOS add get xs QPOS 1
101ret true
102end
103ret false
104end
105if eq s ZAN
106p = get xs QAP
107if and lt p XAPOOL lt sub p get anoff get xs QAC XNAME
108put apool p uchar c
109put xs QAP add p 1
110put xs QPOS add get xs QPOS 1
111ret true
112end
113ret false
114end
115if eq s ZEN
116p = get xs QNB
117if lt p XNAME
118put nbuf p uchar c
119put xs QNB add p 1
120put xs QPOS add get xs QPOS 1
121ret true
122end
123end
124ret false
125end
126
127note wfirst: an ASCII NameStartChar c right after "<" (an element name begins, as zlt) or right after "</"
128note (an end-tag name begins, as zen) stored inline; false for anything else.
129fn wfirst s ulong c ulong to bool
130if eq s ZLT
131if eq get xs QROOT 2
132ret false
133end
134put xname 0 uchar c
135put xs QNL 1
136put xs QAC 0
137put xs QAP 0
138put xs QCP 0
139put xs QCK 0
140put xs QNF 1
141put xs QST ZSN
142put xs QPOS add get xs QPOS 1
143ret true
144end
145if and eq s ZEN eq get xs QNB 0
146put nbuf 0 uchar c
147put xs QNB 1
148put xs QNF 1
149put xs QPOS add get xs QPOS 1
150ret true
151end
152ret false
153end
154
155fn oflush to void
156n = get on 0
157o = ulong 0
158while lt o n bound 1000000
159r = write 1 obuf o sub n o
160if le r 0
161quit 4 twrite
162end
163o = add o ulong r
164end
165put on 0 0
166end
167
168fn oput c ulong to void
169n = get on 0
170put obuf n uchar c
171n = add n 1
172put on 0 n
173if eq n 65536
174oflush
175end
176end
177
178fn otext t view uchar * to void
179for i ulong len t bound 65536
180oput ulong get t i
181end
182end
183
184note xfile: a plain file through the XML reader, 64 KiB at a time.
185fn xfile fd int to void
186while true bound 1000000000
187r = read fd fbuf 0 65536
188if lt r 0
189quit 3 tread
190end
191if eq r 0
192ret
193end
194for i ulong ulong r bound 65536
195xin ulong get fbuf i
196end
197end
198end