|
| 1 | +// C++ Matlab MEX function using C++17 <filesystem> |
| 2 | +// https://www.mathworks.com/help/matlab/matlab_external/data-types-for-passing-mex-function-data-1.html |
| 3 | + |
| 4 | +#include "mex.hpp" |
| 5 | +#include "mexAdapter.hpp" |
| 6 | + |
| 7 | +#include <string> |
| 8 | +// note: <string_view> causes compile failures with MSVC at least |
| 9 | + |
| 10 | +#include <vector> |
| 11 | +#include <memory> |
| 12 | + |
| 13 | +#if __has_include(<filesystem>) |
| 14 | +# include <filesystem> |
| 15 | +# include <system_error> |
| 16 | +#endif |
| 17 | + |
| 18 | + |
| 19 | +static bool fs_set_permissions(std::string path, int readable, int writable, int executable) |
| 20 | +{ |
| 21 | +#ifdef __cpp_lib_filesystem |
| 22 | + |
| 23 | +#if defined(__cpp_using_enum) // C++20 |
| 24 | + using enum std::filesystem::perms; |
| 25 | +#else |
| 26 | + constexpr std::filesystem::perms owner_read = std::filesystem::perms::owner_read; |
| 27 | + constexpr std::filesystem::perms owner_write = std::filesystem::perms::owner_write; |
| 28 | + constexpr std::filesystem::perms owner_exec = std::filesystem::perms::owner_exec; |
| 29 | +#endif |
| 30 | + |
| 31 | + std::filesystem::path p(path); |
| 32 | + std::error_code ec; |
| 33 | + // need to error if path doesn't exist and no operations are requested |
| 34 | + if(!std::filesystem::exists(p)) |
| 35 | + ec = std::make_error_code(std::errc::no_such_file_or_directory); |
| 36 | + |
| 37 | + if (!ec && readable != 0) |
| 38 | + std::filesystem::permissions(p, owner_read, |
| 39 | + (readable > 0) ? std::filesystem::perm_options::add : std::filesystem::perm_options::remove, |
| 40 | + ec); |
| 41 | + |
| 42 | + if (!ec && writable != 0) |
| 43 | + std::filesystem::permissions(p, owner_write, |
| 44 | + (writable > 0) ? std::filesystem::perm_options::add : std::filesystem::perm_options::remove, |
| 45 | + ec); |
| 46 | + |
| 47 | + if (!ec && executable != 0) |
| 48 | + std::filesystem::permissions(p, owner_exec, |
| 49 | + (executable > 0) ? std::filesystem::perm_options::add : std::filesystem::perm_options::remove, |
| 50 | + ec); |
| 51 | + |
| 52 | + if(!ec) |
| 53 | + return true; |
| 54 | + |
| 55 | +#endif |
| 56 | + return false; |
| 57 | + |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +class MexFunction : public matlab::mex::Function { |
| 63 | +public: |
| 64 | + void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) { |
| 65 | +// boilerplate engine & ArrayFactory setup |
| 66 | + std::shared_ptr<matlab::engine::MATLABEngine> matlabPtr = getEngine(); |
| 67 | + |
| 68 | + matlab::data::ArrayFactory factory; |
| 69 | +// boilerplate input checks |
| 70 | + if (inputs.size() != 4) { |
| 71 | + matlabPtr->feval(u"error", 0, |
| 72 | + std::vector<matlab::data::Array>({ factory.createScalar("Four inputs required") })); |
| 73 | + } |
| 74 | + if (inputs[0].getType() != matlab::data::ArrayType::MATLAB_STRING) { |
| 75 | + matlabPtr->feval(u"error", 0, |
| 76 | + std::vector<matlab::data::Array>({ factory.createScalar("Input must be a string") })); |
| 77 | + } |
| 78 | + if (inputs[0].getNumberOfElements() != 1) { |
| 79 | + matlabPtr->feval(u"error", 0, |
| 80 | + std::vector<matlab::data::Array>({ factory.createScalar("Input must be a scalar string") })); |
| 81 | + } |
| 82 | + if (inputs[1].getType() != matlab::data::ArrayType::DOUBLE || |
| 83 | + inputs[2].getType() != matlab::data::ArrayType::DOUBLE || |
| 84 | + inputs[3].getType() != matlab::data::ArrayType::DOUBLE) { |
| 85 | + matlabPtr->feval(u"error", 0, |
| 86 | + std::vector<matlab::data::Array>({ factory.createScalar("Permission inputs must be doubles") })); |
| 87 | + } |
| 88 | + |
| 89 | +// Matlab strings are an array, so we use [0][0] to get the first element |
| 90 | + std::string inputStr = inputs[0][0]; |
| 91 | + |
| 92 | +// actual function algorithm / computation |
| 93 | + bool y = fs_set_permissions(inputStr, inputs[1][0], inputs[2][0], inputs[3][0]); |
| 94 | + |
| 95 | +// convert to Matlab output -- even scalars are arrays in Matlab |
| 96 | +// https://www.mathworks.com/help/matlab/matlab_external/create-matlab-array-with-matlab-data-cpp-api.html |
| 97 | +// https://www.mathworks.com/help/matlab/apiref/matlab.data.arrayfactory.html |
| 98 | + |
| 99 | + outputs[0] = factory.createArray<bool>({1,1}, {y}); |
| 100 | + } |
| 101 | +}; |
0 commit comments