|
| 1 | +#include "mex.hpp" |
| 2 | +#include "mexAdapter.hpp" |
| 3 | + |
1 | 4 | #include <string> |
2 | | -#include <string_view> |
3 | | -#include <algorithm> // std::unique |
4 | 5 |
|
5 | | -#include <filesystem> |
| 6 | +#include <vector> |
| 7 | +#include <memory> |
6 | 8 |
|
7 | 9 | #include "ffilesystem.h" |
8 | 10 |
|
9 | 11 |
|
10 | | -std::string fs_drop_slash(std::string_view in) |
11 | | -{ |
12 | | - // drop all trailing "/" and duplicated internal "/" |
13 | | - |
14 | | - std::filesystem::path p(in); |
15 | | - |
16 | | - std::string s = p.generic_string(); |
17 | | - |
18 | | - if (!fs_is_windows() || (s.length() != 3 || p != p.root_path())){ |
19 | | - while(s.length() > 1 && s.back() == '/') |
20 | | - s.pop_back(); |
| 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_normalize(in); |
| 40 | + |
| 41 | + outputs[0] = factory.createScalar(out); |
21 | 42 | } |
22 | | - |
23 | | - s.erase(std::unique(s.begin(), s.end(), [](char a, char b){ return a == '/' && b == '/'; }), s.end()); |
24 | | - |
25 | | - return s; |
26 | | -} |
| 43 | +}; |
0 commit comments