add definition of verbose
[ecparse.git] / ED2KLink.cpp
1 /**
2  * © 2008 by David ‘Bombe’ Roden <bombe@pterodactylus.net>
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include "ED2KLink.h"
9 #include "GrowingBuffer.h"
10 #include "GlobalSettings.h"
11 #include "GlobalSettings.h"
12
13 static int getDigits(size_t number) {
14         int digits = 0;
15         do {
16                 number /= 10;
17                 digits++;
18         } while (number);
19         return digits;
20 }
21
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);
25
26         strcpy(this->filename, filename);
27         memcpy(this->hash, hash, 16);
28         this->size = size;
29
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));
34         }
35         sprintf(this->link + strlen(this->link), "|/");
36 }
37
38 ED2KLink::~ED2KLink() {
39         if (link) {
40                 free(link);
41         }
42         if (filename) {
43                 free(filename);
44         }
45         if (hash) {
46                 free(hash);
47         }
48 }
49
50 ED2KLink* ED2KLink::parseED2KLink(const char* buffer) {
51         ED2KLink* ed2kLink;
52         const char* tempBuffer = buffer;
53         char* filename;
54         char* sizeString;
55         size_t size;
56         int hashIndex;
57         char hash[16];
58         GrowingBuffer growingBuffer;
59
60         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] trying to parse “%s”...\n", __FILE__, __LINE__, buffer);
61
62         if (strncmp("ed2k://|file|", buffer, 13)) {
63                 return NULL;
64         }
65
66         tempBuffer += 13;
67         while (*tempBuffer != '|') {
68                 growingBuffer.write(tempBuffer++, 1);
69         }
70         filename = (char*) malloc(growingBuffer.getLimit() + 1);
71         growingBuffer.read(filename, growingBuffer.getLimit());
72         growingBuffer.clear();
73         tempBuffer++;
74
75         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] parsed filename: “%s”.\n", __FILE__, __LINE__, filename);
76
77         while (*tempBuffer != '|') {
78                 growingBuffer.write(tempBuffer++, 1);
79         }
80         sizeString = (char*) malloc(growingBuffer.getLimit() + 1);
81         growingBuffer.read(sizeString, growingBuffer.getLimit());
82         growingBuffer.clear();
83         tempBuffer++;
84
85         size = atol(sizeString);
86         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] parsed size “%s” into %d.\n", __FILE__, __LINE__, sizeString, size);
87
88         for (hashIndex = 0; hashIndex < 16; hashIndex++, tempBuffer += 2) {
89                 sscanf(tempBuffer, "%hhx", hash + hashIndex);
90         }
91
92         ed2kLink = new ED2KLink(filename, size, hash);
93         return ed2kLink;
94 }
95
96 const char* ED2KLink::getLink() {
97         return this->link;
98 }
99
100 const char* ED2KLink::getFilename() {
101         return this->filename;
102 }
103
104 const size_t ED2KLink::getSize() {
105         return this->size;
106 }
107
108 const void* ED2KLink::getHash() {
109         return this->hash;
110 }
111
112