Skip to content

Commit 66943a8

Browse files
committed
mex: disk_capacity
1 parent a990281 commit 66943a8

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-1
lines changed

+stdlib/disk_capacity.m

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

buildfile.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ function publishTask(context)
103103
"src/relative_to.cpp", ...
104104
"src/proximate_to.cpp", ...
105105
"src/disk_available.cpp", ...
106+
"src/disk_capacity.cpp", ...
106107
["src/is_wsl.cpp", linux], ...
107108
"src/set_permissions.cpp", ...
108109
["src/is_rosetta.cpp", mac], ...

octave_build.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ function octave_build(overwrite)
1212
%% specific source
1313
srcs = {
1414
fullfile(d, "disk_available.cpp"), ...
15+
fullfile(d, "disk_capacity.cpp"), ...
1516
fullfile(d, "drop_slash.cpp"), ...
1617
fullfile(d, "is_wsl.cpp"), ...
1718
fullfile(d, "is_rosetta.cpp"), ...

src/disk_capacity.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).capacity;
42+
43+
outputs[0] = factory.createScalar(s);
44+
}
45+
};

src/octave/disk_capacity.cpp

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

0 commit comments

Comments
 (0)