mirror of
https://github.com/FirebirdSQL/firebird.git
synced 2025-01-24 00:03:03 +01:00
Little corrections in clumplet handler classes
This commit is contained in:
parent
54e08acaaa
commit
b47700523d
@ -40,7 +40,7 @@ void ClumpletReader::moveNext() {
|
||||
}
|
||||
|
||||
size_t length = clumplet[1];
|
||||
current_pos += length + 1;
|
||||
cur_offset += length + 1;
|
||||
}
|
||||
|
||||
// Methods which work with currently selected clumplet
|
||||
@ -51,7 +51,7 @@ UCHAR ClumpletReader::getClumpTag() {
|
||||
// Check for EOF
|
||||
if (clumplet >= buffer_end) {
|
||||
read_past_eof();
|
||||
return;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return clumplet[0];
|
||||
@ -64,7 +64,7 @@ size_t ClumpletReader::getClumpLength() {
|
||||
// Check for EOF
|
||||
if (clumplet >= buffer_end) {
|
||||
read_past_eof();
|
||||
return;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// See if we have length byte available
|
||||
@ -79,6 +79,8 @@ size_t ClumpletReader::getClumpLength() {
|
||||
invalid_structure();
|
||||
return buffer_end - clumplet - 1;
|
||||
}
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
SLONG ClumpletReader::getInt() {
|
||||
@ -124,7 +126,7 @@ SINT64 ClumpletReader::getBigInt() {
|
||||
string& ClumpletReader::getString(string& str) {
|
||||
UCHAR* clumplet = getBuffer() + cur_offset;
|
||||
size_t length = getClumpLength();
|
||||
str.assign(clumplet + 2, length);
|
||||
str.assign(reinterpret_cast<char*>(clumplet + 2), length);
|
||||
return str;
|
||||
}
|
||||
|
||||
|
@ -24,12 +24,12 @@
|
||||
* Contributor(s): ______________________________________.
|
||||
*
|
||||
*
|
||||
* $Id: ClumpletReader.h,v 1.1 2004-10-22 06:24:40 skidder Exp $
|
||||
* $Id: ClumpletReader.h,v 1.2 2004-10-23 01:21:11 skidder Exp $
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CLUMPLET_BUFFER_H
|
||||
#define CLUMPLET_BUFFER_H
|
||||
#ifndef CLUMPLETREADER_H
|
||||
#define CLUMPLETREADER_H
|
||||
|
||||
#include "../common/classes/alloc.h"
|
||||
#include "../common/classes/array.h"
|
||||
@ -46,7 +46,7 @@ public:
|
||||
static_buffer_end(buffer + buffLen) { };
|
||||
|
||||
// Navigation in clumplet buffer
|
||||
bool isEof() { return current_pos >= getBufferLength(); }
|
||||
bool isEof() { return cur_offset >= getBufferLength(); }
|
||||
void moveNext();
|
||||
|
||||
// Methods which work with currently selected clumplet
|
||||
@ -56,11 +56,13 @@ public:
|
||||
SLONG getInt();
|
||||
SINT64 getBigInt();
|
||||
string& getString(string& str);
|
||||
const char* getBytes() { return clumplet + 2; }
|
||||
const UCHAR* getBytes() { return getBuffer() + cur_offset + 2; }
|
||||
|
||||
// Return the tag for buffer (usually structure version)
|
||||
UCHAR getBufferTag();
|
||||
size_t getBufferLength() { return getBufferEnd() - getBuffer(); }
|
||||
size_t getCurOffset() { return cur_offset; }
|
||||
size_t setCurOffset(size_t newOffset) { cur_offset = newOffset; }
|
||||
protected:
|
||||
size_t cur_offset;
|
||||
|
||||
@ -80,7 +82,7 @@ private:
|
||||
|
||||
const UCHAR* static_buffer;
|
||||
const UCHAR* static_buffer_end;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace Firebird
|
||||
|
||||
|
@ -24,7 +24,7 @@
|
||||
* Contributor(s): ______________________________________.
|
||||
*
|
||||
*
|
||||
* $Id: ClumpletWriter.cpp,v 1.1 2004-10-22 06:24:40 skidder Exp $
|
||||
* $Id: ClumpletWriter.cpp,v 1.2 2004-10-23 01:21:11 skidder Exp $
|
||||
*
|
||||
*/
|
||||
|
||||
@ -35,18 +35,37 @@
|
||||
|
||||
namespace Firebird {
|
||||
|
||||
ClumpletWriter(UCHAR tag, size_t limit) :
|
||||
ClumpletReader(NULL, 0), sizeLimit(limit), dynamic_buffer(getPool(), 128)
|
||||
{
|
||||
dynamic_buffer.add(tag);
|
||||
}
|
||||
|
||||
ClumpletWriter(const UCHAR* buffer, size_t buffLen, size_t limit) :
|
||||
ClumpletReader(NULL, 0), sizeLimit(limit), dynamic_buffer(getPool(), buffer, buffLen)
|
||||
ClumpletWriter::ClumpletWriter(size_t limit) :
|
||||
ClumpletReader(NULL, 0), sizeLimit(limit), dynamic_buffer(getPool())
|
||||
{
|
||||
}
|
||||
|
||||
virtual void ClumpletWriter::size_overflow() {
|
||||
|
||||
ClumpletWriter::ClumpletWriter(UCHAR tag, size_t limit) :
|
||||
ClumpletReader(NULL, 0), sizeLimit(limit), dynamic_buffer(getPool())
|
||||
{
|
||||
dynamic_buffer += static_cast<char>(tag);
|
||||
}
|
||||
|
||||
ClumpletWriter::ClumpletWriter(const UCHAR* buffer, size_t buffLen, size_t limit) :
|
||||
ClumpletReader(NULL, 0), sizeLimit(limit),
|
||||
dynamic_buffer(getPool(), reinterpret_cast<const char*>(buffer), buffLen)
|
||||
{
|
||||
}
|
||||
|
||||
void ClumpletWriter::reset(UCHAR tag) {
|
||||
cur_offset = 1;
|
||||
dynamic_buffer.resize(1);
|
||||
dynamic_buffer[0] = static_cast<char>(tag);
|
||||
}
|
||||
|
||||
void ClumpletWriter::reset(const UCHAR* buffer, size_t buffLen) {
|
||||
cur_offset = 1;
|
||||
dynamic_buffer.assign(reinterpret_cast<const char*>(buffer), buffLen);
|
||||
}
|
||||
|
||||
|
||||
void ClumpletWriter::size_overflow() {
|
||||
fatal_exception::raise("Clumplet buffer size limit reached");
|
||||
}
|
||||
|
||||
@ -60,7 +79,7 @@ void ClumpletWriter::insertInt(UCHAR tag, SLONG value) {
|
||||
bytes[3] = ptr[0];
|
||||
insertBytes(tag, bytes, sizeof(bytes));
|
||||
#else
|
||||
insertBytes(tag, static_cast<UCHAR*>(&value), sizeof(value));
|
||||
insertBytes(tag, reinterpret_cast<UCHAR*>(&value), sizeof(value));
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -78,16 +97,16 @@ void ClumpletWriter::insertBigInt(UCHAR tag, SINT64 value) {
|
||||
bytes[7] = ptr[0];
|
||||
insertBytes(tag, bytes, sizeof(bytes));
|
||||
#else
|
||||
insertBytes(tag, static_cast<UCHAR*>(&value), sizeof(value));
|
||||
insertBytes(tag, reinterpret_cast<UCHAR*>(&value), sizeof(value));
|
||||
#endif
|
||||
}
|
||||
|
||||
void ClumpletWriter::insertString(UCHAR tag, const string& str) {
|
||||
if (str.length() > 255) {
|
||||
size_overflow();
|
||||
insertBytes(str.c_str(), 255);
|
||||
insertBytes(tag, reinterpret_cast<const UCHAR*>(str.c_str()), 255);
|
||||
} else
|
||||
insertBytes(str.c_str(), str.length());
|
||||
insertBytes(tag, reinterpret_cast<const UCHAR*>(str.c_str()), str.length());
|
||||
}
|
||||
|
||||
void ClumpletWriter::insertBytes(UCHAR tag, const UCHAR* bytes, UCHAR len) {
|
||||
@ -106,7 +125,7 @@ void ClumpletWriter::insertBytes(UCHAR tag, const UCHAR* bytes, UCHAR len) {
|
||||
// Insert the data
|
||||
dynamic_buffer.insert(cur_offset, static_cast<char>(tag));
|
||||
dynamic_buffer.insert(cur_offset + 1, static_cast<char>(len));
|
||||
dynamic_buffer.insert(cur_offset + 2, static_cast<char*>(bytes));
|
||||
dynamic_buffer.insert(cur_offset + 2, reinterpret_cast<const char*>(bytes));
|
||||
cur_offset += len + 2;
|
||||
}
|
||||
|
||||
@ -119,11 +138,11 @@ void ClumpletWriter::insertEndMarker(UCHAR tag) {
|
||||
}
|
||||
|
||||
// Check that resulting data doesn't overflow size limit
|
||||
if (current_offset + 1 > sizeLimit) {
|
||||
if (cur_offset + 1 > sizeLimit) {
|
||||
size_overflow();
|
||||
}
|
||||
|
||||
dynamic_buffer.erase(cur_offset, npos);
|
||||
dynamic_buffer.erase(cur_offset, string::npos);
|
||||
dynamic_buffer += static_cast<char>(tag);
|
||||
|
||||
cur_offset += 2; // Go past EOF to indicate we set the marker
|
||||
@ -141,7 +160,7 @@ void ClumpletWriter::deleteClumplet() {
|
||||
|
||||
if (buffer_end - clumplet < 2) {
|
||||
// It appears we're erasing EOF marker
|
||||
dynamic_buffer.erase(cur_offset, npos);
|
||||
dynamic_buffer.erase(cur_offset, string::npos);
|
||||
} else {
|
||||
size_t length = clumplet[1];
|
||||
dynamic_buffer.erase(cur_offset, length + 2);
|
||||
|
@ -24,12 +24,12 @@
|
||||
* Contributor(s): ______________________________________.
|
||||
*
|
||||
*
|
||||
* $Id: ClumpletWriter.h,v 1.1 2004-10-22 06:24:40 skidder Exp $
|
||||
* $Id: ClumpletWriter.h,v 1.2 2004-10-23 01:21:11 skidder Exp $
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CLUMPLET_BUFFER_H
|
||||
#define CLUMPLET_BUFFER_H
|
||||
#ifndef CLUMPLETWRITER_H
|
||||
#define CLUMPLETWRITER_H
|
||||
|
||||
#include "../common/classes/ClumpletReader.h"
|
||||
|
||||
@ -38,13 +38,17 @@ namespace Firebird {
|
||||
// At the moment you can only declare it on stack, permanent objects are not allowed
|
||||
class ClumpletWriter : public ClumpletReader {
|
||||
public:
|
||||
// Create new clumplet buffer
|
||||
ClumpletWriter(UCHAR tag, size_t limit) :
|
||||
ClumpletReader(NULL, 0), sizeLimit(limit), dynamic_buffer(getPool()) { }
|
||||
// Create empty clumplet writer (unusable before you call one of reset methods)
|
||||
ClumpletWriter(size_t limit);
|
||||
|
||||
// Create empty, initialized writer
|
||||
ClumpletWriter(UCHAR tag, size_t limit);
|
||||
|
||||
// Open existing clumplet buffer (taking a copy of it)
|
||||
ClumpletWriter(const UCHAR* buffer, size_t buffLen, size_t limit) :
|
||||
ClumpletReader(NULL, 0), sizeLimit(limit), dynamic_buffer(getPool(), buffer, buffLen) { }
|
||||
// Create writer from a given buffer
|
||||
ClumpletWriter(const UCHAR* buffer, size_t buffLen, size_t limit);
|
||||
|
||||
void reset(UCHAR tag);
|
||||
void reset(const UCHAR* buffer, size_t buffLen);
|
||||
|
||||
// Methods to create new clumplet at current position
|
||||
void insertInt(UCHAR tag, SLONG value);
|
||||
@ -56,9 +60,13 @@ public:
|
||||
// Delete currently selected clumplet from buffer
|
||||
void deleteClumplet();
|
||||
|
||||
virtual UCHAR* getBuffer() { return dynamic_buffer.begin(); }
|
||||
virtual UCHAR* getBuffer() {
|
||||
return reinterpret_cast<UCHAR*>(dynamic_buffer.begin());
|
||||
}
|
||||
protected:
|
||||
virtual UCHAR* getBufferEnd() { return dynamic_buffer.end(); }
|
||||
virtual UCHAR* getBufferEnd() {
|
||||
return reinterpret_cast<UCHAR*>(dynamic_buffer.end());
|
||||
}
|
||||
virtual void size_overflow();
|
||||
private:
|
||||
size_t sizeLimit;
|
||||
|
Loading…
Reference in New Issue
Block a user