Skip to content

Conversation

@castle055
Copy link

Some systems and libraries add leading and/or trailing whitespaces when writing text into XML elements. For instance, the specific system I am working with now outputs XML in this format:

<stream id="L1">
  <ratefactor> 1 </ratefactor>
  <quantization> 8 </quantization>
  <packedbits> 16 </packedbits>
  <alignment> Right </alignment>
  <shift> Left </shift>
  <format> IQ </format>
  <encoding> TC </encoding>
  <band id="L1"/>
</stream>

This is problematic since the API will try to parse every element with the spaces included. So for the ratefactor, it will call atol(" 1 "), which will not parse properly. Another case is string comparison, alignment calls strcmp("Right", " Right "), which returns false.

I propose introducing a TrimString static function to the Translator class. This function accepts a const char* string and a pointer to a size_t. It will return a new pointer to the beginning of the string after any whitespaces and set the size_t* to the actual length. This effectively trims the string in place.

We can then use this new pointer to call atol() and atof(), which will parse numbers properly despite any trailing spaces. For the case of comparing string, we can call strncmp() instead, which will accept our length value and compare just those characters ignoring trailing spaces.

I have tested these changes in my setup and it now parses the metadata file without an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant