| 1 |
if not modules then modules = { } end modules ['util-fil'] = { |
| 2 |
version = 1.001, |
| 3 |
comment = "companion to luat-lib.mkiv", |
| 4 |
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL", |
| 5 |
copyright = "PRAGMA ADE / ConTeXt Development Team", |
| 6 |
license = "see context related readme files" |
| 7 |
} |
| 8 |
|
| 9 |
local byte = string.byte |
| 10 |
local char = string.char |
| 11 |
|
| 12 |
-- Here are a few helpers (the starting point were old ones I used for parsing |
| 13 |
-- flac files). In Lua 5.3 we can probably do this better. Some code will move |
| 14 |
-- here. |
| 15 |
|
| 16 |
utilities = utilities or { } |
| 17 |
local files = { } |
| 18 |
utilities.files = files |
| 19 |
|
| 20 |
local zerobased = { } |
| 21 |
|
| 22 |
function files.open(filename,zb) |
| 23 |
local f = io.open(filename,"rb") |
| 24 |
if f then |
| 25 |
zerobased[f] = zb or false |
| 26 |
end |
| 27 |
return f |
| 28 |
end |
| 29 |
|
| 30 |
function files.close(f) |
| 31 |
zerobased[f] = nil |
| 32 |
f:close() |
| 33 |
end |
| 34 |
|
| 35 |
function files.size(f) |
| 36 |
local current = f:seek() |
| 37 |
local size = f:seek("end") |
| 38 |
f:seek("set",current) |
| 39 |
return size |
| 40 |
end |
| 41 |
|
| 42 |
files.getsize = files.size |
| 43 |
|
| 44 |
function files.setposition(f,n) |
| 45 |
if zerobased[f] then |
| 46 |
f:seek("set",n) |
| 47 |
else |
| 48 |
f:seek("set",n - 1) |
| 49 |
end |
| 50 |
end |
| 51 |
|
| 52 |
function files.getposition(f) |
| 53 |
if zerobased[f] then |
| 54 |
return f:seek() |
| 55 |
else |
| 56 |
return f:seek() + 1 |
| 57 |
end |
| 58 |
end |
| 59 |
|
| 60 |
function files.look(f,n,chars) |
| 61 |
local p = f:seek() |
| 62 |
local s = f:read(n) |
| 63 |
f:seek("set",p) |
| 64 |
if chars then |
| 65 |
return s |
| 66 |
else |
| 67 |
return byte(s,1,#s) |
| 68 |
end |
| 69 |
end |
| 70 |
|
| 71 |
function files.skip(f,n) |
| 72 |
if n == 1 then |
| 73 |
f:read(n) |
| 74 |
else |
| 75 |
f:seek("set",f:seek()+n) |
| 76 |
end |
| 77 |
end |
| 78 |
|
| 79 |
function files.readbyte(f) |
| 80 |
return byte(f:read(1)) |
| 81 |
end |
| 82 |
|
| 83 |
function files.readbytes(f,n) |
| 84 |
return byte(f:read(n),1,n) |
| 85 |
end |
| 86 |
|
| 87 |
function files.readbytetable(f,n) |
| 88 |
-- return { byte(f:read(n),1,n) } |
| 89 |
local s = f:read(n or 1) |
| 90 |
return { byte(s,1,#s) } -- best use the real length |
| 91 |
end |
| 92 |
|
| 93 |
function files.readchar(f) |
| 94 |
return f:read(1) |
| 95 |
end |
| 96 |
|
| 97 |
function files.readstring(f,n) |
| 98 |
return f:read(n or 1) |
| 99 |
end |
| 100 |
|
| 101 |
function files.readinteger1(f) -- one byte |
| 102 |
local n = byte(f:read(1)) |
| 103 |
if n >= 0x80 then |
| 104 |
return n - 0x100 |
| 105 |
else |
| 106 |
return n |
| 107 |
end |
| 108 |
end |
| 109 |
|
| 110 |
files.readcardinal1 = files.readbyte -- one byte |
| 111 |
files.readcardinal = files.readcardinal1 |
| 112 |
files.readinteger = files.readinteger1 |
| 113 |
files.readsignedbyte = files.readinteger1 |
| 114 |
|
| 115 |
function files.readcardinal2(f) |
| 116 |
local a, b = byte(f:read(2),1,2) |
| 117 |
return 0x100 * a + b |
| 118 |
end |
| 119 |
|
| 120 |
function files.readcardinal2le(f) |
| 121 |
local b, a = byte(f:read(2),1,2) |
| 122 |
return 0x100 * a + b |
| 123 |
end |
| 124 |
|
| 125 |
function files.readinteger2(f) |
| 126 |
local a, b = byte(f:read(2),1,2) |
| 127 |
if a >= 0x80 then |
| 128 |
return 0x100 * a + b - 0x10000 |
| 129 |
else |
| 130 |
return 0x100 * a + b |
| 131 |
end |
| 132 |
end |
| 133 |
|
| 134 |
function files.readinteger2le(f) |
| 135 |
local b, a = byte(f:read(2),1,2) |
| 136 |
if a >= 0x80 then |
| 137 |
return 0x100 * a + b - 0x10000 |
| 138 |
else |
| 139 |
return 0x100 * a + b |
| 140 |
end |
| 141 |
end |
| 142 |
|
| 143 |
function files.readcardinal3(f) |
| 144 |
local a, b, c = byte(f:read(3),1,3) |
| 145 |
return 0x10000 * a + 0x100 * b + c |
| 146 |
end |
| 147 |
|
| 148 |
function files.readcardinal3le(f) |
| 149 |
local c, b, a = byte(f:read(3),1,3) |
| 150 |
return 0x10000 * a + 0x100 * b + c |
| 151 |
end |
| 152 |
|
| 153 |
function files.readinteger3(f) |
| 154 |
local a, b, c = byte(f:read(3),1,3) |
| 155 |
if a >= 0x80 then |
| 156 |
return 0x10000 * a + 0x100 * b + c - 0x1000000 |
| 157 |
else |
| 158 |
return 0x10000 * a + 0x100 * b + c |
| 159 |
end |
| 160 |
end |
| 161 |
|
| 162 |
function files.readinteger3le(f) |
| 163 |
local c, b, a = byte(f:read(3),1,3) |
| 164 |
if a >= 0x80 then |
| 165 |
return 0x10000 * a + 0x100 * b + c - 0x1000000 |
| 166 |
else |
| 167 |
return 0x10000 * a + 0x100 * b + c |
| 168 |
end |
| 169 |
end |
| 170 |
|
| 171 |
function files.readcardinal4(f) |
| 172 |
local a, b, c, d = byte(f:read(4),1,4) |
| 173 |
return 0x1000000 * a + 0x10000 * b + 0x100 * c + d |
| 174 |
end |
| 175 |
|
| 176 |
function files.readcardinal4le(f) |
| 177 |
local d, c, b, a = byte(f:read(4),1,4) |
| 178 |
return 0x1000000 * a + 0x10000 * b + 0x100 * c + d |
| 179 |
end |
| 180 |
|
| 181 |
function files.readinteger4(f) |
| 182 |
local a, b, c, d = byte(f:read(4),1,4) |
| 183 |
if a >= 0x80 then |
| 184 |
return 0x1000000 * a + 0x10000 * b + 0x100 * c + d - 0x100000000 |
| 185 |
else |
| 186 |
return 0x1000000 * a + 0x10000 * b + 0x100 * c + d |
| 187 |
end |
| 188 |
end |
| 189 |
|
| 190 |
function files.readinteger4le(f) |
| 191 |
local d, c, b, a = byte(f:read(4),1,4) |
| 192 |
if a >= 0x80 then |
| 193 |
return 0x1000000 * a + 0x10000 * b + 0x100 * c + d - 0x100000000 |
| 194 |
else |
| 195 |
return 0x1000000 * a + 0x10000 * b + 0x100 * c + d |
| 196 |
end |
| 197 |
end |
| 198 |
|
| 199 |
-- function files.readfixed2(f) |
| 200 |
-- local a, b = byte(f:read(2),1,2) |
| 201 |
-- if a >= 0x80 then |
| 202 |
-- return (0x100 * a + b - 0x10000)/256.0 |
| 203 |
-- else |
| 204 |
-- return (0x100 * a + b)/256.0 |
| 205 |
-- end |
| 206 |
-- end |
| 207 |
|
| 208 |
function files.readfixed2(f) |
| 209 |
local a, b = byte(f:read(2),1,2) |
| 210 |
if a >= 0x80 then |
| 211 |
return (a - 0x100) + b/0x100 |
| 212 |
else |
| 213 |
return (a ) + b/0x100 |
| 214 |
end |
| 215 |
end |
| 216 |
|
| 217 |
-- (real) (n>>16) + ((n&0xffff)/65536.0)) |
| 218 |
|
| 219 |
-- function files.readfixed4(f) |
| 220 |
-- local a, b, c, d = byte(f:read(4),1,4) |
| 221 |
-- if a >= 0x80 then |
| 222 |
-- return (0x1000000 * a + 0x10000 * b + 0x100 * c + d - 0x100000000)/65536.0 |
| 223 |
-- else |
| 224 |
-- return (0x1000000 * a + 0x10000 * b + 0x100 * c + d)/65536.0 |
| 225 |
-- end |
| 226 |
-- end |
| 227 |
|
| 228 |
function files.readfixed4(f) |
| 229 |
local a, b, c, d = byte(f:read(4),1,4) |
| 230 |
if a >= 0x80 then |
| 231 |
return (0x100 * a + b - 0x10000) + (0x100 * c + d)/0x10000 |
| 232 |
else |
| 233 |
return (0x100 * a + b ) + (0x100 * c + d)/0x10000 |
| 234 |
end |
| 235 |
end |
| 236 |
|
| 237 |
-- (real) ((n<<16)>>(16+14)) + ((n&0x3fff)/16384.0)) |
| 238 |
|
| 239 |
if bit32 then |
| 240 |
|
| 241 |
local extract = bit32.extract |
| 242 |
local band = bit32.band |
| 243 |
|
| 244 |
function files.read2dot14(f) |
| 245 |
local a, b = byte(f:read(2),1,2) |
| 246 |
if a >= 0x80 then |
| 247 |
local n = -(0x100 * a + b) |
| 248 |
return - (extract(n,14,2) + (band(n,0x3FFF) / 16384.0)) |
| 249 |
else |
| 250 |
local n = 0x100 * a + b |
| 251 |
return (extract(n,14,2) + (band(n,0x3FFF) / 16384.0)) |
| 252 |
end |
| 253 |
end |
| 254 |
|
| 255 |
end |
| 256 |
|
| 257 |
function files.skipshort(f,n) |
| 258 |
f:read(2*(n or 1)) |
| 259 |
end |
| 260 |
|
| 261 |
function files.skiplong(f,n) |
| 262 |
f:read(4*(n or 1)) |
| 263 |
end |
| 264 |
|
| 265 |
-- writers (kind of slow) |
| 266 |
|
| 267 |
if bit32 then |
| 268 |
|
| 269 |
local rshift = bit32.rshift |
| 270 |
|
| 271 |
function files.writecardinal2(f,n) |
| 272 |
local a = char(n % 256) |
| 273 |
n = rshift(n,8) |
| 274 |
local b = char(n % 256) |
| 275 |
f:write(b,a) |
| 276 |
end |
| 277 |
|
| 278 |
else |
| 279 |
|
| 280 |
local floor = math.floor |
| 281 |
|
| 282 |
function files.writecardinal2(f,n) |
| 283 |
local a = char(n % 256) |
| 284 |
n = floor(n/256) |
| 285 |
local b = char(n % 256) |
| 286 |
f:write(b,a) |
| 287 |
end |
| 288 |
|
| 289 |
end |
| 290 |
|
| 291 |
function files.writecardinal4(f,n) |
| 292 |
local a = char(n % 256) |
| 293 |
n = rshift(n,8) |
| 294 |
local b = char(n % 256) |
| 295 |
n = rshift(n,8) |
| 296 |
local c = char(n % 256) |
| 297 |
n = rshift(n,8) |
| 298 |
local d = char(n % 256) |
| 299 |
f:write(d,c,b,a) |
| 300 |
end |
| 301 |
|
| 302 |
function files.writestring(f,s) |
| 303 |
f:write(char(byte(s,1,#s))) |
| 304 |
end |
| 305 |
|
| 306 |
function files.writebyte(f,b) |
| 307 |
f:write(char(b)) |
| 308 |
end |
| 309 |
|
| 310 |
if fio and fio.readcardinal1 then |
| 311 |
|
| 312 |
files.readcardinal1 = fio.readcardinal1 |
| 313 |
files.readcardinal2 = fio.readcardinal2 |
| 314 |
files.readcardinal3 = fio.readcardinal3 |
| 315 |
files.readcardinal4 = fio.readcardinal4 |
| 316 |
files.readinteger1 = fio.readinteger1 |
| 317 |
files.readinteger2 = fio.readinteger2 |
| 318 |
files.readinteger3 = fio.readinteger3 |
| 319 |
files.readinteger4 = fio.readinteger4 |
| 320 |
files.readfixed2 = fio.readfixed2 |
| 321 |
files.readfixed4 = fio.readfixed4 |
| 322 |
files.read2dot14 = fio.read2dot14 |
| 323 |
files.setposition = fio.setposition |
| 324 |
files.getposition = fio.getposition |
| 325 |
|
| 326 |
files.readbyte = files.readcardinal1 |
| 327 |
files.readsignedbyte = files.readinteger1 |
| 328 |
files.readcardinal = files.readcardinal1 |
| 329 |
files.readinteger = files.readinteger1 |
| 330 |
|
| 331 |
local skipposition = fio.skipposition |
| 332 |
files.skipposition = skipposition |
| 333 |
|
| 334 |
files.readbytes = fio.readbytes |
| 335 |
files.readbytetable = fio.readbytetable |
| 336 |
|
| 337 |
function files.skipshort(f,n) |
| 338 |
skipposition(f,2*(n or 1)) |
| 339 |
end |
| 340 |
|
| 341 |
function files.skiplong(f,n) |
| 342 |
skipposition(f,4*(n or 1)) |
| 343 |
end |
| 344 |
|
| 345 |
end |
| 346 |
|
| 347 |
if fio and fio.readcardinaltable then |
| 348 |
|
| 349 |
files.readcardinaltable = fio.readcardinaltable |
| 350 |
files.readintegertable = fio.readintegertable |
| 351 |
|
| 352 |
else |
| 353 |
|
| 354 |
local readcardinal1 = files.readcardinal1 |
| 355 |
local readcardinal2 = files.readcardinal2 |
| 356 |
local readcardinal3 = files.readcardinal3 |
| 357 |
local readcardinal4 = files.readcardinal4 |
| 358 |
|
| 359 |
function files.readcardinaltable(f,n,b) |
| 360 |
local t = { } |
| 361 |
if b == 1 then for i=1,n do t[i] = readcardinal1(f) end |
| 362 |
elseif b == 2 then for i=1,n do t[i] = readcardinal2(f) end |
| 363 |
elseif b == 3 then for i=1,n do t[i] = readcardinal3(f) end |
| 364 |
elseif b == 4 then for i=1,n do t[i] = readcardinal4(f) end end |
| 365 |
return t |
| 366 |
end |
| 367 |
|
| 368 |
local readinteger1 = files.readinteger1 |
| 369 |
local readinteger2 = files.readinteger2 |
| 370 |
local readinteger3 = files.readinteger3 |
| 371 |
local readinteger4 = files.readinteger4 |
| 372 |
|
| 373 |
function files.readintegertable(f,n,b) |
| 374 |
local t = { } |
| 375 |
if b == 1 then for i=1,n do t[i] = readinteger1(f) end |
| 376 |
elseif b == 2 then for i=1,n do t[i] = readinteger2(f) end |
| 377 |
elseif b == 3 then for i=1,n do t[i] = readinteger3(f) end |
| 378 |
elseif b == 4 then for i=1,n do t[i] = readinteger4(f) end end |
| 379 |
return t |
| 380 |
end |
| 381 |
|
| 382 |
end |