-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfile.lua
337 lines (305 loc) · 7.99 KB
/
file.lua
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
local argcheck = require 'argcheck'
local torch = require 'torch.env'
local class = require 'class'
local ffi = require 'ffi'
local iotypes = {
byte = {ctype='unsigned char', format='%hhu'},
char = {ctype='char', format='%hhd'},
short = {ctype='short', format='%hd'},
int = {ctype='int', format='%d'},
long = {ctype='long', format='%ld'},
float = {ctype='float', format='%g'},
double = {ctype='double', format='%lg'},
}
for ioluatype, iotype in pairs(iotypes) do
iotype.type = ioluatype
iotype.Type = string.upper(string.sub(ioluatype, 1, 1)) .. string.sub(ioluatype, 2, -1)
iotype.Storage = torch[iotype.Type .. 'Storage']
iotype.sizeof = ffi.sizeof(iotype.ctype)
iotype.ffictype = ffi.typeof(iotype.ctype)
iotype.ffipval = ffi.typeof(iotype.ctype .. '[1]')
iotype.write =
function(self, cdata, size)
local n
if self.__isBinary then
n = self:__write(cdata, iotype.sizeof, size)
else
n = self:__printf(iotype.format, cdata, size)
if self.__isAutoSpacing and n > 0 then
local ret = '\n'
self:__write(ffi.cast('char*', ret), 1, 1)
end
end
if n ~= size then
if not self.__isQuiet then
error(string.format('wrote %d values instead of %d', n, size))
end
end
return n
end
iotype.read =
function(self, cdata, size)
local n
if self.__isBinary then
n = self:__read(cdata, iotype.sizeof, size)
else
n = self:__scanf(iotype.format, cdata, size)
end
if n ~= size then
if not self.__isQuiet then
error(string.format('read %d values instead of %d', n, size))
end
end
return n
end
end
local File = class('torch.File')
torch.File = File
-- should initialize basic variables (__isBinary, __isAutoSpacing... here in a basic constructor)
File.isQuiet = argcheck{
{name="self", type="torch.File"},
call =
function(self)
return self.__isQuiet
end
}
File.isReadable = argcheck{
{name="self", type="torch.File"},
call =
function(self)
return self.__isReadable
end
}
File.isWritable = argcheck{
{name="self", type="torch.File"},
call =
function(self)
return self.__isWritable
end
}
File.isAutoSpacing = argcheck{
{name="self", type="torch.File"},
call =
function(self)
return self.__isAutoSpacing
end
}
File.hasError = argcheck{
{name="self", type="torch.File"},
call =
function(self)
return self.__hasError
end
}
File.isBinary = argcheck{
{name="self", type="torch.File"},
call =
function(self)
return self.__isBinary
end
}
for _, iotype in pairs(iotypes) do
File['write' .. iotype.Type] = argcheck{
{name="self", type="torch.File"},
{name="storage", type="torch." .. iotype.Type .. "Storage"},
call =
function(self, storage)
return iotype.write(self, storage.__data, tonumber(storage.__size))
end
}
argcheck{
{name="self", type="torch.File"},
{name="value", type="number"},
chain = File['write' .. iotype.Type],
call =
function(self, value)
local p = iotype.ffipval(value)
return iotype.write(self, p, 1)
end
}
File['read' .. iotype.Type] = argcheck{
{name="self", type="torch.File"},
{name="storage", type="torch." .. iotype.Type .. "Storage"},
call =
function(self, storage)
return iotype.read(self, storage.__data, tonumber(storage.__size))
end
}
argcheck{
{name="self", type="torch.File"},
{name="size", type="number"},
chain = File['read' .. iotype.Type],
call =
function(self, size)
local storage = iotype.Storage(size)
local n = iotype.read(self, storage.__data, tonumber(storage.__size))
if n ~= size then
storage:resize(n)
end
return storage
end
}
argcheck{
{name="self", type="torch.File"},
chain = File['read' .. iotype.Type],
call =
function(self)
local p = iotype.ffipval()
local n = iotype.read(self, p, 1)
if n ~= 0 then
return tonumber(p[0])
end
end
}
end
File.binary = argcheck{
{name="self", type="torch.File"},
call =
function(self)
self.__isBinary = true
return self
end
}
File.ascii = argcheck{
{name="self", type="torch.File"},
call =
function(self)
self.__isBinary = false
return self
end
}
File.autoSpacing = argcheck{
{name="self", type="torch.File"},
call =
function(self)
self.__isAutoSpacing = true
return self
end
}
File.noAutoSpacing = argcheck{
{name="self", type="torch.File"},
call =
function(self)
self.__isAutoSpacing = false
return self
end
}
File.quiet = argcheck{
{name="self", type="torch.File"},
call =
function(self)
self.__isQuiet = true
return self
end
}
File.pedantic = argcheck{
{name="self", type="torch.File"},
call =
function(self)
self.__isQuiet = false
return self
end
}
File.clearError = argcheck{
{name="self", type="torch.File"},
call =
function(self)
self.__hasError = false
return self
end
}
local function readchars(self, n)
local buffsize = 1024
local buffer = ffi.cast('char*', ffi.C.malloc(buffsize))
local size = 0
while true do
assert(buffer ~= nil, 'out of memory')
local rlen = math.min(buffsize, n)
local nr = self:__read(buffer+size, 1, rlen)
if nr > 0 then
size = size + nr
n = n - nr
end
if n > 0 and nr == rlen then
buffer = ffi.cast('char*', ffi.C.realloc(buffer, size+buffsize))
else
break
end
end
if size > 0 then
local str = ffi.string(buffer, size)
ffi.C.free(buffer)
return str
else
ffi.C.free(buffer)
end
end
File.read = argcheck{
{name="self", type="torch.File"},
{name="format", type="string"},
call =
function(self, format)
if format:match('^%*n') then
local p = ffi.new('double[1]')
local n = self:__scanf("%lf", p, 1)
if n == 1 then
return p[0]
end
elseif format:match('^%*a') then
return readchars(self, math.huge)
elseif format:match('^%*l') then
return self:__gets()
elseif tonumber(format) then
return readchars(self, tonumber(format))
else
error('invalid format')
end
end
}
File.write = argcheck{
{name="self", type="torch.File"},
{name="value", type="string"},
call =
function(self, value)
self:__write(ffi.cast('char*', value), 1, #value)
end
}
argcheck{
{name="self", type="torch.File"},
{name="value", type="number"},
chain = File.write,
call =
function(self, value)
local p = ffi.new('double[1]')
p[0] = value
self:__printf("%lf", p, 1)
end
}
File.readRaw = argcheck{
{name="self", type="torch.File"},
{name="typename", type="string"},
{name="cdata", type="cdata"},
{name="size", type="number"},
call =
function(self, type, cdata, size)
assert(iotypes[type], 'unknown type')
iotypes[type].read(self, cdata, size)
end
}
File.writeRaw = argcheck{
{name="self", type="torch.File"},
{name="typename", type="string"},
{name="cdata", type="cdata"},
{name="size", type="number"},
call =
function(self, type, cdata, size)
assert(iotypes[type], 'unknown type')
iotypes[type].write(self, cdata, size)
end
}
File.new = argcheck{
call =
function()
error('virtual class')
end
}