aboutsummaryrefslogtreecommitdiffstats
path: root/lib/bufferstore.cc
diff options
context:
space:
mode:
authorFritz Elfert <felfert@to.com>1999-06-28 23:51:15 +0000
committerFritz Elfert <felfert@to.com>1999-06-28 23:51:15 +0000
commitbd48f19dd8931c871c9b79371ff68cf87d2ccc11 (patch)
tree2b024f2b707fb97fc0796611fb4e7527afcad876 /lib/bufferstore.cc
parent7c9e56cea427fb98d84056482f801c4d5264f40e (diff)
downloadplptools-bd48f19dd8931c871c9b79371ff68cf87d2ccc11.tar.gz
plptools-bd48f19dd8931c871c9b79371ff68cf87d2ccc11.tar.bz2
plptools-bd48f19dd8931c871c9b79371ff68cf87d2ccc11.zip
Optimization for speed.
Diffstat (limited to 'lib/bufferstore.cc')
-rw-r--r--lib/bufferstore.cc158
1 files changed, 81 insertions, 77 deletions
diff --git a/lib/bufferstore.cc b/lib/bufferstore.cc
index 8138ad2..886b952 100644
--- a/lib/bufferstore.cc
+++ b/lib/bufferstore.cc
@@ -26,141 +26,145 @@
#include "bufferstore.h"
bufferStore::bufferStore() {
- lenAllocd = 0;
- buff = NULL;
- len = 0;
- start = 0;
+ lenAllocd = 0;
+ buff = NULL;
+ len = 0;
+ start = 0;
}
bufferStore::bufferStore(const bufferStore &a) {
- lenAllocd = (a.getLen() > MIN_LEN) ? a.getLen() : MIN_LEN;
- buff = new unsigned char [lenAllocd];
- len = a.getLen();
- for (long i=0; i<len; i++) buff[i] = a.getByte(i);
- start = 0;
+ lenAllocd = (a.getLen() > MIN_LEN) ? a.getLen() : MIN_LEN;
+ buff = new unsigned char [lenAllocd];
+ len = a.getLen();
+ memcpy(buff, a.getString(0), len);
+ start = 0;
}
bufferStore::bufferStore(const unsigned char*_buff, long _len) {
- lenAllocd = (_len > MIN_LEN) ? _len : MIN_LEN;
- buff = new unsigned char [lenAllocd];
- len = _len;
- for (long i=0; i<len; i++) buff[i] = _buff[i];
- start = 0;
+ lenAllocd = (_len > MIN_LEN) ? _len : MIN_LEN;
+ buff = new unsigned char [lenAllocd];
+ len = _len;
+ memcpy(buff, _buff, len);
+ start = 0;
}
void bufferStore::operator =(const bufferStore &a) {
- checkAllocd(a.getLen());
- len = a.getLen();
- for (long i=0; i<len; i++) buff[i] = a.getByte(i);
- start = 0;
+ checkAllocd(a.getLen());
+ len = a.getLen();
+ memcpy(buff, a.getString(0), len);
+ start = 0;
}
void bufferStore::init() {
- start = 0;
- len = 0;
+ start = 0;
+ len = 0;
}
void bufferStore::init(const unsigned char*_buff, long _len) {
- checkAllocd(_len);
- start = 0;
- len = _len;
- for (long i=0; i<len; i++) buff[i] = _buff[i];
+ checkAllocd(_len);
+ start = 0;
+ len = _len;
+ memcpy(buff, _buff, len);
}
bufferStore::~bufferStore() {
- delete [] buff;
+ delete [] buff;
}
unsigned long bufferStore::getLen() const {
- if (start > len) return 0;
- return len - start;
+ return (start > len) ? 0 : len - start;
}
unsigned char bufferStore::getByte(long pos) const {
- return buff[pos+start];
+ return buff[pos+start];
}
unsigned int bufferStore::getWord(long pos) const {
- return buff[pos+start] + (buff[pos+start+1] << 8);
+ return buff[pos+start] + (buff[pos+start+1] << 8);
}
unsigned int bufferStore::getDWord(long pos) const {
- return buff[pos+start] +
- (buff[pos+start+1] << 8) +
- (buff[pos+start+2] << 16) +
- (buff[pos+start+3] << 24);
+ return buff[pos+start] +
+ (buff[pos+start+1] << 8) +
+ (buff[pos+start+2] << 16) +
+ (buff[pos+start+3] << 24);
}
const char* bufferStore::getString(long pos) const {
- return (const char*)buff+pos+start;
+ return (const char*)buff+pos+start;
}
ostream &operator<<(ostream &s, const bufferStore &m) {
- {
- for (int i = m.start; i < m.len; i++)
- s << hex << setw(2) << setfill('0') << (int)m.buff[i] << " ";
- }
- s << "(";
- {
- for (int i = m.start; i < m.len; i++) {
- unsigned char c = m.buff[i];
- if (c>=' ' && c <= 'z') s << c;
- }
- }
- s<< ")";
- return s;
+ {
+ for (int i = m.start; i < m.len; i++)
+ s << hex << setw(2) << setfill('0') << (int)m.buff[i] << " ";
+ }
+ s << "(";
+ {
+ for (int i = m.start; i < m.len; i++) {
+ unsigned char c = m.buff[i];
+ if (c>=' ' && c <= 'z') s << c;
+ }
+ }
+ s<< ")";
+ return s;
}
void bufferStore::discardFirstBytes(int n) {
- start += n;
- if (start > len) start = len;
+ start += n;
+ if (start > len) start = len;
}
void bufferStore::checkAllocd(long newLen) {
- if (newLen >= lenAllocd) {
- do {
- if (lenAllocd < MIN_LEN)
- lenAllocd = MIN_LEN;
- else
- lenAllocd *= 2;
- } while (newLen >= lenAllocd);
- unsigned char* newBuff = new unsigned char [lenAllocd];
- for (int i=start; i<len; i++) newBuff[i] = buff[i];
- delete [] buff;
- buff = newBuff;
- }
+ if (newLen >= lenAllocd) {
+ do {
+ lenAllocd = (lenAllocd < MIN_LEN)?MIN_LEN:(lenAllocd*2);
+ } while (newLen >= lenAllocd);
+ unsigned char* newBuff = new unsigned char [lenAllocd];
+ memcpy(&newBuff[start], &buff[start], len - start);
+ delete [] buff;
+ buff = newBuff;
+ }
}
void bufferStore::addByte(unsigned char cc) {
- checkAllocd(len + 1);
- buff[len++] = cc;
+ checkAllocd(len + 1);
+ buff[len++] = cc;
}
void bufferStore::addString(const char* s) {
- checkAllocd(len + strlen(s));
- for (int i=0; s[i]; i++) buff[len++] = s[i];
+ int l = strlen(s);
+ checkAllocd(len + l);
+ memcpy(&buff[len], s, l);
+ len += l;
}
void bufferStore::addStringT(const char* s) {
- addString(s);
- addByte(0);
+ addString(s);
+ addByte(0);
}
void bufferStore::addBuff(const bufferStore &s, long maxLen) {
- checkAllocd(len + s.getLen());
- for (unsigned long i=0; i < s.getLen() && (maxLen < 0 || i < (unsigned long)maxLen); i++) buff[len++] = s.getByte(i);
+ long l = s.getLen();
+ checkAllocd(len + l);
+ if ((maxLen >= 0) && (maxLen < l))
+ l = maxLen;
+ if (l > 0) {
+ memcpy(&buff[len], s.getString(0), l);
+ len += l;
+ }
}
void bufferStore::addWord(int a) {
- checkAllocd(len + 2);
- buff[len++] = a & 0xff;
- buff[len++] = (a>>8) & 0xff;
+ checkAllocd(len + 2);
+ buff[len++] = a & 0xff;
+ buff[len++] = (a>>8) & 0xff;
}
void bufferStore::addDWord(long a) {
- checkAllocd(len + 4);
- buff[len++] = a & 0xff;
- buff[len++] = (a>>8) & 0xff;
- buff[len++] = (a>>16) & 0xff;
- buff[len++] = (a>>24) & 0xff;
+ checkAllocd(len + 4);
+ buff[len++] = a & 0xff;
+ buff[len++] = (a>>8) & 0xff;
+ buff[len++] = (a>>16) & 0xff;
+ buff[len++] = (a>>24) & 0xff;
}