forked from Mirrors/openclonk
Change StdBuf to wrapper around std::string
parent
0d08565059
commit
3bdb703009
|
@ -799,7 +799,7 @@ C4AulTokenType C4AulParseState::GetNextToken(char *pToken, long int *pInt, HoldS
|
||||||
if(HoldStrings == Discard) return ATT_STRING;
|
if(HoldStrings == Discard) return ATT_STRING;
|
||||||
// reg string (if not already done so)
|
// reg string (if not already done so)
|
||||||
C4String *pString;
|
C4String *pString;
|
||||||
pString = a->Engine->Strings.RegString(StdStrBuf(StrBuff,static_cast<long>(pStrPos - StrBuff)));
|
pString = a->Engine->Strings.RegString(StdStrBuf(StrBuff,static_cast<size_t>(pStrPos - StrBuff)));
|
||||||
if (HoldStrings == Hold) pString->Hold = 1;
|
if (HoldStrings == Hold) pString->Hold = 1;
|
||||||
// return pointer on string object
|
// return pointer on string object
|
||||||
*pInt = (long) pString;
|
*pInt = (long) pString;
|
||||||
|
|
|
@ -31,7 +31,15 @@
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
#pragma warning(disable : 4786) // long symbol names
|
#pragma warning(disable : 4786) // long symbol names
|
||||||
#pragma warning(disable : 4996) // POSIX name usage
|
#endif
|
||||||
|
|
||||||
|
// Deprecation
|
||||||
|
#if defined(_MSC_VER)
|
||||||
|
# define DEPRECATED __declspec(deprecated)
|
||||||
|
#elif defined(__GNUC__)
|
||||||
|
# define DEPRECATED __attribute__((__deprecated__))
|
||||||
|
#else
|
||||||
|
# define DEPRECATED
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// debug memory management
|
// debug memory management
|
||||||
|
|
|
@ -23,10 +23,9 @@
|
||||||
#ifndef STDBUF_H
|
#ifndef STDBUF_H
|
||||||
#define STDBUF_H
|
#define STDBUF_H
|
||||||
|
|
||||||
#include <zlib.h>
|
|
||||||
#include "Standard.h"
|
#include "Standard.h"
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <zlib.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
|
||||||
|
@ -35,84 +34,49 @@
|
||||||
#include <crtdbg.h>
|
#include <crtdbg.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
// Base buffer class. Either references or holds data.
|
// Base buffer class. Either references or holds data.
|
||||||
class StdBuf
|
class StdBuf : public std::string
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
// *** Construction
|
// *** Construction
|
||||||
// Standard constructor
|
// Standard constructor
|
||||||
StdBuf() : fRef(true), pData(NULL), iSize(0) { }
|
StdBuf() { }
|
||||||
|
|
||||||
// Constructor from other buffer (copy construction):
|
// Constructor from other buffer (copy construction):
|
||||||
// Will take over buffer ownership. Copies data if specified.
|
// Will take over buffer ownership. Copies data if specified.
|
||||||
// Note: Construct with Buf2.getRef() to construct a reference (This will work for a constant Buf2, too)
|
// Note: Construct with Buf2.getRef() to construct a reference (This will work for a constant Buf2, too)
|
||||||
StdBuf(StdBuf RREF Buf2, bool fCopy = false)
|
StdBuf(const std::string &other, bool = false)
|
||||||
: fRef(true), pData(NULL), iSize(0)
|
: std::string(other)
|
||||||
{
|
{}
|
||||||
if(fCopy)
|
|
||||||
Copy(Buf2);
|
|
||||||
else if(!Buf2.isRef())
|
|
||||||
Take(Buf2);
|
|
||||||
else
|
|
||||||
Ref(Buf2);
|
|
||||||
}
|
|
||||||
#ifdef HAVE_RVALUE_REF
|
|
||||||
StdBuf(const StdBuf & Buf2, bool fCopy = true)
|
|
||||||
: fRef(true), pData(NULL), iSize(0)
|
|
||||||
{
|
|
||||||
if(fCopy)
|
|
||||||
Copy(Buf2);
|
|
||||||
else
|
|
||||||
Ref(Buf2);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Set by constant data. Copies data if desired.
|
// Set by constant data. Copies data if desired.
|
||||||
StdBuf(const void *pData, size_t iSize, bool fCopy = false)
|
StdBuf(const void *pData, size_t iSize, bool = false)
|
||||||
: fRef(true), pData(pData), iSize(iSize)
|
: std::string(static_cast<const char*>(pData), iSize)
|
||||||
{
|
{}
|
||||||
if(fCopy) Copy();
|
|
||||||
}
|
|
||||||
|
|
||||||
~StdBuf()
|
~StdBuf()
|
||||||
{
|
{}
|
||||||
Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
ALLOW_TEMP_TO_REF(StdBuf)
|
ALLOW_TEMP_TO_REF(StdBuf)
|
||||||
|
|
||||||
protected:
|
|
||||||
|
|
||||||
// Reference? Otherwise, this object holds the data.
|
|
||||||
bool fRef;
|
|
||||||
// Data
|
|
||||||
union
|
|
||||||
{
|
|
||||||
const void *pData;
|
|
||||||
void *pMData;
|
|
||||||
#if defined(_DEBUG)
|
|
||||||
char *szString; // for debugger preview
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
size_t iSize;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
// *** Getters
|
// *** Getters
|
||||||
|
|
||||||
bool isNull() const { return ! getData(); }
|
bool isNull() const { return empty(); }
|
||||||
const void *getData() const { return fRef ? pData : pMData; }
|
const void *getData() const { return empty()?NULL:c_str(); }
|
||||||
void *getMData() { assert(!fRef); return pMData; }
|
void *getMData() { return &operator[](0); }
|
||||||
size_t getSize() const { return iSize; }
|
const void *getPtr(size_t i) const { return c_str() + i; }
|
||||||
bool isRef() const { return fRef; }
|
void *getMPtr(size_t i) { return &operator[](i); }
|
||||||
|
size_t getSize() const { return size(); }
|
||||||
|
DEPRECATED bool isRef() const { return true; }
|
||||||
|
|
||||||
const void *getPtr(size_t i) const { return reinterpret_cast<const char*>(getData()) + i; }
|
|
||||||
void *getMPtr(size_t i) { return reinterpret_cast<char*>(getMData()) + i; }
|
|
||||||
|
|
||||||
StdBuf getPart(size_t iStart, size_t inSize) const
|
StdBuf getPart(size_t iStart, size_t inSize) const
|
||||||
{
|
{
|
||||||
assert(iStart + inSize <= iSize);
|
assert(iStart + inSize <= size());
|
||||||
return StdBuf(getPtr(iStart), inSize);
|
return StdBuf(getPtr(iStart), inSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -123,49 +87,38 @@ public:
|
||||||
// Reference given data
|
// Reference given data
|
||||||
void Ref(const void *pnData, size_t inSize)
|
void Ref(const void *pnData, size_t inSize)
|
||||||
{
|
{
|
||||||
Clear();
|
if(pnData)
|
||||||
fRef = true; pData = pnData; iSize = inSize;
|
assign(static_cast<const char*>(pnData), inSize);
|
||||||
|
else
|
||||||
|
clear();
|
||||||
}
|
}
|
||||||
// Take over data (hold it)
|
// Take over data (hold it)
|
||||||
void Take(void *pnData, size_t inSize)
|
void Take(const void *pnData, size_t inSize) { Ref(pnData, inSize); }
|
||||||
{
|
|
||||||
Clear();
|
// Returns a copy of the contents
|
||||||
if(pnData)
|
char *GrabPointer() const
|
||||||
{
|
|
||||||
fRef = false; pMData = pnData; iSize = inSize;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Transfer puffer ownership to the caller
|
|
||||||
void *GrabPointer()
|
|
||||||
{
|
{
|
||||||
if(isNull()) return NULL;
|
if(isNull()) return NULL;
|
||||||
// Do not give out a buffer which someone else will free
|
char *ptr = new char[size()+1];
|
||||||
if (fRef) Copy();
|
copy(ptr, size()); // doesn't null-terminate
|
||||||
void *pMData = getMData();
|
ptr[size()] = '\0';
|
||||||
pData = pMData; fRef = true;
|
return ptr;
|
||||||
return pMData;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// * Buffer data operations
|
// * Buffer data operations
|
||||||
|
|
||||||
// Create new buffer with given size
|
// Create new buffer with given size
|
||||||
void New(size_t inSize)
|
void New(size_t inSize)
|
||||||
{
|
{ resize(inSize); }
|
||||||
Clear();
|
|
||||||
pMData = malloc(iSize = inSize);
|
|
||||||
fRef = false;
|
|
||||||
}
|
|
||||||
// Write data into the buffer
|
// Write data into the buffer
|
||||||
void Write(const void *pnData, size_t inSize, size_t iAt = 0)
|
void Write(const void *pnData, size_t inSize, size_t iAt = 0)
|
||||||
{
|
{
|
||||||
assert(iAt + inSize <= iSize);
|
replace(iAt, inSize, static_cast<const char*>(pnData), inSize);
|
||||||
if(pnData && inSize) memcpy(getMPtr(iAt), pnData, inSize);
|
|
||||||
}
|
}
|
||||||
// Move data around inside the buffer (checks overlap)
|
// Move data around inside the buffer (checks overlap)
|
||||||
void Move(size_t iFrom, size_t inSize, size_t iTo = 0)
|
void Move(size_t iFrom, size_t inSize, size_t iTo = 0)
|
||||||
{
|
{
|
||||||
assert(iFrom + inSize <= iSize); assert(iTo + inSize <= iSize);
|
replace(iTo, inSize, *this, iFrom, inSize);
|
||||||
memmove(getMPtr(iTo), getPtr(iFrom), inSize);
|
|
||||||
}
|
}
|
||||||
// Compare to memory
|
// Compare to memory
|
||||||
int Compare(const void *pCData, size_t iCSize, size_t iAt = 0) const
|
int Compare(const void *pCData, size_t iCSize, size_t iAt = 0) const
|
||||||
|
@ -176,32 +129,23 @@ public:
|
||||||
// Grow the buffer
|
// Grow the buffer
|
||||||
void Grow(size_t iGrow)
|
void Grow(size_t iGrow)
|
||||||
{
|
{
|
||||||
// Grow dereferences
|
resize(size() + iGrow);
|
||||||
if(fRef) { Copy(iSize + iGrow); return; }
|
|
||||||
if(!iGrow) return;
|
|
||||||
// Realloc
|
|
||||||
pMData = realloc(pMData, iSize += iGrow);
|
|
||||||
}
|
}
|
||||||
// Shrink the buffer
|
// Shrink the buffer
|
||||||
void Shrink(size_t iShrink)
|
void Shrink(size_t iShrink)
|
||||||
{
|
{
|
||||||
assert(iSize >= iShrink);
|
assert(size() >= iShrink);
|
||||||
// Shrink dereferences
|
resize(size() - iShrink);
|
||||||
if(fRef) { Copy(iSize - iShrink); return; }
|
|
||||||
if(!iShrink) return;
|
|
||||||
// Realloc
|
|
||||||
pMData = realloc(pMData, iSize -= iShrink);
|
|
||||||
}
|
}
|
||||||
// Clear buffer
|
// Clear buffer
|
||||||
void Clear()
|
void Clear()
|
||||||
{
|
{
|
||||||
if(!fRef) free(pMData);
|
clear();
|
||||||
pMData = NULL; fRef = true; iSize = 0;
|
|
||||||
}
|
}
|
||||||
// Free buffer that had been grabbed
|
// Free buffer that had been grabbed
|
||||||
static void DeletePointer(void *data)
|
static void DeletePointer(const void *data)
|
||||||
{
|
{
|
||||||
free(data);
|
delete[] static_cast<const char*>(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
// * Composed actions
|
// * Composed actions
|
||||||
|
@ -209,10 +153,7 @@ public:
|
||||||
// Set buffer size (dereferences)
|
// Set buffer size (dereferences)
|
||||||
void SetSize(size_t inSize)
|
void SetSize(size_t inSize)
|
||||||
{
|
{
|
||||||
if(inSize > iSize)
|
resize(inSize);
|
||||||
Grow(inSize - iSize);
|
|
||||||
else
|
|
||||||
Shrink(iSize - inSize);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write buffer contents into the buffer
|
// Write buffer contents into the buffer
|
||||||
|
@ -227,62 +168,32 @@ public:
|
||||||
return Compare(Buf2.getData(), Buf2.getSize(), iAt);
|
return Compare(Buf2.getData(), Buf2.getSize(), iAt);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a copy of the data (dereferences, obviously)
|
DEPRECATED void Copy(size_t inSize) {}
|
||||||
void Copy(size_t inSize)
|
DEPRECATED void Copy() {}
|
||||||
{
|
void Copy(const void *pnData, size_t inSize) { Ref(pnData, inSize); }
|
||||||
if(isNull() && !inSize) return;
|
|
||||||
const void *pOldData = getData();
|
|
||||||
size_t iOldSize = iSize;
|
|
||||||
New(inSize);
|
|
||||||
Write(pOldData, Min(iOldSize, inSize));
|
|
||||||
}
|
|
||||||
void Copy()
|
|
||||||
{
|
|
||||||
Copy(iSize);
|
|
||||||
}
|
|
||||||
// Copy data from address
|
|
||||||
void Copy(const void *pnData, size_t inSize)
|
|
||||||
{
|
|
||||||
Ref(pnData, inSize); Copy();
|
|
||||||
}
|
|
||||||
// Copy from another buffer
|
// Copy from another buffer
|
||||||
void Copy(const StdBuf &Buf2)
|
void Copy(const StdBuf &Buf2) { assign(Buf2); }
|
||||||
{
|
|
||||||
Copy(Buf2.getData(), Buf2.getSize());
|
|
||||||
}
|
|
||||||
// Create a copy and return it
|
// Create a copy and return it
|
||||||
StdBuf Duplicate() const
|
StdBuf Duplicate() const { return *this; }
|
||||||
{
|
|
||||||
StdBuf Buf; Buf.Copy(*this); return Buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Append data from address
|
// Append data from address
|
||||||
void Append(const void *pnData, int inSize)
|
void Append(const void *pnData, int inSize)
|
||||||
{
|
{
|
||||||
Grow(inSize);
|
append(static_cast<const char*>(pnData), inSize);
|
||||||
Write(pnData, inSize, iSize - inSize);
|
|
||||||
}
|
}
|
||||||
// Append data from another buffer
|
// Append data from another buffer
|
||||||
void Append(const StdBuf &Buf2)
|
void Append(const StdBuf &Buf2)
|
||||||
{
|
{
|
||||||
Append(Buf2.getData(), Buf2.getSize());
|
append(Buf2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reference another buffer's contents
|
// Reference another buffer's contents
|
||||||
void Ref(const StdBuf &Buf2)
|
void Ref(const StdBuf &Buf2) { assign(Buf2); }
|
||||||
{
|
|
||||||
Ref(Buf2.getData(), Buf2.getSize());
|
|
||||||
}
|
|
||||||
// Create a reference to this buffer's contents
|
// Create a reference to this buffer's contents
|
||||||
StdBuf getRef() const
|
StdBuf getRef() const { return *this; }
|
||||||
{
|
|
||||||
return StdBuf(getData(), getSize());
|
|
||||||
}
|
|
||||||
// take over another buffer's contents
|
// take over another buffer's contents
|
||||||
void Take(StdBuf RREF Buf2)
|
void Take(const StdBuf &Buf2) { assign(Buf2); }
|
||||||
{
|
|
||||||
Take(Buf2.GrabPointer(), Buf2.getSize());
|
|
||||||
}
|
|
||||||
|
|
||||||
// * File support
|
// * File support
|
||||||
bool LoadFromFile(const char *szFile);
|
bool LoadFromFile(const char *szFile);
|
||||||
|
@ -291,6 +202,8 @@ public:
|
||||||
// *** Operators
|
// *** Operators
|
||||||
|
|
||||||
// Null check
|
// Null check
|
||||||
|
operator bool() const { return !empty(); }
|
||||||
|
operator const void*() const { return data(); }
|
||||||
bool operator ! () const { return isNull(); }
|
bool operator ! () const { return isNull(); }
|
||||||
|
|
||||||
// Appending
|
// Appending
|
||||||
|
@ -305,17 +218,10 @@ public:
|
||||||
return Buf;
|
return Buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compare
|
|
||||||
bool operator == (const StdBuf &Buf2) const
|
|
||||||
{
|
|
||||||
return getSize() == Buf2.getSize() && !Compare(Buf2);
|
|
||||||
}
|
|
||||||
bool operator != (const StdBuf &Buf2) const { return ! operator == (Buf2); }
|
|
||||||
|
|
||||||
// Set (as constructor: take if possible)
|
// Set (as constructor: take if possible)
|
||||||
StdBuf &operator = (StdBuf RREF Buf2)
|
StdBuf &operator = (const std::string &Buf2)
|
||||||
{
|
{
|
||||||
if(Buf2.isRef()) Ref(Buf2); else Take(Buf2);
|
assign(Buf2);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -349,225 +255,76 @@ template <class elem_t>
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy-Buffer - Just copies data in the copy constructor.
|
// Copy-Buffer - Just copies data in the copy constructor.
|
||||||
class StdCopyBuf : public StdBuf
|
typedef StdBuf StdCopyBuf;
|
||||||
{
|
|
||||||
public:
|
|
||||||
|
|
||||||
StdCopyBuf()
|
|
||||||
{ }
|
|
||||||
|
|
||||||
// Set by buffer. Copies data by default.
|
|
||||||
StdCopyBuf(const StdBuf &Buf2, bool fCopy = true)
|
|
||||||
: StdBuf(Buf2.getRef(), fCopy)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
// Set by buffer. Copies data by default.
|
|
||||||
StdCopyBuf(const StdCopyBuf &Buf2, bool fCopy = true)
|
|
||||||
: StdBuf(Buf2.getRef(), fCopy)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
// Set by constant data. Copies data by default.
|
|
||||||
StdCopyBuf(const void *pData, size_t iSize, bool fCopy = true)
|
|
||||||
: StdBuf(pData, iSize, fCopy)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
StdCopyBuf &operator = (const StdBuf &Buf2) { Copy(Buf2); return *this; }
|
|
||||||
StdCopyBuf &operator = (const StdCopyBuf &Buf2) { Copy(Buf2); return *this; }
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
// Stringbuffer (operates on null-terminated character buffers)
|
// Stringbuffer (operates on null-terminated character buffers)
|
||||||
class StdStrBuf : protected StdBuf
|
class StdStrBuf : public StdBuf
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
// *** Construction
|
// *** Construction
|
||||||
|
// Standard constructor
|
||||||
|
StdStrBuf() { }
|
||||||
|
|
||||||
StdStrBuf()
|
// Constructor from other buffer (copy construction):
|
||||||
: StdBuf()
|
// Will take over buffer ownership. Copies data if specified.
|
||||||
{ }
|
// Note: Construct with Buf2.getRef() to construct a reference (This will work for a constant Buf2, too)
|
||||||
|
StdStrBuf(const char *data, bool = false)
|
||||||
|
: StdBuf(std::string(data))
|
||||||
|
{}
|
||||||
|
|
||||||
// See StdBuf::StdBuf. Will take data if possible.
|
StdStrBuf(const std::string &other, bool = false)
|
||||||
// The static_cast is necessary to pass a rvalue reference to
|
: StdBuf(other)
|
||||||
// the StdBuf constructor. Without it, the const lvalue
|
{}
|
||||||
// StdBuf constructor will be used, which will ref the contents
|
|
||||||
// instead of moving them.
|
|
||||||
StdStrBuf(StdStrBuf RREF Buf2, bool fCopy = false)
|
|
||||||
: StdBuf(static_cast<StdStrBuf RREF>(Buf2), fCopy)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
#ifdef HAVE_RVALUE_REF
|
// Set by constant data. Copies data if desired.
|
||||||
StdStrBuf(const StdStrBuf & Buf2, bool fCopy = true)
|
StdStrBuf(const char *pData, size_t iSize, bool = false)
|
||||||
: StdBuf(Buf2, fCopy)
|
: StdBuf(pData, iSize)
|
||||||
{ }
|
{}
|
||||||
#endif
|
|
||||||
|
|
||||||
// Set by constant data. References data by default, copies if specified.
|
~StdStrBuf()
|
||||||
explicit StdStrBuf(const char *pData, bool fCopy = false)
|
{}
|
||||||
: StdBuf(pData, pData ? strlen(pData) + 1 : 0, fCopy)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
// As previous constructor, but set length manually.
|
|
||||||
StdStrBuf(const char *pData, long int iLength)
|
|
||||||
: StdBuf(pData, pData ? iLength + 1 : 0, false)
|
|
||||||
{ }
|
|
||||||
StdStrBuf(const char *pData, size_t iLength, bool fCopy = false)
|
|
||||||
: StdBuf(pData, pData ? iLength + 1 : 0, fCopy)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
ALLOW_TEMP_TO_REF(StdStrBuf)
|
ALLOW_TEMP_TO_REF(StdStrBuf)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
// *** Getters
|
// *** Getters
|
||||||
|
|
||||||
bool isNull() const { return StdBuf::isNull(); }
|
|
||||||
const char *getData() const { return getBufPtr<char>(*this); }
|
|
||||||
char *getMData() { return getMBufPtr<char>(*this); }
|
|
||||||
size_t getSize() const { return StdBuf::getSize(); }
|
|
||||||
size_t getLength() const { return getSize() ? getSize() - 1 : 0; }
|
size_t getLength() const { return getSize() ? getSize() - 1 : 0; }
|
||||||
bool isRef() const { return StdBuf::isRef(); }
|
const char *getData() const { return empty()?NULL:c_str(); }
|
||||||
|
char *getMData() { return &operator[](0); }
|
||||||
const char *getPtr(size_t i) const { return getBufPtr<char>(*this, i); }
|
const char *getPtr(size_t i) const { return getData() + i; }
|
||||||
char *getMPtr(size_t i) { return getMBufPtr<char>(*this, i); }
|
char *getMPtr(size_t i) { return getMData() + i; }
|
||||||
|
|
||||||
// For convenience. Note that writing can't be allowed.
|
|
||||||
char operator [] (size_t i) const { return *getPtr(i); }
|
|
||||||
|
|
||||||
// Analogous to StdBuf
|
// Analogous to StdBuf
|
||||||
void Ref(const char *pnData) { StdBuf::Ref(pnData, pnData ? strlen(pnData) + 1 : 0); }
|
using StdBuf::Ref;
|
||||||
void Ref(const char *pnData, size_t iLength) { assert((!pnData && !iLength) || strlen(pnData) == iLength); StdBuf::Ref(pnData, iLength + 1); }
|
void Ref(const char *pnData) { if(pnData) assign(pnData); else clear(); }
|
||||||
void Take(char *pnData) { StdBuf::Take(pnData, pnData ? strlen(pnData) + 1 : 0); }
|
using StdBuf::Take;
|
||||||
void Take(char *pnData, size_t iLength) { assert((!pnData && !iLength) || strlen(pnData) == iLength); StdBuf::Take(pnData, iLength + 1); }
|
void Take(const char *pnData) { Ref(pnData); }
|
||||||
char *GrabPointer() { return reinterpret_cast<char *>(StdBuf::GrabPointer()); }
|
using StdBuf::Copy;
|
||||||
|
void Copy(const char *pnData) { Ref(pnData); }
|
||||||
|
|
||||||
void Ref(const StdStrBuf &Buf2) { StdBuf::Ref(Buf2.getData(), Buf2.getSize()); }
|
StdStrBuf getRef() const { return *this; }
|
||||||
StdStrBuf getRef() const { return StdStrBuf(getData(), getLength()); }
|
StdStrBuf Duplicate() const { return *this; }
|
||||||
void Take(StdStrBuf RREF Buf2) { StdBuf::Take(Buf2); }
|
|
||||||
|
|
||||||
void Clear() { StdBuf::Clear(); }
|
void SetLength(size_t inSize) { SetSize(inSize+1); }
|
||||||
void Copy() { StdBuf::Copy(); }
|
|
||||||
void Copy(const char *pnData) { StdBuf::Copy(pnData, pnData ? strlen(pnData) + 1 : 0); }
|
|
||||||
void Copy(const StdStrBuf &Buf2) { StdBuf::Copy(Buf2); }
|
|
||||||
StdStrBuf Duplicate() const { StdStrBuf Buf; Buf.Copy(*this); return Buf; }
|
|
||||||
void Move(size_t iFrom, size_t inSize, size_t iTo = 0) { StdBuf::Move(iFrom, inSize, iTo); }
|
|
||||||
|
|
||||||
// Byte-wise compare (will compare characters up to the length of the second string)
|
// * Operators
|
||||||
int Compare(const StdStrBuf &Buf2, size_t iAt = 0) const
|
|
||||||
{
|
|
||||||
assert(iAt <= getLength());
|
|
||||||
return StdBuf::Compare(Buf2.getData(), Buf2.getLength(), iAt);
|
|
||||||
}
|
|
||||||
int Compare_(const char *pCData, size_t iAt = 0) const
|
|
||||||
{
|
|
||||||
StdStrBuf str(pCData); // GCC needs this, for some obscure reason
|
|
||||||
return Compare(str, iAt);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Grows the string to contain the specified number more/less characters.
|
StdStrBuf &operator += (const std::string &Buf2) { append(Buf2); return *this; }
|
||||||
// Note: Will set the terminator, but won't initialize - use Append* instead.
|
|
||||||
void Grow(size_t iGrow)
|
|
||||||
{
|
|
||||||
StdBuf::Grow(getSize() ? iGrow : iGrow + 1);
|
|
||||||
*getMPtr(getLength()) = '\0';
|
|
||||||
}
|
|
||||||
void Shrink(size_t iShrink)
|
|
||||||
{
|
|
||||||
assert(iShrink <= getLength());
|
|
||||||
StdBuf::Shrink(iShrink);
|
|
||||||
*getMPtr(getLength()) = '\0';
|
|
||||||
}
|
|
||||||
void SetLength(size_t iLength)
|
|
||||||
{
|
|
||||||
if(iLength == getLength() && !isNull()) return;
|
|
||||||
if(iLength >= getLength())
|
|
||||||
Grow(iLength - getLength());
|
|
||||||
else
|
|
||||||
Shrink(getLength() - iLength);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Append string
|
|
||||||
void Append(const char *pnData, size_t iChars)
|
|
||||||
{
|
|
||||||
//assert(iChars <= strlen(pnData));
|
|
||||||
Grow(iChars);
|
|
||||||
Write(pnData, iChars, iSize - iChars - 1);
|
|
||||||
}
|
|
||||||
void Append(const char *pnData)
|
|
||||||
{
|
|
||||||
Append(pnData, strlen(pnData));
|
|
||||||
}
|
|
||||||
void Append(const StdStrBuf &Buf2)
|
|
||||||
{
|
|
||||||
Append(Buf2.getData(), Buf2.getLength());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Copy string
|
|
||||||
void Copy(const char *pnData, size_t iChars)
|
|
||||||
{
|
|
||||||
Clear();
|
|
||||||
Append(pnData, iChars);
|
|
||||||
}
|
|
||||||
|
|
||||||
// * File support
|
|
||||||
bool LoadFromFile(const char *szFile);
|
|
||||||
bool SaveToFile(const char *szFile) const;
|
|
||||||
|
|
||||||
// * Operators
|
|
||||||
|
|
||||||
bool operator ! () const { return isNull(); }
|
|
||||||
|
|
||||||
StdStrBuf &operator += (const StdStrBuf &Buf2) { Append(Buf2); return *this; }
|
|
||||||
StdStrBuf &operator += (const char *szString) { Append(szString); return *this; }
|
StdStrBuf &operator += (const char *szString) { Append(szString); return *this; }
|
||||||
StdStrBuf operator + (const StdStrBuf &Buf2) const { StdStrBuf Buf = getRef(); Buf.Append(Buf2); return Buf; }
|
StdStrBuf operator + (const std::string &Buf2) const { StdStrBuf Buf = getRef(); Buf.append(Buf2); return Buf; }
|
||||||
StdStrBuf operator + (const char *szString) const { StdStrBuf Buf = getRef(); Buf.Append(szString); return Buf; }
|
StdStrBuf operator + (const char *szString) const { StdStrBuf Buf = getRef(); Buf.append(szString); return Buf; }
|
||||||
|
|
||||||
bool operator == (const StdStrBuf &Buf2) const
|
// Note this references the data.
|
||||||
{
|
StdStrBuf &operator = (const std::string &Buf2) { assign(Buf2); return *this; }
|
||||||
return getLength() == Buf2.getLength() && !Compare(Buf2);
|
|
||||||
}
|
|
||||||
bool operator != (const StdStrBuf &Buf2) const { return !operator == (Buf2); }
|
|
||||||
|
|
||||||
bool operator == (const char *szString) const { return StdStrBuf(szString) == *this; }
|
|
||||||
bool operator != (const char *szString) const { return ! operator == (szString); }
|
|
||||||
|
|
||||||
// Note this references the data.
|
|
||||||
StdStrBuf &operator = (const StdStrBuf &Buf2) { Ref(Buf2); return *this; }
|
|
||||||
StdStrBuf &operator = (const char *szString) { Ref(szString); return *this; }
|
StdStrBuf &operator = (const char *szString) { Ref(szString); return *this; }
|
||||||
|
|
||||||
// conversion to "bool"
|
|
||||||
operator const void *() const { return getData(); }
|
|
||||||
|
|
||||||
// less-than operation for map
|
|
||||||
inline bool operator <(const StdStrBuf &v2)
|
|
||||||
{
|
|
||||||
int iLen = getLength(), iLen2 = v2.getLength();
|
|
||||||
if (iLen == iLen2)
|
|
||||||
return iLen ? (strcmp(getData(), v2.getData())<0) : false;
|
|
||||||
else
|
|
||||||
return iLen < iLen2;
|
|
||||||
}
|
|
||||||
|
|
||||||
// * String specific
|
// * String specific
|
||||||
|
using StdBuf::Append;
|
||||||
|
void Append(const char *str) { if(str) append(str); }
|
||||||
|
|
||||||
void AppendChars(char cChar, size_t iCnt)
|
void AppendChars(char cChar, size_t iCnt) { append(iCnt, cChar); }
|
||||||
{
|
void AppendChar(char cChar) { push_back(cChar); }
|
||||||
Grow(iCnt);
|
void InsertChar(char cChar, size_t insert_before) { insert(insert_before, 1, cChar); }
|
||||||
for(size_t i = getLength() - iCnt; i < getLength(); i++)
|
|
||||||
*getMPtr(i) = cChar;
|
|
||||||
}
|
|
||||||
void AppendChar(char cChar)
|
|
||||||
{
|
|
||||||
AppendChars(cChar, 1);
|
|
||||||
}
|
|
||||||
void InsertChar(char cChar, size_t insert_before)
|
|
||||||
{
|
|
||||||
assert(insert_before <= getLength());
|
|
||||||
Grow(1);
|
|
||||||
for(size_t i = getLength()-1; i > insert_before; --i)
|
|
||||||
*getMPtr(i) = *getPtr(i-1);
|
|
||||||
*getMPtr(insert_before) = cChar;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Append data until given character (or string end) occurs.
|
// Append data until given character (or string end) occurs.
|
||||||
void AppendUntil(const char *szString, char cUntil)
|
void AppendUntil(const char *szString, char cUntil)
|
||||||
|
@ -576,7 +333,7 @@ public:
|
||||||
if(pPos)
|
if(pPos)
|
||||||
Append(szString, pPos - szString);
|
Append(szString, pPos - szString);
|
||||||
else
|
else
|
||||||
Append(szString);
|
append(szString);
|
||||||
}
|
}
|
||||||
// See above
|
// See above
|
||||||
void CopyUntil(const char *szString, char cUntil)
|
void CopyUntil(const char *szString, char cUntil)
|
||||||
|
@ -603,7 +360,7 @@ public:
|
||||||
|
|
||||||
StdStrBuf copyPart(size_t iStart, size_t inSize) const
|
StdStrBuf copyPart(size_t iStart, size_t inSize) const
|
||||||
{
|
{
|
||||||
assert(iStart + inSize <= iSize);
|
assert(iStart + inSize <= size());
|
||||||
if (!inSize) return StdStrBuf();
|
if (!inSize) return StdStrBuf();
|
||||||
StdStrBuf sResult;
|
StdStrBuf sResult;
|
||||||
sResult.Copy(getPtr(iStart), inSize);
|
sResult.Copy(getPtr(iStart), inSize);
|
||||||
|
@ -629,12 +386,6 @@ public:
|
||||||
// check if a string consists only of the given chars
|
// check if a string consists only of the given chars
|
||||||
bool ValidateChars(const char *szInitialChars, const char *szMidChars);
|
bool ValidateChars(const char *szInitialChars, const char *szMidChars);
|
||||||
|
|
||||||
// build a simple hash
|
|
||||||
int GetHash() const
|
|
||||||
{
|
|
||||||
return StdBuf::GetHash();
|
|
||||||
}
|
|
||||||
|
|
||||||
void EscapeString()
|
void EscapeString()
|
||||||
{
|
{
|
||||||
Replace("\\", "\\\\");
|
Replace("\\", "\\\\");
|
||||||
|
@ -650,31 +401,7 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
// Copy-Stringbuffer - Just copies data in the copy constructor.
|
// Copy-Stringbuffer - Just copies data in the copy constructor.
|
||||||
class StdCopyStrBuf : public StdStrBuf
|
typedef StdStrBuf StdCopyStrBuf;
|
||||||
{
|
|
||||||
public:
|
|
||||||
|
|
||||||
StdCopyStrBuf()
|
|
||||||
{ }
|
|
||||||
|
|
||||||
explicit StdCopyStrBuf(const StdStrBuf &Buf2, bool fCopy = true)
|
|
||||||
: StdStrBuf(Buf2.getRef(), fCopy)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
StdCopyStrBuf(const StdCopyStrBuf &Buf2, bool fCopy = true)
|
|
||||||
: StdStrBuf(Buf2.getRef(), fCopy)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
// Set by constant data. Copies data if desired.
|
|
||||||
explicit StdCopyStrBuf(const char *pData, bool fCopy = true)
|
|
||||||
: StdStrBuf(pData, fCopy)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
StdCopyStrBuf &operator = (const StdStrBuf &Buf2) { Copy(Buf2); return *this; }
|
|
||||||
StdCopyStrBuf &operator = (const StdCopyStrBuf &Buf2) { Copy(Buf2); return *this; }
|
|
||||||
StdCopyStrBuf &operator = (const char *szString) { Copy(szString); return *this; }
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
// Wrappers
|
// Wrappers
|
||||||
extern StdStrBuf FormatString(const char *szFmt, ...) GNUC_FORMAT_ATTRIBUTE;
|
extern StdStrBuf FormatString(const char *szFmt, ...) GNUC_FORMAT_ATTRIBUTE;
|
||||||
|
|
|
@ -74,7 +74,7 @@ bool StdBuf::SaveToFile(const char *szFile) const
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool StdStrBuf::LoadFromFile(const char *szFile)
|
/*bool StdStrBuf::LoadFromFile(const char *szFile)
|
||||||
{
|
{
|
||||||
// Open file
|
// Open file
|
||||||
int fh = open(szFile, O_BINARY | O_RDONLY | O_SEQUENTIAL, S_IREAD | S_IWRITE);
|
int fh = open(szFile, O_BINARY | O_RDONLY | O_SEQUENTIAL, S_IREAD | S_IWRITE);
|
||||||
|
@ -105,22 +105,21 @@ bool StdStrBuf::SaveToFile(const char *szFile) const
|
||||||
close(fh);
|
close(fh);
|
||||||
// Ok
|
// Ok
|
||||||
return true;
|
return true;
|
||||||
}
|
}*/
|
||||||
|
|
||||||
void StdBuf::CompileFunc(StdCompiler *pComp, int iType)
|
void StdBuf::CompileFunc(StdCompiler *pComp, int iType)
|
||||||
{
|
{
|
||||||
// Size (guess it is a small value most of the time - if it's big, an extra byte won't hurt anyway)
|
// Size (guess it is a small value most of the time - if it's big, an extra byte won't hurt anyway)
|
||||||
uint32_t tmp = iSize; pComp->Value(mkIntPackAdapt(tmp)); iSize = tmp;
|
uint32_t tmp = size(); pComp->Value(mkIntPackAdapt(tmp));
|
||||||
pComp->Seperator(StdCompiler::SEP_PART2);
|
pComp->Seperator(StdCompiler::SEP_PART2);
|
||||||
// Read/write data
|
// Read/write data
|
||||||
if(pComp->isCompiler())
|
if(pComp->isCompiler())
|
||||||
{
|
{
|
||||||
New(iSize);
|
pComp->Raw(getMData(), size(), StdCompiler::RawCompileType(iType));
|
||||||
pComp->Raw(getMData(), iSize, StdCompiler::RawCompileType(iType));
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
pComp->Raw(const_cast<void *>(getData()), iSize, StdCompiler::RawCompileType(iType));
|
pComp->Raw(const_cast<void *>(getData()), size(), StdCompiler::RawCompileType(iType));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -44,7 +44,7 @@ public:
|
||||||
Entry * e = &(Entries[h % Capacity]);
|
Entry * e = &(Entries[h % Capacity]);
|
||||||
// Search an empty spot
|
// Search an empty spot
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while (*e) {
|
while (e->Data) {
|
||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
if (e->Hash == h) printf("Hash Collision: %d (\"%.50s\")\nSTRINGTABLE WILL BREAK\n", h, table);
|
if (e->Hash == h) printf("Hash Collision: %d (\"%.50s\")\nSTRINGTABLE WILL BREAK\n", h, table);
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue