--[[ Copyright 2020 megagrump@pm.me Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ]]-- local ffi, bit = require('ffi'), require('bit') local C = ffi.C local File = { getBuffer = function(self) return self._bufferMode, self._bufferSize end, getFilename = function(self) return self._name end, getMode = function(self) return self._mode end, isOpen = function(self) return self._mode ~= 'c' and self._handle ~= nil end, } local fopen, getcwd, chdir, unlink, mkdir, rmdir local BUFFERMODE, MODEMAP local ByteArray = ffi.typeof('unsigned char[?]') local function _ptr(p) return p ~= nil and p or nil end -- NULL pointer to nil function File:open(mode) if self._mode ~= 'c' then return false, "File " .. self._name .. " is already open" end if not MODEMAP[mode] then return false, "Invalid open mode for " .. self._name .. ": " .. mode end local handle = _ptr(fopen(self._name, MODEMAP[mode])) if not handle then return false, "Could not open " .. self._name .. " in mode " .. mode end self._handle, self._mode = ffi.gc(handle, C.fclose), mode self:setBuffer(self._bufferMode, self._bufferSize) return true end function File:close() if self._mode == 'c' then return false, "File is not open" end C.fclose(ffi.gc(self._handle, nil)) self._handle, self._mode = nil, 'c' return true end function File:setBuffer(mode, size) local bufferMode = BUFFERMODE[mode] if not bufferMode then return false, "Invalid buffer mode " .. mode .. " (expected 'none', 'full', or 'line')" end if mode == 'none' then size = math.max(0, size or 0) else size = math.max(2, size or 2) -- Windows requires buffer to be at least 2 bytes end local success = self._mode == 'c' or C.setvbuf(self._handle, nil, bufferMode, size) == 0 if not success then self._bufferMode, self._bufferSize = 'none', 0 return false, "Could not set buffer mode" end self._bufferMode, self._bufferSize = mode, size return true end function File:getSize() -- NOTE: The correct way to do this would be a stat() call, which requires a -- lot more (system-specific) code. This is a shortcut that requires the file -- to be readable. local mustOpen = not self:isOpen() if mustOpen and not self:open('r') then return 0 end local pos = mustOpen and 0 or self:tell() C.fseek(self._handle, 0, 2) local size = self:tell() if mustOpen then self:close() else self:seek(pos) end return size end function File:read(containerOrBytes, bytes) if self._mode ~= 'r' then return nil, 0 end local container = bytes ~= nil and containerOrBytes or 'string' if container ~= 'string' and container ~= 'data' then error("Invalid container type: " .. container) end bytes = not bytes and containerOrBytes or 'all' bytes = bytes == 'all' and self:getSize() - self:tell() or math.min(self:getSize() - self:tell(), bytes) if bytes <= 0 then local data = container == 'string' and '' or love.data.newFileData('', self._name) return data, 0 end local data = love.data.newByteData(bytes) local r = tonumber(C.fread(data:getFFIPointer(), 1, bytes, self._handle)) local str = data:getString() data:release() data = container == 'data' and love.filesystem.newFileData(str, self._name) or str return data, r end local function lines(file, autoclose) local BUFFERSIZE = 4096 local buffer, bufferPos = ByteArray(BUFFERSIZE), 0 local bytesRead = tonumber(C.fread(buffer, 1, BUFFERSIZE, file._handle)) local offset = file:tell() return function() file:seek(offset) local line = {} while bytesRead > 0 do for i = bufferPos, bytesRead - 1 do if buffer[i] == 10 then -- end of line bufferPos = i + 1 return table.concat(line) end if buffer[i] ~= 13 then -- ignore CR table.insert(line, string.char(buffer[i])) end end bytesRead = tonumber(C.fread(buffer, 1, BUFFERSIZE, file._handle)) offset, bufferPos = offset + bytesRead, 0 end if not line[1] then if autoclose then file:close() end return nil end return table.concat(line) end end function File:lines() if self._mode ~= 'r' then error("File is not opened for reading") end return lines(self) end function File:write(data, size) if self._mode ~= 'w' and self._mode ~= 'a' then return false, "File " .. self._name .. " not opened for writing" end local toWrite, writeSize if type(data) == 'string' then writeSize = (size == nil or size == 'all') and #data or size toWrite = data else writeSize = (size == nil or size == 'all') and data:getSize() or size toWrite = data:getFFIPointer() end if tonumber(C.fwrite(toWrite, 1, writeSize, self._handle)) ~= writeSize then return false, "Could not write data" end return true end function File:seek(pos) return self._handle and C.fseek(self._handle, pos, 0) == 0 end function File:tell() if not self._handle then return nil, "Invalid position" end return tonumber(C.ftell(self._handle)) end function File:flush() if self._mode ~= 'w' and self._mode ~= 'a' then return nil, "File is not opened for writing" end return C.fflush(self._handle) == 0 end function File:isEOF() return not self:isOpen() or C.feof(self._handle) ~= 0 or self:tell() == self:getSize() end function File:release() if self._mode ~= 'c' then self:close() end self._handle = nil end function File:type() return 'File' end function File:typeOf(t) return t == 'File' end File.__index = File ----------------------------------------------------------------------------- local nativefs = {} local loveC = ffi.os == 'Windows' and ffi.load('love') or C function nativefs.newFile(name) if type(name) ~= 'string' then error("bad argument #1 to 'newFile' (string expected, got " .. type(name) .. ")") end return setmetatable({ _name = name, _mode = 'c', _handle = nil, _bufferSize = 0, _bufferMode = 'none' }, File) end function nativefs.newFileData(filepath) local f = nativefs.newFile(filepath) local ok, err = f:open('r') if not ok then return nil, err end local data, err = f:read('data', 'all') f:close() return data, err end function nativefs.mount(archive, mountPoint, appendToPath) return loveC.PHYSFS_mount(archive, mountPoint, appendToPath and 1 or 0) ~= 0 end function nativefs.unmount(archive) return loveC.PHYSFS_unmount(archive) ~= 0 end function nativefs.read(containerOrName, nameOrSize, sizeOrNil) local container, name, size if sizeOrNil then container, name, size = containerOrName, nameOrSize, sizeOrNil elseif not nameOrSize then container, name, size = 'string', containerOrName, 'all' else if type(nameOrSize) == 'number' or nameOrSize == 'all' then container, name, size = 'string'
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - 066stream.mu</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="none">
<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=">
<meta name="colorscheme"