Skip to content

Commit

Permalink
使用optional代替expected
Browse files Browse the repository at this point in the history
  • Loading branch information
actboy168 committed Apr 25, 2024
1 parent 628dc91 commit 069f745
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 34 deletions.
8 changes: 4 additions & 4 deletions bee/sys/path.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#pragma once

#include <bee/nonstd/expected.h>
#include <bee/nonstd/filesystem.h>

#include <optional>

namespace bee::sys {
using path_expected = expected<fs::path, std::string>;
path_expected exe_path() noexcept;
path_expected dll_path() noexcept;
std::optional<fs::path> exe_path() noexcept;
std::optional<fs::path> dll_path() noexcept;
}
16 changes: 8 additions & 8 deletions bee/sys/path_bsd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,38 @@

namespace bee::sys {
#if defined(__FreeBSD__)
path_expected exe_path() noexcept {
std::optional<fs::path> exe_path() noexcept {
int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
char exe[1024];
size_t length = 1024;
int error = sysctl(name, 4, exe, &length, NULL, 0);
if (error < 0 || length <= 1) {
return unexpected<std::string>(error::sys_errmsg("exe_path"));
return std::nullopt;
}
return fs::path(exe, exe + length - 1);
}
#elif defined(__OpenBSD__)
path_expected exe_path() noexcept {
std::optional<fs::path> exe_path() noexcept {
int name[] = { CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV };
size_t argc;
if (sysctl(name, 4, NULL, &argc, NULL, 0) < 0) {
return unexpected<std::string>(error::sys_errmsg("exe_path"));
return std::nullopt;
}
const char** argv = (const char**)malloc(argc);
if (!argv) {
return unexpected<std::string>(error::sys_errmsg("exe_path"));
return std::nullopt;
}
if (sysctl(name, 4, argv, &argc, NULL, 0) < 0) {
return unexpected<std::string>(error::sys_errmsg("exe_path"));
return std::nullopt;
}
return fs::path(argv[0]);
}
#else
path_expected exe_path() noexcept {
std::optional<fs::path> exe_path() noexcept {
std::error_code ec;
auto res = fs::read_symlink("/proc/self/exe", ec);
if (ec) {
return unexpected<std::string>(error::sys_errmsg("exe_path"));
return std::nullopt;
}
return res;
}
Expand Down
4 changes: 2 additions & 2 deletions bee/sys/path_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
#include <unistd.h>

namespace bee::sys {
path_expected exe_path() noexcept {
std::optional<fs::path> exe_path() noexcept {
std::error_code ec;
auto res = fs::read_symlink("/proc/self/exe", ec);
if (ec) {
return unexpected<std::string>(error::sys_errmsg("exe_path"));
return std::nullopt;
}
return res;
}
Expand Down
12 changes: 5 additions & 7 deletions bee/sys/path_osx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@
#include <bee/utility/dynarray.h>
#include <mach-o/dyld.h>

#include <cassert>

namespace bee::sys {
path_expected exe_path() noexcept {
std::optional<fs::path> exe_path() noexcept {
uint32_t path_len = 0;
_NSGetExecutablePath(0, &path_len);
if (path_len <= 1) {
return unexpected<std::string>("_NSGetExecutablePath failed.");
}
assert(path_len > 1);
dynarray<char> buf(path_len);
int rv = _NSGetExecutablePath(buf.data(), &path_len);
if (rv != 0) {
return unexpected<std::string>("_NSGetExecutablePath failed.");
}
assert(rv == 0);
return fs::path(buf.data(), buf.data() + path_len - 1);
}
}
9 changes: 5 additions & 4 deletions bee/sys/path_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@
#include <dlfcn.h>

namespace bee::sys {
static path_expected dll_path(void* module_handle) noexcept {
static std::optional<fs::path> dll_path(void* module_handle) noexcept {
::Dl_info dl_info;
dl_info.dli_fname = 0;
const int ret = ::dladdr(module_handle, &dl_info);
if (0 != ret && dl_info.dli_fname != NULL) {
std::error_code ec;
auto path = fs::absolute(dl_info.dli_fname, ec);
if (ec) {
return unexpected<std::string>(ec.message());
return std::nullopt;
}
return path.lexically_normal();
}
return unexpected<std::string>("::dladdr failed.");
errno = EFAULT;
return std::nullopt;
}

path_expected dll_path() noexcept {
std::optional<fs::path> dll_path() noexcept {
return dll_path((void*)&exe_path);
}
}
13 changes: 7 additions & 6 deletions bee/sys/path_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
extern "C" IMAGE_DOS_HEADER __ImageBase;

namespace bee::sys {
static path_expected dll_path(HMODULE module_handle) noexcept {
static std::optional<fs::path> dll_path(HMODULE module_handle) noexcept {
wchar_t buffer[MAX_PATH];
DWORD path_len = ::GetModuleFileNameW(module_handle, buffer, _countof(buffer));
if (path_len == 0) {
return unexpected<std::string>(error::sys_errmsg("GetModuleFileNameW"));
return std::nullopt;
}
if (path_len < _countof(buffer)) {
return fs::path(buffer, buffer + path_len);
Expand All @@ -21,20 +21,21 @@ namespace bee::sys {
dynarray<wchar_t> buf(buf_len);
path_len = ::GetModuleFileNameW(module_handle, buf.data(), buf_len);
if (path_len == 0) {
return unexpected<std::string>(error::sys_errmsg("GetModuleFileNameW"));
return std::nullopt;
}
if (path_len < _countof(buffer)) {
return fs::path(buf.data(), buf.data() + path_len);
}
}
return unexpected<std::string>("::GetModuleFileNameW return too long.");
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
return std::nullopt;
}

path_expected exe_path() noexcept {
std::optional<fs::path> exe_path() noexcept {
return dll_path(NULL);
}

path_expected dll_path() noexcept {
std::optional<fs::path> dll_path() noexcept {
return dll_path(reinterpret_cast<HMODULE>(&__ImageBase));
}
}
4 changes: 2 additions & 2 deletions binding/lua_filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ namespace bee::lua_filesystem {
static int exe_path(lua_State* L) {
auto r = sys::exe_path();
if (!r) {
return lua::push_error(L, r.error());
return lua::push_error(L, error::sys_errmsg("exe_path"));
}
lua::newudata<fs::path>(L, r.value());
return 1;
Expand All @@ -937,7 +937,7 @@ namespace bee::lua_filesystem {
static int dll_path(lua_State* L) {
auto r = sys::dll_path();
if (!r) {
return lua::push_error(L, r.error());
return lua::push_error(L, error::sys_errmsg("dll_path"));
}
lua::newudata<fs::path>(L, r.value());
return 1;
Expand Down
3 changes: 2 additions & 1 deletion bootstrap/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# include <bee/win/wtf8.h>
#endif

#include <bee/error.h>
#include <bee/lua/module.h>
#include <bee/nonstd/filesystem.h>
#include <bee/nonstd/unreachable.h>
Expand Down Expand Up @@ -138,7 +139,7 @@ static int pushargs(lua_State *L) {
static fs::path getprogdir(lua_State *L) {
auto r = bee::sys::exe_path();
if (!r) {
luaL_error(L, "unable to get progdir: %s\n", r.error().c_str());
luaL_error(L, "unable to get progdir: %s\n", bee::error::sys_errmsg("exe_path").c_str());
std::unreachable();
}
return r.value().remove_filename();
Expand Down

0 comments on commit 069f745

Please sign in to comment.