Skip to content

Commit

Permalink
Printing an error also in Release when a field is not existing
Browse files Browse the repository at this point in the history
Edited the way we check if a field exists to avoid multiple class to matio functions
  • Loading branch information
S-Dafarra committed Jul 5, 2024
1 parent ee32137 commit 554b733
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 15 deletions.
5 changes: 3 additions & 2 deletions include/matioCpp/Variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,10 @@ class matioCpp::Variable
/**
* @brief Get the index of the specified field in the variable, considered as a struct
* @param field The field to search
* @return The index of the field, the output of getStructNumberOfFields() if not found.
* @param index The index of the field
* @return True if successfull, false otherwise.
*/
size_t getStructFieldIndex(const std::string& field) const;
bool getStructFieldIndex(const std::string& field, size_t& index) const;

/**
* @brief Set the field of the struct at the specified position
Expand Down
7 changes: 5 additions & 2 deletions src/Struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,15 @@ void matioCpp::Struct::clear()

bool matioCpp::Struct::isFieldExisting(const std::string &field) const
{
return getStructFieldIndex(field) < numberOfFields();
size_t index;
return getStructFieldIndex(field, index);
}

size_t matioCpp::Struct::getFieldIndex(const std::string &field) const
{
return getStructFieldIndex(field);
size_t index;
getStructFieldIndex(field, index);
return index;
}

bool matioCpp::Struct::setField(matioCpp::Struct::index_type index, const matioCpp::Variable &newValue)
Expand Down
7 changes: 5 additions & 2 deletions src/StructArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,12 +371,15 @@ void matioCpp::StructArray::clear()

bool matioCpp::StructArray::isFieldExisting(const std::string &field) const
{
return getStructFieldIndex(field) < numberOfFields();
size_t index;
return getStructFieldIndex(field, index);
}

size_t matioCpp::StructArray::getFieldIndex(const std::string &field) const
{
return getStructFieldIndex(field);
size_t index;
getStructFieldIndex(field, index);
return index;
}

bool matioCpp::StructArray::addField(const std::string &newField)
Expand Down
32 changes: 23 additions & 9 deletions src/Variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,23 +232,26 @@ char * const * matioCpp::Variable::getStructFields() const
return Mat_VarGetStructFieldnames(m_handler->get());
}

size_t matioCpp::Variable::getStructFieldIndex(const std::string &field) const
bool matioCpp::Variable::getStructFieldIndex(const std::string& field, size_t& index) const
{
size_t i = 0;
size_t numberOfFields = getStructNumberOfFields();
char * const * fields = getStructFields();
index = numberOfFields;

if (!fields)
{
return numberOfFields;
return false;
}

while (i < numberOfFields && (strcmp(fields[i], field.c_str()) != 0))
{
++i;
}

return i;
index = i;

return index < numberOfFields;
}

bool matioCpp::Variable::setStructField(size_t index, const matioCpp::Variable &newValue, size_t structPositionInArray)
Expand Down Expand Up @@ -312,13 +315,14 @@ bool matioCpp::Variable::setStructField(const std::string& field, const matioCpp
return false;
}

size_t fieldindex = getStructFieldIndex(field);
size_t fieldindex;

if ((fieldindex == getStructNumberOfFields()) && !((getArrayNumberOfElements() == 1) && addStructField(field)))
if (!getStructFieldIndex(field, fieldindex) && !((getArrayNumberOfElements() == 1) && addStructField(field)))
{
//This is the case when the field has not been found and, either there are more than one elements (i.e. it is part of an array), or there was an error in adding the field
return false;
}
//If it was not found, but the field has been added, the fieldindex is the last one

return setStructField(fieldindex, newValue, structPositionInArray);
}
Expand Down Expand Up @@ -552,8 +556,13 @@ matioCpp::Variable matioCpp::Variable::operator[](const std::string& el)
assert(false);
return matioCpp::Variable();
}
size_t index = getStructFieldIndex(el);
assert(index < getStructNumberOfFields() && "The specified field does not exist.");
size_t index;
if (!getStructFieldIndex(el, index))
{
std::cerr << "[ERROR][matioCpp::Variable::operator[]] The field " << el << " does not exist." << std::endl;
assert(false);
return matioCpp::Variable();
}
return getStructField(index);
}

Expand All @@ -565,8 +574,13 @@ const matioCpp::Variable matioCpp::Variable::operator[](const std::string& el) c
assert(false);
return matioCpp::Variable();
}
size_t index = getStructFieldIndex(el);
assert(index < getStructNumberOfFields() && "The specified field does not exist.");
size_t index;
if (!getStructFieldIndex(el, index))
{
std::cerr << "[ERROR][matioCpp::Variable::operator[]] The field " << el << " does not exist." << std::endl;
assert(false);
return matioCpp::Variable();
}
return getStructField(index);
}

Expand Down
2 changes: 2 additions & 0 deletions test/StructArrayUnitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ TEST_CASE("Assignments and modifications")
size_t numberOfFields = in.numberOfFields();
REQUIRE(numberOfFields == arrayFields.size());

REQUIRE(in.getFieldIndex("notExisting") == numberOfFields);

in.addField("addedField");

REQUIRE(in.numberOfFields() == numberOfFields + 1);
Expand Down

0 comments on commit 554b733

Please sign in to comment.