Skip to content

Commit a990281

Browse files
committed
mex: disk_available
1 parent a97a659 commit a990281

File tree

5 files changed

+67
-1
lines changed

5 files changed

+67
-1
lines changed

+stdlib/disk_available.m

Lines changed: 1 addition & 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
%

buildfile.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ 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", ...
105106
["src/is_wsl.cpp", linux], ...
106107
"src/set_permissions.cpp", ...
107108
["src/is_rosetta.cpp", mac], ...

octave_build.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ function octave_build(overwrite)
1111

1212
%% specific source
1313
srcs = {
14+
fullfile(d, "disk_available.cpp"), ...
1415
fullfile(d, "drop_slash.cpp"), ...
1516
fullfile(d, "is_wsl.cpp"), ...
1617
fullfile(d, "is_rosetta.cpp"), ...

src/disk_available.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
public:
17+
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
18+
// boilerplate engine & ArrayFactory setup
19+
std::shared_ptr<matlab::engine::MATLABEngine> matlabEng = getEngine();
20+
21+
matlab::data::ArrayFactory factory;
22+
// wrangle inputs
23+
std::string in;
24+
25+
if (inputs.size() != 1) {
26+
matlabEng->feval(u"error", 0,
27+
std::vector<matlab::data::Array>({ factory.createScalar("Mex: One input required") }));
28+
}
29+
if ((inputs[0].getType() == matlab::data::ArrayType::MATLAB_STRING && inputs[0].getNumberOfElements() == 1)){
30+
matlab::data::TypedArray<matlab::data::MATLABString> stringArr = inputs[0];
31+
in = stringArr[0];
32+
} else if (inputs[0].getType() == matlab::data::ArrayType::CHAR){
33+
matlab::data::CharArray charArr = inputs[0];
34+
in.assign(charArr.begin(), charArr.end());
35+
} else {
36+
matlabEng->feval(u"error", 0,
37+
std::vector<matlab::data::Array>({ factory.createScalar("Mex: First input must be a scalar string or char vector") }));
38+
}
39+
40+
// actual function algorithm / computation
41+
std::uintmax_t s = std::filesystem::space(in).available;
42+
43+
outputs[0] = factory.createScalar(s);
44+
}
45+
};

src/octave/disk_available.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <octave/oct.h>
2+
3+
#include <string>
4+
5+
#include <filesystem>
6+
7+
8+
DEFUN_DLD (disk_available, args, nargout,
9+
"disk space available to user")
10+
{
11+
if (args.length() != 1){
12+
octave_stdout << "Oct: One input required\n";
13+
return octave_value(0);
14+
}
15+
16+
std::uintmax_t s = std::filesystem::space(args(0).string_value()).available;
17+
18+
return octave_value(s);
19+
}

0 commit comments

Comments
 (0)