summaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
authorruki <[email protected]>2022-03-09 18:59:46 +0800
committerGitHub <[email protected]>2022-03-09 18:59:46 +0800
commit50c63bf3d9d04a358600d6ec66cdcc74d48ad492 (patch)
tree4dbe07ba15c4f2119b6bcded86b5bd1ef38085db /core/src
parent96a0a4860880a87ac9f6503df28a4c0bcb291bf1 (diff)
parent8485c3591a80f2b53610adcc2efae9a9e1bf5599 (diff)
Merge pull request #2137 from xmake-io/path
Improve path
Diffstat (limited to 'core/src')
m---------core/src/tbox/tbox0
-rw-r--r--core/src/xmake/engine.c2
-rw-r--r--core/src/xmake/makefile1
-rw-r--r--core/src/xmake/path/absolute.c4
-rw-r--r--core/src/xmake/path/directory.c51
-rw-r--r--core/src/xmake/path/relative.c2
-rw-r--r--core/src/xmake/path/translate.c19
7 files changed, 69 insertions, 10 deletions
diff --git a/core/src/tbox/tbox b/core/src/tbox/tbox
-Subproject cfb72e0c568537c61f672e492013af0d96cb9ae
+Subproject 19a1e565216e9797c3c145d2db176d54c27991c
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c
index efc89977c..2f4efd60c 100644
--- a/core/src/xmake/engine.c
+++ b/core/src/xmake/engine.c
@@ -173,6 +173,7 @@ tb_int_t xm_io_poller_wait(lua_State* lua);
tb_int_t xm_path_relative(lua_State* lua);
tb_int_t xm_path_absolute(lua_State* lua);
tb_int_t xm_path_translate(lua_State* lua);
+tb_int_t xm_path_directory(lua_State* lua);
tb_int_t xm_path_is_absolute(lua_State* lua);
// the hash functions
@@ -366,6 +367,7 @@ static luaL_Reg const g_path_functions[] =
{ "relative", xm_path_relative }
, { "absolute", xm_path_absolute }
, { "translate", xm_path_translate }
+, { "directory", xm_path_directory }
, { "is_absolute", xm_path_is_absolute }
, { tb_null, tb_null }
};
diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile
index c85696f1e..d25a12a9b 100644
--- a/core/src/xmake/makefile
+++ b/core/src/xmake/makefile
@@ -92,6 +92,7 @@ xmake_C_FILES += \
path/relative \
path/absolute \
path/translate \
+ path/directory \
path/is_absolute \
hash/uuid4 \
hash/sha256 \
diff --git a/core/src/xmake/path/absolute.c b/core/src/xmake/path/absolute.c
index b4baac03d..d8489d25f 100644
--- a/core/src/xmake/path/absolute.c
+++ b/core/src/xmake/path/absolute.c
@@ -45,10 +45,8 @@ tb_int_t xm_path_absolute(lua_State* lua)
// get the root
tb_char_t const* root = luaL_optstring(lua, 2, tb_null);
- // done path:absolute(root)
+ // do path:absolute(root)
tb_char_t data[TB_PATH_MAXN];
lua_pushstring(lua, tb_path_absolute_to(root, path, data, sizeof(data) - 1));
-
- // ok
return 1;
}
diff --git a/core/src/xmake/path/directory.c b/core/src/xmake/path/directory.c
new file mode 100644
index 000000000..b8ec7476d
--- /dev/null
+++ b/core/src/xmake/path/directory.c
@@ -0,0 +1,51 @@
+/*!A cross-platform build utility based on Lua
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Copyright (C) 2015-present, TBOOX Open Source Group.
+ *
+ * @author ruki
+ * @file directory.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "directory"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+tb_int_t xm_path_directory(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ // get the path
+ tb_char_t const* path = luaL_checkstring(lua, 1);
+ tb_check_return_val(path, 0);
+
+ // do path:directory()
+ tb_char_t data[TB_PATH_MAXN];
+ tb_char_t const* dir = tb_path_directory(path, data, sizeof(data));
+ if (dir) lua_pushstring(lua, dir);
+ else lua_pushnil(lua);
+ return 1;
+}
diff --git a/core/src/xmake/path/relative.c b/core/src/xmake/path/relative.c
index 782f0f635..f64667747 100644
--- a/core/src/xmake/path/relative.c
+++ b/core/src/xmake/path/relative.c
@@ -48,7 +48,5 @@ tb_int_t xm_path_relative(lua_State* lua)
// done path:relative(root)
tb_char_t data[TB_PATH_MAXN];
lua_pushstring(lua, tb_path_relative_to(root, path, data, sizeof(data) - 1));
-
- // ok
return 1;
}
diff --git a/core/src/xmake/path/translate.c b/core/src/xmake/path/translate.c
index 4a9733781..87059c85c 100644
--- a/core/src/xmake/path/translate.c
+++ b/core/src/xmake/path/translate.c
@@ -39,15 +39,24 @@ tb_int_t xm_path_translate(lua_State* lua)
tb_assert_and_check_return_val(lua, 0);
// get the path
- tb_char_t const* path = luaL_checkstring(lua, 1);
+ size_t path_size = 0;
+ tb_char_t const* path = luaL_checklstring(lua, 1, &path_size);
tb_check_return_val(path, 0);
- // copy path
- tb_char_t data[TB_PATH_MAXN];
- tb_strlcpy(data, path, sizeof(data));
+ // get the option argument, e.g. {reduce_dot2 = true}
+ tb_bool_t reduce_dot2 = tb_false;
+ if (lua_istable(lua, 2))
+ {
+ lua_pushstring(lua, "reduce_dot2");
+ lua_gettable(lua, 2);
+ if (lua_toboolean(lua, -1))
+ reduce_dot2 = tb_true;
+ lua_pop(lua, 1);
+ }
// do path:translate()
- tb_size_t size = tb_path_translate(data, 0, sizeof(data) - 1);
+ tb_char_t data[TB_PATH_MAXN];
+ tb_size_t size = tb_path_translate_to(path, (tb_size_t)path_size, data, sizeof(data), reduce_dot2);
if (size) lua_pushlstring(lua, data, (size_t)size);
else lua_pushnil(lua);
return 1;