Commit 695afd32 authored by Purves, Murray's avatar Purves, Murray
Browse files

Updating to use string::resize instead of radix method

parent 3defde8f
Pipeline #16212 passed with stages
in 9 minutes and 49 seconds
......@@ -151,30 +151,4 @@ struct std::tm utc_to_time(const std::string &time, int &zone, int &daylight)
int ordinal(char c) { return (unsigned char)c; }
std::string make_length(const std::string &string, const int length)
{
std::string output;
if (string.length() == length)
{
output = string;
}
else if (string.length() > length)
{
output = string.substr(0, length);
}
else
{
std::stringstream ss;
ss << string;
for (int i = string.length(); i < length; ++i)
{
ss << " ";
}
output = ss.str();
}
return output;
}
} // namespace radix
......@@ -107,17 +107,6 @@ std::string RADIX_PUBLIC remove_substring(const char *msg, const char *sub);
*/
std::string RADIX_PUBLIC remove_extra_whitespace(const std::string &str);
/**
* @brief make_length Force a string to be of a specified length. If string is
* longer than length, returned string is trimmed to length. If string is
* shorter than length, whitespace is added.
* @param string The string to make a specific length
* @param length The required length of the returned string
* @return A string based on the input string, with the specified length
*/
std::string RADIX_PUBLIC make_length(const std::string &string,
const int length);
} // namespace radix
//! Templated functions
......
......@@ -151,21 +151,3 @@ TEST(Radix, OrdinalItoa)
EXPECT_EQ(140, i);
}
}
TEST(StringFunctions, MakeLength)
{
std::string shortString = "this string is too short";
std::string testString = make_length(shortString, 30);
EXPECT_EQ(30, testString.length());
EXPECT_STREQ("this string is too short ", testString.c_str());
std::string longString = "this string is too long";
testString = make_length(longString, 15);
EXPECT_EQ(15, testString.length());
EXPECT_STREQ("this string is ", testString.c_str());
std::string sameString = "this string is just right";
testString = make_length(sameString, 25);
EXPECT_EQ(sameString.length(), testString.length());
EXPECT_STREQ(sameString.c_str(), testString.c_str());
}
......@@ -566,18 +566,20 @@ void eafstream::writeString(const std::string &var)
mBytesWritten += var.size();
}
void eafstream::writeString(const std::string &var, size_t length, char filler)
{
std::string copy(var);
copy.resize(length, filler);
this->writeString(copy);
}
/**
* \brief Returns the number of bytes read since the last headers marker
* \return int
*/
int eafstream::bytesRead() const { return mBytesRead; } // bytesRead
void eafstream::setBytesRead(int bytesRead) { mBytesRead = bytesRead; }
int eafstream::bytesWritten() const { return mBytesWritten; }
void eafstream::setBytesWritten(int bytesWritten)
{
mBytesWritten = bytesWritten;
}
int eafstream::readHeader()
{
......
......@@ -94,20 +94,19 @@ class RADIX_PUBLIC eafstream : public std::fstream
*/
std::string readString(int size);
void writeString(const std::string &var);
void writeString(const std::string &var, size_t length, char filler = ' ');
/**
* \brief Returns the number of bytes read since the last header marker
* \return int
*/
int bytesRead() const;
void setBytesRead(int bytesRead);
/**
* \brief Returns the number of bytes written since the last header marker
* \return int
*/
int bytesWritten() const;
void setBytesWritten(int bytesWritten);
/**
* @brief readHeader Reads and returns the fortran header record
......
......@@ -332,43 +332,35 @@ bool SpectrumPCFStream<data_type>::write_to(const std::string &file) const
if (mData->dhsVersion())
{
radix_line(" File has DHS-type header");
stream.write("DHS", 3);
stream.write(make_length(mData->lastModifiedHash(), 7).c_str(), 7);
stream.write(make_length(mData->uuid(), 36).c_str(), 36);
stream.write(make_length(mData->inspection(), 16).c_str(), 16);
stream.writeString("DHS");
stream.writeString(mData->lastModifiedHash(), 7);
stream.writeString(mData->uuid(), 36);
stream.writeString(mData->inspection(), 16);
stream.writeShort(mData->laneNumber());
stream.write(make_length(mData->measurementRemark(), 26).c_str(), 26);
stream.write(make_length(mData->instrumentType(), 28).c_str(), 28);
stream.write(make_length(mData->manufacturer(), 28).c_str(), 28);
stream.write(make_length(mData->instrumentModel(), 18).c_str(), 18);
stream.write(make_length(mData->instrumentID(), 18).c_str(), 18);
stream.write(make_length(mData->itemDescription(), 20).c_str(), 20);
stream.write(make_length(mData->measurementLocationName(), 16).c_str(), 16);
stream.write(make_length(mData->measurementLocationCoords(), 16).c_str(),
16);
stream.writeString(mData->measurementRemark(), 26);
stream.writeString(mData->instrumentType(), 28);
stream.writeString(mData->manufacturer(), 28);
stream.writeString(mData->instrumentModel(), 18);
stream.writeString(mData->instrumentID(), 18);
stream.writeString(mData->itemDescription(), 20);
stream.writeString(mData->measurementLocationName(), 16);
stream.writeString(mData->measurementLocationCoords(), 16);
stream.writeShort(mData->itemDetectorDistance());
stream.writeShort(mData->occupancyNumber());
stream.write(make_length(mData->cargoType(), 16).c_str(), 16);
// Add bytes written directly to eafstream count
stream.setBytesWritten(stream.bytesWritten() + 248);
stream.writeString(mData->cargoType(), 16);
}
else
{
radix_line(" File has non-DHS-type header");
stream.write(" ", 3);
stream.write(make_length(mData->energyCalibrationLabel(), 4).c_str(), 4);
stream.writeString(" ");
stream.writeString(mData->energyCalibrationLabel(), 4);
stream.writeFloat(mData->energyCalibrationOffset());
stream.writeFloat(mData->energyCalibrationGain());
stream.writeFloat(mData->energyCalibrationQuadraticTerm());
stream.writeFloat(mData->energyCalibrationCubicTerm());
stream.writeFloat(mData->energyCalibrationLowEnergy());
// Add bytes written directly to eafstream count
stream.setBytesWritten(stream.bytesWritten() + 7);
// Skip to 256 bytes (end of header)
stream.write(make_length(" ", 256 - stream.bytesWritten()).c_str(),
256 - stream.bytesWritten());
// Add bytes written directly to eafstream count
stream.setBytesWritten(256);
stream.writeString(" ", 256 - stream.bytesWritten());
}
radix_line(" End of header: " << stream.bytesWritten()
<< " bytes written so far");
......@@ -379,14 +371,8 @@ bool SpectrumPCFStream<data_type>::write_to(const std::string &file) const
{
radix_line(" Regular deviation pairs found:");
// Write regular deviation pairs
stream.write(make_length(mData->deviationPairPresence(), 20).c_str(), 20);
// Add bytes written directly to eafstream count
stream.setBytesWritten(stream.bytesWritten() + 20);
// Skip to 512 bytes (start of pairs)
stream.write(make_length(" ", 512 - stream.bytesWritten()).c_str(),
512 - stream.bytesWritten());
// Add bytes written directly to eafstream count
stream.setBytesWritten(512);
stream.writeString(mData->deviationPairPresence(), 20);
stream.writeString(" ", 512 - stream.bytesWritten());
// Write the pairs themselves
for (size_t column = 0; column < 2; ++column)
......@@ -412,26 +398,18 @@ bool SpectrumPCFStream<data_type>::write_to(const std::string &file) const
// Skip to 256*82 bytes (start of spectral data)
radix_line("Skipping " << (256 * 82) - stream.bytesWritten()
<< " bytes to start of spectra data (256*83 = "
<< " bytes to start of spectra data (256*82 = "
<< 256 * 82 << " bytes)");
stream.write(make_length(" ", (256 * 82) - stream.bytesWritten()).c_str(),
(256 * 82) - stream.bytesWritten());
// Add bytes written directly to eafstream count
stream.setBytesWritten(256 * 82);
stream.writeString(" ", (256 * 82) - stream.bytesWritten());
}
else if (mData->deviationPairPresence().compare(
"DeviationPairsInFileCompressed") == 0)
{
radix_line(" Compressed deviation pairs found:");
// Write compressed deviation pairs
stream.write(make_length(mData->deviationPairPresence(), 30).c_str(), 30);
// Add bytes written directly to eafstream count
stream.setBytesWritten(stream.bytesWritten() + 30);
stream.writeString(mData->deviationPairPresence(), 30);
// Skip to 512 bytes (start of pairs)
stream.write(make_length(" ", 512 - stream.bytesWritten()).c_str(),
512 - stream.bytesWritten());
// Add bytes written directly to eafstream count
stream.setBytesWritten(512);
stream.writeString(" ", 512 - stream.bytesWritten());
// Write the pairs themselves
for (size_t column = 0; column < 4; ++column)
......@@ -461,12 +439,9 @@ bool SpectrumPCFStream<data_type>::write_to(const std::string &file) const
// Skip to 256*82 bytes (start of spectral data)
radix_line("Skipping " << (256 * 82) - stream.bytesWritten()
<< " bytes to start of spectra data (256*83 = "
<< " bytes to start of spectra data (256*82 = "
<< 256 * 82 << " bytes)");
stream.write(make_length(" ", (256 * 82) - stream.bytesWritten()).c_str(),
(256 * 82) - stream.bytesWritten());
// Add bytes written directly to eafstream count
stream.setBytesWritten(256 * 82);
stream.writeString(" ", (256 * 82) - stream.bytesWritten());
}
else
{
......@@ -501,11 +476,9 @@ bool SpectrumPCFStream<data_type>::write_to(const std::string &file) const
char delim = 255;
ss << delim << trim_string(title) << delim << trim_string(description)
<< delim << trim_string(source);
stream.write(make_length(ss.str(), 180).c_str(), 180);
stream.write(make_length(dateTime, 23).c_str(), 23);
stream.write(make_length(tag, 1).c_str(), 1);
// Add bytes written directly to eafstream count
stream.setBytesWritten(stream.bytesWritten() + 204);
stream.writeString(ss.str(), 180);
stream.writeString(dateTime, 23);
stream.writeString(tag, 1);
stream.writeFloat(liveTime);
stream.writeFloat(totalTime);
// Three unused floats in data structure
......
......@@ -18,6 +18,7 @@ TEST(RadixIO, eafstream)
std::vector<int> intVec1 = {1, 2, 3};
std::vector<float> floatVec1 = {4.f, 5.f, 6.f, 7.f};
std::vector<double> doubleVec1 = {8., 9.};
std::string string = "test string";
{ // write binary file
// write small input file
eafstream stream(file.c_str(), std::ios::binary | std::ios::out);
......@@ -44,6 +45,14 @@ TEST(RadixIO, eafstream)
stream.writeHeader(record_length);
stream << doubleVec1;
stream.writeFooter(record_length);
// string
record_length = int(string.length()) + 15 + 20 + 4;
stream.writeHeader(record_length);
stream.writeString(string);
stream.writeString(string, 15);
stream.writeString(string, 20, '}');
stream.writeString(string, 4);
stream.writeFooter(record_length);
stream.close();
}
int intValue2;
......@@ -52,6 +61,7 @@ TEST(RadixIO, eafstream)
std::vector<int> intVec2;
std::vector<float> floatVec2;
std::vector<double> doubleVec2;
std::string samesize, longer1, longer2, shorter;
{ // read the binary file
// write small input file
eafstream stream(file.c_str(), std::ios::binary | std::ios::in);
......@@ -82,6 +92,14 @@ TEST(RadixIO, eafstream)
doubleVec2.resize(2);
stream >> doubleVec2;
EXPECT_EQ(record_length, stream.readFooter());
// string
record_length = stream.readHeader();
EXPECT_EQ(50, record_length);
samesize = stream.readString(11);
longer1 = stream.readString(15);
longer2 = stream.readString(20);
shorter = stream.readString(4);
EXPECT_EQ(record_length, stream.readFooter());
stream.close();
}
EXPECT_EQ(intValue1, intValue2);
......@@ -90,5 +108,9 @@ TEST(RadixIO, eafstream)
EXPECT_EQ(intVec1, intVec2);
EXPECT_EQ(floatVec1, floatVec2);
EXPECT_EQ(doubleVec1, doubleVec2);
EXPECT_STREQ(string.c_str(), samesize.c_str());
EXPECT_STREQ(string.substr(0, 4).c_str(), shorter.c_str());
EXPECT_STREQ(std::string(string + " ").c_str(), longer1.c_str());
EXPECT_STREQ(std::string(string + "}}}}}}}}}").c_str(), longer2.c_str());
}
}
Markdown is supported
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