Commit 5ebc61b3 authored by LEFEBVREJP email's avatar LEFEBVREJP email
Browse files

Adding support for unsigned short data type in eafstream.

parent b4cb677e
Pipeline #16465 passed with stages
in 15 minutes and 48 seconds
...@@ -50,6 +50,16 @@ eafstream &eafstream::operator>>(short &value) ...@@ -50,6 +50,16 @@ eafstream &eafstream::operator>>(short &value)
value = readShort(); value = readShort();
return *this; return *this;
} // short } // short
/**
* Operator overload for read unsigned short
* @param value unsigned short reference to be populated
* @return
*/
eafstream &eafstream::operator>>(unsigned short &value)
{
value = readUShort();
return *this;
} // short
/** /**
* Operator overload for read int * Operator overload for read int
* @param value int reference to be populated * @param value int reference to be populated
...@@ -210,6 +220,50 @@ void eafstream::writeShort(short var) ...@@ -210,6 +220,50 @@ void eafstream::writeShort(short var)
mBytesWritten += short_size; mBytesWritten += short_size;
} }
/**
* \brief read a unsigned short integer from the offset in the file, accounting
* for endianness \return unsigned integer value .
*
*/
unsigned short eafstream::readUShort()
{
unsigned short var = 0;
this->read((char *)(&var), short_size);
if (this->bad() || this->eof())
{
std::stringstream ss;
ss << "Error, reading unsigned short from file at offset." << this->tellg();
std::cout << ss.str() << std::endl;
throw std::logic_error(ss.str());
}
if (mReverseBytes == true)
{
var = reverse_ushort_bytes(var);
}
// increment bytes read
mBytesRead += short_size;
return var;
} // readShort
void eafstream::writeUShort(unsigned short var)
{
if (mReverseBytes == true)
{
var = reverse_ushort_bytes(var);
}
this->write((char *)(&var), short_size);
if (this->bad() || this->eof())
{
std::stringstream ss;
ss << "Error, writing unsigned short to file at offset." << this->tellg();
std::cout << ss.str() << std::endl;
throw std::logic_error(ss.str());
}
mBytesWritten += short_size;
}
/** /**
* \brief read an integer from the offset in the file, accounting for * \brief read an integer from the offset in the file, accounting for
* endianness \return integer value or -1 on error. * endianness \return integer value or -1 on error.
......
...@@ -45,6 +45,12 @@ class RADIX_PUBLIC eafstream : public std::fstream ...@@ -45,6 +45,12 @@ class RADIX_PUBLIC eafstream : public std::fstream
*/ */
short readShort(); short readShort();
void writeShort(short var); void writeShort(short var);
/**
* \brief read a unsigned short integer from the file, accounting for
* endianness \return unsigned short value or -1 on error.
*/
unsigned short readUShort();
void writeUShort(unsigned short var);
/** /**
* \brief read an integer from the file, accounting for endianness * \brief read an integer from the file, accounting for endianness
* \return integer value or -1 on error. * \return integer value or -1 on error.
...@@ -132,6 +138,7 @@ class RADIX_PUBLIC eafstream : public std::fstream ...@@ -132,6 +138,7 @@ class RADIX_PUBLIC eafstream : public std::fstream
int footer() const; int footer() const;
eafstream &operator>>(short &value); eafstream &operator>>(short &value);
eafstream &operator>>(unsigned short &value);
eafstream &operator>>(int &value); eafstream &operator>>(int &value);
eafstream &operator>>(float &value); eafstream &operator>>(float &value);
eafstream &operator>>(double &value); eafstream &operator>>(double &value);
......
...@@ -44,7 +44,18 @@ short reverse_short_bytes(short value) ...@@ -44,7 +44,18 @@ short reverse_short_bytes(short value)
short returnVal; short returnVal;
reverse_bytes((char *)&value, (void *)&returnVal, sizeof(short)); reverse_bytes((char *)&value, (void *)&returnVal, sizeof(short));
return returnVal; return returnVal;
} // reverse_int_bytes } // reverse_short_bytes
/*!
* Reverses the bytes of an unsigned short
* */
unsigned short reverse_ushort_bytes(unsigned short value)
{
unsigned short returnVal;
reverse_bytes((char *)&value, (void *)&returnVal, sizeof(short));
return returnVal;
} // reverse_ushort_bytes
/*! /*!
* Reverses the bytes of an integer * Reverses the bytes of an integer
* */ * */
......
...@@ -11,6 +11,7 @@ namespace radix ...@@ -11,6 +11,7 @@ namespace radix
{ {
void RADIX_PUBLIC reverse_bytes(char *original, void *returnVal, int length); void RADIX_PUBLIC reverse_bytes(char *original, void *returnVal, int length);
short RADIX_PUBLIC reverse_short_bytes(short value); short RADIX_PUBLIC reverse_short_bytes(short value);
unsigned short RADIX_PUBLIC reverse_ushort_bytes(unsigned short value);
int RADIX_PUBLIC reverse_int_bytes(int value); int RADIX_PUBLIC reverse_int_bytes(int value);
void RADIX_PUBLIC reverse_array_int_bytes(int *array, int size); void RADIX_PUBLIC reverse_array_int_bytes(int *array, int size);
void RADIX_PUBLIC reverse_array_float_bytes(float *array, int size); void RADIX_PUBLIC reverse_array_float_bytes(float *array, int size);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment