add parsing of text links
[ecparse.git] / GrowingBuffer.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 <stddef.h>
9 #include "GrowingBuffer.h"
10 #include "GlobalSettings.h"
11
12 GrowingBuffer::GrowingBuffer(size_t initialSize) {
13         this->data = malloc(initialSize);
14         this->size = initialSize;
15         this->limit = 0;
16         this->position = 0;
17         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] created new GrowingBuffer with a size of %d.\n", __FILE__, __LINE__, initialSize);
18 }
19
20 GrowingBuffer::~GrowingBuffer() {
21         if (this->data) {
22                 free(this->data);
23         }
24         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] destroyed GrowingBuffer.\n", __FILE__, __LINE__);
25 }
26
27 size_t GrowingBuffer::getPosition() {
28         return position;
29 }
30
31 size_t GrowingBuffer::getLimit() {
32         return limit;
33 }
34
35 size_t GrowingBuffer::getSize() {
36         return size;
37 }
38
39 void GrowingBuffer::seek(size_t position) {
40         this->position = (position > limit) ? limit : position;
41         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] setting position to %d.\n", __FILE__, __LINE__, position);
42 }
43
44 void* GrowingBuffer::read(void* buffer, size_t length) {
45         size_t bytesToCopy = (length > (limit - position)) ? (limit - position) : length;
46         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] copying %d bytes (%d requested) into buffer.\n", __FILE__, __LINE__, bytesToCopy, length);
47         memcpy(buffer, (ptrdiff_t*) data + position, bytesToCopy);
48         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] advancing position from %d to %d.\n", __FILE__, __LINE__, position, position + bytesToCopy);
49         position += bytesToCopy;
50         return buffer;
51 }
52
53 void GrowingBuffer::write(const void* buffer, size_t length) {
54         if (length > (size - limit)) {
55                 int newSize = size;
56                 do {
57                         newSize *= 2;
58                 } while (length > (newSize - limit));
59                 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] resizing buffer from %d to %d bytes to fit in %d bytes.\n", __FILE__, __LINE__, size, newSize, length);
60                 void* newData = malloc(newSize);
61                 memcpy(newData, data, position);
62                 free(data);
63                 data = newData;
64         }
65         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] writing %d bytes to buffer at position %d.\n", __FILE__, __LINE__, length, limit);
66         memcpy((ptrdiff_t*) data + limit, buffer, length);
67         limit += length;
68 }
69
70 void GrowingBuffer::cut() {
71         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] discarding %d bytes.\n", __FILE__, __LINE__, position);
72         memcpy(data, (ptrdiff_t*) data + position, position);
73         limit = position;
74         position = 0;
75 }
76
77 void GrowingBuffer::truncate() {
78         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] truncating %d bytes at position %d.\n", __FILE__, __LINE__, (limit - position), position);
79         limit = position;
80 }
81
82 void GrowingBuffer::clear() {
83         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] clearing buffer, discarding %d bytes.\n", __FILE__, __LINE__, limit);
84         position = 0;
85         limit = 0;
86 }
87
88 void GrowingBuffer::resize(double factor) {
89         if (factor < 1.0) {
90                 factor = 1.0;
91         }
92         if (size != (limit * factor)) {
93                 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] resizing buffer from %d to %d bytes.\n", __FILE__, __LINE__, size, (size_t) (limit * factor));
94                 void* newData = malloc((size_t) (limit * factor));
95                 memcpy(newData, data, limit);
96                 free(data);
97                 data = newData;
98         }
99 }
100