Lua開源庫 lfs的介紹與使用

2021-08-09 17:14:16 字數 3899 閱讀 6535

lua lfs庫

這個庫可以實現平台無關(linux和windows通用)的檔案系統訪問

官網位址

github專案位址

如何配置:

5.1以上的lua已經包含了lfs庫,路徑是lua5.1\clibs\lfs.dll,我們無需配置直接require 「lfs」使用即可。

提供的功能:

lfs的開發提供了功能的介紹:官方手冊

下面給出精簡了內容的中文版(方便不喜歡看英文版的同學):

- lfs.attributes (filepath [, aname])

返回這個path的屬性table,如果filepath是nil則會出錯並列印錯誤資訊,屬性列表如下所示:

mode屬性是字串,其他屬性都是陣列。

屬性描述

dev不常用不翻譯了:on unix systems, this represents the device that the inode resides on. on windows systems, represents the drive number of the disk containing the file

inounix系統下表示inode數目,windows系統下無意義

mode

這個字串表示關聯的保護模式,值可能是file、directory、link、socket、named pipe、char device、block device or other

nlink

檔案上的硬鏈結數

uid目錄的user-id(unix only, always 0 on windows)

gid使用者的group-id(unix only, always 0 on windows)

rdev

linux系統下rdev表示裝置型別,windows系統下和dev值相同

access

最近一次訪問時間

modification

最近一次修改時間

change

最近一次檔案狀態修改時間

size

檔案大小(以位元組為單位)

blocks

分配給檔案的block(unix only)

blksize

不常用不翻譯了:optimal file system i/o blocksize; (unix only)

- lfs.chdir (path)

將當前目錄改為給定的path

- lfs.currentdir ()

獲取當前目錄

- lfs.dir (path)

lua遍歷目錄下的所有入口,每次迭代都返回值為入口名的字串

- lfs.lock (filehandle, mode[, start[, length]])

鎖定乙個檔案或這檔案的部分內容

- lfs.mkdir (dirname)

建立乙個目錄

- lfs.rmdir (dirname)

移除乙個已存在的目錄

- lfs.setmode (file, mode)

設定檔案的寫入模式,mode字串可以是binary或text

- lfs.symlinkattributes (filepath [, aname])

比lfs.attributes多了the link itself (not the file it refers to)資訊,其他都和lfs.attribute一樣

- lfs.touch (filepath [, atime [, mtime]])

設定上一次使用和修改檔案的時間值

- lfs.unlock (filehandle[, start[, length]])

解鎖檔案或解鎖檔案的部分內容

小例項 傳入乙個根目錄路徑,遞迴獲取該路徑子目錄的所有檔案全路徑:

local lfs = require

"lfs"

local allfilepath = {}

function

main

() -- 列印lfs庫的版本

printlfsversion()

-- 列印當前目錄位址

printcurrrentdir()

local rootpath = lfs.currentdir()

printdirchildren(rootpath)

-- 遞迴列印當前目錄下面所有層級的子目錄並將檔案位址儲存到表中

getallfiles(rootpath)

printtable(allfilepath)

endfunction

printlfsversion

( ... )

print(lfs._version)

endfunction

printcurrrentdir

( ... )

local rootpath = lfs.currentdir()

print(rootpath)

endfunction

printdirchildren

(rootpath)

for entry in lfs.dir(rootpath) do

if entry~='.'

and entry~='..'

then

local path = rootpath.."\\"..entry

local attr = lfs.attributes(path)

assert(type(attr)=="table") --如果獲取不到屬性表則報錯

-- printtable(attr)

if(attr.mode == "directory") then

print("dir:",path)

elseif attr.mode=="file"

then

print("file:",path)

endendend

endfunction

getallfiles

(rootpath)

for entry in lfs.dir(rootpath) do

if entry~='.'

and entry~='..'

then

local path = rootpath.."\\"..entry

local attr = lfs.attributes(path)

assert(type(attr)=="table") --如果獲取不到屬性表則報錯

-- printtable(attr)

if(attr.mode == "directory") then

-- print("dir:",path)

getallfiles(path) --自呼叫遍歷子目錄

elseif attr.mode=="file"

then

-- print(attr.mode,path)

table.insert(allfilepath,path)

endendend

endfunction

printtable

( tbl , level, filtedefault)

local msg = ""

filtedefault = filtedefault or

true

--預設過濾關鍵字(deleteme, _class_type)

level = level or

1local indent_str = ""

for i = 1, level do

indent_str = indent_str.." "

endprint(indent_str .. "")

endmain()

lua使用lfs.dll庫進行檔案操作

LuaForUnity1 Lua介紹與使用

c 是編譯型語言 lua是解析型語言,關於編譯型語言和解釋性語言的介紹 一般來講lua不能單獨用於一款遊戲的開發,因此對於乙個unity專案,一般先用c 進行開發,後續的熱更新再通過lua來完成,從這裡可以知道兩點 lua語言的目的是 嵌入式 一般用於為其他語言開發出來的專案進行功能的擴充套件和補丁...

base64開源庫介紹及使用

網上有一些開源的base64編譯碼庫的實現,下面介紹幾個 cppcodec是乙個僅包括標頭檔案的c 11庫,用於編譯碼rfc 4648中指定的base64,base64url,base32,base32hex等,它的license為mit,原始碼在 最新發布版本為v0.2,在windows下需要vs...

開源框架VTMagic的使用介紹

vtmagic 特性概要 更多特性請參見vtmagicview.h檔案。預覽圖使用 vtmagic支援cocoapods,只需在podfile檔案中新增如下 即可 1 pod vtmagic 整合 關於vtmagic的整合方法主要有以下兩種 1.直接例項化vtmagiccontroller物件,然後...