omega-office
Omega office documents: read and write office files in Omega
Brought by drakestapleton
omega-office / src / main.om
| 1 | note ======== src/main.om: the officeread command line and the handler dispatch ======== |
| 2 | note officeread text FILE.docx the text of the main document part, one paragraph per line (UTF-8) |
| 3 | note officeread csv FILE.xlsx [SHEET] one worksheet as CSV (RFC 4180); SHEET is a name, else a position |
| 4 | note 1, 2, ... when no sheet has that name; default the first sheet |
| 5 | note officeread sheets FILE.xlsx the sheet names in workbook order, one per line |
| 6 | note officeread xmlev FILE [ENTRY] the XML event sequence of FILE, or of zip entry ENTRY of FILE |
| 7 | |
| 8 | text kusage "usage: officeread text FILE.docx | officeread csv FILE.xlsx [SHEET] | officeread sheets FILE.xlsx | officeread xmlev FILE [ENTRY]" |
| 9 | text kctext "text" |
| 10 | text kccsv "csv" |
| 11 | text kcsheet "sheets" |
| 12 | text kcxmlev "xmlev" |
| 13 | text kopen "cannot open the file" |
| 14 | |
| 15 | |
| 16 | global ms ulong 4 |
| 17 | const MRUN ulong 0 |
| 18 | |
| 19 | fn usage to void |
| 20 | quit 1 kusage |
| 21 | end |
| 22 | |
| 23 | note argfd: open the file named by argument i for reading. |
| 24 | fn argfd i ulong to int |
| 25 | p = new uchar 4096 0 |
| 26 | k = argread i p 0 4096 |
| 27 | if le k 0 |
| 28 | usage |
| 29 | end |
| 30 | if gt k 4095 |
| 31 | usage |
| 32 | end |
| 33 | fd = fopen p 0 ulong k 0 0 |
| 34 | if lt fd 0 |
| 35 | quit 2 kopen |
| 36 | end |
| 37 | ret int fd |
| 38 | end |
| 39 | |
| 40 | note ---- xmlev: one line per event ---- |
| 41 | note "( {URI}LOCAL" start, "A {URI}LOCAL VALUE" each attribute in document order (namespace declarations |
| 42 | note are not shown), "T TEXT" a run of character data, ") {URI}LOCAL" end. In TEXT and VALUE a backslash, |
| 43 | note LF, tab and CR are shown as \\, \n, \t and \r. {} is no namespace. |
| 44 | fn evesc c ulong to void |
| 45 | if eq c 92 |
| 46 | oput 92 |
| 47 | oput 92 |
| 48 | ret |
| 49 | end |
| 50 | if eq c 10 |
| 51 | oput 92 |
| 52 | oput 110 |
| 53 | ret |
| 54 | end |
| 55 | if eq c 9 |
| 56 | oput 92 |
| 57 | oput 116 |
| 58 | ret |
| 59 | end |
| 60 | if eq c 13 |
| 61 | oput 92 |
| 62 | oput 114 |
| 63 | ret |
| 64 | end |
| 65 | oput c |
| 66 | end |
| 67 | |
| 68 | fn evrun to void |
| 69 | if eq get ms MRUN 1 |
| 70 | oput 10 |
| 71 | put ms MRUN 0 |
| 72 | end |
| 73 | end |
| 74 | |
| 75 | fn evuri b ulong to void |
| 76 | oput 123 |
| 77 | if ne b txnone |
| 78 | o = get buo b |
| 79 | for i ulong get bul b bound 262144 |
| 80 | oput ulong get bpool add o i |
| 81 | end |
| 82 | end |
| 83 | oput 125 |
| 84 | end |
| 85 | |
| 86 | fn evname to void |
| 87 | evuri get xs QEB |
| 88 | o = get xs QELO |
| 89 | for i ulong sub get xs QNL o bound 256 |
| 90 | oput ulong get xname add o i |
| 91 | end |
| 92 | oput 10 |
| 93 | end |
| 94 | |
| 95 | fn evattr i ulong to void |
| 96 | b = get abind i |
| 97 | if eq b BDECL |
| 98 | ret |
| 99 | end |
| 100 | oput 65 |
| 101 | oput 32 |
| 102 | evuri b |
| 103 | l = get alo i |
| 104 | o = add get anoff i l |
| 105 | for j ulong sub get anlen i l bound 256 |
| 106 | oput ulong get apool add o j |
| 107 | end |
| 108 | oput 32 |
| 109 | o = get avoff i |
| 110 | for j ulong get avlen i bound 262144 |
| 111 | evesc ulong get apool add o j |
| 112 | end |
| 113 | oput 10 |
| 114 | end |
| 115 | |
| 116 | fn evstart to void |
| 117 | evrun |
| 118 | oput 40 |
| 119 | oput 32 |
| 120 | evname |
| 121 | for i ulong get xs QAC bound 256 |
| 122 | evattr i |
| 123 | end |
| 124 | end |
| 125 | |
| 126 | fn evend to void |
| 127 | evrun |
| 128 | oput 41 |
| 129 | oput 32 |
| 130 | evname |
| 131 | end |
| 132 | |
| 133 | fn evtext to void |
| 134 | if eq get ms MRUN 0 |
| 135 | oput 84 |
| 136 | oput 32 |
| 137 | put ms MRUN 1 |
| 138 | end |
| 139 | for i ulong get xs QTL bound 65536 |
| 140 | evesc ulong get tbuf i |
| 141 | end |
| 142 | end |
| 143 | |
| 144 | note ---- dispatch ---- |
| 145 | fn hstart to void |
| 146 | m = get xs QMODE |
| 147 | if eq m MDOC |
| 148 | dstart |
| 149 | ret |
| 150 | end |
| 151 | if eq m MSH |
| 152 | shstart |
| 153 | ret |
| 154 | end |
| 155 | if eq m MEV |
| 156 | evstart |
| 157 | ret |
| 158 | end |
| 159 | if eq m MRL |
| 160 | rlstart |
| 161 | ret |
| 162 | end |
| 163 | if eq m MCT |
| 164 | ctstart |
| 165 | ret |
| 166 | end |
| 167 | if eq m MWB |
| 168 | wbstart |
| 169 | ret |
| 170 | end |
| 171 | ssstart |
| 172 | end |
| 173 | |
| 174 | fn hend to void |
| 175 | m = get xs QMODE |
| 176 | if eq m MDOC |
| 177 | dend |
| 178 | ret |
| 179 | end |
| 180 | if eq m MSH |
| 181 | shend |
| 182 | ret |
| 183 | end |
| 184 | if eq m MEV |
| 185 | evend |
| 186 | ret |
| 187 | end |
| 188 | if eq m MSS |
| 189 | ssend |
| 190 | end |
| 191 | end |
| 192 | |
| 193 | fn htext to void |
| 194 | m = get xs QMODE |
| 195 | if eq m MDOC |
| 196 | dtext |
| 197 | ret |
| 198 | end |
| 199 | if eq m MSH |
| 200 | shtext |
| 201 | ret |
| 202 | end |
| 203 | if eq m MEV |
| 204 | evtext |
| 205 | ret |
| 206 | end |
| 207 | if eq m MSS |
| 208 | sstext |
| 209 | end |
| 210 | end |
| 211 | |
| 212 | note ---- commands ---- |
| 213 | fn cmdev n ulong to void |
| 214 | if eq n 3 |
| 215 | fd = argfd 2 |
| 216 | xreset MEV |
| 217 | xfile fd |
| 218 | xeof |
| 219 | evrun |
| 220 | oflush |
| 221 | exit 0 |
| 222 | end |
| 223 | if ne n 4 |
| 224 | usage |
| 225 | end |
| 226 | zopenp 2 |
| 227 | k = argread 3 pname 0 4096 |
| 228 | if or le k 0 gt k 4095 |
| 229 | usage |
| 230 | end |
| 231 | put os OPNL ulong k |
| 232 | f = zpart MEV true |
| 233 | evrun |
| 234 | oflush |
| 235 | exit 0 |
| 236 | end |
| 237 | |
| 238 | fn cmdtext n ulong to void |
| 239 | if ne n 3 |
| 240 | usage |
| 241 | end |
| 242 | zopenp 2 |
| 243 | pkgmain 2 |
| 244 | dreset |
| 245 | f = zpart MDOC true |
| 246 | oflush |
| 247 | exit 0 |
| 248 | end |
| 249 | |
| 250 | fn cmdsheet n ulong to void |
| 251 | if ne n 3 |
| 252 | usage |
| 253 | end |
| 254 | zopenp 2 |
| 255 | pkgmain 3 |
| 256 | sreset |
| 257 | put ss SLIST 1 |
| 258 | f = zpart MWB true |
| 259 | oflush |
| 260 | exit 0 |
| 261 | end |
| 262 | |
| 263 | note sargnum: the SHEET argument as a position 1, 2, ... (0 when it is not a decimal number). |
| 264 | fn sargnum to ulong |
| 265 | n = get ss SARGL |
| 266 | if or eq n 0 gt n 7 |
| 267 | ret 0 |
| 268 | end |
| 269 | v = ulong 0 |
| 270 | for i ulong n bound 8 |
| 271 | c = ulong get sarg i |
| 272 | if or lt c 48 gt c 57 |
| 273 | ret 0 |
| 274 | end |
| 275 | v = add mul v 10 sub c 48 |
| 276 | end |
| 277 | ret v |
| 278 | end |
| 279 | |
| 280 | fn cmdcsv n ulong to void |
| 281 | if or lt n 3 gt n 4 |
| 282 | usage |
| 283 | end |
| 284 | zopenp 2 |
| 285 | pkgmain 3 |
| 286 | sreset |
| 287 | if eq n 4 |
| 288 | k = argread 3 sarg 0 4096 |
| 289 | if or lt k 0 gt k 4095 |
| 290 | usage |
| 291 | end |
| 292 | put ss SARGL ulong k |
| 293 | put ss SWANT 1 |
| 294 | put ss SNUM sargnum |
| 295 | end |
| 296 | f = zpart MWB true |
| 297 | if eq get ss SFND 0 |
| 298 | if eq get ss SNFND 0 |
| 299 | quit 83 e83 |
| 300 | end |
| 301 | m = get rid2l 0 |
| 302 | for i ulong m bound 1024 |
| 303 | put rid i get rid2 i |
| 304 | end |
| 305 | put os ORIDL m |
| 306 | end |
| 307 | csvsheet |
| 308 | end |
| 309 | |
| 310 | note csvsheet: the sheet whose relationship id is rid, through the workbook's Relationships part. |
| 311 | fn csvsheet to void |
| 312 | relsname |
| 313 | put os OWANT 2 |
| 314 | put os OFND 0 |
| 315 | put os ORTY 0 |
| 316 | put os OSSF 0 |
| 317 | f = zpart MRL true |
| 318 | if eq get os OFND 0 |
| 319 | quitp 81 e81r |
| 320 | end |
| 321 | if ne get os ORTY 1 |
| 322 | quit 84 e84 |
| 323 | end |
| 324 | resolve |
| 325 | n = get os OPNL |
| 326 | if eq get os OSSF 1 |
| 327 | svtotv |
| 328 | for i ulong n bound 4096 |
| 329 | put sval i get pname i |
| 330 | end |
| 331 | resolve |
| 332 | put ss SCNT 0 |
| 333 | put ss SSPL 0 |
| 334 | put sso 0 0 |
| 335 | f = zpart MSS true |
| 336 | for i ulong n bound 4096 |
| 337 | put pname i get sval i |
| 338 | end |
| 339 | put os OPNL n |
| 340 | end |
| 341 | put ss SPASS 1 |
| 342 | f = zpart MSH true |
| 343 | put ss SPASS 2 |
| 344 | put ss SPR 0 |
| 345 | put ss SPRT 0 |
| 346 | f = zpart MSH true |
| 347 | oflush |
| 348 | exit 0 |
| 349 | end |
| 350 | |
| 351 | fn main to int |
| 352 | crcinit |
| 353 | tabinit |
| 354 | n = ulong argcnt |
| 355 | if lt n 3 |
| 356 | usage |
| 357 | end |
| 358 | a = new uchar 16 0 |
| 359 | k = argread 1 a 0 16 |
| 360 | if or le k 0 gt k 16 |
| 361 | usage |
| 362 | end |
| 363 | m = ulong k |
| 364 | if tx_eq part a 0 m kctext |
| 365 | cmdtext n |
| 366 | end |
| 367 | if tx_eq part a 0 m kccsv |
| 368 | cmdcsv n |
| 369 | end |
| 370 | if tx_eq part a 0 m kcsheet |
| 371 | cmdsheet n |
| 372 | end |
| 373 | if tx_eq part a 0 m kcxmlev |
| 374 | cmdev n |
| 375 | end |
| 376 | usage |
| 377 | ret 1 |
| 378 | end |