1
0
mirror of https://foundry.openuru.org/gitblit/r/CWE-ou-minkata.git synced 2025-07-17 10:52:46 +00:00

Manual merge of Pull Request #232 from zrax/plString

Conflicts:
	Sources/Plasma/CoreLib/HeadSpin.h
This commit is contained in:
2012-12-20 17:22:31 -05:00
74 changed files with 746 additions and 572 deletions

View File

@ -80,8 +80,12 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
# define NOMINMAX // Needed to prevent NxMath conflicts
# endif
# include <Windows.h>
#endif
// This needs to be after #include <windows.h>, since it also includes windows.h
# ifdef USE_VLD
# include <vld.h>
# endif
#endif // HS_BUILD_FOR_WIN32
//======================================
// We don't want the Windows.h min/max!

View File

@ -112,17 +112,6 @@ void hsStream::CopyToMem(void* mem)
//////////////////////////////////////////////////////////////////////////////////
hsStream::~hsStream()
{
}
uint32_t hsStream::WriteString(const char cstring[])
{
if (cstring)
return Write(strlen(cstring), cstring);
return 0;
}
uint32_t hsStream::WriteFmt(const char * fmt, ...)
{
va_list av;
@ -159,64 +148,81 @@ uint32_t hsStream::WriteSafeStringLong(const plString &string)
uint32_t hsStream::WriteSafeWStringLong(const plString &string)
{
plStringBuffer<wchar_t> wbuff = string.ToWchar();
plStringBuffer<uint16_t> wbuff = string.ToUtf16();
uint32_t len = wbuff.GetSize();
WriteLE32(len);
if (len > 0)
{
const wchar_t *buffp = wbuff.GetData();
const uint16_t *buffp = wbuff.GetData();
for (uint32_t i=0; i<len; i++)
{
WriteLE16((uint16_t)~buffp[i]);
WriteLE16(~buffp[i]);
}
WriteLE16((uint16_t)L'\0');
WriteLE16(static_cast<uint16_t>(0));
}
return 0;
}
char *hsStream::ReadSafeStringLong()
plString hsStream::ReadSafeStringLong_TEMP()
{
char *name = nil;
plStringBuffer<char> name;
uint32_t numChars = ReadLE32();
if (numChars > 0 && numChars <= GetSizeLeft())
{
name = new char[numChars+1];
Read(numChars, name);
name[numChars] = '\0';
char *buff = name.CreateWritableBuffer(numChars);
Read(numChars, buff);
buff[numChars] = 0;
// if the high bit is set, flip the bits. Otherwise it's a normal string, do nothing.
if (name[0] & 0x80)
if (buff[0] & 0x80)
{
int i;
for (i = 0; i < numChars; i++)
name[i] = ~name[i];
for (int i = 0; i < numChars; i++)
buff[i] = ~buff[i];
}
}
return name;
}
wchar_t *hsStream::ReadSafeWStringLong()
char *hsStream::ReadSafeStringLong()
{
wchar_t *retVal = nil;
plString name = ReadSafeStringLong_TEMP();
char *buff = new char[name.GetSize() + 1];
memcpy(buff, name.c_str(), name.GetSize() + 1);
return buff;
}
plString hsStream::ReadSafeWStringLong_TEMP()
{
plStringBuffer<uint16_t> retVal;
uint32_t numChars = ReadLE32();
if (numChars > 0 && numChars <= (GetSizeLeft()/2)) // divide by two because each char is two bytes
{
retVal = new wchar_t[numChars+1];
int i;
for (i=0; i<numChars; i++)
retVal[i] = (wchar_t)ReadLE16();
retVal[numChars] = (wchar_t)ReadLE16(); // we wrote the null out, read it back in
uint16_t *buff = retVal.CreateWritableBuffer(numChars);
for (int i=0; i<numChars; i++)
buff[i] = ReadLE16();
ReadLE16(); // we wrote the null out, read it back in
buff[numChars] = 0; // But terminate it safely anyway
if (retVal[0]* 0x80)
if (buff[0]* 0x80)
{
int i;
for (i=0; i<numChars; i++)
retVal[i] = ~retVal[i];
for (int i=0; i<numChars; i++)
buff[i] = ~buff[i];
}
}
return retVal;
return plString::FromUtf16(retVal);
}
wchar_t *hsStream::ReadSafeWStringLong()
{
// Horribly inefficient (convert to UTF-8 and then back to UTF-16), which
// is why this should go away completely after plString has taken over
// the world^H^H^H^H^HPlasma
plStringBuffer<wchar_t> retVal = ReadSafeWStringLong_TEMP().ToWchar();
wchar_t *buff = new wchar_t[retVal.GetSize() + 1];
memcpy(buff, retVal.GetData(), retVal.GetSize() + 1);
return buff;
}
uint32_t hsStream::WriteSafeString(const plString &string)
@ -242,27 +248,27 @@ uint32_t hsStream::WriteSafeString(const plString &string)
uint32_t hsStream::WriteSafeWString(const plString &string)
{
plStringBuffer<wchar_t> wbuff = string.ToWchar();
plStringBuffer<uint16_t> wbuff = string.ToUtf16();
uint32_t len = wbuff.GetSize();
hsAssert(len<0xf000, xtl::format("string len of %d is too long for WriteSafeWString, use WriteSafeWStringLong",
hsAssert(len<0xf000, plString::Format("string len of %d is too long for WriteSafeWString, use WriteSafeWStringLong",
len).c_str() );
WriteLE16(len | 0xf000);
if (len > 0)
{
const wchar_t *buffp = wbuff.GetData();
const uint16_t *buffp = wbuff.GetData();
for (uint32_t i=0; i<len; i++)
{
WriteLE16((uint16_t)~buffp[i]);
WriteLE16(~buffp[i]);
}
WriteLE16((uint16_t)L'\0');
WriteLE16(static_cast<uint16_t>(0));
}
return 0;
}
char *hsStream::ReadSafeString()
plString hsStream::ReadSafeString_TEMP()
{
char *name = nil;
plStringBuffer<char> name;
uint16_t numChars = ReadLE16();
#ifndef REMOVE_ME_SOON
@ -276,62 +282,64 @@ char *hsStream::ReadSafeString()
hsAssert(numChars <= GetSizeLeft(), "Bad string");
if (numChars > 0 && numChars <= GetSizeLeft())
{
name = new char[numChars+1];
Read(numChars, name);
name[numChars] = '\0';
char *buff = name.CreateWritableBuffer(numChars);
Read(numChars, buff);
buff[numChars] = 0;
// if the high bit is set, flip the bits. Otherwise it's a normal string, do nothing.
if (name[0] & 0x80)
if (buff[0] & 0x80)
{
int i;
for (i = 0; i < numChars; i++)
name[i] = ~name[i];
buff[i] = ~buff[i];
}
}
return name;
}
wchar_t *hsStream::ReadSafeWString()
char *hsStream::ReadSafeString()
{
wchar_t *retVal = nil;
plString name = ReadSafeString_TEMP();
char *buff = new char[name.GetSize() + 1];
memcpy(buff, name.c_str(), name.GetSize() + 1);
return buff;
}
plString hsStream::ReadSafeWString_TEMP()
{
plStringBuffer<uint16_t> retVal;
uint32_t numChars = ReadLE16();
numChars &= ~0xf000;
hsAssert(numChars <= GetSizeLeft()/2, "Bad string");
if (numChars > 0 && numChars <= (GetSizeLeft()/2)) // divide by two because each char is two bytes
{
retVal = new wchar_t[numChars+1];
int i;
for (i=0; i<numChars; i++)
retVal[i] = (wchar_t)ReadLE16();
retVal[numChars] = (wchar_t)ReadLE16(); // we wrote the null out, read it back in
uint16_t *buff = retVal.CreateWritableBuffer(numChars);
for (int i=0; i<numChars; i++)
buff[i] = ReadLE16();
ReadLE16(); // we wrote the null out, read it back in
buff[numChars] = 0; // But terminate it safely anyway
if (retVal[0]* 0x80)
if (buff[0]* 0x80)
{
int i;
for (i=0; i<numChars; i++)
retVal[i] = ~retVal[i];
for (int i=0; i<numChars; i++)
buff[i] = ~buff[i];
}
}
return retVal;
return plString::FromUtf16(retVal);
}
plString hsStream::ReadSafeString_TEMP()
wchar_t *hsStream::ReadSafeWString()
{
char *buffer = ReadSafeString();
plString result = plString::FromIso8859_1(buffer);
delete [] buffer;
return result;
}
plString hsStream::ReadSafeWString_TEMP()
{
wchar_t *wbuffer = ReadSafeWString();
plString result = plString::FromWchar(wbuffer);
delete [] wbuffer;
return result;
// Horribly inefficient (convert to UTF-8 and then back to UTF-16), which
// is why this should go away completely after plString has taken over
// the world^H^H^H^H^HPlasma
plStringBuffer<wchar_t> retVal = ReadSafeWString_TEMP().ToWchar();
wchar_t *buff = new wchar_t[retVal.GetSize() + 1];
memcpy(buff, retVal.GetData(), retVal.GetSize() + 1);
return buff;
}
bool hsStream::Read4Bytes(void *pv) // Virtual, faster version in sub classes

View File

@ -81,7 +81,7 @@ protected:
bool IsTokenSeparator(char c);
public:
hsStream() : fBytesRead(0), fPosition(0) {}
virtual ~hsStream();
virtual ~hsStream() { }
virtual bool Open(const char *, const char * = "rb")=0;
virtual bool Open(const wchar_t *, const wchar_t * = L"rb")=0;
@ -123,8 +123,7 @@ public:
virtual void CopyToMem(void* mem);
virtual bool IsCompressed() { return false; }
uint32_t WriteString(const char cstring[]);
uint32_t WriteString(const plString & string) { return WriteString(string.c_str()); }
uint32_t WriteString(const plString & string) { return Write(string.GetSize(), string.c_str()); }
uint32_t WriteFmt(const char * fmt, ...);
uint32_t WriteFmtV(const char * fmt, va_list av);
@ -138,6 +137,8 @@ public:
char * ReadSafeString();
wchar_t * ReadSafeWString();
plString ReadSafeStringLong_TEMP();
plString ReadSafeWStringLong_TEMP();
plString ReadSafeString_TEMP();
plString ReadSafeWString_TEMP();

View File

@ -58,6 +58,11 @@ struct WinThreadParam
static unsigned int __stdcall gEntryPointBT(void* param)
{
#ifdef USE_VLD
// Needs to be enabled for each thread except the WinMain
VLDEnable();
#endif
WinThreadParam* wtp = (WinThreadParam*)param;
unsigned int result = wtp->fThread->Run();
::ReleaseSemaphore(wtp->fQuitSemaH, 1, nil); // signal that we've quit

View File

@ -700,6 +700,60 @@ plString plString::ToLower() const
return str;
}
static bool ch_in_set(char ch, const char *set)
{
for (const char *s = set; *s; ++s) {
if (ch == *s)
return true;
}
return false;
}
std::vector<plString> plString::Tokenize(const char *delims) const
{
std::vector<plString> result;
const char *next = c_str();
const char *end = next + GetSize(); // So binary strings work
while (next != end) {
const char *cur = next;
while (cur != end && !ch_in_set(*cur, delims))
++cur;
// Found a delimiter
if (cur != next)
result.push_back(plString::FromUtf8(next, cur - next));
next = cur;
while (next != end && ch_in_set(*next, delims))
++next;
}
return result;
}
//TODO: Not binary safe
std::vector<plString> plString::Split(const char *split, size_t maxSplits) const
{
std::vector<plString> result;
const char *next = c_str();
size_t splitlen = strlen(split);
while (maxSplits > 0) {
const char *sp = strstr(next, split);
if (!sp)
break;
result.push_back(plString::FromUtf8(next, sp - next));
next = sp + splitlen;
--maxSplits;
}
result.push_back(plString::FromUtf8(next));
return result;
}
plString operator+(const plString &left, const plString &right)
{
plString cat;
@ -711,6 +765,30 @@ plString operator+(const plString &left, const plString &right)
return cat;
}
plString operator+(const plString &left, const char *right)
{
plString cat;
size_t rsize = strlen(right);
char *catstr = cat.fUtf8Buffer.CreateWritableBuffer(left.GetSize() + rsize);
memcpy(catstr, left.c_str(), left.GetSize());
memcpy(catstr + left.GetSize(), right, rsize);
catstr[cat.fUtf8Buffer.GetSize()] = 0;
return cat;
}
plString operator+(const char *left, const plString &right)
{
plString cat;
size_t lsize = strlen(left);
char *catstr = cat.fUtf8Buffer.CreateWritableBuffer(lsize + right.GetSize());
memcpy(catstr, left, lsize);
memcpy(catstr + lsize, right.c_str(), right.GetSize());
catstr[cat.fUtf8Buffer.GetSize()] = 0;
return cat;
}
plStringStream &plStringStream::append(const char *data, size_t length)
{
if (fLength + length > fBufSize) {

View File

@ -44,7 +44,7 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
#define plString_Defined
#include "HeadSpin.h"
#include <stddef.h>
#include <vector>
typedef unsigned int UniChar;
@ -163,18 +163,15 @@ private:
public:
plString() { }
#ifndef PLSTRING_POLLUTE_ASCII_CAST
plString(const char *cstr) { IConvertFromUtf8(cstr, kSizeAuto); }
#endif
plString(const plString &copy) : fUtf8Buffer(copy.fUtf8Buffer) { }
plString(const plStringBuffer<char> &init) { operator=(init); }
#ifndef PLSTRING_POLLUTE_ASCII_CAST
plString &operator=(const char *cstr) { IConvertFromUtf8(cstr, kSizeAuto); return *this; }
#endif
plString &operator=(const plString &copy) { fUtf8Buffer = copy.fUtf8Buffer; return *this; }
plString &operator=(const plStringBuffer<char> &init);
plString &operator+=(const char *cstr) { return operator=(*this + cstr); }
plString &operator+=(const plString &str) { return operator=(*this + str); }
static inline plString FromUtf8(const char *utf8, size_t size = kSizeAuto)
@ -205,10 +202,8 @@ public:
return str;
}
#ifndef PLSTRING_POLLUTE_C_STR
const char *c_str(const char *substitute = "") const
{ return IsEmpty() ? substitute : fUtf8Buffer.GetData(); }
#endif
char CharAt(size_t position) const { return c_str()[position]; }
@ -263,8 +258,15 @@ public:
: strnicmp(c_str(), str, count);
}
int CompareI(const plString &str) const { return Compare(str, kCaseInsensitive); }
int CompareI(const char *str) const { return Compare(str, kCaseInsensitive); }
int CompareNI(const plString &str, size_t count) const { return CompareN(str, count, kCaseInsensitive); }
int CompareNI(const char *str, size_t count) const { return CompareN(str, count, kCaseInsensitive); }
bool operator<(const plString &other) const { return Compare(other) < 0; }
bool operator==(const char *other) const { return Compare(other) == 0; }
bool operator==(const plString &other) const { return Compare(other) == 0; }
bool operator!=(const char *other) const { return Compare(other) != 0; }
bool operator!=(const plString &other) const { return Compare(other) != 0; }
int Find(char ch, CaseSensitivity sense = kCaseSensitive) const;
@ -287,6 +289,13 @@ public:
plString ToUpper() const;
plString ToLower() const;
// Should replace other tokenization methods. The difference between Split
// and Tokenize is that Tokenize never returns a blank string (it strips
// all delimiters and only returns the pieces left between them), whereas
// Split will split on a full string, returning whatever is left between.
std::vector<plString> Split(const char *split, size_t maxSplits = kSizeAuto) const;
std::vector<plString> Tokenize(const char *delims = " \t\r\n\f\v") const;
public:
struct less
{
@ -416,9 +425,13 @@ public:
private:
friend plString operator+(const plString &left, const plString &right);
friend plString operator+(const plString &left, const char *right);
friend plString operator+(const char *left, const plString &right);
};
plString operator+(const plString &left, const plString &right);
plString operator+(const plString &left, const char *right);
plString operator+(const char *left, const plString &right);
class plStringStream