Skip to content

Commit f01b067

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

31 files changed

+243
-197
lines changed

+stdlib/windows_shortname.m

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,23 @@
1313
end
1414

1515
s = "";
16+
if stdlib.is_url(p), return, end
1617

17-
if ~ispc || stdlib.is_url(p)
18-
return
19-
end
18+
s = p;
19+
if ispc
20+
fso = actxserver('Scripting.FileSystemObject');
2021

21-
fso = actxserver('Scripting.FileSystemObject');
22+
if isfolder(p)
23+
s = fso.GetFolder(p).ShortPath;
24+
elseif isfile(p)
25+
s = fso.GetFile(p).ShortPath;
26+
end
2227

23-
if isfolder(p)
24-
s = fso.GetFolder(p).ShortPath;
25-
elseif isfile(p)
26-
s = fso.GetFile(p).ShortPath;
28+
delete(fso);
2729
end
2830

2931
s = string(s);
3032

31-
delete(fso);
3233

3334
end
3435

buildfile.m

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +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-
if ispc
90-
win(end+1) = "src/windows.cpp";
91-
end
89+
win = [pure, "src/windows.cpp"];
9290

9391
mac = "src/macos.cpp";
9492
linux = "src/linux_fs.cpp";
@@ -103,6 +101,7 @@ function publishTask(context)
103101
"src/set_permissions.cpp", ...
104102
["src/is_rosetta.cpp", mac], ...
105103
["src/windows_shortname.cpp", win], ...
104+
["src/drop_slash.cpp", normal], ...
106105
};
107106

108107
%% 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: 2 additions & 2 deletions
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 {
@@ -21,7 +21,7 @@ class MexFunction : public matlab::mex::Function {
2121

2222
if (inputs.size() != 2) {
2323
matlabEng->feval(u"error", 0,
24-
std::vector<matlab::data::Array>({ factory.createScalar("Two inputs required") }));
24+
std::vector<matlab::data::Array>({ factory.createScalar("Mex: Two inputs required") }));
2525
}
2626

2727
if ((inputs[0].getType() == matlab::data::ArrayType::MATLAB_STRING && inputs[0].getNumberOfElements() == 1)){

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: 3 additions & 24 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

@@ -70,7 +49,7 @@ class MexFunction : public matlab::mex::Function {
7049

7150
if (inputs.size() != 1) {
7251
matlabEng->feval(u"error", 0,
73-
std::vector<matlab::data::Array>({ factory.createScalar("One input required") }));
52+
std::vector<matlab::data::Array>({ factory.createScalar("Mex: One input required") }));
7453
}
7554
if ((inputs[0].getType() == matlab::data::ArrayType::MATLAB_STRING && inputs[0].getNumberOfElements() == 1)){
7655
matlab::data::TypedArray<matlab::data::MATLABString> stringArr = inputs[0];

0 commit comments

Comments
 (0)