plptools
Loading...
Searching...
No Matches
bufferstore.h
Go to the documentation of this file.
1/*
2 * This file is part of plptools.
3 *
4 * Copyright (C) 1999 Philip Proudman <philip.proudman@btinternet.com>
5 * Copyright (C) 1999-2001 Fritz Elfert <felfert@to.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * along with this program; if not, see <https://www.gnu.org/licenses/>.
19 *
20 */
21#pragma once
22
23#include "config.h"
24
25#include <iostream>
26#include <cstdint>
27
28#include <sys/types.h>
29
37public:
42
50 BufferStore(const unsigned char *buf, long len);
51
56
64 BufferStore(const BufferStore &b);
65
70
76 unsigned long getLen() const;
77
85 unsigned char getByte(long pos = 0) const;
86
94 uint16_t getWord(long pos = 0) const;
95
103 uint32_t getDWord(long pos = 0) const;
104
112 int32_t getSDWord(long pos = 0) const;
113
121 const char * getString(long pos = 0) const;
122
128 void discardFirstBytes(int len = 0);
129
138 friend std::ostream &operator<<(std::ostream &s, const BufferStore &m);
139
145 bool empty() const;
146
153 void init();
154
162 void init(const unsigned char * buf, long len);
163
169 void addByte(unsigned char c);
170
176 void addWord(int w);
177
183 void addDWord(long dw);
184
193 void addString(const char *s);
194
203 void addStringT(const char *s);
204
211 void addBytes(const unsigned char *buf, int len);
212
222 void addBuff(const BufferStore &b, long maxLen = -1);
223
230 void truncate(long newLen);
231
237 void prependByte(unsigned char c);
238
244 void prependWord(int w);
245
246private:
247 void checkAllocd(long newLen);
248
249 long len;
251 long start;
252 unsigned char * buff;
253
254 enum c { MIN_LEN = 300 };
255};
256
257inline bool BufferStore::empty() const {
258 return (len - start) == 0;
259}
A generic container for an array of bytes.
Definition: bufferstore.h:36
unsigned char * buff
Definition: bufferstore.h:252
const char * getString(long pos=0) const
Retrieves the characters at index pos.
Definition: bufferstore.cc:118
void prependByte(unsigned char c)
Prepends a byte to the content of this instance.
Definition: bufferstore.cc:211
uint16_t getWord(long pos=0) const
Retrieves the word at index pos.
Definition: bufferstore.cc:100
~BufferStore()
Destroys a BufferStore instance.
Definition: bufferstore.cc:86
uint32_t getDWord(long pos=0) const
Retrieves the dword at index pos.
Definition: bufferstore.cc:104
void truncate(long newLen)
Truncates the buffer.
Definition: bufferstore.cc:205
void prependWord(int w)
Prepends a word to the content of this instance.
Definition: bufferstore.cc:217
void addByte(unsigned char c)
Appends a byte to the content of this instance.
Definition: bufferstore.cc:157
void addWord(int w)
Appends a word to the content of this instance.
Definition: bufferstore.cc:191
void addBuff(const BufferStore &b, long maxLen=-1)
Appends data to the content of this instance.
Definition: bufferstore.cc:180
void addStringT(const char *s)
Appends a string to the content of this instance.
Definition: bufferstore.cc:169
void addDWord(long dw)
Appends a dword to the content of this instance.
Definition: bufferstore.cc:197
long lenAllocd
Definition: bufferstore.h:250
unsigned long getLen() const
Retrieves the length of a BufferStore.
Definition: bufferstore.cc:92
BufferStore()
Constructs a new BufferStore.
Definition: bufferstore.cc:39
void checkAllocd(long newLen)
Definition: bufferstore.cc:146
bool empty() const
Tests if the BufferStore is empty.
Definition: bufferstore.h:257
unsigned char getByte(long pos=0) const
Retrieves the byte at index pos.
Definition: bufferstore.cc:96
friend std::ostream & operator<<(std::ostream &s, const BufferStore &m)
Prints a dump of the content.
Definition: bufferstore.cc:122
void addBytes(const unsigned char *buf, int len)
Appends data to the content of this instance.
Definition: bufferstore.cc:174
BufferStore & operator=(const BufferStore &)
Copies a BufferStore.
Definition: bufferstore.cc:64
void addString(const char *s)
Appends a string to the content of this instance.
Definition: bufferstore.cc:162
int32_t getSDWord(long pos=0) const
Retrieves the signed dword at index pos.
Definition: bufferstore.cc:111
void discardFirstBytes(int len=0)
Removes bytes from the start of the buffer.
Definition: bufferstore.cc:141
void init()
Initializes the BufferStore.
Definition: bufferstore.cc:74