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