2 * © 2008 by David ‘Bombe’ Roden <bombe@pterodactylus.net>
9 #include "GrowingBuffer.h"
10 #include "GlobalSettings.h"
11 #include "GlobalSettings.h"
13 static int getDigits(size_t number) {
22 ED2KLink::ED2KLink(const char* filename, const size_t size, const void* hash) {
23 this->filename = (char*) malloc(strlen(filename));
24 this->hash = malloc(16);
26 strcpy(this->filename, filename);
27 memcpy(this->hash, hash, 16);
30 this->link = (char*) calloc(13 + (strlen(filename) + 1) + 1 + getDigits(size) + 1 + 32 + 2, 1);
31 sprintf(this->link, "ed2k://|file|%s|%u|", filename, size);
32 for (int i = 0; i < 16; i++) {
33 sprintf(this->link + strlen(this->link), "%02x", *((int*) this->hash + i));
35 sprintf(this->link + strlen(this->link), "|/");
38 ED2KLink::~ED2KLink() {
50 ED2KLink* ED2KLink::parseED2KLink(const char* buffer) {
52 const char* tempBuffer = buffer;
58 GrowingBuffer growingBuffer;
60 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] trying to parse “%s”...\n", __FILE__, __LINE__, buffer);
62 if (strncmp("ed2k://|file|", buffer, 13)) {
67 while (*tempBuffer != '|') {
68 growingBuffer.write(tempBuffer++, 1);
70 filename = (char*) malloc(growingBuffer.getLimit() + 1);
71 growingBuffer.read(filename, growingBuffer.getLimit());
72 growingBuffer.clear();
75 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] parsed filename: “%s”.\n", __FILE__, __LINE__, filename);
77 while (*tempBuffer != '|') {
78 growingBuffer.write(tempBuffer++, 1);
80 sizeString = (char*) malloc(growingBuffer.getLimit() + 1);
81 growingBuffer.read(sizeString, growingBuffer.getLimit());
82 growingBuffer.clear();
85 size = atol(sizeString);
86 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] parsed size “%s” into %d.\n", __FILE__, __LINE__, sizeString, size);
88 for (hashIndex = 0; hashIndex < 16; hashIndex++, tempBuffer += 2) {
89 sscanf(tempBuffer, "%hhx", hash + hashIndex);
92 ed2kLink = new ED2KLink(filename, size, hash);
96 const char* ED2KLink::getLink() {
100 const char* ED2KLink::getFilename() {
101 return this->filename;
104 const size_t ED2KLink::getSize() {
108 const void* ED2KLink::getHash() {