📄 contain.cxx
字号:
else {
if ((newArray = (char *)malloc(newsizebytes)) == NULL)
return FALSE;
memcpy(newArray, theArray, PMIN(newsizebytes, oldsizebytes));
allocatedDynamically = TRUE;
}
}
else if (newsizebytes != 0) {
if ((newArray = (char *)malloc(newsizebytes)) == NULL)
return FALSE;
}
else
newArray = NULL;
reference->size = newSize;
}
if (newsizebytes > oldsizebytes)
memset(newArray+oldsizebytes, 0, newsizebytes-oldsizebytes);
theArray = newArray;
return TRUE;
}
void PAbstractArray::Attach(const void *buffer, PINDEX bufferSize)
{
if (allocatedDynamically && theArray != NULL)
free(theArray);
#if PCONTAINER_USES_CRITSEC
PEnterAndLeave m(reference->critSec);
#endif
theArray = (char *)buffer;
reference->size = bufferSize;
allocatedDynamically = FALSE;
}
void * PAbstractArray::GetPointer(PINDEX minSize)
{
PAssert(SetMinSize(minSize), POutOfMemory);
return theArray;
}
BOOL PAbstractArray::Concatenate(const PAbstractArray & array)
{
if (!allocatedDynamically || array.elementSize != elementSize)
return FALSE;
PINDEX oldLen = GetSize();
PINDEX addLen = array.GetSize();
if (!SetSize(oldLen + addLen))
return FALSE;
memcpy(theArray+oldLen*elementSize, array.theArray, addLen*elementSize);
return TRUE;
}
void PAbstractArray::PrintElementOn(ostream & /*stream*/, PINDEX /*index*/) const
{
}
void PAbstractArray::ReadElementFrom(istream & /*stream*/, PINDEX /*index*/)
{
}
///////////////////////////////////////////////////////////////////////////////
void PCharArray::PrintOn(ostream & strm) const
{
PINDEX width = strm.width();
if (width > GetSize())
width -= GetSize();
else
width = 0;
BOOL left = (strm.flags()&ios::adjustfield) == ios::left;
if (left)
strm.write(theArray, GetSize());
while (width-- > 0)
strm << (char)strm.fill();
if (!left)
strm.write(theArray, GetSize());
}
void PCharArray::ReadFrom(istream &strm)
{
PINDEX size = 0;
SetSize(size+100);
while (strm.good()) {
strm >> theArray[size++];
if (size >= GetSize())
SetSize(size+100);
}
SetSize(size);
}
void PBYTEArray::PrintOn(ostream & strm) const
{
PINDEX line_width = strm.width();
if (line_width == 0)
line_width = 16;
strm.width(0);
PINDEX indent = strm.precision();
PINDEX val_width = ((strm.flags()&ios::basefield) == ios::hex) ? 2 : 3;
PINDEX i = 0;
while (i < GetSize()) {
if (i > 0)
strm << '\n';
PINDEX j;
for (j = 0; j < indent; j++)
strm << ' ';
for (j = 0; j < line_width; j++) {
if (j == line_width/2)
strm << ' ';
if (i+j < GetSize())
strm << setw(val_width) << (theArray[i+j]&0xff);
else {
PINDEX k;
for (k = 0; k < val_width; k++)
strm << ' ';
}
strm << ' ';
}
if ((strm.flags()&ios::floatfield) != ios::fixed) {
strm << " ";
for (j = 0; j < line_width; j++) {
if (i+j < GetSize()) {
unsigned val = theArray[i+j]&0xff;
if (isprint(val))
strm << (char)val;
else
strm << '.';
}
}
}
i += line_width;
}
}
void PBYTEArray::ReadFrom(istream &strm)
{
PINDEX size = 0;
SetSize(size+100);
while (strm.good()) {
unsigned v;
strm >> v;
theArray[size] = (BYTE)v;
if (!strm.fail()) {
size++;
if (size >= GetSize())
SetSize(size+100);
}
}
SetSize(size);
}
///////////////////////////////////////////////////////////////////////////////
PBitArray::PBitArray(PINDEX initialSize)
: PBYTEArray((initialSize+7)>>3)
{
}
PBitArray::PBitArray(const void * buffer,
PINDEX length,
BOOL dynamic)
: PBYTEArray((const BYTE *)buffer, (length+7)>>3, dynamic)
{
}
PObject * PBitArray::Clone() const
{
return new PBitArray(*this);
}
PINDEX PBitArray::GetSize() const
{
return PBYTEArray::GetSize()<<3;
}
BOOL PBitArray::SetSize(PINDEX newSize)
{
return PBYTEArray::SetSize((newSize+7)>>3);
}
BOOL PBitArray::SetAt(PINDEX index, BOOL val)
{
if (!SetMinSize(index+1))
return FALSE;
if (val)
theArray[index>>3] |= (1 << (index&7));
else
theArray[index>>3] &= ~(1 << (index&7));
return TRUE;
}
BOOL PBitArray::GetAt(PINDEX index) const
{
PASSERTINDEX(index);
if (index >= GetSize())
return FALSE;
return (theArray[index>>3]&(1 << (index&7))) != 0;
}
void PBitArray::Attach(const void * buffer, PINDEX bufferSize)
{
PBYTEArray::Attach((const BYTE *)buffer, (bufferSize+7)>>3);
}
BYTE * PBitArray::GetPointer(PINDEX minSize)
{
return PBYTEArray::GetPointer((minSize+7)>>3);
}
BOOL PBitArray::Concatenate(const PBitArray & array)
{
return PAbstractArray::Concatenate(array);
}
///////////////////////////////////////////////////////////////////////////////
PString::PString(const char * cstr)
: PCharArray(cstr != NULL ? strlen(cstr)+1 : 1)
{
if (cstr != NULL)
memcpy(theArray, cstr, GetSize());
}
PString::PString(const WORD * ustr)
{
if (ustr == NULL)
SetSize(1);
else {
PINDEX len = 0;
while (ustr[len] != 0)
len++;
InternalFromUCS2(ustr, len);
}
}
PString::PString(const char * cstr, PINDEX len)
: PCharArray(len+1)
{
if (len > 0)
memcpy(theArray, PAssertNULL(cstr), len);
}
PString::PString(const WORD * ustr, PINDEX len)
: PCharArray(len+1)
{
InternalFromUCS2(ustr, len);
}
PString::PString(const PWORDArray & ustr)
{
InternalFromUCS2(ustr, ustr.GetSize());
}
static int TranslateHex(char x)
{
if (x >= 'a')
return x - 'a' + 10;
if (x >= 'A')
return x - 'A' + '\x0a';
return x - '0';
}
static const unsigned char PStringEscapeCode[] = { 'a', 'b', 'f', 'n', 'r', 't', 'v' };
static const unsigned char PStringEscapeValue[] = { '\a', '\b', '\f', '\n', '\r', '\t', '\v' };
static void TranslateEscapes(const char * src, char * dst)
{
if (*src == '"')
src++;
while (*src != '\0') {
int c = *src++ & 0xff;
if (c == '"' && *src == '\0')
c = '\0'; // Trailing '"' is ignored
else if (c == '\\') {
c = *src++ & 0xff;
for (PINDEX i = 0; i < PARRAYSIZE(PStringEscapeCode); i++) {
if (c == PStringEscapeCode[i])
c = PStringEscapeValue[i];
}
if (c == 'x' && isxdigit(*src & 0xff)) {
c = TranslateHex(*src++);
if (isxdigit(*src & 0xff))
c = (c << 4) + TranslateHex(*src++);
}
else if (c >= '0' && c <= '7') {
int count = c <= '3' ? 3 : 2;
src--;
c = 0;
do {
c = (c << 3) + *src++ - '0';
} while (--count > 0 && *src >= '0' && *src <= '7');
}
}
*dst++ = (char)c;
}
}
PString::PString(ConversionType type, const char * str, ...)
{
switch (type) {
case Pascal :
if (*str != '\0') {
PINDEX len = *str & 0xff;
PAssert(SetSize(len+1), POutOfMemory);
memcpy(theArray, str+1, len);
}
break;
case Basic :
if (str[0] != '\0' && str[1] != '\0') {
PINDEX len = (str[0] & 0xff) | ((str[1] & 0xff) << 8);
PAssert(SetSize(len+1), POutOfMemory);
memcpy(theArray, str+2, len);
}
break;
case Literal :
PAssert(SetSize(strlen(str)+1), POutOfMemory);
TranslateEscapes(str, theArray);
PAssert(MakeMinimumSize(), POutOfMemory);
break;
case Printf : {
va_list args;
va_start(args, str);
vsprintf(str, args);
va_end(args);
break;
}
default :
PAssertAlways(PInvalidParameter);
}
}
template <class T> char * p_unsigned2string(T value, T base, char * str)
{
if (value >= base)
str = p_unsigned2string<T>(value/base, base, str);
value %= base;
if (value < 10)
*str = (char)(value + '0');
else
*str = (char)(value + 'A'-10);
return str+1;
}
template <class T> char * p_signed2string(T value, T base, char * str)
{
if (value >= 0)
return p_unsigned2string<T>(value, base, str);
*str = '-';
return p_unsigned2string<T>(-value, base, str+1);
}
PString::PString(short n)
: PCharArray(sizeof(short)*3+1)
{
p_signed2string<int>(n, 10, theArray);
MakeMinimumSize();
}
PString::PString(unsigned short n)
: PCharArray(sizeof(unsigned short)*3+1)
{
p_unsigned2string<unsigned short>(n, 10, theArray);
MakeMinimumSize();
}
PString::PString(int n)
: PCharArray(sizeof(int)*3+1)
{
p_signed2string<int>(n, 10, theArray);
MakeMinimumSize();
}
PString::PString(unsigned int n)
: PCharArray(sizeof(unsigned int)*3+1)
{
p_unsigned2string<unsigned int>(n, 10, theArray);
MakeMinimumSize();
}
PString::PString(long n)
: PCharArray(sizeof(long)*3+1)
{
p_signed2string<long>(n, 10, theArray);
MakeMinimumSize();
}
PString::PString(unsigned long n)
: PCharArray(sizeof(unsigned long)*3+1)
{
p_unsigned2string<unsigned long>(n, 10, theArray);
MakeMinimumSize();
}
PString::PString(PInt64 n)
: PCharArray(sizeof(PInt64)*3+1)
{
p_signed2string<PInt64>(n, 10, theArray);
MakeMinimumSize();
}
PString::PString(PUInt64 n)
: PCharArray(sizeof(PUInt64)*3+1)
{
p_unsigned2string<PUInt64>(n, 10, theArray);
MakeMinimumSize();
}
PString::PString(ConversionType type, long value, unsigned base)
: PCharArray(sizeof(long)*3+1)
{
PAssert(base >= 2 && base <= 36, PInvalidParameter);
switch (type) {
case Signed :
p_signed2string<long>(value, base, theArray);
break;
case Unsigned :
p_unsigned2string<unsigned long>(value, base, theArray);
break;
default :
PAssertAlways(PInvalidParameter);
}
MakeMinimumSize();
}
PString::PString(ConversionType type, double value, unsigned places)
{
switch (type) {
case Decimal :
sprintf("%0.*f", (int)places, value);
break;
case Exponent :
sprintf("%0.*e", (int)places, value);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -