Skip to content

Commit

Permalink
be sure the string is properly padded for the field type
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Smith committed Nov 14, 2023
1 parent a370468 commit 1075b4e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
3 changes: 0 additions & 3 deletions modules/c++/nitf/include/nitf/TRE.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,6 @@ DECLARE_CLASS(TRE)
*/
std::string getID() const;

private:
std::string truncate(const std::string& value, size_t maxDigits) const;

mutable nitf_Error error{};
};
}
Expand Down
38 changes: 36 additions & 2 deletions modules/c++/nitf/source/TRE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ static bool endsWith(const std::string& s, const std::string& match) noexcept
return sLen >= mLen;
}

std::string TRE::truncate(const std::string& value, size_t maxDigits) const
static std::string truncate(const std::string& value, size_t maxDigits)
{
const size_t decimalIndex = value.find('.');
if (decimalIndex == std::string::npos)
Expand All @@ -184,6 +184,40 @@ std::string TRE::truncate(const std::string& value, size_t maxDigits) const
}
return value;
}
static std::string truncate(const nitf_Field& field, const std::string& value)
{
auto retval = truncate(value, field.length);

// From Field.h
if (field.type == NITF_BCS_A)
{
// is BCS-A data, it is space-filled, right-aligned.
while (retval.length() < field.length)
{
// copyAndFillSpaces() in Field.c "Spaces are added to the right"
retval += " ";
}
}
else if (field.type == NITF_BCS_N)
{
const auto decimalIndex = retval.find('.');

// If it is BCS-N, we expect zero-filled, left-aligned.
while (retval.length() < field.length)
{
if (decimalIndex == std::string::npos)
{
retval = "0" + retval;
}
else
{
retval += "0";
}
}
}

return retval;
}

void TRE::setFieldValue(const std::string& key, const void* data, size_t dataLength, bool forceUpdate)
{
Expand All @@ -207,7 +241,7 @@ void TRE::setFieldValue(const nitf_Field& field, const std::string& key, const s
else
{
// call truncate() first
const auto s = truncate(data, field.length);
const auto s = truncate(field, data);
setFieldValue(key, s.c_str(), s.size(), forceUpdate);
}
}
Expand Down

0 comments on commit 1075b4e

Please sign in to comment.