check number of values on each row v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 13 Nov 2021 17:05:03 +0100
branchv_0
changeset 22 3f6488171e34
parent 21 22eb4838e8d0
child 23 1ade50eee150
check number of values on each row
src/CSVCommand.cpp
--- a/src/CSVCommand.cpp	Sat Nov 13 11:52:54 2021 +0100
+++ b/src/CSVCommand.cpp	Sat Nov 13 17:05:03 2021 +0100
@@ -141,7 +141,11 @@
 	while (readValue(input, currentValue, lastInRecord) && input.good()) {
 		if (headerDone) {
 			writer->writeAttribute(convertor.from_bytes(currentValue.str()));
+
 			valueCount++;
+			if (valueCount > metadata.size()) throw RelpipeWriterException(L"The number of values " + std::to_wstring(valueCount) + L" exceeds declared column count: " + std::to_wstring(metadata.size()));
+			if (lastInRecord && valueCount != metadata.size()) throw RelpipeWriterException(L"The number of values " + std::to_wstring(valueCount) + L" is lower than declared column count: " + std::to_wstring(metadata.size()));
+			if (lastInRecord) valueCount = 0;
 		} else {
 			AttributeMetadata am;
 			am.attributeName = convertor.from_bytes(currentValue.str());
@@ -181,7 +185,7 @@
 	 *  - Each line should contain the same number of fields throughout the file.
 	 *  - The last field in the record must not be followed by a comma.
 	 */
-	if (valueCount % metadata.size()) throw RelpipeWriterException(L"The total number of values " + std::to_wstring(valueCount) + L" does not match the number of declared columns " + std::to_wstring(metadata.size()));
+	if (valueCount && valueCount != metadata.size()) throw RelpipeWriterException(L"Unexpected EOF: The number of values " + std::to_wstring(valueCount) + L" is lower than declared column count: " + std::to_wstring(metadata.size()));
 }
 
 }