Improved keydecoding.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@20214 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
FredKiefer 2004-10-09 15:40:23 +00:00
parent 0f373a108e
commit e17a91003f
3 changed files with 182 additions and 74 deletions

View file

@ -163,47 +163,58 @@ static Class GSMutableAttributedStringClass;
- (id) initWithCoder: (NSCoder*)aDecoder - (id) initWithCoder: (NSCoder*)aDecoder
{ {
NSString *string = [aDecoder decodeObject]; if ([aDecoder allowsKeyedCoding])
unsigned length = [string length];
if (length == 0)
{ {
self = [self initWithString: string attributes: nil]; NSString *string = [aDecoder decodeObjectForKey: @"NSString"];
NSDictionary *attributes = [aDecoder decodeObjectForKey: @"NSAttributes"];
self = [self initWithString: string attributes: attributes];
} }
else else
{ {
unsigned index; NSString *string = [aDecoder decodeObject];
NSDictionary *attrs; unsigned length = [string length];
[aDecoder decodeValueOfObjCType: @encode(unsigned int) at: &index]; if (length == 0)
attrs = [aDecoder decodeObject]; {
if (index == length) self = [self initWithString: string attributes: nil];
{
self = [self initWithString: string attributes: attrs];
} }
else else
{ {
NSRange r = NSMakeRange(0, index); unsigned index;
unsigned last = index; NSDictionary *attrs;
NSMutableAttributedString *m;
[aDecoder decodeValueOfObjCType: @encode(unsigned int) at: &index];
m = [NSMutableAttributedString alloc]; attrs = [aDecoder decodeObject];
m = [m initWithString: string attributes: nil]; if (index == length)
[m setAttributes: attrs range: r];
while (index < length)
{ {
[aDecoder decodeValueOfObjCType: @encode(unsigned int) self = [self initWithString: string attributes: attrs];
at: &index]; }
attrs = [aDecoder decodeObject]; else
r = NSMakeRange(last, index - last); {
[m setAttributes: attrs range: r]; NSRange r = NSMakeRange(0, index);
last = index; unsigned last = index;
NSMutableAttributedString *m;
m = [NSMutableAttributedString alloc];
m = [m initWithString: string attributes: nil];
[m setAttributes: attrs range: r];
while (index < length)
{
[aDecoder decodeValueOfObjCType: @encode(unsigned int)
at: &index];
attrs = [aDecoder decodeObject];
r = NSMakeRange(last, index - last);
[m setAttributes: attrs range: r];
last = index;
}
RELEASE(self);
self = [m copy];
RELEASE(m);
} }
RELEASE(self);
self = [m copy];
RELEASE(m);
} }
} }
return self; return self;
} }

View file

@ -188,26 +188,91 @@
- (id) initWithCoder: (NSCoder*)decoder - (id) initWithCoder: (NSCoder*)decoder
{ {
[decoder decodeValueOfObjCType: @encode(BOOL) at: &_hasThousandSeparators]; if ([decoder allowsKeyedCoding])
[decoder decodeValueOfObjCType: @encode(BOOL) at: &_allowsFloats]; {
[decoder decodeValueOfObjCType: @encode(BOOL) at: &_localizesFormat]; if ([decoder containsValueForKey: @"NS.allowsfloats"])
[decoder decodeValueOfObjCType: @encode(unichar) at: &_thousandSeparator]; {
[decoder decodeValueOfObjCType: @encode(unichar) at: &_decimalSeparator]; [self setAllowsFloats: [decoder decodeBoolForKey: @"NS.allowsfloats"]];
}
[decoder decodeValueOfObjCType: @encode(id) at: &_roundingBehavior]; if ([decoder containsValueForKey: @"NS.decimal"])
[decoder decodeValueOfObjCType: @encode(id) at: &_maximum]; {
[decoder decodeValueOfObjCType: @encode(id) at: &_minimum]; [self setDecimalSeparator: [decoder decodeObjectForKey: @"NS.decimal"]];
[decoder decodeValueOfObjCType: @encode(id) at: &_attributedStringForNil]; }
[decoder decodeValueOfObjCType: @encode(id) if ([decoder containsValueForKey: @"NS.hasthousands"])
at: &_attributedStringForNotANumber]; {
[decoder decodeValueOfObjCType: @encode(id) at: &_attributedStringForZero]; [self setHasThousandSeparators: [decoder decodeBoolForKey: @"NS.hasthousands"]];
[decoder decodeValueOfObjCType: @encode(id) at: &_negativeFormat]; }
[decoder decodeValueOfObjCType: @encode(id) at: &_positiveFormat]; if ([decoder containsValueForKey: @"NS.localized"])
[decoder decodeValueOfObjCType: @encode(id) {
at: &_attributesForPositiveValues]; [self setLocalizesFormat: [decoder decodeBoolForKey: @"NS.localized"]];
[decoder decodeValueOfObjCType: @encode(id) }
at: &_attributesForNegativeValues]; if ([decoder containsValueForKey: @"NS.max"])
{
[self setMaximum: [decoder decodeObjectForKey: @"NS.max"]];
}
if ([decoder containsValueForKey: @"NS.min"])
{
[self setMinimum: [decoder decodeObjectForKey: @"NS.min"]];
}
if ([decoder containsValueForKey: @"NS.nan"])
{
[self setAttributedStringForNotANumber: [decoder decodeObjectForKey: @"NS.nan"]];
}
if ([decoder containsValueForKey: @"NS.negativeattrs"])
{
[self setTextAttributesForNegativeValues: [decoder decodeObjectForKey: @"NS.negativeattrs"]];
}
if ([decoder containsValueForKey: @"NS.negativeformat"])
{
[self setNegativeFormat: [decoder decodeObjectForKey: @"NS.negativeformat"]];
}
if ([decoder containsValueForKey: @"NS.nil"])
{
[self setAttributedStringForNil: [decoder decodeObjectForKey: @"NS.nil"]];
}
if ([decoder containsValueForKey: @"NS.positiveattrs"])
{
[self setTextAttributesForPositiveValues: [decoder decodeObjectForKey: @"NS.positiveattrs"]];
}
if ([decoder containsValueForKey: @"NS.positiveformat"])
{
[self setPositiveFormat: [decoder decodeObjectForKey: @"NS.positiveformat"]];
}
if ([decoder containsValueForKey: @"NS.rounding"])
{
[self setRoundingBehavior: [decoder decodeObjectForKey: @"NS.rounding"]];
}
if ([decoder containsValueForKey: @"NS.thousand"])
{
[self setThousandSeparator: [decoder decodeObjectForKey: @"NS.thousand"]];
}
if ([decoder containsValueForKey: @"NS.zero"])
{
[self setAttributedStringForZero: [decoder decodeObjectForKey: @"NS.zero"]];
}
}
else
{
[decoder decodeValueOfObjCType: @encode(BOOL) at: &_hasThousandSeparators];
[decoder decodeValueOfObjCType: @encode(BOOL) at: &_allowsFloats];
[decoder decodeValueOfObjCType: @encode(BOOL) at: &_localizesFormat];
[decoder decodeValueOfObjCType: @encode(unichar) at: &_thousandSeparator];
[decoder decodeValueOfObjCType: @encode(unichar) at: &_decimalSeparator];
[decoder decodeValueOfObjCType: @encode(id) at: &_roundingBehavior];
[decoder decodeValueOfObjCType: @encode(id) at: &_maximum];
[decoder decodeValueOfObjCType: @encode(id) at: &_minimum];
[decoder decodeValueOfObjCType: @encode(id) at: &_attributedStringForNil];
[decoder decodeValueOfObjCType: @encode(id)
at: &_attributedStringForNotANumber];
[decoder decodeValueOfObjCType: @encode(id) at: &_attributedStringForZero];
[decoder decodeValueOfObjCType: @encode(id) at: &_negativeFormat];
[decoder decodeValueOfObjCType: @encode(id) at: &_positiveFormat];
[decoder decodeValueOfObjCType: @encode(id)
at: &_attributesForPositiveValues];
[decoder decodeValueOfObjCType: @encode(id)
at: &_attributesForNegativeValues];
}
return self; return self;
} }

View file

@ -57,7 +57,8 @@ extern BOOL GSScanDouble(unichar*, unsigned, double*);
NSPropertyListMutabilityOptions mutability; NSPropertyListMutabilityOptions mutability;
const unsigned char *_bytes; const unsigned char *_bytes;
NSData *data; NSData *data;
unsigned size; // Number of bytes per table entry unsigned offset_size; // Number of bytes per table entry
unsigned index_size; // Number of bytes per table entry
unsigned table_start; // Start address of object table unsigned table_start; // Start address of object table
unsigned table_len; // Length of object table unsigned table_len; // Length of object table
} }
@ -2437,12 +2438,19 @@ GSPropertyListMake(id obj, NSDictionary *loc, BOOL xml,
// FIXME: Get more of the details // FIXME: Get more of the details
[plData getBytes: postfix range: NSMakeRange(length-32, 32)]; [plData getBytes: postfix range: NSMakeRange(length-32, 32)];
size = postfix[6]; offset_size = postfix[6];
index_size = postfix[7];
table_start = 256*256*postfix[29] + 256*postfix[30] + postfix[31]; table_start = 256*256*postfix[29] + 256*postfix[30] + postfix[31];
if (size < 1 || size > 3) if (offset_size < 1 || offset_size > 3)
{ {
[NSException raise: NSGenericException [NSException raise: NSGenericException
format: @"Unknown table size %d", size]; format: @"Unknown table size %d", offset_size];
DESTROY(self); // Bad format
}
else if (index_size < 1 || index_size > 3)
{
[NSException raise: NSGenericException
format: @"Unknown table size %d", index_size];
DESTROY(self); // Bad format DESTROY(self); // Bad format
} }
else if (table_start > length - 32) else if (table_start > length - 32)
@ -2469,7 +2477,7 @@ GSPropertyListMake(id obj, NSDictionary *loc, BOOL xml,
format: @"Object table index out of bounds %d.", index]; format: @"Object table index out of bounds %d.", index];
} }
if (size == 1) if (offset_size == 1)
{ {
unsigned char offset; unsigned char offset;
@ -2477,7 +2485,7 @@ GSPropertyListMake(id obj, NSDictionary *loc, BOOL xml,
return offset; return offset;
} }
else if (size == 2) else if (offset_size == 2)
{ {
unsigned short offset; unsigned short offset;
@ -2487,12 +2495,12 @@ GSPropertyListMake(id obj, NSDictionary *loc, BOOL xml,
} }
else else
{ {
unsigned char buffer[size]; unsigned char buffer[offset_size];
int i; int i;
unsigned num = 0; unsigned num = 0;
[data getBytes: &buffer range: NSMakeRange(table_start + size*index, size)]; [data getBytes: &buffer range: NSMakeRange(table_start + offset_size*index, offset_size)];
for (i = 0; i < size; i++) for (i = 0; i < offset_size; i++)
{ {
num = num*256 + buffer[i]; num = num*256 + buffer[i];
} }
@ -2503,7 +2511,7 @@ GSPropertyListMake(id obj, NSDictionary *loc, BOOL xml,
- (unsigned) readObjectIndexAt: (unsigned*)counter - (unsigned) readObjectIndexAt: (unsigned*)counter
{ {
if (size == 1) if (index_size == 1)
{ {
unsigned char oid; unsigned char oid;
@ -2511,24 +2519,33 @@ GSPropertyListMake(id obj, NSDictionary *loc, BOOL xml,
*counter += 1; *counter += 1;
return oid; return oid;
} }
else if ((size == 2) || (size == 3)) else if (index_size == 2)
{ {
unsigned short oid; unsigned short oid;
[data getBytes: &oid range: NSMakeRange(*counter,sizeof(short))]; [data getBytes: &oid range: NSMakeRange(*counter, 2)];
*counter += sizeof(short); *counter += 2;
return NSSwapBigShortToHost(oid); return NSSwapBigShortToHost(oid);
} }
else else
{ {
[NSException raise: NSGenericException unsigned char buffer[index_size];
format: @"Unkown table size %d", size]; int i;
unsigned num = 0;
[data getBytes: &buffer range: NSMakeRange(*counter, index_size)];
*counter += index_size;
for (i = 0; i < index_size; i++)
{
num = num*256 + buffer[i];
}
return num;
} }
return 0; return 0;
} }
- (unsigned) readCountAt: (unsigned*) counter - (unsigned long) readCountAt: (unsigned*) counter
{ {
unsigned char c; unsigned char c;
@ -2551,11 +2568,26 @@ GSPropertyListMake(id obj, NSDictionary *loc, BOOL xml,
*counter += 2; *counter += 2;
return NSSwapBigShortToHost(count); return NSSwapBigShortToHost(count);
} }
else if ((c > 0x11) && (c <= 0x13))
{
unsigned len = 1 << (c - 0x10);
unsigned char buffer[len];
int i;
unsigned num = 0;
[data getBytes: &buffer range: NSMakeRange(*counter, len)];
*counter += len;
for (i = 0; i < len; i++)
{
num = num*256 + buffer[i];
}
return num;
}
else else
{ {
//FIXME //FIXME
[NSException raise: NSGenericException [NSException raise: NSGenericException
format: @"Unkown coutn type %d", c]; format: @"Unknown count type %d", c];
} }
return 0; return 0;
@ -2589,7 +2621,7 @@ GSPropertyListMake(id obj, NSDictionary *loc, BOOL xml,
else if ((next >= 0x10) && (next < 0x1F)) else if ((next >= 0x10) && (next < 0x1F))
{ {
// integer number // integer number
unsigned len = next - 0x10 + 1; unsigned len = 1 << (next - 0x10);
int num = 0; int num = 0;
unsigned i; unsigned i;
unsigned char buffer[16]; unsigned char buffer[16];
@ -2645,7 +2677,7 @@ GSPropertyListMake(id obj, NSDictionary *loc, BOOL xml,
else if (next == 0x4F) else if (next == 0x4F)
{ {
// long data // long data
unsigned len; unsigned long len;
len = [self readCountAt: &counter]; len = [self readCountAt: &counter];
if (mutability == NSPropertyListMutableContainersAndLeaves) if (mutability == NSPropertyListMutableContainersAndLeaves)
@ -2678,7 +2710,7 @@ GSPropertyListMake(id obj, NSDictionary *loc, BOOL xml,
else if (next == 0x5F) else if (next == 0x5F)
{ {
// long string // long string
unsigned len; unsigned long len;
char *buffer; char *buffer;
len = [self readCountAt: &counter]; len = [self readCountAt: &counter];
@ -2722,7 +2754,7 @@ GSPropertyListMake(id obj, NSDictionary *loc, BOOL xml,
else if (next == 0x6F) else if (next == 0x6F)
{ {
// long unicode string // long unicode string
unsigned len; unsigned long len;
unsigned i; unsigned i;
unichar *buffer; unichar *buffer;
@ -2791,7 +2823,7 @@ GSPropertyListMake(id obj, NSDictionary *loc, BOOL xml,
else if (next == 0xAF) else if (next == 0xAF)
{ {
// big array // big array
unsigned len; unsigned long len;
unsigned i; unsigned i;
id *objects; id *objects;
@ -2855,7 +2887,7 @@ GSPropertyListMake(id obj, NSDictionary *loc, BOOL xml,
else if (next == 0xDF) else if (next == 0xDF)
{ {
// big dictionary // big dictionary
unsigned len; unsigned long len;
unsigned i; unsigned i;
id *keys; id *keys;
id *values; id *values;