-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileio.e
246 lines (216 loc) · 5.43 KB
/
fileio.e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
---------------------------------------------------------------------------------
-- FILE IO ROUTINES --
-- written by Jason Mirwald
-- NOTE: max number of files that can be open at one time
-- by Euphoria is 25 including 0,1,2 (in/out/error)
atom
M_ALLOC = 16
, M_FREE = 17
, NULL = 0
---------------------------------------------------------------------------------
function alloc(atom size)
return machine_func(M_ALLOC,size)
end function
procedure dealloc(atom lp)
machine_proc(M_FREE,lp)
end procedure
---------------------------------------------------------------------------------
constant lp256 = alloc(256)
---------------------------------------------------------------------------------
-------------------------------------------------
global function getn(integer fn, integer n)
-- Return a sequence of n bytes from a file.
-- If EOF is reached before n bytes have been read, the remainder will be filled with -1's
sequence s
s = repeat(-1, n)
for i = 1 to n do
s[i] = getc(fn)
end for
return s
end function
------------------------------------------------
global function get_string(object x)
------------------------------------------------
-- syntax: s = get_string(fn)
-- returns: a string of chars, up to, but not including binary 0 or EOF
-- description:
-- Get a 0-terminated string from a file.
------------------------------------------------
sequence s
integer c
if atom(x) then
s = {}
c = getc(x)
while c > 0 do
s &= c
c = getc(x)
end while
else
s = repeat(NULL,x[2])
for i = 1 to x[2] do
s[i] = get_string(x[1])
end for
end if
return s
end function
------------------------------------------------
----------------------------------------------
global function get2u(object o)
sequence s
integer fn
if atom(o) then
return getc(o) + (getc(o)*#0100)
else
if length(o) = 2 then
fn = o[1]
s = repeat(0,o[2])
for n = 1 to o[2] do
s[n] = getc(fn) + (getc(fn)*#0100)
end for
return s
end if
end if
return 0
end function
----------------------------------------------
global function get2s(object o)
sequence s
integer fn
atom a
if atom(o) then
a = getc(o) + (getc(o)*#0100)
return a-(#10000*(and_bits(a,#8000)!=0))
else
if length(o) = 2 then
fn = o[1]
s = repeat(0,o[2])
for n = 1 to o[2] do
a = getc(fn) + (getc(fn)*#0100)
s[n] = a-(#10000*(and_bits(a,#8000)!=0))
end for
return s
end if
end if
return 0
end function
----------------------------------------------
global function get4u(object o)
sequence s
atom lp
lp = lp256
if atom(o) then
o = {o,1}
else
lp = alloc(o[2]*4)
end if
poke(lp,getn(o[1],o[2]*4))
s = peek4u({lp,o[2]})
if lp = lp256 then return s[1] end if
dealloc(lp)
return s
end function
----------------------------------------------
global function get4s(object o)
sequence s
atom lp
lp = lp256
if atom(o) then
o = {o,1}
else
lp = alloc(o[2]*4)
end if
poke(lp,getn(o[1],o[2]*4))
s = peek4s({lp,o[2]})
if lp = lp256 then return s[1] end if
dealloc(lp)
return s
end function
----------------------------------------------
global function get_float32(object o)
sequence s
integer fn
if atom(o) then
return machine_func(49, getn(o,4))
else
if length(o) = 2 then
fn = o[1]
s = repeat(0,o[2])
for n = 1 to o[2] do
s[n] = machine_func(49, getn(fn,4))
end for
return s
end if
end if
return 0
end function
----------------------------------------------
global function get_float64(object o)
sequence s
integer fn
if atom(o) then
return machine_func(47, getn(o,8))
else
if length(o) = 2 then
fn = o[1]
s = repeat(0,o[2])
for n = 1 to o[2] do
s[n] = machine_func(47, getn(fn,8))
end for
return s
end if
end if
return -1
end function
----------------------------------------------
global procedure put_string( integer fn, sequence s)
if length(s) and sequence(s[1]) then -- an array
for i = 1 to length(s) do
put_string(fn,s[i])
end for
else -- a null string "" or a single string
puts(fn,s&NULL)
end if
end procedure
----------------------------------------------
global procedure put2( integer fn, object o )
sequence s
atom a
s = {}
if atom(o) then
o = {o}
end if
for n = 1 to length(o) do
a = o[n]
if a < 0 then
a = #FFFF+a+1
end if
puts(fn,{remainder(a,#100),floor(a/#100)})
end for
end procedure
----------------------------------------------
global procedure put4(integer fn, object o)
atom lp
lp = lp256
if atom(o) then
o = {o}
else
lp = alloc(length(o)*4)
end if
poke4(lp,o)
puts(fn,peek({lp,length(o)*4}))
if lp != lp256 then dealloc(lp) end if
end procedure
----------------------------------------------
global procedure put_float32(integer fn, object o)
if atom(o) then o = {o} end if
for n = 1 to length(o) do
puts(fn,machine_func(48,o[n]))
end for
end procedure
----------------------------------------------
global procedure put_float64(integer fn, object o)
if atom(o) then o = {o} end if
for n = 1 to length(o) do
puts(fn,machine_func(46,o[n]))
end for
end procedure