From 440d9987544bc516dd2e6ba19ab459cf016952dd Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 5 Jul 2008 16:02:38 +0200 Subject: [PATCH] add parsing of text links --- ED2KLink.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ ED2KLink.h | 2 ++ 2 files changed, 51 insertions(+) diff --git a/ED2KLink.cpp b/ED2KLink.cpp index 8664d0c..2b4d08a 100644 --- a/ED2KLink.cpp +++ b/ED2KLink.cpp @@ -6,6 +6,9 @@ #include #include #include "ED2KLink.h" +#include "GrowingBuffer.h" +#include "GlobalSettings.h" +#include "GlobalSettings.h" static int getDigits(size_t number) { int digits = 0; @@ -44,6 +47,52 @@ ED2KLink::~ED2KLink() { } } +ED2KLink* ED2KLink::parseED2KLink(const char* buffer) { + ED2KLink* ed2kLink; + const char* tempBuffer = buffer; + char* filename; + char* sizeString; + size_t size; + int hashIndex; + char hash[16]; + GrowingBuffer growingBuffer; + + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] trying to parse “%s”...\n", __FILE__, __LINE__, buffer); + + if (strncmp("ed2k://|file|", buffer, 13)) { + return NULL; + } + + tempBuffer += 13; + while (*tempBuffer != '|') { + growingBuffer.write(tempBuffer++, 1); + } + filename = (char*) malloc(growingBuffer.getLimit() + 1); + growingBuffer.read(filename, growingBuffer.getLimit()); + growingBuffer.clear(); + tempBuffer++; + + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] parsed filename: “%s”.\n", __FILE__, __LINE__, filename); + + while (*tempBuffer != '|') { + growingBuffer.write(tempBuffer++, 1); + } + sizeString = (char*) malloc(growingBuffer.getLimit() + 1); + growingBuffer.read(sizeString, growingBuffer.getLimit()); + growingBuffer.clear(); + tempBuffer++; + + size = atol(sizeString); + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] parsed size “%s” into %d.\n", __FILE__, __LINE__, sizeString, size); + + for (hashIndex = 0; hashIndex < 16; hashIndex++, tempBuffer += 2) { + sscanf(tempBuffer, "%hhx", hash + hashIndex); + } + + ed2kLink = new ED2KLink(filename, size, hash); + return ed2kLink; +} + const char* ED2KLink::getLink() { return this->link; } diff --git a/ED2KLink.h b/ED2KLink.h index 465478f..832387a 100644 --- a/ED2KLink.h +++ b/ED2KLink.h @@ -10,6 +10,8 @@ public: ED2KLink(const char* filename, const size_t size, const void* hash); ~ED2KLink(); + static ED2KLink* parseED2KLink(const char* buffer); + const char* getLink(); const char* getFilename(); const size_t getSize(); -- 2.7.4