diff --git a/engine/src/C4AulParse.cpp b/engine/src/C4AulParse.cpp index c09dad557..3083404b1 100644 --- a/engine/src/C4AulParse.cpp +++ b/engine/src/C4AulParse.cpp @@ -799,7 +799,7 @@ C4AulTokenType C4AulParseState::GetNextToken(char *pToken, long int *pInt, HoldS if(HoldStrings == Discard) return ATT_STRING; // reg string (if not already done so) C4String *pString; - pString = a->Engine->Strings.RegString(StdStrBuf(StrBuff,static_cast(pStrPos - StrBuff))); + pString = a->Engine->Strings.RegString(StdStrBuf(StrBuff,static_cast(pStrPos - StrBuff))); if (HoldStrings == Hold) pString->Hold = 1; // return pointer on string object *pInt = (long) pString; diff --git a/standard/inc/Standard.h b/standard/inc/Standard.h index 06ea6af4d..b18e3bb44 100644 --- a/standard/inc/Standard.h +++ b/standard/inc/Standard.h @@ -31,7 +31,15 @@ #ifdef _MSC_VER #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 // debug memory management diff --git a/standard/inc/StdBuf.h b/standard/inc/StdBuf.h index e54e854d1..b57d84abb 100644 --- a/standard/inc/StdBuf.h +++ b/standard/inc/StdBuf.h @@ -23,10 +23,9 @@ #ifndef STDBUF_H #define STDBUF_H -#include #include "Standard.h" -#include +#include #include #include @@ -35,84 +34,49 @@ #include #endif +#include + // Base buffer class. Either references or holds data. -class StdBuf +class StdBuf : public std::string { public: - // *** Construction // Standard constructor - StdBuf() : fRef(true), pData(NULL), iSize(0) { } + StdBuf() { } // Constructor from other buffer (copy construction): // 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) - StdBuf(StdBuf RREF Buf2, bool fCopy = false) - : fRef(true), pData(NULL), iSize(0) - { - 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 + StdBuf(const std::string &other, bool = false) + : std::string(other) + {} // Set by constant data. Copies data if desired. - StdBuf(const void *pData, size_t iSize, bool fCopy = false) - : fRef(true), pData(pData), iSize(iSize) - { - if(fCopy) Copy(); - } + StdBuf(const void *pData, size_t iSize, bool = false) + : std::string(static_cast(pData), iSize) + {} ~StdBuf() - { - Clear(); - } + {} 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: // *** Getters - bool isNull() const { return ! getData(); } - const void *getData() const { return fRef ? pData : pMData; } - void *getMData() { assert(!fRef); return pMData; } - size_t getSize() const { return iSize; } - bool isRef() const { return fRef; } + bool isNull() const { return empty(); } + const void *getData() const { return empty()?NULL:c_str(); } + void *getMData() { return &operator[](0); } + const void *getPtr(size_t i) const { return c_str() + i; } + 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(getData()) + i; } - void *getMPtr(size_t i) { return reinterpret_cast(getMData()) + i; } StdBuf getPart(size_t iStart, size_t inSize) const { - assert(iStart + inSize <= iSize); + assert(iStart + inSize <= size()); return StdBuf(getPtr(iStart), inSize); } @@ -123,49 +87,38 @@ public: // Reference given data void Ref(const void *pnData, size_t inSize) { - Clear(); - fRef = true; pData = pnData; iSize = inSize; + if(pnData) + assign(static_cast(pnData), inSize); + else + clear(); } // Take over data (hold it) - void Take(void *pnData, size_t inSize) - { - Clear(); - if(pnData) - { - fRef = false; pMData = pnData; iSize = inSize; - } - } - // Transfer puffer ownership to the caller - void *GrabPointer() + void Take(const void *pnData, size_t inSize) { Ref(pnData, inSize); } + + // Returns a copy of the contents + char *GrabPointer() const { if(isNull()) return NULL; - // Do not give out a buffer which someone else will free - if (fRef) Copy(); - void *pMData = getMData(); - pData = pMData; fRef = true; - return pMData; + char *ptr = new char[size()+1]; + copy(ptr, size()); // doesn't null-terminate + ptr[size()] = '\0'; + return ptr; } // * Buffer data operations // Create new buffer with given size void New(size_t inSize) - { - Clear(); - pMData = malloc(iSize = inSize); - fRef = false; - } + { resize(inSize); } // Write data into the buffer void Write(const void *pnData, size_t inSize, size_t iAt = 0) { - assert(iAt + inSize <= iSize); - if(pnData && inSize) memcpy(getMPtr(iAt), pnData, inSize); + replace(iAt, inSize, static_cast(pnData), inSize); } // Move data around inside the buffer (checks overlap) void Move(size_t iFrom, size_t inSize, size_t iTo = 0) { - assert(iFrom + inSize <= iSize); assert(iTo + inSize <= iSize); - memmove(getMPtr(iTo), getPtr(iFrom), inSize); + replace(iTo, inSize, *this, iFrom, inSize); } // Compare to memory int Compare(const void *pCData, size_t iCSize, size_t iAt = 0) const @@ -176,32 +129,23 @@ public: // Grow the buffer void Grow(size_t iGrow) { - // Grow dereferences - if(fRef) { Copy(iSize + iGrow); return; } - if(!iGrow) return; - // Realloc - pMData = realloc(pMData, iSize += iGrow); + resize(size() + iGrow); } // Shrink the buffer void Shrink(size_t iShrink) { - assert(iSize >= iShrink); - // Shrink dereferences - if(fRef) { Copy(iSize - iShrink); return; } - if(!iShrink) return; - // Realloc - pMData = realloc(pMData, iSize -= iShrink); + assert(size() >= iShrink); + resize(size() - iShrink); } // Clear buffer void Clear() { - if(!fRef) free(pMData); - pMData = NULL; fRef = true; iSize = 0; + clear(); } // Free buffer that had been grabbed - static void DeletePointer(void *data) + static void DeletePointer(const void *data) { - free(data); + delete[] static_cast(data); } // * Composed actions @@ -209,10 +153,7 @@ public: // Set buffer size (dereferences) void SetSize(size_t inSize) { - if(inSize > iSize) - Grow(inSize - iSize); - else - Shrink(iSize - inSize); + resize(inSize); } // Write buffer contents into the buffer @@ -227,62 +168,32 @@ public: return Compare(Buf2.getData(), Buf2.getSize(), iAt); } - // Create a copy of the data (dereferences, obviously) - void Copy(size_t 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(); - } + DEPRECATED void Copy(size_t inSize) {} + DEPRECATED void Copy() {} + void Copy(const void *pnData, size_t inSize) { Ref(pnData, inSize); } // Copy from another buffer - void Copy(const StdBuf &Buf2) - { - Copy(Buf2.getData(), Buf2.getSize()); - } + void Copy(const StdBuf &Buf2) { assign(Buf2); } + // Create a copy and return it - StdBuf Duplicate() const - { - StdBuf Buf; Buf.Copy(*this); return Buf; - } + StdBuf Duplicate() const { return *this; } // Append data from address void Append(const void *pnData, int inSize) { - Grow(inSize); - Write(pnData, inSize, iSize - inSize); + append(static_cast(pnData), inSize); } // Append data from another buffer void Append(const StdBuf &Buf2) { - Append(Buf2.getData(), Buf2.getSize()); + append(Buf2); } // Reference another buffer's contents - void Ref(const StdBuf &Buf2) - { - Ref(Buf2.getData(), Buf2.getSize()); - } + void Ref(const StdBuf &Buf2) { assign(Buf2); } // Create a reference to this buffer's contents - StdBuf getRef() const - { - return StdBuf(getData(), getSize()); - } + StdBuf getRef() const { return *this; } // take over another buffer's contents - void Take(StdBuf RREF Buf2) - { - Take(Buf2.GrabPointer(), Buf2.getSize()); - } + void Take(const StdBuf &Buf2) { assign(Buf2); } // * File support bool LoadFromFile(const char *szFile); @@ -291,6 +202,8 @@ public: // *** Operators // Null check + operator bool() const { return !empty(); } + operator const void*() const { return data(); } bool operator ! () const { return isNull(); } // Appending @@ -305,17 +218,10 @@ public: 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) - StdBuf &operator = (StdBuf RREF Buf2) + StdBuf &operator = (const std::string &Buf2) { - if(Buf2.isRef()) Ref(Buf2); else Take(Buf2); + assign(Buf2); return *this; } @@ -349,225 +255,76 @@ template } // Copy-Buffer - Just copies data in the copy constructor. -class StdCopyBuf : public StdBuf -{ -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; } - -}; +typedef StdBuf StdCopyBuf; // Stringbuffer (operates on null-terminated character buffers) -class StdStrBuf : protected StdBuf +class StdStrBuf : public StdBuf { public: - // *** Construction + // Standard constructor + StdStrBuf() { } - StdStrBuf() - : StdBuf() - { } + // Constructor from other buffer (copy construction): + // 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. - // The static_cast is necessary to pass a rvalue reference to - // 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(Buf2), fCopy) - { } + StdStrBuf(const std::string &other, bool = false) + : StdBuf(other) + {} -#ifdef HAVE_RVALUE_REF - StdStrBuf(const StdStrBuf & Buf2, bool fCopy = true) - : StdBuf(Buf2, fCopy) - { } -#endif + // Set by constant data. Copies data if desired. + StdStrBuf(const char *pData, size_t iSize, bool = false) + : StdBuf(pData, iSize) + {} - // Set by constant data. References data by default, copies if specified. - 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) - { } + ~StdStrBuf() + {} ALLOW_TEMP_TO_REF(StdStrBuf) - public: // *** Getters - - bool isNull() const { return StdBuf::isNull(); } - const char *getData() const { return getBufPtr(*this); } - char *getMData() { return getMBufPtr(*this); } - size_t getSize() const { return StdBuf::getSize(); } size_t getLength() const { return getSize() ? getSize() - 1 : 0; } - bool isRef() const { return StdBuf::isRef(); } - - const char *getPtr(size_t i) const { return getBufPtr(*this, i); } - char *getMPtr(size_t i) { return getMBufPtr(*this, i); } - - // For convenience. Note that writing can't be allowed. - char operator [] (size_t i) const { return *getPtr(i); } + const char *getData() const { return empty()?NULL:c_str(); } + char *getMData() { return &operator[](0); } + const char *getPtr(size_t i) const { return getData() + i; } + char *getMPtr(size_t i) { return getMData() + i; } // Analogous to StdBuf - void Ref(const char *pnData) { StdBuf::Ref(pnData, pnData ? strlen(pnData) + 1 : 0); } - void Ref(const char *pnData, size_t iLength) { assert((!pnData && !iLength) || strlen(pnData) == iLength); StdBuf::Ref(pnData, iLength + 1); } - void Take(char *pnData) { StdBuf::Take(pnData, pnData ? strlen(pnData) + 1 : 0); } - void Take(char *pnData, size_t iLength) { assert((!pnData && !iLength) || strlen(pnData) == iLength); StdBuf::Take(pnData, iLength + 1); } - char *GrabPointer() { return reinterpret_cast(StdBuf::GrabPointer()); } + using StdBuf::Ref; + void Ref(const char *pnData) { if(pnData) assign(pnData); else clear(); } + using StdBuf::Take; + void Take(const char *pnData) { Ref(pnData); } + 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 StdStrBuf(getData(), getLength()); } - void Take(StdStrBuf RREF Buf2) { StdBuf::Take(Buf2); } + StdStrBuf getRef() const { return *this; } + StdStrBuf Duplicate() const { return *this; } - void Clear() { StdBuf::Clear(); } - 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); } + void SetLength(size_t inSize) { SetSize(inSize+1); } - // Byte-wise compare (will compare characters up to the length of the second string) - 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); - } + // * Operators - // Grows the string to contain the specified number more/less characters. - // 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 std::string &Buf2) { append(Buf2); 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 char *szString) const { StdStrBuf Buf = getRef(); Buf.Append(szString); 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; } - bool operator == (const StdStrBuf &Buf2) const - { - 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; } + // Note this references the data. + StdStrBuf &operator = (const std::string &Buf2) { assign(Buf2); 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 + using StdBuf::Append; + void Append(const char *str) { if(str) append(str); } - void AppendChars(char cChar, size_t iCnt) - { - Grow(iCnt); - 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; - } + void AppendChars(char cChar, size_t iCnt) { append(iCnt, cChar); } + void AppendChar(char cChar) { push_back(cChar); } + void InsertChar(char cChar, size_t insert_before) { insert(insert_before, 1, cChar); } // Append data until given character (or string end) occurs. void AppendUntil(const char *szString, char cUntil) @@ -576,7 +333,7 @@ public: if(pPos) Append(szString, pPos - szString); else - Append(szString); + append(szString); } // See above void CopyUntil(const char *szString, char cUntil) @@ -603,7 +360,7 @@ public: StdStrBuf copyPart(size_t iStart, size_t inSize) const { - assert(iStart + inSize <= iSize); + assert(iStart + inSize <= size()); if (!inSize) return StdStrBuf(); StdStrBuf sResult; sResult.Copy(getPtr(iStart), inSize); @@ -629,12 +386,6 @@ public: // check if a string consists only of the given chars bool ValidateChars(const char *szInitialChars, const char *szMidChars); - // build a simple hash - int GetHash() const - { - return StdBuf::GetHash(); - } - void EscapeString() { Replace("\\", "\\\\"); @@ -650,31 +401,7 @@ public: }; // Copy-Stringbuffer - Just copies data in the copy constructor. -class StdCopyStrBuf : public StdStrBuf -{ -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; } - -}; +typedef StdStrBuf StdCopyStrBuf; // Wrappers extern StdStrBuf FormatString(const char *szFmt, ...) GNUC_FORMAT_ATTRIBUTE; diff --git a/standard/src/StdBuf.cpp b/standard/src/StdBuf.cpp index 894e3f171..d13296039 100644 --- a/standard/src/StdBuf.cpp +++ b/standard/src/StdBuf.cpp @@ -74,7 +74,7 @@ bool StdBuf::SaveToFile(const char *szFile) const return true; } -bool StdStrBuf::LoadFromFile(const char *szFile) +/*bool StdStrBuf::LoadFromFile(const char *szFile) { // Open file 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); // Ok return true; -} +}*/ 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) - uint32_t tmp = iSize; pComp->Value(mkIntPackAdapt(tmp)); iSize = tmp; + uint32_t tmp = size(); pComp->Value(mkIntPackAdapt(tmp)); pComp->Seperator(StdCompiler::SEP_PART2); // Read/write data if(pComp->isCompiler()) { - New(iSize); - pComp->Raw(getMData(), iSize, StdCompiler::RawCompileType(iType)); + pComp->Raw(getMData(), size(), StdCompiler::RawCompileType(iType)); } else { - pComp->Raw(const_cast(getData()), iSize, StdCompiler::RawCompileType(iType)); + pComp->Raw(const_cast(getData()), size(), StdCompiler::RawCompileType(iType)); } } diff --git a/standard/src/StdResStr2.cpp b/standard/src/StdResStr2.cpp index a03e417b8..d9ea32f17 100644 --- a/standard/src/StdResStr2.cpp +++ b/standard/src/StdResStr2.cpp @@ -44,7 +44,7 @@ public: Entry * e = &(Entries[h % Capacity]); // Search an empty spot int i = 0; - while (*e) { + while (e->Data) { #ifdef _DEBUG if (e->Hash == h) printf("Hash Collision: %d (\"%.50s\")\nSTRINGTABLE WILL BREAK\n", h, table); #endif