diff --git a/ChangeLog b/ChangeLog index 01afad227..68df3d64a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2007-12-03 Fred Kiefer + + * Source/NSIndexSet.m (-initWithCoder:, -encodeWithCoder) + * Source/NSData.m (NSMutableData-initWithCoder:, + -encodeWithCoder): Add some keyed encoding/decoding. + 2007-12-03 Richard Frith-Macdonald * Source/NSDebug.m: diff --git a/Source/NSData.m b/Source/NSData.m index dae163d6c..63d86b260 100644 --- a/Source/NSData.m +++ b/Source/NSData.m @@ -1525,6 +1525,7 @@ failure: if ([coder allowsKeyedCoding]) { + // FIXME obj = [coder decodeObject]; } else @@ -1776,13 +1777,22 @@ failure: unsigned length = [self length]; void *bytes = [self mutableBytes]; - [aCoder encodeValueOfObjCType: @encode(unsigned int) - at: &length]; - if (length) + if ([aCoder allowsKeyedCoding]) { - [aCoder encodeArrayOfObjCType: @encode(unsigned char) - count: length - at: bytes]; + [aCoder encodeBytes: bytes + length: length + forKey:@"NS.data"]; + } + else + { + [aCoder encodeValueOfObjCType: @encode(unsigned int) + at: &length]; + if (length) + { + [aCoder encodeArrayOfObjCType: @encode(unsigned char) + count: length + at: bytes]; + } } } @@ -1800,31 +1810,44 @@ failure: - (id) initWithCoder: (NSCoder*)aCoder { unsigned l; - NSZone *zone; -#if GS_WITH_GC - zone = GSAtomicMallocZone(); -#else - zone = [self zone]; -#endif - - [aCoder decodeValueOfObjCType: @encode(unsigned int) at: &l]; - if (l) + if ([aCoder allowsKeyedCoding]) { - void *b = NSZoneMalloc(zone, l); + const uint8_t *data; - if (b == 0) - { - NSLog(@"[NSDataMalloc -initWithCoder:] unable to get %lu bytes", l); - RELEASE(self); - return nil; - } - [aCoder decodeArrayOfObjCType: @encode(unsigned char) count: l at: b]; - self = [self initWithBytesNoCopy: b length: l]; + data = [aCoder decodeBytesForKey: @"NS.data" + returnedLength: &l]; + self = [self initWithBytes: data length: l]; } else - { - self = [self initWithBytesNoCopy: 0 length: 0]; + { + [aCoder decodeValueOfObjCType: @encode(unsigned int) at: &l]; + if (l) + { + void *b; + NSZone *zone; + +#if GS_WITH_GC + zone = GSAtomicMallocZone(); +#else + zone = [self zone]; +#endif + + b = NSZoneMalloc(zone, l); + + if (b == 0) + { + NSLog(@"[NSDataMalloc -initWithCoder:] unable to get %lu bytes", l); + RELEASE(self); + return nil; + } + [aCoder decodeArrayOfObjCType: @encode(unsigned char) count: l at: b]; + self = [self initWithBytesNoCopy: b length: l]; + } + else + { + self = [self initWithBytesNoCopy: 0 length: 0]; + } } return self; } diff --git a/Source/NSIndexSet.m b/Source/NSIndexSet.m index 82ececbe3..34efcaa81 100644 --- a/Source/NSIndexSet.m +++ b/Source/NSIndexSet.m @@ -23,6 +23,7 @@ */ +#include "Foundation/NSCoder.h" #include #include #include @@ -274,7 +275,32 @@ static unsigned posForIndex(GSIArray array, unsigned index) - (void) encodeWithCoder: (NSCoder*)aCoder { - [self notImplemented:_cmd]; + int rangeCount = 0; + + if (_array != 0) + { + rangeCount = GSIArrayCount(_array); + } + + [aCoder encodeInt: rangeCount forKey: @"NSRangeCount"]; + + if (rangeCount == 0) + { + // Do nothing + } + else if (rangeCount == 1) + { + NSRange r; + + r = GSIArrayItemAtIndex(_array, 0).ext; + [aCoder encodeInt: r.location forKey: @"NSLocation"]; + [aCoder encodeInt: r.length forKey: @"NSLength"]; + } + else + { + // FIXME + [self notImplemented:_cmd]; + } } - (unsigned int) firstIndex @@ -456,7 +482,53 @@ static unsigned posForIndex(GSIArray array, unsigned index) - (id) initWithCoder: (NSCoder*)aCoder { - [self notImplemented:_cmd]; + int rangeCount = 0; + + if ([aCoder containsValueForKey: @"NSRangeCount"]) + { + rangeCount = [aCoder decodeIntForKey: @"NSRangeCount"]; + } + + if (rangeCount == 0) + { + // Do nothing + } + else if (rangeCount == 1) + { + unsigned len = 0; + unsigned loc = 0; + + if ([aCoder containsValueForKey: @"NSLocation"]) + { + loc = [aCoder decodeIntForKey: @"NSLocation"]; + } + if ([aCoder containsValueForKey: @"NSLength"]) + { + len = [aCoder decodeIntForKey: @"NSLength"]; + } + self = [self initWithIndexesInRange: NSMakeRange(loc, len)]; + } + else + { + NSData * data = nil; + + if ([aCoder containsValueForKey: @"NSRangeData"]) + { + data = [aCoder decodeIntForKey: @"NSRangeData"]; + } + + /* + FIXME: + NSLog(@"Decoded count %d, data %@", rangeCount, data); + This is a very strange format: + + 5 + 6 + 9 gives <05020901> + 5 + 6 + 23 gives <05021701> + 155 + 156 + 223 gives <9b0102df 0101> + */ + [self notImplemented:_cmd]; + } + return self; }