-
Notifications
You must be signed in to change notification settings - Fork 2
/
diskfile.lua
362 lines (335 loc) · 10.5 KB
/
diskfile.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
local argcheck = require 'argcheck'
local torch = require 'torch.env'
local class = require 'class'
local ffi = require 'ffi'
ffi.cdef[[
typedef struct FILE FILE;
FILE* fopen(const char *restrict filename, const char *restrict mode);
size_t fread(void *restrict ptr, size_t size, size_t nitems, FILE *restrict stream);
size_t fwrite(const void *restrict ptr, size_t size, size_t nitems, FILE *restrict stream);
int fclose(FILE *stream);
int fseek(FILE *stream, long offset, int whence);
long ftell(FILE *stream);
int fflush(FILE *stream);
int fprintf(FILE *restrict stream, const char *restrict format, ...);
int fscanf(FILE *restrict stream, const char *restrict format, ...);
char *fgets(char *restrict s, int n, FILE *restrict stream);
size_t strlen(const char *s);
int fgetc(FILE *stream);
int ungetc(int c, FILE *stream);
]]
local DiskFile = class('torch.DiskFile', 'torch.File')
torch.DiskFile = DiskFile
DiskFile.SEEK_SET = 0
DiskFile.SEEK_END = 2
local function reversememory(dst, src, blocksize, n)
local halfblocksize = blocksize/2
local charsrc = ffi.cast('char*', src)
local chardst = ffi.cast('char*', dst)
for b=0,n-1 do
for i=0,halfblocksize-1 do
local z = charsrc[i]
chardst[i] = charsrc[blocksize-1-i]
chardst[blocksize-1-i] = z
end
charsrc = charsrc + blocksize
chardst = chardst + blocksize
end
end
DiskFile.name = argcheck{
{name="self", type="torch.DiskFile"},
call =
function(self)
assert(self.__handle, 'attempt to use a closed file')
return self.__name
end
}
DiskFile.isOpened = argcheck{
{name="self", type="torch.DiskFile"},
call =
function(self)
assert(self.__handle, 'attempt to use a closed file')
return self.__handle ~= nil
end
}
DiskFile.synchronize = argcheck{
{name="self", type="torch.DiskFile"},
call =
function(self)
assert(self.__handle, 'attempt to use a closed file')
ffi.C.fflush(self.__handle)
return self
end
}
DiskFile.seek = argcheck{
{name="self", type="torch.DiskFile"},
{name="position", type="number"},
call =
function(self, position)
assert(self.__handle, 'attempt to use a closed file')
if ffi.C.fseek(self.__handle, position, self.SEEK_SET) < 0 then
self.__hasError = 1
if not self.__isQuiet then
error('unable to seek in file')
end
end
return self
end
}
DiskFile.seekEnd = argcheck{
{name="self", type="torch.DiskFile"},
call =
function(self)
assert(self.__handle, 'attempt to use a closed file')
if ffi.C.fseek(self.__handle, 0, self.SEEK_END) < 0 then
self.__hasError = 1
if not self.__isQuiet then
error('unable to seek in file')
end
end
return self
end
}
DiskFile.position = argcheck{
{name="self", type="torch.DiskFile"},
call =
function(self)
assert(self.__handle, 'attempt to use a closed file')
local position = tonumber(ffi.C.ftell(self.__handle))
if position < 0 then
self.__hasError = 1
if not self.__isQuiet then
error('unable to get position in file')
end
end
return position
end
}
DiskFile.close = argcheck{
{name="self", type="torch.DiskFile"},
call =
function(self)
assert(self.__handle, 'attempt to use a closed file')
ffi.gc(self.__handle, nil)
ffi.C.fclose(self.__handle)
self.__handle = nil
return self
end
}
DiskFile.__write = argcheck{
{name="self", type="torch.DiskFile"},
{name="data", type="cdata"},
{name="elemsize", type="number"},
{name="size", type="number"},
call =
function(self, data, elemsize, size)
assert(self.__handle, 'attempt to write in a closed file')
assert(self.__isWritable, 'read-only file')
if self.__isNativeEncoding or elemsize == 1 then
return tonumber(ffi.C.fwrite(data, elemsize, size, self.__handle))
else
local buffer = ffi.C.malloc(elemsize*size)
assert(buffer ~= nil, 'out of memory')
reversememory(buffer, data, elemsize, size)
local n = tonumber(ffi.C.fwrite(buffer, elemsize, size, self.__handle))
ffi.C.free(buffer)
return n
end
end
}
DiskFile.isLittleEndianCPU = argcheck{
call =
function()
local x = ffi.new('int[1]', 7)
local ptr = ffi.cast('char*', x)
if ptr[0] == 0 then
return false
else
return true
end
end
}
DiskFile.isBigEndianCPU = argcheck{
call =
function()
return not DiskFile.isLittleEndianCPU()
end
}
DiskFile.nativeEndianEncoding = argcheck{
{name="self", type="torch.DiskFile"},
call =
function(self)
assert(self.__handle, 'attempt to write in a closed file')
self.__isNativeEncoding = true
return self
end
}
DiskFile.littleEndianEncoding = argcheck{
{name="self", type="torch.DiskFile"},
call =
function(self)
assert(self.__handle, 'attempt to write in a closed file')
self.__isNativeEncoding = DiskFile.isLittleEndianCPU()
return self
end
}
DiskFile.bigEndianEncoding = argcheck{
{name="self", type="torch.DiskFile"},
call =
function(self)
assert(self.__handle, 'attempt to write in a closed file')
self.__isNativeEncoding = DiskFile.isBigEndianCPU()
return self
end
}
DiskFile.__read = argcheck{
{name="self", type="torch.DiskFile"},
{name="data", type="cdata"},
{name="elemsize", type="number"},
{name="size", type="number"},
call =
function(self, data, elemsize, size)
assert(self.__handle, 'attempt to write in a closed file')
assert(self.__isReadable, 'write-only file')
local n = tonumber(ffi.C.fread(data, elemsize, size, self.__handle))
if not self.__isNativeEncoding and elemsize > 1 then
reversememory(data, data, elemsize, n)
end
return n
end
}
local format2cast = {
['%hhu'] = ffi.typeof('unsigned char'),
['%hhd'] = ffi.typeof('char'),
['%hd'] = ffi.typeof('short'),
['%d'] = ffi.typeof('int'),
['%ld'] = ffi.typeof('long'),
['%g'] = ffi.typeof('float'),
['%lg'] = ffi.typeof('double'),
}
DiskFile.__printf = argcheck{
{name="self", type="torch.DiskFile"},
{name="format", type="string"},
{name="data", type="cdata"},
{name="size", type="number"},
call =
function(self, format, data, size)
assert(self.__handle, 'attempt to write in a closed file')
assert(self.__isWritable, 'read-only file')
local n = 0
local cast = format2cast[format]
for i=0,size-1 do
local ret = ffi.C.fprintf(self.__handle, format, cast(data[i]))
if ret <= 0 then
break
else
n = n + 1
end
if i < size-1 then
ffi.C.fprintf(self.__handle, ' ')
end
end
return n
end
}
DiskFile.__scanf = argcheck{
{name="self", type="torch.DiskFile"},
{name="format", type="string"},
{name="data", type="cdata"},
{name="size", type="number"},
call =
function(self, format, data, size)
assert(self.__handle, 'attempt to write in a closed file')
assert(self.__isReadable, 'write-only file')
local n = 0
for i=0,size-1 do
local ret = ffi.C.fscanf(self.__handle, format, data+i)
if ret <= 0 then
break
else
n = n + 1
end
end
if self.__isAutoSpacing and size > 0 then
local c = ffi.C.fgetc(self.__handle)
if string.char(c) ~= '\n' and c ~= -1 then
ffi.C.ungetc(c, self.__handle)
end
end
return n
end
}
DiskFile.__gets = argcheck{
{name="self", type="torch.DiskFile"},
call =
function(self)
assert(self.__handle, 'attempt to write in a closed file')
assert(self.__isReadable, 'write-only file')
local size = 0
local buffsize = 1024
local buffer = ffi.cast('char*', ffi.C.malloc(buffsize))
local eof
while true do
assert(buffer ~= nil, 'out of memory')
if ffi.C.fgets(buffer+size, buffsize, self.__handle) == nil then
eof = true
break
end
local l = tonumber(ffi.C.strlen(buffer+size))
if l == 0 or string.char(buffer[size+l-1]) ~= '\n' then
size = size + l
buffer = ffi.cast('char*', ffi.C.realloc(buffer, size+buffsize))
else
size = size + l - 1 -- do not add eol
break
end
end
if not eof then
local str = ffi.string(buffer, size)
ffi.C.free(buffer)
return str
else
ffi.C.free(buffer)
end
end
}
DiskFile.__init = argcheck{
{name="self", type="torch.DiskFile"},
{name="name", type="string"},
{name="mode", type="string", default='r'},
{name="quiet", type="boolean", default=false},
call =
function(self, name, mode, quiet)
assert(mode == 'r' or mode == 'w' or mode == 'rw', 'invalid mode (r, w or rw expected)')
local handle
if mode == 'rw' then
handle = ffi.C.fopen(name, 'r+b')
if handle == nil then
handle = ffi.C.fopen(name, 'wb')
if handle ~= nil then
ffi.C.fclose(handle)
handle = ffi.C.fopen(name, 'r+b')
end
end
else
handle = ffi.C.fopen(name, mode == 'r' and 'rb' or 'wb')
end
if handle == nil then
if quiet then
return
else
error(string.format('cannot open file <%s> in mode <%s>)', name, mode))
end
end
self.__handle = handle
self.__name = name
self.__isQuiet = quiet
self.__isReadable = (mode == 'r') or (mode == 'rw')
self.__isWritable = (mode == 'w') or (mode == 'rw')
self.__isBinary = false
self.__isNativeEncoding = true
self.__isAutoSpacing = true
self.__hasError = false
ffi.gc(self.__handle, ffi.C.fclose)
end
}