From 88e69cc8c710fa06616219bcf347c4e9b89550d3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 5 Jul 2008 23:00:01 +0200 Subject: [PATCH] handle last line without line break better --- CollectionReader.cpp | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/CollectionReader.cpp b/CollectionReader.cpp index 10cd9dc..4aa5173 100644 --- a/CollectionReader.cpp +++ b/CollectionReader.cpp @@ -23,13 +23,13 @@ bool CollectionReader::isLineBreakPresent() { } ED2KLink* CollectionReader::getNextLink() { - if (readerInput->isEOF()) { + if (readerInput->isEOF() && !growingBuffer.getRemaining()) { GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] readInput EOF reached.\n", __FILE__, __LINE__); return NULL; } if (firstLink) { identifyCollectionType(); - if (readerInput->isEOF()) { + if (readerInput->isEOF() && !growingBuffer.getRemaining()) { GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] readInput EOF reached.\n", __FILE__, __LINE__); return NULL; } @@ -39,16 +39,27 @@ ED2KLink* CollectionReader::getNextLink() { while (!readerInput->isEOF() && !isLineBreakPresent()) { readMoreBytes(); } - if (readerInput->isEOF()) { + if (readerInput->isEOF() && !growingBuffer.getRemaining()) { GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] readInput EOF reached.\n", __FILE__, __LINE__); return NULL; } size_t indexOfLineBreak = growingBuffer.indexOf('\n'); - char* line = (char*) calloc(indexOfLineBreak + 1, 1); - growingBuffer.read(line, indexOfLineBreak + 1); - line[indexOfLineBreak] = '\0'; - if (line[indexOfLineBreak - 1] == '\r') { - line[indexOfLineBreak - 1] = '\0'; + char* line; + if (indexOfLineBreak == (size_t) -1) { + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] could not find line break, using remainder of file.\n", __FILE__, __LINE__); + indexOfLineBreak = growingBuffer.getRemaining(); + line = (char*) malloc(indexOfLineBreak + 1); + growingBuffer.read(line, indexOfLineBreak); + line[indexOfLineBreak] = '\0'; + } else { + line = (char*) malloc(indexOfLineBreak + 1); + growingBuffer.read(line, indexOfLineBreak + 1); + if (line[indexOfLineBreak] == '\n') { + line[indexOfLineBreak] = '\0'; + } + if (line[indexOfLineBreak - 1] == '\r') { + line[indexOfLineBreak - 1] = '\0'; + } } growingBuffer.cut(); GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] got line: %s\n", __FILE__, __LINE__, line); -- 2.7.4