summaryrefslogtreecommitdiff
path: root/lib/Buffer.cc
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Buffer.cc')
-rw-r--r--lib/Buffer.cc416
1 files changed, 208 insertions, 208 deletions
diff --git a/lib/Buffer.cc b/lib/Buffer.cc
index d3e836c..9d2f66a 100644
--- a/lib/Buffer.cc
+++ b/lib/Buffer.cc
@@ -1,208 +1,208 @@
-/*
- * Baltisot
- * Copyright (C) 1999-2003 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
- */
-
-/* $Id: Buffer.cc,v 1.24 2004-11-27 21:35:19 pixel Exp $ */
-
-#include <string.h>
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-#include "Buffer.h"
-#include "generic.h"
-
-Buffer::Buffer(bool _seekable) : Handle(-1), buffer(0), zero(0), realsiz(0), bufsiz(0), ptr(0), wptr(0), seekable(_seekable) { }
-
-Buffer::~Buffer() {
- free(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) {
- buffer = (Byte *) malloc(bufsiz);
- memcpy(buffer, b.buffer, bufsiz);
-}
-
-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) {
- return 0;
- }
-
- 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 (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;
-}
-
-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];
- }
-}
-
-bool Buffer::CanSeek() const {
- return seekable;
-}
-
-off_t Buffer::seek(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:
- ptr = off;
- break;
- case SEEK_CUR:
- ptr += off;
- break;
- case SEEK_END:
- ptr = realsiz + off;
- break;
- }
- 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;
-}
+/*
+ * Baltisot
+ * Copyright (C) 1999-2003 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
+ */
+
+/* $Id: Buffer.cc,v 1.25 2004-11-27 21:46:04 pixel Exp $ */
+
+#include <string.h>
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include "Buffer.h"
+#include "generic.h"
+
+Buffer::Buffer(bool _seekable) : Handle(-1), buffer(0), zero(0), realsiz(0), bufsiz(0), ptr(0), wptr(0), seekable(_seekable) { }
+
+Buffer::~Buffer() {
+ free(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) {
+ buffer = (Byte *) malloc(bufsiz);
+ memcpy(buffer, b.buffer, bufsiz);
+}
+
+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) {
+ return 0;
+ }
+
+ 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 (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;
+}
+
+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];
+ }
+}
+
+bool Buffer::CanSeek() const {
+ return seekable;
+}
+
+off_t Buffer::seek(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:
+ ptr = off;
+ break;
+ case SEEK_CUR:
+ ptr += off;
+ break;
+ case SEEK_END:
+ ptr = realsiz + off;
+ break;
+ }
+ 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;
+}