summaryrefslogtreecommitdiff
path: root/lib/Buffer.cc
blob: 9b8aa8d3516ff28a680cb81a3f3de5d7a207f80b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
 *  Baltisot
 *  Copyright (C) 1999-2008 Nicolas "Pixel" Noble
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <string.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "Buffer.h"
#include "generic.h"
#include "Atomic.h"

int Buffer::nb_buffer = 0;

int Buffer::GetNbBuffer() {
    return nb_buffer;
}

Buffer::Buffer(bool _seekable) : Handle(-1), buffer(0), zero(0), realsiz(0), bufsiz(0), ptr(0), wptr(0), seekable(_seekable), got_eof(false) {
    Atomic::Increment(&nb_buffer);
}

Buffer::~Buffer() {
    free(buffer);
    Atomic::Decrement(&nb_buffer);
}

Buffer::Buffer(const Buffer & b) : Handle(-1), buffer(0), zero(b.zero), realsiz(b.realsiz), bufsiz(b.bufsiz), ptr(b.ptr), wptr(b.wptr), seekable(b.seekable), got_eof(b.got_eof) {
    buffer = (Byte *) malloc(bufsiz);
    memcpy(buffer, b.buffer, bufsiz);
    Atomic::Increment(&nb_buffer);
}

ssize_t Buffer::write(const void *buf, size_t count) throw (GeneralException) {
    if (!count) {
	return 0;
    }
    if (count + wptr > bufsiz) {
	int numblocks = (count + wptr) / realloc_threshold;
	int remains = (count + wptr) % realloc_threshold;
	buffer = (Byte *) realloc(buffer, bufsiz = ((numblocks + (remains ? 1 : 0)) * realloc_threshold));
    }
    memcpy(buffer + wptr, buf, count);
    wptr += count;

    if (wptr > realsiz) {
        realsiz = wptr;
    }
    
    return count;
}

ssize_t Buffer::read(void *buf, size_t count) throw (GeneralException) {
    count = MIN(count, realsiz - ptr);

    if (!count) {
        if (got_eof)
            close();
        got_eof = true;
	return 0;
    }
    
    got_eof = false;
    
    if (buf)
        memcpy(buf, buffer + ptr, count);
    ptr += count;

    if (!seekable) {
        if (ptr >= realloc_threshold) {
	    int numblocks = (bufsiz / realloc_threshold) - (ptr / realloc_threshold);
	    memmove(buffer, buffer + (bufsiz - numblocks * realloc_threshold), numblocks * realloc_threshold);
	    ptr -= (bufsiz - numblocks * realloc_threshold);
	    wptr -= (bufsiz - numblocks * realloc_threshold);
	    realsiz -= (bufsiz - numblocks * realloc_threshold);
	    buffer = (Byte *) realloc(buffer, bufsiz = (numblocks * realloc_threshold));
        }
    }
    
    return count;
}

bool Buffer::CanRead() const {
    return true;
}

bool Buffer::CanWrite() const {
    return true;
}

String Buffer::GetName() const {
    if (fake_name.strlen() != 0)
        return fake_name;
    if (seekable)
        return "Buffer";
    else
        return "Fifo";
}

Buffer Buffer::operator=(const Buffer & b) {
    if (b.buffer != buffer) {
	free(buffer);
	realsiz = b.realsiz;
	ptr = b.ptr;
        wptr = b.wptr;
        seekable = b.seekable;
	if ((bufsiz = b.bufsiz)) {
	    buffer = (Byte *) malloc(bufsiz);
	    memcpy(buffer, b.buffer, realsiz);
	} else {
	    buffer = 0;
	}
    }
    return *this;
}

bool Buffer::CanWatch() const {
    return false;
}

ssize_t Buffer::GetSize() const {
    return realsiz;
}

ssize_t Buffer::GetRemaining() const {
    return realsiz - ptr;
}

Byte Buffer::operator[](size_t p) const {
    if (p >= realsiz) {
	return 0;
    } else {
        if (seekable) {
            return buffer[p];
        } else {
            return buffer[ptr + p];
        }
    }
}

Byte & Buffer::operator[](size_t p) {
    p++;
    if (p > bufsiz) {
	int numblocks = p / realloc_threshold;
	int remains = p % realloc_threshold;
	buffer = (Byte *) realloc(buffer, bufsiz = ((numblocks + (remains ? 1 : 0)) * realloc_threshold));
    }
    if (p > realsiz) {
        memset(buffer + realsiz, 0, p - realsiz);
        realsiz = p;
    }
    p--;

    if (seekable) {
        return buffer[p];
    } else {
        return buffer[ptr + p];
    }
}

const Byte * Buffer::GetBuffer() const {
    if (seekable) {
        return buffer;
    } else {
        return buffer + ptr;
    }
}

bool Buffer::CanSeek() const {
    return seekable;
}

off_t Buffer::seek(off_t off, int wheel) throw (GeneralException) {
    size_t old_ptr;
    if (!seekable) {
        throw GeneralException("This buffer is a fifo, thus is not seekable");
    }
    old_ptr = ptr;
    switch (wheel) {
    case SEEK_SET:
        ptr = off;
        break;
    case SEEK_CUR:
        ptr += off;
        break;
    case SEEK_END:
        ptr = realsiz + off;
        break;
    }
    if (ptr < 0) {
        ptr = old_ptr;
        throw GeneralException("Can't seek a Buffer before its start.");
    }
    operator[](ptr);
    return ptr;
}

off_t Buffer::tell() const {
    return ptr;
}

off_t Buffer::wseek(off_t off, int wheel) throw (GeneralException) {
    if (!seekable) {
        throw GeneralException("This buffer is a fifo, thus is not seekable");
    }
    switch (wheel) {
    case SEEK_SET:
        wptr = off;
        break;
    case SEEK_CUR:
        wptr += off;
        break;
    case SEEK_END:
        wptr = realsiz + off;
        break;
    }
    operator[](wptr);
    return wptr;
}

off_t Buffer::wtell() const {
    return wptr;
}

void Buffer::reset() {
    free(buffer);
    realsiz = bufsiz = ptr = wptr = 0;
    got_eof = false;
}

void Buffer::SetFakeName(const String & _fake_name) {
    fake_name = _fake_name;
}