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