diff options
| author | ruki <[email protected]> | 2018-05-02 23:25:59 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2018-05-02 14:35:58 +0800 |
| commit | 4bda9b04b16460c93f6342a17c93cf07f191b7cc (patch) | |
| tree | b2aafa6646ec79db030fe71f056cc64c8d326e0c | |
| parent | cb8d8dcf2b7a698e7a6c31f1e58722b505132f32 (diff) | |
improve os.match
| -rw-r--r-- | core/src/tbox/src/tbox/platform/directory.h | 4 | ||||
| -rw-r--r-- | core/src/tbox/src/tbox/platform/posix/directory.c | 10 | ||||
| -rw-r--r-- | core/src/tbox/src/tbox/platform/windows/directory.c | 10 | ||||
| -rw-r--r-- | core/src/xmake/os/find.c | 6 | ||||
| -rw-r--r-- | xmake/core/base/os.lua | 28 | ||||
| -rw-r--r-- | xmake/modules/detect/sdks/find_cuda.lua | 4 | ||||
| -rw-r--r-- | xmake/modules/detect/sdks/find_ndk.lua | 2 | ||||
| -rw-r--r-- | xmake/modules/detect/sdks/find_qt.lua | 4 | ||||
| -rw-r--r-- | xmake/modules/detect/sdks/find_wdk.lua | 6 |
9 files changed, 45 insertions, 29 deletions
diff --git a/core/src/tbox/src/tbox/platform/directory.h b/core/src/tbox/src/tbox/platform/directory.h index 9b6cb0771..d5e3b49b9 100644 --- a/core/src/tbox/src/tbox/platform/directory.h +++ b/core/src/tbox/src/tbox/platform/directory.h @@ -109,13 +109,13 @@ tb_size_t tb_directory_temporary(tb_char_t* path, tb_size_t maxn); /*! the directory walk * * @param path the directory path - * @param recursion is recursion? + * @param recursion the recursion level, 0, 1, 2 or -1 (infinite) * @param prefix is prefix recursion? directory is the first item * @param func the callback func * @param data the callback data * */ -tb_void_t tb_directory_walk(tb_char_t const* path, tb_bool_t recursion, tb_bool_t prefix, tb_directory_walk_func_t func, tb_cpointer_t priv); +tb_void_t tb_directory_walk(tb_char_t const* path, tb_long_t recursion, tb_bool_t prefix, tb_directory_walk_func_t func, tb_cpointer_t priv); /*! copy directory * diff --git a/core/src/tbox/src/tbox/platform/posix/directory.c b/core/src/tbox/src/tbox/platform/posix/directory.c index fc75b3cf3..fddbc428d 100644 --- a/core/src/tbox/src/tbox/platform/posix/directory.c +++ b/core/src/tbox/src/tbox/platform/posix/directory.c @@ -96,7 +96,7 @@ static tb_bool_t tb_directory_walk_copy(tb_char_t const* path, tb_file_info_t co // continue return tb_true; } -static tb_bool_t tb_directory_walk_impl(tb_char_t const* path, tb_bool_t recursion, tb_bool_t prefix, tb_directory_walk_func_t func, tb_cpointer_t priv) +static tb_bool_t tb_directory_walk_impl(tb_char_t const* path, tb_long_t recursion, tb_bool_t prefix, tb_directory_walk_func_t func, tb_cpointer_t priv) { // check tb_assert_and_check_return_val(path && func, tb_false); @@ -136,7 +136,7 @@ static tb_bool_t tb_directory_walk_impl(tb_char_t const* path, tb_bool_t recursi tb_check_break(ok); // walk to the next directory - if (info.type == TB_FILE_TYPE_DIRECTORY && recursion) ok = tb_directory_walk_impl(temp, recursion, prefix, func, priv); + if (info.type == TB_FILE_TYPE_DIRECTORY && recursion) ok = tb_directory_walk_impl(temp, recursion > 0? recursion - 1 : recursion, prefix, func, priv); tb_check_break(ok); // do callback @@ -203,7 +203,7 @@ tb_bool_t tb_directory_remove(tb_char_t const* path) tb_assert_and_check_return_val(path, tb_false); // walk remove - tb_directory_walk_impl(path, tb_true, tb_false, tb_directory_walk_remove, tb_null); + tb_directory_walk_impl(path, -1, tb_false, tb_directory_walk_remove, tb_null); // remove it return !remove(path)? tb_true : tb_false; @@ -253,7 +253,7 @@ tb_size_t tb_directory_temporary(tb_char_t* path, tb_size_t maxn) return tb_strlcpy(path, "/tmp", maxn); } #endif -tb_void_t tb_directory_walk(tb_char_t const* path, tb_bool_t recursion, tb_bool_t prefix, tb_directory_walk_func_t func, tb_cpointer_t priv) +tb_void_t tb_directory_walk(tb_char_t const* path, tb_long_t recursion, tb_bool_t prefix, tb_directory_walk_func_t func, tb_cpointer_t priv) { // check tb_assert_and_check_return(path && func); @@ -290,7 +290,7 @@ tb_bool_t tb_directory_copy(tb_char_t const* path, tb_char_t const* dest) tuple[0].cstr = dest; tuple[1].ul = tb_strlen(path); tuple[2].b = tb_true; - tb_directory_walk_impl(path, tb_true, tb_true, tb_directory_walk_copy, tuple); + tb_directory_walk_impl(path, -1, tb_true, tb_directory_walk_copy, tuple); // ok? tb_bool_t ok = tuple[2].b; diff --git a/core/src/tbox/src/tbox/platform/windows/directory.c b/core/src/tbox/src/tbox/platform/windows/directory.c index 7a67f3b88..3c7e96a0f 100644 --- a/core/src/tbox/src/tbox/platform/windows/directory.c +++ b/core/src/tbox/src/tbox/platform/windows/directory.c @@ -98,7 +98,7 @@ static tb_bool_t tb_directory_walk_copy(tb_char_t const* path, tb_file_info_t co // continue return tb_true; } -static tb_bool_t tb_directory_walk_impl(tb_wchar_t const* path, tb_bool_t recursion, tb_bool_t prefix, tb_directory_walk_func_t func, tb_cpointer_t priv) +static tb_bool_t tb_directory_walk_impl(tb_wchar_t const* path, tb_long_t recursion, tb_bool_t prefix, tb_directory_walk_func_t func, tb_cpointer_t priv) { // check tb_assert_and_check_return_val(path && func, tb_false); @@ -141,7 +141,7 @@ static tb_bool_t tb_directory_walk_impl(tb_wchar_t const* path, tb_bool_t recurs tb_check_break(ok); // walk to the next directory - if (info.type == TB_FILE_TYPE_DIRECTORY && recursion) ok = tb_directory_walk_impl(temp_w, recursion, prefix, func, priv); + if (info.type == TB_FILE_TYPE_DIRECTORY && recursion) ok = tb_directory_walk_impl(temp_w, recursion > 0? recursion - 1 : recursion, prefix, func, priv); tb_check_break(ok); // do callback @@ -212,7 +212,7 @@ tb_bool_t tb_directory_remove(tb_char_t const* path) if (!tb_path_absolute_w(path, full, TB_PATH_MAXN)) return tb_false; // walk remove - tb_directory_walk_impl(full, tb_true, tb_false, tb_directory_walk_remove, tb_null); + tb_directory_walk_impl(full, -1, tb_false, tb_directory_walk_remove, tb_null); // remove it return RemoveDirectoryW(full)? tb_true : tb_false; @@ -289,7 +289,7 @@ tb_size_t tb_directory_temporary(tb_char_t* path, tb_size_t maxn) // ok? return size != -1? size : 0; } -tb_void_t tb_directory_walk(tb_char_t const* path, tb_bool_t recursion, tb_bool_t prefix, tb_directory_walk_func_t func, tb_cpointer_t priv) +tb_void_t tb_directory_walk(tb_char_t const* path, tb_long_t recursion, tb_bool_t prefix, tb_directory_walk_func_t func, tb_cpointer_t priv) { // check tb_assert_and_check_return(path && func); @@ -327,7 +327,7 @@ tb_bool_t tb_directory_copy(tb_char_t const* path, tb_char_t const* dest) tuple[0].cstr = dest; tuple[1].ul = tb_strlen(path); tuple[2].b = tb_true; - tb_directory_walk(path, tb_true, tb_true, tb_directory_walk_copy, tuple); + tb_directory_walk(path, -1, tb_true, tb_directory_walk_copy, tuple); // ok? tb_bool_t ok = tuple[2].b; diff --git a/core/src/xmake/os/find.c b/core/src/xmake/os/find.c index 79f281f2b..194494f0b 100644 --- a/core/src/xmake/os/find.c +++ b/core/src/xmake/os/find.c @@ -180,8 +180,8 @@ tb_int_t xm_os_find(lua_State* lua) tb_char_t const* pattern = luaL_checkstring(lua, 2); tb_check_return_val(pattern, 0); - // is recurse? - tb_bool_t recurse = lua_toboolean(lua, 3); + // the recursion level + tb_long_t recursion = lua_tointeger(lua, 3); // the match mode tb_long_t mode = lua_tointeger(lua, 4); @@ -198,7 +198,7 @@ tb_int_t xm_os_find(lua_State* lua) tuple[1].cstr = pattern; tuple[2].l = mode; tuple[3].ul = 0; - tb_directory_walk(rootdir, recurse, tb_true, xm_os_find_walk, tuple); + tb_directory_walk(rootdir, recursion, tb_true, xm_os_find_walk, tuple); // pop string package lua_pop(lua, 1); diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index 32028b231..71a6119f6 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -265,14 +265,30 @@ function os.match(pattern, mode, callback) -- get the root directory local rootdir = pattern - local starpos = pattern:find("%*") - if starpos then - rootdir = rootdir:sub(1, starpos - 1) + local startpos = pattern:find("*", 1, true) + if startpos then + rootdir = rootdir:sub(1, startpos - 1) end rootdir = path.directory(rootdir) - -- is recurse? - local recurse = pattern:find("**", nil, true) + -- compute the recursion level + -- + -- infinite recursion: src/**.c + -- limit recursion level: src/*/*.c + local recursion = 0 + if pattern:find("**", 1, true) then + recursion = -1 + else + -- "src/*/*.c" -> "*/" -> recursion level: 1 + -- "src/*/main.c" -> "*/" -> recursion level: 1 + -- "src/*/subdir/main.c" -> "*//" -> recursion level: 2 + if startpos then + local _, seps = pattern:sub(startpos):gsub("[/\\]", "") + if seps > 0 then + recursion = seps + end + end + end -- convert pattern to a lua pattern pattern = pattern:gsub("([%+%.%-%^%$%(%)%%])", "%%%1") @@ -282,7 +298,7 @@ function os.match(pattern, mode, callback) pattern = pattern:gsub("\002", "[^/]*") -- find it - return os.find(rootdir, pattern, recurse, mode, excludes, callback) + return os.find(rootdir, pattern, recursion, mode, excludes, callback) end -- match directories diff --git a/xmake/modules/detect/sdks/find_cuda.lua b/xmake/modules/detect/sdks/find_cuda.lua index 16e66107f..1e657ddc1 100644 --- a/xmake/modules/detect/sdks/find_cuda.lua +++ b/xmake/modules/detect/sdks/find_cuda.lua @@ -31,11 +31,11 @@ function _find_cudadir() -- init the search directories local pathes = {} if os.host() == "macosx" then - table.insert(pathes, "/Developer/NVIDIA/CUDA**/bin") + table.insert(pathes, "/Developer/NVIDIA/CUDA*/bin") elseif os.host() == "windows" then table.insert(pathes, "$(env CUDA_PATH)/bin") else - table.insert(pathes, "/usr/local/cuda**/bin") + table.insert(pathes, "/usr/local/cuda*/bin") end -- attempt to find nvcc diff --git a/xmake/modules/detect/sdks/find_ndk.lua b/xmake/modules/detect/sdks/find_ndk.lua index 235466661..dfbc7454a 100644 --- a/xmake/modules/detect/sdks/find_ndk.lua +++ b/xmake/modules/detect/sdks/find_ndk.lua @@ -61,7 +61,7 @@ function main(ndkdir, opt) -- save the toolchains directory local toolchains = {} - for _, bindir in ipairs(os.dirs(path.join(ndkdir, "toolchains", cross .. "**", "prebuilt/*/bin"))) do + for _, bindir in ipairs(os.dirs(path.join(ndkdir, "toolchains", cross .. "*", "prebuilt/*/bin"))) do local binfiles = os.files(path.join(bindir, cross .. "*")) if binfiles and #binfiles > 0 then table.insert(toolchains, {bin = bindir, cross = cross}) diff --git a/xmake/modules/detect/sdks/find_qt.lua b/xmake/modules/detect/sdks/find_qt.lua index 8c4b02ee0..655b0a6e0 100644 --- a/xmake/modules/detect/sdks/find_qt.lua +++ b/xmake/modules/detect/sdks/find_qt.lua @@ -33,7 +33,7 @@ import("core.project.config") function _find_sdkdir(sdkdir, sdkver) -- init sub-directory - local subdir = sdkver or "**" + local subdir = sdkver or "*" -- append target sub-directory local targetdir = nil @@ -56,7 +56,7 @@ function _find_sdkdir(sdkdir, sdkver) if targetdir then subdir = path.join(subdir, targetdir) else - subdir = path.join(subdir, "**") + subdir = path.join(subdir, "*") end subdir = path.join(subdir, "bin") diff --git a/xmake/modules/detect/sdks/find_wdk.lua b/xmake/modules/detect/sdks/find_wdk.lua index dae209737..327099be3 100644 --- a/xmake/modules/detect/sdks/find_wdk.lua +++ b/xmake/modules/detect/sdks/find_wdk.lua @@ -48,7 +48,7 @@ function _find_umdfver(libdir, includedir) -- find versions local versions = {} - for _, p in ipairs(os.files(path.join(includedir, "wdf", "umdf", "**", "wdf.h"))) do + for _, p in ipairs(os.files(path.join(includedir, "wdf", "umdf", "*", "wdf.h"))) do table.insert(versions, path.filename(path.directory(p))) end @@ -68,7 +68,7 @@ function _find_kmdfver(libdir, includedir) -- find versions local versions = {} - for _, p in ipairs(os.files(path.join(includedir, "wdf", "kmdf", "**", "wdf.h"))) do + for _, p in ipairs(os.files(path.join(includedir, "wdf", "kmdf", "*", "wdf.h"))) do table.insert(versions, path.filename(path.directory(p))) end @@ -95,7 +95,7 @@ function _find_wdk(sdkdir, sdkver) -- get sdk version if not sdkver then local vers = {} - for _, dir in ipairs(os.dirs(path.join(sdkdir, "Include", "**", "km"))) do + for _, dir in ipairs(os.dirs(path.join(sdkdir, "Include", "*", "km"))) do table.insert(vers, path.filename(path.directory(dir))) end for _, ver in ipairs(vers) do |
