8 #define _CRT_SECURE_NO_WARNINGS
12 #include <matioCpp/Config.h>
13 #include <sys/types.h>
61 return "The input variable is not valid.";
66 if (inputName.
size() == 0)
68 return std::string(
"The input variable has an empty name.");
71 if (inputName.
size() > 63)
73 return std::string(
"The input variable name is too long (maximum 63 char allowed).");
76 if (!isalpha(inputName[0]))
78 return std::string(
"The first character of the variable name has to be a letter (Variable name = " + inputName +
").");
81 for (
size_t i = 1; i < inputName.
size(); ++i)
83 if (!isalnum(inputName[i]) && (inputName[i] !=
'_'))
85 return std::string(
"The variable name can contain only alphanumeric characters or underscores (Variable name = \"" + inputName +
"\").");
94 : m_pimpl(
std::make_unique<
Impl>())
100 : m_pimpl(
std::make_unique<
Impl>())
107 operator=(std::forward<matioCpp::File>(other));
128 m_pimpl->reset(Mat_Open(name.
c_str(), matio_mode), mode);
138 if (found != std::string::npos)
144 if( stat( path.
c_str(), &info ) != 0 )
146 std::cerr <<
"[ERROR][matioCpp::File::Create] The path "<< path
147 <<
" does not exists (input file name " << name
151 else if(!(info.st_mode & S_IFDIR))
153 std::cerr <<
"[ERROR][matioCpp::File::Create] The path "<< path
154 <<
" is not a directory (input file name " << name
163 std::cerr <<
"[ERROR][matioCpp::File::Create] Cannot use Undefined as input version type." <<
std::endl;
172 fileVer = mat_ft::MAT_FT_MAT4;
175 fileVer = mat_ft::MAT_FT_MAT5;
178 fileVer = mat_ft::MAT_FT_MAT73;
181 fileVer = MAT_FT_DEFAULT;
187 if (!fileHeader.
size())
193 timeString = timeString.
substr(0,end);
195 if (fileVer == mat_ft::MAT_FT_MAT73)
197 fileHeader =
std::string(
"MATLAB 7.3 MAT-file, created by matioCpp v") + MATIOCPP_VER +
200 " on " + timeString +
" HDF5 schema 0.5.";
202 else if (fileVer == mat_ft::MAT_FT_MAT5)
204 fileHeader =
std::string(
"MATLAB 5.0 MAT-file, created by matioCpp v") + MATIOCPP_VER +
207 " on " + timeString +
".";
212 const char * matioHeader = fileHeader.
size() ? fileHeader.
c_str() : NULL;
218 std::cerr <<
"[ERROR][matioCpp::File::Create] Failed to open the file named "<< name <<
"." <<
std::endl;
242 return Mat_GetFilename(m_pimpl->mat_ptr);
252 #if MATIO_VERSION >= 1515
253 return Mat_GetHeader(m_pimpl->mat_ptr);
255 std::cerr <<
"[ERROR][matioCpp::File::header] The file header can be retrieved only with matio >= 1.5.15" <<
std::endl;
267 switch (Mat_GetVersion(m_pimpl->mat_ptr))
269 case mat_ft::MAT_FT_MAT4:
272 case mat_ft::MAT_FT_MAT5:
275 case mat_ft::MAT_FT_MAT73:
285 return m_pimpl->fileMode;
294 char*
const* list = Mat_GetDir(m_pimpl->mat_ptr, &list_size);
296 outputNames.
resize(list_size);
297 for (
size_t i = 0; i < list_size; ++i)
299 outputNames[i] = list[i];
314 #if defined(_MSC_VER) && MATIO_VERSION < 1519
317 std::cerr <<
"[ERROR][matioCpp::File::read] Reading to a 7.3 file on Windows with a matio version previous to 1.5.19 causes segfaults. The output will be an invalid Variable." <<
std::endl;
322 matvar_t *matVar = Mat_VarRead(m_pimpl->mat_ptr, name.
c_str());
328 std::cerr <<
"[ERROR][matioCpp::File::read] Failed to read variable " << name <<
". The output is not valid." <<
std::endl;
344 std::cerr <<
"[ERROR][matioCpp::File::write] The file cannot be written." <<
std::endl;
348 std::string error = m_pimpl->isVariableValid(variable);
349 if (error.
size() != 0)
357 matio_compression matioCompression =
371 std::cerr <<
"[ERROR][matioCpp::File::write] A MAT4 version does not support arrays with number of dimensions greater than 2." <<
std::endl;
376 std::cerr <<
"[ERROR][matioCpp::File::write] A MAT4 supports only element, vectors or matrices." <<
std::endl;
386 std::cerr <<
"[ERROR][matioCpp::File::write] A MAT4 supports only variables of type LOGICAL, DOUBLE, SINGLE, UINT8, UINT16, INT16 and INT32." <<
std::endl;
391 bool success = Mat_VarWrite(m_pimpl->mat_ptr, shallowCopy.
get(), matioCompression) == 0;
395 std::cerr <<
"[ERROR][matioCpp::File::write] Failed to write the variable to the file." <<
std::endl;
404 return m_pimpl->mat_ptr;
void reset(mat_t *newPtr, matioCpp::FileMode mode)
matioCpp::FileMode fileMode
std::string isVariableValid(const matioCpp::Variable &input)
matioCpp::FileVersion version() const
The MAT file version.
void close()
Close the file.
static bool Exists(const std::string &name)
Check if file exists and can be opened.
matioCpp::FileMode mode() const
The mode with which the file has been opened.
bool isOpen() const
Check if the file is open.
void operator=(const File &other)=delete
Deleted copy assignment, to avoid confusion on whether the content has been copied or not.
bool open(const std::string &name, matioCpp::FileMode mode=matioCpp::FileMode::ReadAndWrite)
Open the specified file.
std::string header() const
The file header.
std::vector< std::string > variableNames() const
Get the list of variables in the file.
File()
Default Constructor.
static bool Delete(const std::string &name)
Delete the specified file.
std::string name() const
The file name.
matioCpp::Variable read(const std::string &name) const
Read a variable given the name.
static File Create(const std::string &name, matioCpp::FileVersion version=matioCpp::FileVersion::Default, const std::string &header="")
Create a new file (static)
bool write(const Variable &variable, matioCpp::Compression compression=matioCpp::Compression::None)
Write a Variable to a file.
static SharedMatvar GetMatvarShallowDuplicate(const matvar_t *inputPtr)
Get the shallow duplicate of an input Matvar.
virtual matvar_t * get() const final
Docs inherited.
The matioCpp::Variable class is the equivalent of matvar_t in matio.
std::string name() const
Get the name of the Variable.
const matvar_t * toMatio() const
Convert this Variable to a matio variable.
matioCpp::Span< const size_t > dimensions() const
Get the dimensions of this object.
bool isValid() const
Check if the variable is valid.
matioCpp::VariableType variableType() const
Get the VariableType.
matioCpp::ValueType valueType() const
Get the ValueType.
T find_last_of(T... args)
FileVersion
The supported file versions.
@ MAT4
This is one of the following three depending on the matio installation.
@ MAT5
Matlab version 4 file
@ MAT7_3
Matlab version 5 file
@ Undefined
Matlab version 7.3 file
FileMode
The available modes with which a file can be opened.
ValueType
The list of types for an element of a certain variable type.
Compression
Compression type when writing to a file.