From b11d90aaee4332e80c2f308113899fddebf2643a Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 5 Jul 2008 20:44:08 +0200 Subject: [PATCH] first not-really working implementation --- CollectionReader.cpp | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++- CollectionReader.h | 10 +++++++ 2 files changed, 91 insertions(+), 1 deletion(-) diff --git a/CollectionReader.cpp b/CollectionReader.cpp index 27f9ca6..ac33a7e 100644 --- a/CollectionReader.cpp +++ b/CollectionReader.cpp @@ -2,13 +2,93 @@ * © 2008 by David Roden */ +#include +#include #include "CollectionReader.h" +#include "GlobalSettings.h" CollectionReader::CollectionReader(ReaderInput* readerInput) { this->readerInput = readerInput; + firstLink = true; } CollectionReader::~CollectionReader() { - delete readerInput; +} + +bool CollectionReader::isLineBreakPresent() { + size_t indexOfLineBreak = growingBuffer.indexOf('\n'); + GlobalSettings::isVerbose() && (indexOfLineBreak != (size_t) -1) && fprintf(stderr, "[%s:%d] found line break at %d.\n", __FILE__, __LINE__, indexOfLineBreak); + GlobalSettings::isVerbose() && (indexOfLineBreak == (size_t) -1) && fprintf(stderr, "[%s:%d] could not find line break.\n", __FILE__, __LINE__); + return indexOfLineBreak != (size_t) -1; +} + +ED2KLink* CollectionReader::getNextLink() { + if (readerInput->isEOF()) { + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] readInput EOF reached.\n", __FILE__, __LINE__); + return NULL; + } + if (firstLink) { + identifyCollectionType(); + if (readerInput->isEOF()) { + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] readInput EOF reached.\n", __FILE__, __LINE__); + return NULL; + } + firstLink = false; + } + if (isTextCollection) { + while (!readerInput->isEOF() && !isLineBreakPresent()) { + readMoreBytes(); + } + if (readerInput->isEOF()) { + 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'; + } + growingBuffer.cut(); + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] got line: %s\n", __FILE__, __LINE__, line); + ED2KLink* ed2kLink = ED2KLink::parseED2KLink(line); + return ed2kLink; + } else { + } + return NULL; +} + +void CollectionReader::readMoreBytes() { + char buffer[1024]; + size_t readBytes; + + readBytes = readerInput->read(buffer, 1024); + growingBuffer.write(buffer, readBytes); +} + +void CollectionReader::identifyCollectionType() { + int version; + size_t readBytes; + + readBytes = readerInput->read(&version, 4); + if ((readBytes < 4) || readerInput->isEOF()) { + return; + } + if (version == 0x01) { + isTextCollection = false; + this->version = 1; + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] identified binary collection, version 1\n", __FILE__, __LINE__); + } else if (version == 0x02) { + isTextCollection = false; + this->version = 2; + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] identified binary collection, version 2\n", __FILE__, __LINE__); + } else if (!strncmp("ed2k", (char*) &version, 4)) { + isTextCollection = true; + growingBuffer.write(&version, 4); + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] identified text collection\n", __FILE__, __LINE__); + } else { + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] could not identify collection!\n", __FILE__, __LINE__); + } } diff --git a/CollectionReader.h b/CollectionReader.h index c01d00b..ef71c72 100644 --- a/CollectionReader.h +++ b/CollectionReader.h @@ -6,6 +6,7 @@ #include "ReaderInput.h" #include "ED2KLink.h" +#include "GrowingBuffer.h" class CollectionReader { @@ -16,7 +17,16 @@ public: ED2KLink* getNextLink(); private: + void identifyCollectionType(); + bool isLineBreakPresent(); + void readMoreBytes(); + +private: ReaderInput* readerInput; + GrowingBuffer growingBuffer; + int version; + bool firstLink; + bool isTextCollection; }; -- 2.7.4