From: Tomek Mrugalski Date: Tue, 21 Oct 2014 12:10:06 +0000 (+0200) Subject: [3555] CSVRow.append() implemented. X-Git-Tag: trac3629a_base~5^2~9 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=0984c85a59fd353ddc97c54e9054f053ebab58f7;p=thirdparty%2Fkea.git [3555] CSVRow.append() implemented. --- diff --git a/src/lib/util/csv_file.h b/src/lib/util/csv_file.h index aab9decfde..561d182262 100644 --- a/src/lib/util/csv_file.h +++ b/src/lib/util/csv_file.h @@ -178,6 +178,20 @@ public: writeAt(at, value.c_str()); } + /// @brief Appends the value as a new column. + /// + /// @param value Value to be written. + /// @tparam T Type of the value being written. + template + void append(const T value) { + try { + values_.push_back(boost::lexical_cast(value)); + } catch (const boost::bad_lexical_cast& ex) { + isc_throw(CSVFileError, "unable to stringify the value to be " + "appended to the CSV file row."); + } + } + /// @brief Replaces the value at specified index. /// /// This function is used to set values to be rendered using diff --git a/src/lib/util/tests/csv_file_unittest.cc b/src/lib/util/tests/csv_file_unittest.cc index 29c3f13cab..0a0b12d297 100644 --- a/src/lib/util/tests/csv_file_unittest.cc +++ b/src/lib/util/tests/csv_file_unittest.cc @@ -90,6 +90,25 @@ TEST(CSVRow, writeAt) { EXPECT_THROW(row.writeAt(3, "foo"), CSVFileError); } +// Checks whether writeAt() and append() can be mixed together. +TEST(CSVRow, append) { + CSVRow row(3); + + EXPECT_EQ(3, row.getValuesCount()); + + row.writeAt(0, "alpha"); + ASSERT_NO_THROW(row.append("delta")); + EXPECT_EQ(4, row.getValuesCount()); + row.writeAt(1, "beta"); + row.writeAt(2, "gamma"); + ASSERT_NO_THROW(row.append("epsilon")); + EXPECT_EQ(5, row.getValuesCount()); + + std::string text; + ASSERT_NO_THROW(text = row.render()); + EXPECT_EQ("alpha,beta,gamma,delta,epsilon", text); +} + /// @brief Test fixture class for testing operations on CSV file. /// /// It implements basic operations on files, such as reading writing