Browse Source

Take advantage of plString more in hsStream's String I/O functions

Michael Hansen 12 years ago
parent
commit
1b57055fd3
  1. 154
      Sources/Plasma/CoreLib/hsStream.cpp
  2. 7
      Sources/Plasma/CoreLib/hsStream.h

154
Sources/Plasma/CoreLib/hsStream.cpp

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

7
Sources/Plasma/CoreLib/hsStream.h

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

Loading…
Cancel
Save