From: David ‘Bombe’ Roden Date: Fri, 4 Jul 2008 20:00:21 +0000 (+0200) Subject: add ED2KLink class X-Git-Tag: 0.1~64 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=f931be4f06ed62465a0c2c09f56dbc2cf6a7170a;p=ecparse.git add ED2KLink class --- diff --git a/ED2KLink.cpp b/ED2KLink.cpp new file mode 100644 index 0000000..cd26940 --- /dev/null +++ b/ED2KLink.cpp @@ -0,0 +1,46 @@ +/** + * © 2008 by David ‘Bombe’ Roden + */ + +#include +#include +#include +#include "ED2KLink.h" + +static int getDigits(size_t number) { + int digits = 0; + do { + number /= 10; + digits++; + } while (number); + return digits; +} + +ED2KLink::ED2KLink(const char* filename, const size_t size, const void* hash) { + this->filename = (char*) malloc(strlen(filename)); + this->hash = malloc(16); + + strcpy(this->filename, filename); + memcpy(this->hash, hash, 16); + this->size = size; + + this->link = (char*) calloc(13 + (strlen(filename) + 1) + 1 + getDigits(size) + 1 + 32 + 2, 1); + sprintf(this->link, "ed2k://|file|%s|%u|", filename, size); + for (int i = 0; i < 16; i++) { + sprintf(this->link + strlen(this->link), "%02x", *((int*) this->hash + i)); + } + sprintf(this->link + strlen(this->link), "|/"); +} + +ED2KLink::~ED2KLink() { + if (link) { + free(link); + } + if (filename) { + free(filename); + } + if (hash) { + free(hash); + } +} + diff --git a/ED2KLink.h b/ED2KLink.h new file mode 100644 index 0000000..465478f --- /dev/null +++ b/ED2KLink.h @@ -0,0 +1,24 @@ +/** + * © 2008 by David ‘Bombe’ Roden + */ + +#pragma once + +class ED2KLink { + +public: + ED2KLink(const char* filename, const size_t size, const void* hash); + ~ED2KLink(); + + const char* getLink(); + const char* getFilename(); + const size_t getSize(); + const void* getHash(); + +private: + char* link; + char* filename; + size_t size; + void* hash; +}; +