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 / main.om

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