1- // C++ Matlab MEX function using C++17 <filesystem>
2- // build examples:
3- // * MSVC: mex COMPFLAGS="/EHsc /std:c++17" is_char_device.cpp
4- // * GCC: mex CXXFLAGS="-std=c++17" is_char_device.cpp
5-
6- // https://www.mathworks.com/help/matlab/matlab_external/data-types-for-passing-mex-function-data-1.html
7-
81#include " mex.hpp"
92#include " mexAdapter.hpp"
103
1710#if defined(_MSC_VER)
1811# define WIN32_LEAN_AND_MEAN
1912# include < windows.h>
20- #elif __has_include(<filesystem>)
13+ #else
2114# include < filesystem>
2215# include < system_error>
2316#endif
2417
25- #include < sys/types.h>
26- #include < sys/stat.h>
27-
28- static int fs_st_mode (std::string_view path)
29- {
30- // https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/stat-functions
31- struct stat s;
32-
33- return stat (path.data (), &s) == 0 ? s.st_mode : 0 ;
34- }
35-
3618
3719static bool fs_is_char_device (std::string_view path)
3820{
@@ -48,12 +30,9 @@ static bool fs_is_char_device(std::string_view path)
4830 const DWORD type = GetFileType (h);
4931 CloseHandle (h);
5032 return type == FILE_TYPE_CHAR;
51- #elif defined(__cpp_lib_filesystem)
33+ #else
5234 std::error_code ec;
5335 return std::filesystem::is_character_file (path, ec) && !ec;
54- #else
55- // Windows: https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fstat-fstat32-fstat64-fstati64-fstat32i64-fstat64i32
56- return S_ISCHR (fs_st_mode (path));
5736#endif
5837}
5938
@@ -70,7 +49,7 @@ class MexFunction : public matlab::mex::Function {
7049
7150 if (inputs.size () != 1 ) {
7251 matlabEng->feval (u" error" , 0 ,
73- std::vector<matlab::data::Array>({ factory.createScalar (" One input required" ) }));
52+ std::vector<matlab::data::Array>({ factory.createScalar (" Mex: One input required" ) }));
7453 }
7554 if ((inputs[0 ].getType () == matlab::data::ArrayType::MATLAB_STRING && inputs[0 ].getNumberOfElements () == 1 )){
7655 matlab::data::TypedArray<matlab::data::MATLABString> stringArr = inputs[0 ];
0 commit comments