Skip to content

Commit d30c199

Browse files
committed
mex: disk_available
mex: disk_capacity mex: inline
1 parent a97a659 commit d30c199

25 files changed

+276
-281
lines changed

+stdlib/disk_available.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
%% DISK_AVAILABLE disk available space (bytes)
2-
% requires: java
2+
% requires: mex or java
33
%
44
% example: stdlib.disk_available('/')
55
%
@@ -12,6 +12,8 @@
1212

1313
f = javaFileObject(d).getUsableSpace();
1414

15+
f = uint64(f);
16+
1517
end
1618

1719
%!assert (disk_available('.') > 0)

+stdlib/disk_capacity.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
%% DISK_CAPACITY disk total capacity (bytes)
2-
% requires: java
2+
% requires: mex or java
33
%
44
% example: stdlib.disk_capacity('/')
55

@@ -10,6 +10,8 @@
1010

1111
f = javaFileObject(d).getTotalSpace();
1212

13+
f = uint64(f);
14+
1315
end
1416

1517
%!assert (disk_capacity('.') > 0)

buildfile.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ function publishTask(context)
102102
["src/parent.cpp", "src/parent_fs.cpp", normal], ...
103103
"src/relative_to.cpp", ...
104104
"src/proximate_to.cpp", ...
105+
"src/disk_available.cpp", ...
106+
"src/disk_capacity.cpp", ...
105107
["src/is_wsl.cpp", linux], ...
106108
"src/set_permissions.cpp", ...
107109
["src/is_rosetta.cpp", mac], ...

octave_build.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ function octave_build(overwrite)
1111

1212
%% specific source
1313
srcs = {
14+
fullfile(d, "disk_available.cpp"), ...
15+
fullfile(d, "disk_capacity.cpp"), ...
1416
fullfile(d, "drop_slash.cpp"), ...
1517
fullfile(d, "is_wsl.cpp"), ...
1618
fullfile(d, "is_rosetta.cpp"), ...

src/create_symlink.cpp

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,49 +10,22 @@
1010

1111

1212
class MexFunction : public matlab::mex::Function {
13+
private:
14+
#include "mex2string.inl"
15+
1316
public:
1417
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
15-
// boilerplate engine & ArrayFactory setup
16-
std::shared_ptr<matlab::engine::MATLABEngine> matlabEng = getEngine();
1718

1819
matlab::data::ArrayFactory factory;
19-
// wrangle inputs
20+
2021
std::string target, link;
2122

22-
if (inputs.size() != 2) {
23-
matlabEng->feval(u"error", 0,
24-
std::vector<matlab::data::Array>({ factory.createScalar("Mex: Two inputs required") }));
25-
}
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-
target = stringArr[0];
30-
} else if (inputs[0].getType() == matlab::data::ArrayType::CHAR){
31-
matlab::data::CharArray charArr = inputs[0];
32-
target.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-
if ((inputs[1].getType() == matlab::data::ArrayType::MATLAB_STRING && inputs[1].getNumberOfElements() == 1)){
39-
matlab::data::TypedArray<matlab::data::MATLABString> stringArr = inputs[1];
40-
link = stringArr[0];
41-
} else if (inputs[1].getType() == matlab::data::ArrayType::CHAR){
42-
matlab::data::CharArray charArr = inputs[1];
43-
link.assign(charArr.begin(), charArr.end());
44-
} else {
45-
matlabEng->feval(u"error", 0,
46-
std::vector<matlab::data::Array>({ factory.createScalar("Mex: Second input must be a scalar string or char vector") }));
47-
}
48-
49-
// actual function algorithm / computation
50-
bool y = fs_create_symlink(target, link);
23+
matlab_2string(inputs, &target, &link);
5124

5225
// convert to Matlab output
5326
// https://www.mathworks.com/help/matlab/matlab_external/create-matlab-array-with-matlab-data-cpp-api.html
5427
// https://www.mathworks.com/help/matlab/apiref/matlab.data.arrayfactory.html
5528

56-
outputs[0] = factory.createScalar<bool>(y);
29+
outputs[0] = factory.createScalar<bool>(fs_create_symlink(target, link));
5730
}
5831
};

src/disk_available.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "mex.hpp"
2+
#include "mexAdapter.hpp"
3+
4+
#include <string>
5+
#include <string_view>
6+
7+
#include <vector>
8+
#include <memory>
9+
10+
#include <cstdint>
11+
#include <filesystem>
12+
#include <system_error>
13+
14+
15+
class MexFunction : public matlab::mex::Function {
16+
private:
17+
#include "mex1string.inl"
18+
19+
public:
20+
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
21+
22+
matlab::data::ArrayFactory factory;
23+
24+
std::error_code ec;
25+
std::uintmax_t s = std::filesystem::space(matlab_1string_input(inputs), ec).available;
26+
27+
if (ec)
28+
s = 0;
29+
30+
outputs[0] = factory.createScalar(s);
31+
}
32+
};

src/disk_capacity.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "mex.hpp"
2+
#include "mexAdapter.hpp"
3+
4+
#include <string>
5+
#include <string_view>
6+
7+
#include <vector>
8+
#include <memory>
9+
10+
#include <cstdint>
11+
#include <filesystem>
12+
#include <system_error>
13+
14+
15+
class MexFunction : public matlab::mex::Function {
16+
private:
17+
#include "mex1string.inl"
18+
19+
public:
20+
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
21+
22+
matlab::data::ArrayFactory factory;
23+
24+
std::error_code ec;
25+
std::uintmax_t s = std::filesystem::space(matlab_1string_input(inputs), ec).capacity;
26+
27+
if (ec)
28+
s = 0;
29+
30+
outputs[0] = factory.createScalar(s);
31+
}
32+
};

src/drop_slash.cpp

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,14 @@
1010

1111

1212
class MexFunction : public matlab::mex::Function {
13+
private:
14+
#include "mex1string.inl"
15+
1316
public:
1417
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
15-
// boilerplate engine & ArrayFactory setup
16-
std::shared_ptr<matlab::engine::MATLABEngine> matlabEng = getEngine();
1718

1819
matlab::data::ArrayFactory factory;
1920

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);
21+
outputs[0] = factory.createScalar(fs_drop_slash(matlab_1string_input(inputs)));
4222
}
4323
};

src/is_admin.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,15 @@
88

99

1010
class MexFunction : public matlab::mex::Function {
11+
private:
12+
#include "mex0.inl"
13+
1114
public:
1215
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
13-
// boilerplate engine & ArrayFactory setup
14-
std::shared_ptr<matlab::engine::MATLABEngine> matlabEng = getEngine();
15-
matlab::data::ArrayFactory factory;
1616

17-
if (inputs.size() != 0) {
18-
matlabEng->feval(u"error", 0,
19-
std::vector<matlab::data::Array>({ factory.createScalar("Mex: No input required") }));
20-
}
17+
matlab_0_input(inputs);
2118

22-
// actual function algorithm / computation
23-
bool y = fs_is_admin();
24-
25-
outputs[0] = factory.createScalar<bool>(y);
19+
matlab::data::ArrayFactory factory;
20+
outputs[0] = factory.createScalar<bool>(fs_is_admin());
2621
}
2722
};

src/is_char_device.cpp

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -38,38 +38,14 @@ static bool fs_is_char_device(std::string_view path)
3838

3939

4040
class MexFunction : public matlab::mex::Function {
41-
public:
42-
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
43-
// boilerplate engine & ArrayFactory setup
44-
std::shared_ptr<matlab::engine::MATLABEngine> matlabEng = getEngine();
41+
private:
42+
#include "mex1string.inl"
4543

44+
public:
45+
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs)
46+
{
4647
matlab::data::ArrayFactory factory;
47-
// wrangle inputs
48-
std::string in;
49-
50-
if (inputs.size() != 1) {
51-
matlabEng->feval(u"error", 0,
52-
std::vector<matlab::data::Array>({ factory.createScalar("Mex: One input required") }));
53-
}
54-
if ((inputs[0].getType() == matlab::data::ArrayType::MATLAB_STRING && inputs[0].getNumberOfElements() == 1)){
55-
matlab::data::TypedArray<matlab::data::MATLABString> stringArr = inputs[0];
56-
in = stringArr[0];
57-
} else if (inputs[0].getType() == matlab::data::ArrayType::CHAR){
58-
matlab::data::CharArray charArr = inputs[0];
59-
in.assign(charArr.begin(), charArr.end());
60-
} else {
61-
matlabEng->feval(u"error", 0,
62-
std::vector<matlab::data::Array>({ factory.createScalar("Mex: First input must be a scalar string or char vector") }));
63-
}
64-
65-
// actual function algorithm / computation
66-
bool y = fs_is_char_device(in);
67-
68-
// convert to Matlab output
69-
// https://www.mathworks.com/help/matlab/matlab_external/create-matlab-array-with-matlab-data-cpp-api.html
70-
// https://www.mathworks.com/help/matlab/apiref/matlab.data.arrayfactory.html
71-
72-
outputs[0] = factory.createScalar<bool>(y);
7348

49+
outputs[0] = factory.createScalar<bool>(fs_is_char_device(matlab_1string_input(inputs)));
7450
}
7551
};

0 commit comments

Comments
 (0)