first not-really working implementation
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 5 Jul 2008 18:44:08 +0000 (20:44 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 5 Jul 2008 18:44:08 +0000 (20:44 +0200)
CollectionReader.cpp
CollectionReader.h

index 27f9ca6..ac33a7e 100644 (file)
@@ -2,13 +2,93 @@
  * © 2008 by David Roden <droden@gmail.com>
  */
 
+#include <stdlib.h>
+#include <string.h>
 #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__);
+       }
 }
 
index c01d00b..ef71c72 100644 (file)
@@ -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;
 
 };