* Copyright 2003, Tyler Dauwalder, tyler@dauwalder.net.
* Distributed under the terms of the MIT License.
*/
#include "DString.h"
#include <string.h>
DString::DString()
:
fLength(0),
fString(NULL)
{
}
DString::DString(const DString &ref)
:
fLength(0),
fString(NULL)
{
SetTo(ref);
}
at most the first \c (fieldLength-1) bytes of \a string.Cs0().
*/
DString::DString(const UdfString &string, uint8 fieldLength)
:
fLength(0),
fString(NULL)
{
SetTo(string, fieldLength);
}
at most the first \c (fieldLength-1) bytes of the Cs0 representation
of the NULL-terminated UTF8 string \a utf8.
*/
DString::DString(const char *utf8, uint8 fieldLength)
:
fLength(0),
fString(NULL)
{
SetTo(utf8, fieldLength);
}
DString::~DString()
{
delete[] fString;
}
void
DString::SetTo(const DString &ref)
{
_Clear();
if (ref.Length() > 0) {
fString = new(nothrow) uint8[ref.Length()];
if (fString != NULL) {
fLength = ref.Length();
memcpy(fString, ref.String(), fLength);
}
}
}
at most the first \c (fieldLength-1) bytes of \a string.Cs0().
*/
void
DString::SetTo(const UdfString &string, uint8 fieldLength)
{
_Clear();
if (fieldLength > 0) {
fString = new(nothrow) uint8[fieldLength];
status_t error = fString ? B_OK : B_NO_MEMORY;
if (!error) {
uint32 sourceLength = string.Cs0Length();
if (sourceLength > 0) {
uint8 destLength = sourceLength > uint8(fieldLength - 1)
? uint8(fieldLength - 1) : uint8(sourceLength);
if (string.Cs0()[1] == '\x10' && destLength > 0
&& destLength % 2 == 0)
destLength--;
memcpy(fString, string.Cs0(), destLength);
if (destLength < fieldLength - 1)
memset(&fString[destLength], 0, fieldLength - 1 - destLength);
fString[fieldLength - 1] = destLength;
} else {
memset(fString, 0, fieldLength);
}
}
}
}
at most the first \c (fieldLength-1) bytes of the Cs0 representation
of the NULL-terminated UTF8 string \a utf8.
*/
void
DString::SetTo(const char *utf8, uint8 fieldLength)
{
UdfString string(utf8);
SetTo(string, fieldLength);
}
void
DString::_Clear()
{
DEBUG_INIT("DString");
delete[] fString;
fString = NULL;
fLength = 0;
}