欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Lua腳本實現(xiàn)遞歸刪除一個文件夾

 更新時間:2015年05月27日 09:27:41   投稿:junjie  
這篇文章主要介紹了Lua腳本實現(xiàn)遞歸刪除一個文件夾,本文給出了C++和Lua兩個版本的實現(xiàn)代碼,需要的朋友可以參考下

復制代碼 代碼如下:

rmdir in quick-cocos2d-x with lua.

在使用 quick-cocos2d-x 做項目熱更新的時候,我需要建立臨時文件夾以保存下載的更新包。在更新完成后,我需要刪除這些臨時文件和文件夾。

cocos2d-x 和 quick-cocos2d-x 都沒有提供刪除文件夾功能。我做了如下2個嘗試:

1. 使用C++

在 cocos2d-x 2.x 中的 AssetsManager 包中提供了一個 CreateDirectory 方法。這個方法可以跨平臺支持創(chuàng)建文件夾。在實際項目中運行沒有問題。

復制代碼 代碼如下:

bool AssetsManager::createDirectory(const char *path)
{
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
    mode_t processMask = umask(0);
    int ret = mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO);
    umask(processMask);
    if (ret != 0 && (errno != EEXIST))
    {
        return false;
    }

    return true;
#else
    BOOL ret = CreateDirectoryA(path, NULL);
if (!ret && ERROR_ALREADY_EXISTS != GetLastError())
{
return false;
}
    return true;
#endif
}

在 cocos2d-x 2.x 的 AssetsManager sample 范例中提供了一個 reset 方法,這個方法使用系統(tǒng)命令遞歸刪除文件夾。

復制代碼 代碼如下:

void UpdateLayer::reset(cocos2d::CCObject *pSender)
{
    pProgressLabel->setString(" ");

    // Remove downloaded files
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
    string command = "rm -r ";
    // Path may include space.
    command += "\"" + pathToSave + "\"";
    system(command.c_str());
#else
    string command = "rd /s /q ";
    // Path may include space.
    command += "\"" + pathToSave + "\"";
    system(command.c_str());
#endif
    // Delete recorded version codes.
    getAssetsManager()->deleteVersion();

    createDownloadedDir();
}

但是,這個 reset 在 ios 模擬器中運行的時候,xcode會報這樣的warinng:

The iOS Simulator libSystem was initialized out of order. This is most often caused by running host executables or inserting host dylibs. In the future, this will cause an abort.

因此,我轉(zhuǎn)而考慮另一個方案。

2. 純lua

純 lua 其實是個噱頭。這里還是要依賴 lfs(lua file sytem),好在 quick-cocos2d-x 已經(jīng)包含了這個庫。

lfs.rmdir 命令 和 os.remove 命令一樣,只能刪除空文件夾。因此實現(xiàn)類似 rm -rf 的功能, 必須要遞歸刪除文件夾中所有的文件和子文件夾。

讓我們擴展一下 os 包。

復制代碼 代碼如下:

require("lfs")

function os.exists(path)
    return CCFileUtils:sharedFileUtils():isFileExist(path)
end

function os.mkdir(path)
    if not os.exists(path) then
        return lfs.mkdir(path)
    end
    return true
end

function os.rmdir(path)
    print("os.rmdir:", path)
    if os.exists(path) then
        local function _rmdir(path)
            local iter, dir_obj = lfs.dir(path)
            while true do
                local dir = iter(dir_obj)
                if dir == nil then break end
                if dir ~= "." and dir ~= ".." then
                    local curDir = path..dir
                    local mode = lfs.attributes(curDir, "mode")
                    if mode == "directory" then
                        _rmdir(curDir.."/")
                    elseif mode == "file" then
                        os.remove(curDir)
                    end
                end
            end
            local succ, des = os.remove(path)
            if des then print(des) end
            return succ
        end
        _rmdir(path)
    end
    return true
end

上面的代碼在 iOS 模擬器和 Android 真機上測試成功。Windows系統(tǒng)、Mac OSX 以及 iOS 真機還沒有測試。我測試后會立即更新。

相關文章

  • Lua中算術運算符的使用示例

    Lua中算術運算符的使用示例

    這篇文章主要介紹了Lua中算術運算符的使用示例,是Lua入門學習中的基礎知識,需要的朋友可以參考下
    2015-05-05
  • Lua判斷Table是否為空的方法(空的table即{})

    Lua判斷Table是否為空的方法(空的table即{})

    這篇文章主要介紹了Lua判斷Table是否為空的方法(空的table即{}),如何判斷l(xiāng)ua中的table是否是空的table呢,本文就試驗了多個方法,最后得出比較好的判斷方法,需要的朋友可以參考下
    2015-04-04
  • Lua中的函數(shù)知識總結(jié)

    Lua中的函數(shù)知識總結(jié)

    這篇文章主要介紹了Lua中的函數(shù)知識總結(jié),本文講解了函數(shù)的一些基礎知識、多重返回值問題、變長參數(shù)、內(nèi)嵌函數(shù)等內(nèi)容,需要的朋友可以參考下
    2014-09-09
  • Lua讀寫文件代碼示例

    Lua讀寫文件代碼示例

    這篇文章主要介紹了Lua讀寫文件代碼示例,本文講解了讀寫文件的模式以及讀寫文件代碼實例,需要的朋友可以參考下
    2015-04-04
  • Golang使用ChatGPT生成單元測試實踐

    Golang使用ChatGPT生成單元測試實踐

    這篇文章主要為大家介紹了Golang使用ChatGPT生成單元測試實踐詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • lua調(diào)用C/C++的方法詳解

    lua調(diào)用C/C++的方法詳解

    lua是腳本語言,優(yōu)點是門檻低,可以熱更新,缺點當然就是性能,C/C++是編譯型語言,有點是性能高,但是相對的,門檻高,lua語言本身就是用C實現(xiàn)的,而且,可以將很多能力封裝成lua的接口供lua調(diào)用,本文將給大家介紹lua如何調(diào)用C/C++,需要的朋友可以參考下
    2023-10-10
  • Lua中使用元表(metatable)執(zhí)行算術類元方法實例

    Lua中使用元表(metatable)執(zhí)行算術類元方法實例

    這篇文章主要介紹了Lua中使用元表(metatable)執(zhí)行算術類元方法實例,本文給出了加法、減法、乘法、除法、相反數(shù)、取模等內(nèi)容,需要的朋友可以參考下
    2014-09-09
  • Lua中string.lower()使用指南

    Lua中string.lower()使用指南

    這篇文章主要介紹了Lua中操作字符串的基本方法整理,是Lua入門學習中的基礎知識,需要的朋友可以參考下
    2016-08-08
  • Lua教程(二):基礎知識、類型與值介紹

    Lua教程(二):基礎知識、類型與值介紹

    這篇文章主要介紹了Lua教程(二):基礎知識、類型與值介紹,本文講解了Hello World程序、代碼規(guī)范、全局變量、類型與值等內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • Lua中的__index和__newindex實例

    Lua中的__index和__newindex實例

    這篇文章主要介紹了Lua中的__index和__newindex實例,本文講解了具有默認值的table、記錄table的訪問、只讀的table等內(nèi)容,需要的朋友可以參考下
    2014-09-09

最新評論