Skip to content

Commit d4a0353

Browse files
committed
mex: drop_slash(), is_url()
mex: require <filesystem> mex: consolidate headers
1 parent 959ace5 commit d4a0353

29 files changed

+203
-165
lines changed

buildfile.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ function publishTask(context)
8383

8484
function srcs = get_mex_sources()
8585

86-
limits = "src/limits_fs.cpp";
86+
pure = "src/pure.cpp";
87+
normal = ["src/normalize.cpp", pure];
8788

88-
win = limits;
89+
win = pure;
8990
if ispc
9091
win(end+1) = "src/windows.cpp";
9192
end
@@ -103,6 +104,7 @@ function publishTask(context)
103104
"src/set_permissions.cpp", ...
104105
["src/is_rosetta.cpp", mac], ...
105106
["src/windows_shortname.cpp", win], ...
107+
["src/drop_slash.cpp", normal], ...
106108
};
107109

108110
%% new in R2024b

octave_build.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
%% specific source
1111
srcs = {
12+
fullfile(d, "drop_slash.cpp"), ...
1213
fullfile(d, "is_wsl.cpp"), ...
1314
fullfile(d, "is_rosetta.cpp"), ...
1415
fullfile(d, "is_admin.cpp"), ...

src/admin_fs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <sys/types.h> // geteuid, pid_t
77
#endif
88

9-
#include "admin_fs.h"
9+
#include "ffilesystem.h"
1010

1111

1212
bool fs_is_admin(){

src/admin_fs.h

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/create_symlink.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <vector>
77
#include <memory>
88

9-
#include "symlink_fs.h"
9+
#include "ffilesystem.h"
1010

1111

1212
class MexFunction : public matlab::mex::Function {

src/drop_slash.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "mex.hpp"
2+
#include "mexAdapter.hpp"
3+
4+
#include <string>
5+
6+
#include <vector>
7+
#include <memory>
8+
9+
#include "ffilesystem.h"
10+
11+
12+
class MexFunction : public matlab::mex::Function {
13+
public:
14+
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
15+
// boilerplate engine & ArrayFactory setup
16+
std::shared_ptr<matlab::engine::MATLABEngine> matlabEng = getEngine();
17+
18+
matlab::data::ArrayFactory factory;
19+
20+
// wrangle Inputs
21+
std::string in;
22+
23+
if (inputs.size() != 1) {
24+
matlabEng->feval(u"error", 0,
25+
std::vector<matlab::data::Array>({ factory.createScalar("Mex: One input required") }));
26+
}
27+
if ((inputs[0].getType() == matlab::data::ArrayType::MATLAB_STRING && inputs[0].getNumberOfElements() == 1)){
28+
matlab::data::TypedArray<matlab::data::MATLABString> stringArr = inputs[0];
29+
in = stringArr[0];
30+
} else if (inputs[0].getType() == matlab::data::ArrayType::CHAR){
31+
matlab::data::CharArray charArr = inputs[0];
32+
in.assign(charArr.begin(), charArr.end());
33+
} else {
34+
matlabEng->feval(u"error", 0,
35+
std::vector<matlab::data::Array>({ factory.createScalar("Mex: First input must be a scalar string or char vector") }));
36+
}
37+
38+
// actual function algorithm / computation
39+
std::string out = fs_drop_slash(in);
40+
41+
outputs[0] = factory.createScalar(out);
42+
}
43+
};

src/ffilesystem.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <string>
2+
#include <string_view>
3+
#include <cstddef>
4+
5+
bool fs_is_windows();
6+
std::size_t fs_get_max_path();
7+
std::string fs_as_posix(std::string_view);
8+
std::string fs_drop_slash(std::string_view);
9+
std::string fs_as_posix(std::string_view);
10+
11+
bool fs_is_url(std::string_view);
12+
int fs_is_wsl();
13+
bool fs_is_rosetta();
14+
bool fs_is_admin();
15+
bool fs_is_symlink(std::string_view);
16+
bool fs_create_symlink(std::string_view, std::string_view);
17+
std::string fs_read_symlink(std::string_view);
18+
19+
bool fs_win32_is_symlink(std::string_view);
20+
std::string fs_shortname(std::string_view);

src/is_admin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <vector>
55
#include <memory>
66

7-
#include "admin_fs.h"
7+
#include "ffilesystem.h"
88

99

1010
class MexFunction : public matlab::mex::Function {

src/is_char_device.cpp

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
// C++ Matlab MEX function using C++17 <filesystem>
2-
// build examples:
3-
// * MSVC: mex COMPFLAGS="/EHsc /std:c++17" is_char_device.cpp
4-
// * GCC: mex CXXFLAGS="-std=c++17" is_char_device.cpp
5-
6-
// https://www.mathworks.com/help/matlab/matlab_external/data-types-for-passing-mex-function-data-1.html
7-
81
#include "mex.hpp"
92
#include "mexAdapter.hpp"
103

@@ -17,22 +10,11 @@
1710
#if defined(_MSC_VER)
1811
# define WIN32_LEAN_AND_MEAN
1912
# include <windows.h>
20-
#elif __has_include(<filesystem>)
13+
#else
2114
# include <filesystem>
2215
# include <system_error>
2316
#endif
2417

25-
#include <sys/types.h>
26-
#include <sys/stat.h>
27-
28-
static int fs_st_mode(std::string_view path)
29-
{
30-
// https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/stat-functions
31-
struct stat s;
32-
33-
return stat(path.data(), &s) == 0 ? s.st_mode : 0;
34-
}
35-
3618

3719
static bool fs_is_char_device(std::string_view path)
3820
{
@@ -48,12 +30,9 @@ static bool fs_is_char_device(std::string_view path)
4830
const DWORD type = GetFileType(h);
4931
CloseHandle(h);
5032
return type == FILE_TYPE_CHAR;
51-
#elif defined(__cpp_lib_filesystem)
33+
#else
5234
std::error_code ec;
5335
return std::filesystem::is_character_file(path, ec) && !ec;
54-
#else
55-
// Windows: https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fstat-fstat32-fstat64-fstati64-fstat32i64-fstat64i32
56-
return S_ISCHR(fs_st_mode(path));
5736
#endif
5837
}
5938

src/is_rosetta.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <vector>
55
#include <memory>
66

7-
#include "macos_fs.h"
7+
#include "ffilesystem.h"
88

99

1010
class MexFunction : public matlab::mex::Function {

0 commit comments

Comments
 (0)