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

Lua中實(shí)現(xiàn)遞歸刪除一個(gè)文件夾

 更新時(shí)間:2015年01月13日 09:08:15   投稿:junjie  
這篇文章主要介紹了Lua中實(shí)現(xiàn)遞歸刪除一個(gè)文件夾,本文給出了使用C++和使用純LUA兩種方式實(shí)現(xiàn),需要的朋友可以參考下

在使用 quick-cocos2d-x 做項(xiàng)目熱更新的時(shí)候,我需要建立臨時(shí)文件夾以保存下載的更新包。在更新完成后,我需要?jiǎng)h除這些臨時(shí)文件和文件夾。

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

1. 使用C++

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

復(fù)制代碼 代碼如下:

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 范例中提供了一個(gè) reset 方法,這個(gè)方法使用系統(tǒng)命令遞歸刪除文件夾。
復(fù)制代碼 代碼如下:

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();
}


但是,這個(gè) reset 在 ios 模擬器中運(yùn)行的時(shí)候,xcode會(huì)報(bào)這樣的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)而考慮另一個(gè)方案。

2. 純lua

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

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

讓我們擴(kuò)展一下 os 包。

復(fù)制代碼 代碼如下:

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 真機(jī)上測(cè)試成功。Windows系統(tǒng)、Mac OSX 以及 iOS 真機(jī)還沒有測(cè)試。我測(cè)試后會(huì)立即更新。

相關(guān)文章

  • 舉例詳解Lua中的協(xié)同程序編程

    舉例詳解Lua中的協(xié)同程序編程

    這篇文章主要介紹了Lua中的協(xié)同程序編程,是Lua入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-05-05
  • Lua table中安全移除元素的方法

    Lua table中安全移除元素的方法

    這篇文章主要介紹了Lua table中安全移除元素的方法,本文給出3種實(shí)現(xiàn)方法,分別是從后往前刪除和while刪除,需要的朋友可以參考下
    2015-06-06
  • vs2012 error c4996: This function or variable may be unsafe

    vs2012 error c4996: This function or variable may be unsafe

    這篇文章主要介紹了vs2012 error c4996: This function or variable may be unsafe,需要的朋友可以參考下
    2015-04-04
  • 在Lua程序中使用MySQL的教程

    在Lua程序中使用MySQL的教程

    這篇文章主要介紹了在Lua程序中使用MySQL的教程,是Lua入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-05-05
  • Lua中讓回調(diào)函數(shù)支持回調(diào)對(duì)象方法的解決方法

    Lua中讓回調(diào)函數(shù)支持回調(diào)對(duì)象方法的解決方法

    這篇文章主要介紹了Lua中讓回調(diào)支持對(duì)象方法,一般情況下,Lua中只支持回調(diào)一個(gè)函數(shù),本文方法實(shí)現(xiàn)可以回調(diào)一個(gè)對(duì)象的方法,需要的朋友可以參考下
    2014-12-12
  • Lua判斷一個(gè)目錄或文件是否存在的方法

    Lua判斷一個(gè)目錄或文件是否存在的方法

    這篇文章主要介紹了Lua判斷一個(gè)目錄或文件是否存在的方法,Lua中可以使用io.open判斷文件或目錄是否存在,本文總結(jié)了判斷方法,并給出了一個(gè)自定義函數(shù),需要的朋友可以參考下
    2015-04-04
  • Lua中的metatable介紹

    Lua中的metatable介紹

    這篇文章主要介紹了Lua中的metatable介紹,Lua 中的每個(gè)值都可以用一個(gè) metatable,個(gè) metatable 就是一個(gè)原始的 Lua table,它用來定義原始值在特定操作下的行為,要的朋友可以參考下
    2015-04-04
  • Lua中模塊以及實(shí)現(xiàn)方法指南

    Lua中模塊以及實(shí)現(xiàn)方法指南

    從Lua 5.1開始,我們可以使用require和module函數(shù)來獲取和創(chuàng)建Lua中的模塊。從使用者的角度來看,一個(gè)模塊就是一個(gè)程序庫,可以通過require來加載,之后便得到一個(gè)類型為table的全局變量。
    2015-04-04
  • Lua數(shù)據(jù)類型介紹

    Lua數(shù)據(jù)類型介紹

    這篇文章主要介紹了Lua數(shù)據(jù)類型介紹,本文講解了Lua中的nil(空)、boolean(布爾)、number(數(shù)字)、string(字符串)、table(表)、function(函數(shù))、thread(線程)、userdata(自定義類型)等數(shù)據(jù)類型,需要的朋友可以參考下
    2014-12-12
  • Lua中的元表與元方法學(xué)習(xí)總結(jié)

    Lua中的元表與元方法學(xué)習(xí)總結(jié)

    這篇文章主要介紹了Lua中的元表與元方法學(xué)習(xí)總結(jié),本文講解了算術(shù)類的元方法、__tostring元方法等內(nèi)容,需要的朋友可以參考下
    2014-09-09

最新評(píng)論