Added filesystem.name(path[, removeEndSlash) option

This commit is contained in:
Igor Timofeev 2019-01-21 09:25:36 +03:00
parent 272924ab4f
commit 9eab5d6d65
8 changed files with 114 additions and 115 deletions

View File

@ -3,40 +3,14 @@ local filesystem = require("Filesystem")
-----------------------------------------------------------------------------------------------
local compressor = {}
local OCAFSignature = "OCAF"
local readBufferSize = 1024
local ignoredFiles = {
[".DS_Store"] = true
local compressor = {
readBufferSize = 1024,
ignoredFiles = {
[".DS_Store"] = true
}
}
-----------------------------------------------------------------------------------------------
local function numberToByteArray(number)
local byteArray = {}
repeat
table.insert(byteArray, 1, bit32.band(number, 0xFF))
number = bit32.rshift(number, 8)
until number <= 0
return byteArray
end
-- Записываем путь в виде <кол-во байт для размера пути> <размер пути> <путь>
local function writePath(handle, path)
-- Получаем юникод-байтики названия файла или папки
local pathBytes = {string.byte(path, 1, #path)}
-- Записываем количество всякой хуйни
local bytesForCountPathBytes = numberToByteArray(#pathBytes)
handle:writeBytes(#bytesForCountPathBytes)
handle:writeBytes(table.unpack(bytesForCountPathBytes))
-- Записываем путь
handle:write(path)
end
local OCAFSignature = "OCAF"
-----------------------------------------------------------------------------------------------
@ -49,22 +23,49 @@ function compressor.pack(archivePath, fileList)
if handle then
-- Writing signature
handle:write(OCAFSignature)
-- Writing encoding method (maybe will be used in future)
handle:writeBytes(0)
local function numberToByteArray(number)
local byteArray, a, b = {}
repeat
a = number / 256
b = a - a % 1
table.insert(byteArray, 1, number - b * 256)
number = b
until number <= 0
return byteArray
end
-- Recursive packing
local function doPack(fileList, localPath)
for i = 1, #fileList do
local filename = filesystem.name(fileList[i])
local currentLocalPath = localPath .. "/" .. filename
local currentLocalPath = localPath .. filename
-- print("Writing path:", currentLocalPath)
if not ignoredFiles[filename] then
if filesystem.isDirectory(fileList[i]) then
handle:writeBytes(1)
writePath(handle, currentLocalPath)
if not compressor.ignoredFiles[filename] then
local isDirectory = filesystem.isDirectory(fileList[i])
-- Writing byte of data type (0 is a file, 1 is a directory)
handle:writeBytes(isDirectory and 1 or 0)
-- Writing string path as
-- <N bytes for reading a number that represents J bytes of path length>
-- <J bytes for reading a number that represents path length>
-- <PathLength bytes of path>
local bytesForCountPathBytes = numberToByteArray(#currentLocalPath)
handle:writeBytes(#bytesForCountPathBytes)
handle:writeBytes(table.unpack(bytesForCountPathBytes))
handle:write(currentLocalPath)
if isDirectory then
-- Obtaining new file list
local newList = filesystem.list(fileList[i])
-- Forming absolute paths for list
-- Creating absolute paths for list elements
for j = 1, #newList do
newList[j] = fileList[i] .. newList[j]
end
@ -75,21 +76,17 @@ function compressor.pack(archivePath, fileList)
return success, reason
end
else
handle:writeBytes(0)
writePath(handle, currentLocalPath)
local otherHandle, reason = filesystem.open(fileList[i], "rb")
if otherHandle then
-- Пишем размер файла
-- Writing file size
local fileSizeBytes = numberToByteArray(filesystem.size(fileList[i]))
handle:writeBytes(#fileSizeBytes)
handle:writeBytes(table.unpack(fileSizeBytes))
-- Пишем содержимое
-- Writing file contents
local data
while true do
data = otherHandle:readString(readBufferSize)
data = otherHandle:readString(compressor.readBufferSize)
if data then
handle:write(data)
@ -124,10 +121,12 @@ function compressor.unpack(archivePath, unpackPath)
if handle then
local readedSignature = handle:readString(#OCAFSignature)
if readedSignature == OCAFSignature then
-- Reading encoding method *just in case*
handle:readBytes(1)
-- Reading contents
while true do
local typeData = handle:readString(1)
if typeData then
local type = string.byte(typeData)
local type = handle:readBytes(1)
if type then
-- Reading path
local localPath = unpackPath .. handle:readString(handle:readBytes(handle:readBytes(1)))
-- print("Readed path:", localPath)
@ -140,7 +139,7 @@ function compressor.unpack(archivePath, unpackPath)
if otherHandle then
local readedCount, needToRead, data = 0
while readedCount < fileSize do
needToRead = math.min(readBufferSize, fileSize - readedCount)
needToRead = math.min(compressor.readBufferSize, fileSize - readedCount)
otherHandle:write(handle:readString(needToRead))
readedCount = readedCount + needToRead
end

View File

@ -21,8 +21,8 @@ function filesystem.path(path)
return path:match("^(.+%/).") or ""
end
function filesystem.name(path)
return path:match("%/?([^%/]+)%/?$")
function filesystem.name(path, removeSlashes)
return path:match(removeSlashes and "%/?([^%/]+)%/?$" or "%/?([^%/]+%/?)$")
end
function filesystem.extension(path, lower)

View File

@ -2482,7 +2482,7 @@ local function filesystemTreeUpdateFileListRecursively(tree, path, offset)
if tree.showMode == GUI.IO_MODE_BOTH or tree.showMode == GUI.IO_MODE_DIRECTORY then
for i = 1, #expandables do
tree:addItem(filesystem.name(expandables[i]), path .. expandables[i], offset, true)
tree:addItem(filesystem.name(expandables[i], true), path .. expandables[i], offset, true)
if tree.expandedItems[path .. expandables[i]] then
filesystemTreeUpdateFileListRecursively(tree, path .. expandables[i], offset + 2)

View File

@ -482,9 +482,9 @@ function system.icon(x, y, path, textColor, selectionColor)
icon.path = path
icon.extension = filesystem.extension(icon.path)
icon.name = filesystem.name(path)
icon.nameWithoutExtension = filesystem.hideExtension(icon.name)
icon.isDirectory = filesystem.isDirectory(icon.path)
icon.name = filesystem.name(path, true)
icon.nameWithoutExtension = filesystem.hideExtension(icon.name)
icon.isShortcut = false
icon.selected = false
@ -830,7 +830,7 @@ local function iconOnRightClick(icon, e1, e2, e3, e4)
for i = 1, #selectedIcons do
if not selectedIcons[i].isShortcut then
system.createShortcut(
paths.user.desktop .. "/" .. selectedIcons[i].nameWithoutExtension .. ".lnk",
paths.user.desktop .. selectedIcons[i].nameWithoutExtension .. ".lnk",
selectedIcons[i].path
)
end