interplane.Get Omega
Read in your language

cortex

aien-dev/cortex

Brought by drakestapleton

branch main Branches Tags History

cortex / tests / brain / mkimp.om

163 lines · 2782 bytes Raw Blame History
1note mkimp.om: Company Brain gate helper. usage: mkimp SPACE OWNER DATE FILE...
2note Writes to stdout one pdrive corpus line per FILE (spec/PARITY.md corpus format):
3note "brain-import TAB POST TAB /api/cortex/brain/import TAB aj TAB {JSON}" where the JSON body is
4note {"space":SPACE,"owner":OWNER,"date":DATE,"text":<the file bytes as a JSON string>}.
5note The file is read and escaped in 65536-byte pieces, so no file size is refused here; the server's
6note request limit (2,097,152 bytes) applies to the request. Exit 0 ok, 2 usage, 3 a file cannot be
7note read, 4 write error.
8global pa uchar 4096
9global ib uchar 65536
10global ob uchar 524288
11global obn ulong 1
12text u_use "usage: mkimp SPACE OWNER DATE FILE...\n"
13text u_open "mkimp: cannot read a file\n"
14text u_wr "mkimp: write error\n"
15text l_head "brain-import\tPOST\t/api/cortex/brain/import\taj\t{\"space\":"
16text l_own ",\"owner\":"
17text l_date ",\"date\":"
18text l_text ",\"text\":\""
19text l_tail "\"}\n"
20text hx "0123456789abcdef"
21
22fn say t view uchar * to void
23w = write 2 t 0 len t
24if lt w 0
25ret
26end
27end
28fn argp i ulong to ulong
29n = argread i pa 0 len pa
30if lt n 0
31ret 0
32end
33ret ulong n
34end
35fn flush to bool
36n = get obn 0
37k = ulong 0
38for it ulong 4611686018427387904
39if ge k n
40break
41end
42w = write 1 ob k sub n k
43if le w 0
44ret false
45end
46k = add k ulong w
47end
48put obn 0 0
49ret true
50end
51fn ob1 c uchar to bool
52if ge get obn 0 len ob
53if not flush
54ret false
55end
56end
57n = get obn 0
58put ob n c
59put obn 0 add n 1
60ret true
61end
62fn obt t view uchar * to bool
63for i ulong len t
64if not ob1 get t i
65ret false
66end
67end
68ret true
69end
70note one byte as JSON string content
71fn esc c uchar to bool
72d = uchar 0
73if eq c 34
74d = 34
75end
76if eq c 92
77d = 92
78end
79if eq c 10
80d = 110
81end
82if eq c 9
83d = 116
84end
85if ne d 0
86ok = ob1 92
87ret and ok ob1 d
88end
89if lt c 32
90ok = ob1 92
91ok = and ok ob1 117
92ok = and ok ob1 48
93ok = and ok ob1 48
94ok = and ok ob1 get hx shr c 4
95ok = and ok ob1 get hx and c 15
96ret ok
97end
98ret ob1 c
99end
100note argument i as a JSON string
101fn argjs i ulong to bool
102n = argp i
103ok = ob1 34
104for k ulong n
105ok = and ok esc get pa k
106end
107ret and ok ob1 34
108end
109fn onefile i ulong to int
110n = argp i
111fd = fopen pa 0 n 524288 0
112if lt fd 0
113say u_open
114ret 3
115end
116ok = obt l_head
117ok = and ok argjs 1
118ok = and ok obt l_own
119ok = and ok argjs 2
120ok = and ok obt l_date
121ok = and ok argjs 3
122ok = and ok obt l_text
123for it ulong 4611686018427387904
124r = read int fd ib 0 len ib
125if lt r 0
126x = fclose int fd
127say u_open
128ret 3
129end
130if eq r 0
131break
132end
133for k ulong ulong r
134ok = and ok esc get ib k
135end
136end
137x = fclose int fd
138ok = and ok obt l_tail
139if not ok
140say u_wr
141ret 4
142end
143ret 0
144end
145fn main to int
146c = ulong argcnt
147if lt c 5
148say u_use
149ret 2
150end
151put obn 0 0
152for i ulong sub c 4
153r = onefile add i 4
154if ne r 0
155ret r
156end
157end
158if not flush
159say u_wr
160ret 4
161end
162ret 0
163end