]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[3555] CSVRow.append() implemented.
authorTomek Mrugalski <tomasz@isc.org>
Tue, 21 Oct 2014 12:10:06 +0000 (14:10 +0200)
committerTomek Mrugalski <tomasz@isc.org>
Tue, 21 Oct 2014 12:10:06 +0000 (14:10 +0200)
src/lib/util/csv_file.h
src/lib/util/tests/csv_file_unittest.cc

index aab9decfdeaefff2ec0c2fd30133e4f1036ffb5d..561d1822626145e8cb7fa1e182d07c3d8054c563 100644 (file)
@@ -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<typename T>
+    void append(const T value) {
+        try {
+            values_.push_back(boost::lexical_cast<std::string>(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
index 29c3f13cab32c921c85461ea3b979760a72688eb..0a0b12d2978f7940cb7ac8746b7633c3504e6420 100644 (file)
@@ -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