1995-04-03 22:59:20 +00:00
|
|
|
/* NSDictionary - Dictionary object to store key/value pairs
|
1997-01-06 22:43:08 +00:00
|
|
|
Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
|
1995-04-04 16:15:31 +00:00
|
|
|
|
1996-04-17 20:17:45 +00:00
|
|
|
Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
|
1995-05-05 18:31:51 +00:00
|
|
|
From skeleton by: Adam Fedor <fedor@boulder.colorado.edu>
|
1995-04-03 22:59:20 +00:00
|
|
|
Date: Mar 1995
|
1995-04-04 16:15:31 +00:00
|
|
|
|
1996-05-12 00:56:10 +00:00
|
|
|
This file is part of the GNUstep Base Library.
|
1995-04-04 16:15:31 +00:00
|
|
|
|
1995-04-03 22:59:20 +00:00
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
1995-04-04 16:15:31 +00:00
|
|
|
|
1995-04-03 22:59:20 +00:00
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with this library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
1995-04-04 16:15:31 +00:00
|
|
|
*/
|
1995-04-03 22:59:20 +00:00
|
|
|
|
1997-11-06 00:51:23 +00:00
|
|
|
#include <config.h>
|
1998-12-20 21:27:47 +00:00
|
|
|
#include <base/behavior.h>
|
1995-04-17 21:13:20 +00:00
|
|
|
#include <Foundation/NSDictionary.h>
|
1995-05-05 18:31:51 +00:00
|
|
|
#include <Foundation/NSArray.h>
|
|
|
|
#include <Foundation/NSUtilities.h>
|
1998-01-19 15:20:15 +00:00
|
|
|
#include <Foundation/NSString.h>
|
|
|
|
#include <Foundation/NSException.h>
|
|
|
|
#include <Foundation/NSAutoreleasePool.h>
|
1998-12-02 21:00:54 +00:00
|
|
|
#include <Foundation/NSFileManager.h>
|
1995-04-03 22:59:20 +00:00
|
|
|
|
1997-10-16 23:56:27 +00:00
|
|
|
@interface NSDictionaryNonCore : NSDictionary
|
|
|
|
@end
|
|
|
|
@interface NSMutableDictionaryNonCore: NSMutableDictionary
|
|
|
|
@end
|
|
|
|
|
1995-04-03 22:59:20 +00:00
|
|
|
@implementation NSDictionary
|
|
|
|
|
1998-10-01 16:07:02 +00:00
|
|
|
@class NSGDictionary;
|
|
|
|
@class NSGMutableDictionary;
|
|
|
|
|
1995-06-30 14:21:45 +00:00
|
|
|
static Class NSDictionary_concrete_class;
|
|
|
|
static Class NSMutableDictionary_concrete_class;
|
|
|
|
|
|
|
|
+ (void) _setConcreteClass: (Class)c
|
|
|
|
{
|
|
|
|
NSDictionary_concrete_class = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void) _setMutableConcreteClass: (Class)c
|
|
|
|
{
|
|
|
|
NSMutableDictionary_concrete_class = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (Class) _concreteClass
|
|
|
|
{
|
|
|
|
return NSDictionary_concrete_class;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (Class) _mutableConcreteClass
|
|
|
|
{
|
|
|
|
return NSMutableDictionary_concrete_class;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
{
|
1997-10-16 23:56:27 +00:00
|
|
|
if (self == [NSDictionary class])
|
|
|
|
{
|
|
|
|
NSDictionary_concrete_class = [NSGDictionary class];
|
|
|
|
NSMutableDictionary_concrete_class = [NSGMutableDictionary class];
|
|
|
|
behavior_class_add_class (self, [NSDictionaryNonCore class]);
|
|
|
|
}
|
1995-06-30 14:21:45 +00:00
|
|
|
}
|
|
|
|
|
1995-05-05 18:31:51 +00:00
|
|
|
+ allocWithZone: (NSZone*)z
|
1995-04-03 22:59:20 +00:00
|
|
|
{
|
1999-01-11 17:28:51 +00:00
|
|
|
if ([self class] == [NSDictionary class])
|
|
|
|
return NSAllocateObject([self _concreteClass], 0, z);
|
|
|
|
return [super allocWithZone: z];
|
1995-04-03 22:59:20 +00:00
|
|
|
}
|
|
|
|
|
1997-10-16 23:56:27 +00:00
|
|
|
/* This is the designated initializer */
|
|
|
|
- initWithObjects: (id*)objects
|
1998-10-27 08:12:49 +00:00
|
|
|
forKeys: (id*)keys
|
1997-10-16 23:56:27 +00:00
|
|
|
count: (unsigned)count
|
|
|
|
{
|
1999-04-08 12:17:15 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
1997-10-16 23:56:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (unsigned) count
|
|
|
|
{
|
1999-04-08 12:17:15 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
1997-10-16 23:56:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1998-10-27 08:12:49 +00:00
|
|
|
- objectForKey: (id)aKey
|
1997-10-16 23:56:27 +00:00
|
|
|
{
|
1999-04-08 12:17:15 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
1997-10-16 23:56:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSEnumerator*) keyEnumerator
|
|
|
|
{
|
1999-04-08 12:17:15 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
1997-10-16 23:56:27 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSEnumerator*) objectEnumerator
|
|
|
|
{
|
1999-04-08 12:17:15 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
1997-10-16 23:56:27 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
1998-07-30 16:34:32 +00:00
|
|
|
- copyWithZone: (NSZone*)z
|
|
|
|
{
|
1999-04-08 12:17:15 +00:00
|
|
|
return [self retain];
|
1998-07-30 16:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- mutableCopyWithZone: (NSZone*)z
|
|
|
|
{
|
1999-04-08 12:17:15 +00:00
|
|
|
return [[[[self class] _mutableConcreteClass] allocWithZone: z]
|
|
|
|
initWithDictionary: self];
|
1998-07-30 16:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
|
|
|
{
|
1999-04-08 12:17:15 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
1998-07-30 16:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithCoder: (NSCoder*)aCoder
|
|
|
|
{
|
1999-04-08 12:17:15 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
1998-07-30 16:34:32 +00:00
|
|
|
return nil;
|
|
|
|
}
|
1997-10-16 23:56:27 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSDictionaryNonCore
|
|
|
|
|
1995-05-05 18:31:51 +00:00
|
|
|
+ dictionary
|
1995-04-03 22:59:20 +00:00
|
|
|
{
|
1995-11-03 22:18:47 +00:00
|
|
|
return [[[self alloc] init]
|
1995-05-05 18:31:51 +00:00
|
|
|
autorelease];
|
1995-04-03 22:59:20 +00:00
|
|
|
}
|
|
|
|
|
1998-03-23 20:49:54 +00:00
|
|
|
+ dictionaryWithDictionary: (NSDictionary*)otherDictionary
|
|
|
|
{
|
|
|
|
return [[[self alloc] initWithDictionary: otherDictionary] autorelease];
|
|
|
|
}
|
|
|
|
|
1995-05-05 18:31:51 +00:00
|
|
|
+ dictionaryWithObjects: (id*)objects
|
1998-10-27 08:12:49 +00:00
|
|
|
forKeys: (id*)keys
|
1995-05-05 18:31:51 +00:00
|
|
|
count: (unsigned)count
|
1995-04-03 22:59:20 +00:00
|
|
|
{
|
1999-04-08 12:17:15 +00:00
|
|
|
return [[[self alloc] initWithObjects: objects
|
|
|
|
forKeys: keys
|
|
|
|
count: count]
|
1995-05-05 18:31:51 +00:00
|
|
|
autorelease];
|
1995-04-03 22:59:20 +00:00
|
|
|
}
|
|
|
|
|
1998-10-27 08:12:49 +00:00
|
|
|
- (unsigned) hash
|
|
|
|
{
|
|
|
|
return [self count];
|
|
|
|
}
|
|
|
|
|
1995-05-05 18:31:51 +00:00
|
|
|
- initWithObjects: (NSArray*)objects forKeys: (NSArray*)keys
|
1995-04-03 22:59:20 +00:00
|
|
|
{
|
1998-10-27 08:12:49 +00:00
|
|
|
int objectCount = [objects count];
|
|
|
|
id os[objectCount], ks[objectCount];
|
1995-05-05 18:31:51 +00:00
|
|
|
int i;
|
|
|
|
|
1998-10-27 08:12:49 +00:00
|
|
|
if (objectCount != [keys count])
|
1995-05-05 18:31:51 +00:00
|
|
|
{
|
1998-10-27 08:12:49 +00:00
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"init with obj and key arrays of different sizes"];
|
1995-05-05 18:31:51 +00:00
|
|
|
}
|
1998-10-27 08:12:49 +00:00
|
|
|
[objects getObjects: os];
|
|
|
|
[keys getObjects: ks];
|
1999-04-08 12:17:15 +00:00
|
|
|
return [self initWithObjects: os forKeys: ks count: objectCount];
|
1995-04-03 22:59:20 +00:00
|
|
|
}
|
|
|
|
|
1998-10-31 05:17:54 +00:00
|
|
|
- (id) initWithObjectsAndKeys: (id)firstObject, ...
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int capacity = 16;
|
|
|
|
int num_pairs = 0;
|
|
|
|
id *objects;
|
|
|
|
id *keys;
|
|
|
|
id arg;
|
|
|
|
int argi = 1;
|
|
|
|
|
|
|
|
va_start (ap, firstObject);
|
1999-01-11 17:28:51 +00:00
|
|
|
if (firstObject == nil)
|
1998-10-31 05:17:54 +00:00
|
|
|
{
|
|
|
|
return [self init];
|
|
|
|
}
|
|
|
|
/* Gather all the arguments in a simple array, in preparation for
|
|
|
|
calling the designated initializer. */
|
|
|
|
OBJC_MALLOC (objects, id, capacity);
|
|
|
|
OBJC_MALLOC (keys, id, capacity);
|
|
|
|
|
|
|
|
objects[num_pairs] = firstObject;
|
|
|
|
/* Keep grabbing arguments until we get a nil... */
|
|
|
|
while ((arg = va_arg (ap, id)))
|
|
|
|
{
|
|
|
|
if (num_pairs >= capacity)
|
|
|
|
{
|
|
|
|
/* Must increase capacity in order to fit additional ARG's. */
|
|
|
|
capacity *= 2;
|
|
|
|
OBJC_REALLOC (objects, id, capacity);
|
|
|
|
OBJC_REALLOC (keys, id, capacity);
|
|
|
|
}
|
|
|
|
/* ...and alternately dump them into OBJECTS and KEYS */
|
|
|
|
if (argi++ % 2 == 0)
|
|
|
|
objects[num_pairs] = arg;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
keys[num_pairs] = arg;
|
|
|
|
num_pairs++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (argi %2 != 0)
|
|
|
|
{
|
|
|
|
OBJC_FREE(objects);
|
|
|
|
OBJC_FREE(keys);
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"init dictionary with nil key"];
|
|
|
|
}
|
|
|
|
self = [self initWithObjects: objects forKeys: keys count: num_pairs];
|
|
|
|
OBJC_FREE(objects);
|
|
|
|
OBJC_FREE(keys);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
1996-10-25 23:43:00 +00:00
|
|
|
+ dictionaryWithObjectsAndKeys: (id)firstObject, ...
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int capacity = 16;
|
1996-10-31 20:06:15 +00:00
|
|
|
int num_pairs = 0;
|
1996-10-25 23:43:00 +00:00
|
|
|
id *objects;
|
|
|
|
id *keys;
|
|
|
|
id arg;
|
|
|
|
int argi = 1;
|
|
|
|
|
|
|
|
va_start (ap, firstObject);
|
|
|
|
/* Gather all the arguments in a simple array, in preparation for
|
|
|
|
calling the designated initializer. */
|
|
|
|
OBJC_MALLOC (objects, id, capacity);
|
|
|
|
OBJC_MALLOC (keys, id, capacity);
|
|
|
|
if (firstObject != nil)
|
|
|
|
{
|
1998-10-01 16:07:02 +00:00
|
|
|
NSDictionary *d;
|
1996-10-25 23:43:00 +00:00
|
|
|
objects[num_pairs] = firstObject;
|
|
|
|
/* Keep grabbing arguments until we get a nil... */
|
|
|
|
while ((arg = va_arg (ap, id)))
|
|
|
|
{
|
|
|
|
if (num_pairs >= capacity)
|
|
|
|
{
|
|
|
|
/* Must increase capacity in order to fit additional ARG's. */
|
|
|
|
capacity *= 2;
|
|
|
|
OBJC_REALLOC (objects, id, capacity);
|
|
|
|
OBJC_REALLOC (keys, id, capacity);
|
|
|
|
}
|
|
|
|
/* ...and alternately dump them into OBJECTS and KEYS */
|
|
|
|
if (argi++ % 2 == 0)
|
|
|
|
objects[num_pairs] = arg;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
keys[num_pairs] = arg;
|
|
|
|
num_pairs++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NSAssert (argi % 2 == 0, NSInvalidArgumentException);
|
1998-10-01 16:07:02 +00:00
|
|
|
d = [[[self alloc] initWithObjects: objects forKeys: keys
|
|
|
|
count: num_pairs] autorelease];
|
|
|
|
OBJC_FREE(objects);
|
|
|
|
OBJC_FREE(keys);
|
|
|
|
return d;
|
1996-10-25 23:43:00 +00:00
|
|
|
}
|
|
|
|
/* FIRSTOBJECT was nil; just return an empty NSDictionary object. */
|
|
|
|
return [self dictionary];
|
|
|
|
}
|
|
|
|
|
1995-05-05 18:31:51 +00:00
|
|
|
+ dictionaryWithObjects: (NSArray*)objects forKeys: (NSArray*)keys
|
1995-04-03 22:59:20 +00:00
|
|
|
{
|
1999-04-08 12:17:15 +00:00
|
|
|
return [[[self alloc] initWithObjects: objects forKeys: keys]
|
1995-05-05 18:31:51 +00:00
|
|
|
autorelease];
|
1995-04-03 22:59:20 +00:00
|
|
|
}
|
|
|
|
|
1998-07-30 16:34:32 +00:00
|
|
|
+ dictionaryWithObject: (id)object forKey: (id)key
|
|
|
|
{
|
|
|
|
return [[[self alloc] initWithObjects: &object forKeys: &key count: 1]
|
|
|
|
autorelease];
|
|
|
|
}
|
|
|
|
|
1995-05-05 18:31:51 +00:00
|
|
|
/* Override superclass's designated initializer */
|
|
|
|
- init
|
|
|
|
{
|
1999-04-08 12:17:15 +00:00
|
|
|
return [self initWithObjects: NULL forKeys: NULL count: 0];
|
1995-05-05 18:31:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- initWithDictionary: (NSDictionary*)other
|
1998-10-27 08:12:49 +00:00
|
|
|
{
|
|
|
|
return [self initWithDictionary: other copyItems: NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
- initWithDictionary: (NSDictionary*)other copyItems: (BOOL)shouldCopy
|
1995-05-05 18:31:51 +00:00
|
|
|
{
|
|
|
|
int c = [other count];
|
|
|
|
id os[c], ks[c], k, e = [other keyEnumerator];
|
|
|
|
int i = 0;
|
|
|
|
|
1998-10-27 08:12:49 +00:00
|
|
|
if (shouldCopy)
|
|
|
|
{
|
|
|
|
NSZone *z = [self zone];
|
|
|
|
|
|
|
|
while ((k = [e nextObject]))
|
|
|
|
{
|
|
|
|
ks[i] = k;
|
|
|
|
os[i] = [[other objectForKey: k] copyWithZone: z];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
self = [self initWithObjects: os forKeys: ks count: i];
|
|
|
|
while (i > 0)
|
|
|
|
{
|
|
|
|
[os[--i] release];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
else
|
1995-05-05 18:31:51 +00:00
|
|
|
{
|
1998-10-27 08:12:49 +00:00
|
|
|
while ((k = [e nextObject]))
|
|
|
|
{
|
|
|
|
ks[i] = k;
|
1999-04-08 12:17:15 +00:00
|
|
|
os[i] = [other objectForKey: k];
|
1998-10-27 08:12:49 +00:00
|
|
|
i++;
|
|
|
|
}
|
1999-04-08 12:17:15 +00:00
|
|
|
return [self initWithObjects: os forKeys: ks count: c];
|
1995-05-05 18:31:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- initWithContentsOfFile: (NSString*)path
|
1995-04-03 22:59:20 +00:00
|
|
|
{
|
1997-01-06 22:43:08 +00:00
|
|
|
NSString *myString;
|
|
|
|
|
1999-02-01 10:36:05 +00:00
|
|
|
myString = [[NSString alloc] initWithContentsOfFile: path];
|
1997-01-06 22:43:08 +00:00
|
|
|
if (myString)
|
|
|
|
{
|
1999-02-04 13:51:29 +00:00
|
|
|
id result;
|
1999-02-01 10:36:05 +00:00
|
|
|
|
1999-02-04 13:51:29 +00:00
|
|
|
NS_DURING
|
|
|
|
{
|
|
|
|
result = [myString propertyList];
|
|
|
|
}
|
|
|
|
NS_HANDLER
|
|
|
|
{
|
|
|
|
result = nil;
|
|
|
|
}
|
|
|
|
NS_ENDHANDLER
|
1999-02-01 10:36:05 +00:00
|
|
|
[myString release];
|
|
|
|
if ([result isKindOfClass: [NSDictionary class]])
|
1997-09-13 17:52:31 +00:00
|
|
|
{
|
|
|
|
[self initWithDictionary: result];
|
|
|
|
return self;
|
|
|
|
}
|
1997-01-06 22:43:08 +00:00
|
|
|
}
|
1998-05-13 19:25:38 +00:00
|
|
|
NSLog(@"Contents of file does not contain a dictionary");
|
1999-02-01 10:36:05 +00:00
|
|
|
[self release];
|
1997-01-06 22:43:08 +00:00
|
|
|
return nil;
|
1995-04-03 22:59:20 +00:00
|
|
|
}
|
|
|
|
|
1999-04-08 12:17:15 +00:00
|
|
|
+ dictionaryWithContentsOfFile: (NSString *)path
|
1997-01-09 16:05:09 +00:00
|
|
|
{
|
1999-04-08 12:17:15 +00:00
|
|
|
return [[[self alloc] initWithContentsOfFile: path]
|
1997-01-09 16:05:09 +00:00
|
|
|
autorelease];
|
|
|
|
}
|
|
|
|
|
1995-05-05 18:31:51 +00:00
|
|
|
- (BOOL) isEqual: other
|
|
|
|
{
|
1999-04-08 12:17:15 +00:00
|
|
|
if ([other isKindOfClass: [NSDictionary class]])
|
|
|
|
return [self isEqualToDictionary: other];
|
1995-05-05 18:31:51 +00:00
|
|
|
return NO;
|
|
|
|
}
|
1995-04-03 22:59:20 +00:00
|
|
|
|
1995-05-05 18:31:51 +00:00
|
|
|
- (BOOL) isEqualToDictionary: (NSDictionary*)other
|
1995-04-03 22:59:20 +00:00
|
|
|
{
|
1995-05-05 18:31:51 +00:00
|
|
|
if ([self count] != [other count])
|
|
|
|
return NO;
|
|
|
|
{
|
|
|
|
id k, e = [self keyEnumerator];
|
|
|
|
while ((k = [e nextObject]))
|
1997-10-16 23:56:27 +00:00
|
|
|
{
|
|
|
|
id o1 = [self objectForKey: k];
|
|
|
|
id o2 = [other objectForKey: k];
|
|
|
|
if (![o1 isEqual: o2])
|
|
|
|
return NO;
|
|
|
|
/*
|
1999-04-08 12:17:15 +00:00
|
|
|
if (![[self objectForKey: k] isEqual: [other objectForKey: k]])
|
1997-10-16 23:56:27 +00:00
|
|
|
return NO; */
|
|
|
|
}
|
1995-05-05 18:31:51 +00:00
|
|
|
}
|
|
|
|
/* xxx Recheck this. */
|
|
|
|
return YES;
|
1995-04-03 22:59:20 +00:00
|
|
|
}
|
|
|
|
|
1995-05-05 18:31:51 +00:00
|
|
|
- (NSArray*) allKeys
|
1995-04-03 22:59:20 +00:00
|
|
|
{
|
1995-05-05 18:31:51 +00:00
|
|
|
id e = [self keyEnumerator];
|
|
|
|
int i, c = [self count];
|
|
|
|
id k[c];
|
|
|
|
|
|
|
|
for (i = 0; i < c; i++)
|
|
|
|
{
|
|
|
|
k[i] = [e nextObject];
|
1998-10-27 08:12:49 +00:00
|
|
|
NSAssert (k[i], NSInternalInconsistencyException);
|
1995-05-05 18:31:51 +00:00
|
|
|
}
|
1998-10-27 08:12:49 +00:00
|
|
|
NSAssert (![e nextObject], NSInternalInconsistencyException);
|
1999-04-08 12:17:15 +00:00
|
|
|
return [[[NSArray alloc] initWithObjects: k count: c]
|
1995-05-05 18:31:51 +00:00
|
|
|
autorelease];
|
1995-04-03 22:59:20 +00:00
|
|
|
}
|
|
|
|
|
1995-05-05 18:31:51 +00:00
|
|
|
- (NSArray*) allValues
|
1995-04-03 22:59:20 +00:00
|
|
|
{
|
1995-05-05 18:31:51 +00:00
|
|
|
id e = [self objectEnumerator];
|
|
|
|
int i, c = [self count];
|
|
|
|
id k[c];
|
|
|
|
|
|
|
|
for (i = 0; i < c; i++)
|
|
|
|
{
|
|
|
|
k[i] = [e nextObject];
|
1998-10-27 08:12:49 +00:00
|
|
|
NSAssert (k[i], NSInternalInconsistencyException);
|
1995-05-05 18:31:51 +00:00
|
|
|
}
|
1998-10-27 08:12:49 +00:00
|
|
|
NSAssert (![e nextObject], NSInternalInconsistencyException);
|
1999-04-08 12:17:15 +00:00
|
|
|
return [[[NSArray alloc] initWithObjects: k count: c]
|
1995-05-05 18:31:51 +00:00
|
|
|
autorelease];
|
1995-04-03 22:59:20 +00:00
|
|
|
}
|
|
|
|
|
1995-05-05 18:31:51 +00:00
|
|
|
- (NSArray*) allKeysForObject: anObject
|
1995-04-03 22:59:20 +00:00
|
|
|
{
|
1995-05-05 18:31:51 +00:00
|
|
|
id k, e = [self keyEnumerator];
|
|
|
|
id a[[self count]];
|
|
|
|
int c = 0;
|
|
|
|
|
|
|
|
while ((k = [e nextObject]))
|
1996-04-08 17:19:16 +00:00
|
|
|
if ([anObject isEqual: [self objectForKey: k]])
|
1995-05-05 18:31:51 +00:00
|
|
|
a[c++] = k;
|
1998-10-27 08:12:49 +00:00
|
|
|
if (c == 0)
|
|
|
|
return nil;
|
1996-04-08 17:19:16 +00:00
|
|
|
return [[[NSArray alloc] initWithObjects: a count: c]
|
1995-05-05 18:31:51 +00:00
|
|
|
autorelease];
|
1995-04-03 22:59:20 +00:00
|
|
|
}
|
|
|
|
|
1998-10-27 09:35:06 +00:00
|
|
|
struct foo { NSDictionary *d; SEL s; IMP i; };
|
1998-01-19 15:20:15 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
compareIt(id o1, id o2, void* context)
|
|
|
|
{
|
|
|
|
struct foo *f = (struct foo*)context;
|
1998-10-27 09:35:06 +00:00
|
|
|
o1 = (*f->i)(f->d, @selector(objectForKey:), o1);
|
|
|
|
o2 = (*f->i)(f->d, @selector(objectForKey:), o2);
|
1998-01-19 15:20:15 +00:00
|
|
|
return (int)[o1 performSelector: f->s withObject: o2];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray*)keysSortedByValueUsingSelector: (SEL)comp
|
|
|
|
{
|
|
|
|
struct foo info;
|
|
|
|
id k;
|
|
|
|
|
|
|
|
info.d = self;
|
|
|
|
info.s = comp;
|
1998-10-27 09:35:06 +00:00
|
|
|
info.i = [self methodForSelector: @selector(objectForKey:)];
|
1998-01-19 15:20:15 +00:00
|
|
|
k = [[self allKeys] sortedArrayUsingFunction: compareIt context: &info];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray*) objectsForKeys: (NSArray*)keys notFoundMarker: (id)marker
|
|
|
|
{
|
|
|
|
int i, c = [keys count];
|
|
|
|
id obuf[c];
|
|
|
|
|
|
|
|
for (i = 0; i < c; i++)
|
|
|
|
{
|
|
|
|
id o = [self objectForKey: [keys objectAtIndex: i]];
|
|
|
|
|
|
|
|
if (o)
|
|
|
|
obuf[i] = o;
|
|
|
|
else
|
|
|
|
obuf[i] = marker;
|
|
|
|
}
|
|
|
|
return [NSArray arrayWithObjects: obuf count: c];
|
|
|
|
}
|
|
|
|
|
1999-04-08 12:17:15 +00:00
|
|
|
- (BOOL)writeToFile: (NSString *)path atomically: (BOOL)useAuxiliaryFile
|
1995-04-03 22:59:20 +00:00
|
|
|
{
|
1999-04-08 12:17:15 +00:00
|
|
|
return [[self description] writeToFile: path atomically: useAuxiliaryFile];
|
1995-04-03 22:59:20 +00:00
|
|
|
}
|
|
|
|
|
1998-01-19 15:20:15 +00:00
|
|
|
- (NSString*) description
|
|
|
|
{
|
|
|
|
return [self descriptionWithLocale: nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) descriptionInStringsFileFormat
|
|
|
|
{
|
1998-12-18 17:05:44 +00:00
|
|
|
NSMutableString *result = [NSMutableString stringWithCapacity: 1024];
|
|
|
|
NSEnumerator *enumerator;
|
|
|
|
id key;
|
1998-01-19 15:20:15 +00:00
|
|
|
|
1998-12-18 17:05:44 +00:00
|
|
|
enumerator = [self keyEnumerator];
|
|
|
|
while ((key = [enumerator nextObject]) != nil)
|
|
|
|
{
|
|
|
|
id val = [self objectForKey: key];
|
|
|
|
|
|
|
|
[key descriptionWithLocale: nil
|
|
|
|
to: (id<GNUDescriptionDestination>)result];
|
|
|
|
if (val != nil && [val isEqualToString: @""] == NO)
|
|
|
|
{
|
|
|
|
[result appendString: @" = "];
|
|
|
|
[val descriptionWithLocale: nil
|
|
|
|
to: (id<GNUDescriptionDestination>)result];
|
|
|
|
}
|
|
|
|
[result appendString: @";\n"];
|
1998-01-19 15:20:15 +00:00
|
|
|
}
|
|
|
|
|
1998-12-18 17:05:44 +00:00
|
|
|
return result;
|
1998-01-19 15:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) descriptionWithLocale: (NSDictionary*)locale
|
|
|
|
{
|
1998-12-18 17:05:44 +00:00
|
|
|
return [self descriptionWithLocale: locale indent: 0];
|
1998-01-19 15:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) descriptionWithLocale: (NSDictionary*)locale
|
|
|
|
indent: (unsigned int)level
|
|
|
|
{
|
1998-12-18 17:05:44 +00:00
|
|
|
NSMutableString *result;
|
|
|
|
|
|
|
|
result = [NSMutableString stringWithCapacity: 20*[self count]];
|
|
|
|
[self descriptionWithLocale: locale
|
|
|
|
indent: level
|
|
|
|
to: (id<GNUDescriptionDestination>)result];
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NSString *indentStrings[] = {
|
|
|
|
@"",
|
|
|
|
@" ",
|
|
|
|
@"\t",
|
|
|
|
@"\t ",
|
|
|
|
@"\t\t",
|
|
|
|
@"\t\t ",
|
|
|
|
@"\t\t\t",
|
|
|
|
@"\t\t\t ",
|
|
|
|
@"\t\t\t\t",
|
|
|
|
@"\t\t\t\t ",
|
|
|
|
@"\t\t\t\t\t",
|
|
|
|
@"\t\t\t\t\t ",
|
|
|
|
@"\t\t\t\t\t\t"
|
|
|
|
};
|
|
|
|
|
|
|
|
- (void) descriptionWithLocale: (NSDictionary*)locale
|
|
|
|
indent: (unsigned int)level
|
|
|
|
to: (id<GNUDescriptionDestination>)result
|
|
|
|
{
|
|
|
|
NSEnumerator *enumerator;
|
|
|
|
id key;
|
|
|
|
BOOL canCompare = YES;
|
|
|
|
NSString *iBaseString;
|
|
|
|
NSString *iSizeString;
|
|
|
|
int i;
|
|
|
|
NSArray *keyArray = [self allKeys];
|
|
|
|
NSMutableArray *theKeys = [NSMutableArray arrayWithArray: keyArray];
|
|
|
|
int numKeys = [theKeys count];
|
|
|
|
NSString *plists[numKeys];
|
|
|
|
NSString *keys[numKeys];
|
|
|
|
SEL appSel;
|
|
|
|
IMP appImp;
|
|
|
|
|
|
|
|
appSel = @selector(appendString:);
|
|
|
|
appImp = [(NSObject*)result methodForSelector: appSel];
|
|
|
|
|
|
|
|
if (level < sizeof(indentStrings)/sizeof(NSString*))
|
|
|
|
iBaseString = indentStrings[level];
|
|
|
|
else
|
|
|
|
iBaseString = indentStrings[sizeof(indentStrings)/sizeof(NSString*)-1];
|
|
|
|
level++;
|
|
|
|
if (level < sizeof(indentStrings)/sizeof(NSString*))
|
|
|
|
iSizeString = indentStrings[level];
|
|
|
|
else
|
|
|
|
iSizeString = indentStrings[sizeof(indentStrings)/sizeof(NSString*)-1];
|
1998-01-19 15:20:15 +00:00
|
|
|
|
1998-12-18 17:05:44 +00:00
|
|
|
enumerator = [self keyEnumerator];
|
|
|
|
while ((key = [enumerator nextObject]) != nil)
|
|
|
|
{
|
|
|
|
if ([key respondsToSelector: @selector(compare:)] == NO)
|
|
|
|
{
|
|
|
|
canCompare = NO;
|
|
|
|
break;
|
|
|
|
}
|
1998-01-19 15:20:15 +00:00
|
|
|
}
|
|
|
|
|
1998-12-18 17:05:44 +00:00
|
|
|
if (canCompare)
|
|
|
|
{
|
|
|
|
[theKeys sortUsingSelector: @selector(compare:)];
|
1998-01-19 15:20:15 +00:00
|
|
|
}
|
|
|
|
|
1998-12-18 17:05:44 +00:00
|
|
|
[theKeys getObjects: keys];
|
|
|
|
for (i = 0; i < numKeys; i++)
|
|
|
|
{
|
|
|
|
plists[i] = [self objectForKey: keys[i]];
|
1998-01-19 15:20:15 +00:00
|
|
|
}
|
|
|
|
|
1998-12-18 17:05:44 +00:00
|
|
|
(*appImp)(result, appSel, @"{\n");
|
|
|
|
for (i = 0; i < numKeys; i++)
|
|
|
|
{
|
|
|
|
id item = plists[i];
|
1998-01-19 15:20:15 +00:00
|
|
|
|
1998-12-18 17:05:44 +00:00
|
|
|
(*appImp)(result, appSel, iSizeString);
|
1998-01-19 15:20:15 +00:00
|
|
|
|
1998-12-18 17:05:44 +00:00
|
|
|
[keys[i] descriptionTo: result];
|
1998-01-19 15:20:15 +00:00
|
|
|
|
1998-12-18 17:05:44 +00:00
|
|
|
(*appImp)(result, appSel, @" = ");
|
1998-01-19 15:20:15 +00:00
|
|
|
|
1999-04-08 12:17:15 +00:00
|
|
|
if ([item respondsToSelector: @selector(descriptionWithLocale:indent:)])
|
1998-12-18 17:05:44 +00:00
|
|
|
{
|
|
|
|
[item descriptionWithLocale: locale indent: level to: result];
|
1998-01-19 15:20:15 +00:00
|
|
|
}
|
1999-04-08 12:17:15 +00:00
|
|
|
else if ([item respondsToSelector: @selector(descriptionWithLocale:)])
|
1998-12-18 17:05:44 +00:00
|
|
|
{
|
|
|
|
[item descriptionWithLocale: locale to: result];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[item descriptionTo: result];
|
1998-01-19 15:20:15 +00:00
|
|
|
}
|
|
|
|
|
1998-12-18 17:05:44 +00:00
|
|
|
(*appImp)(result, appSel, @";\n");
|
|
|
|
}
|
|
|
|
(*appImp)(result, appSel, iBaseString);
|
|
|
|
(*appImp)(result, appSel, @"}");
|
1998-01-19 15:20:15 +00:00
|
|
|
}
|
|
|
|
|
1995-04-03 22:59:20 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSMutableDictionary
|
|
|
|
|
1997-10-16 23:56:27 +00:00
|
|
|
+ (void)initialize
|
1995-04-03 22:59:20 +00:00
|
|
|
{
|
1997-10-16 23:56:27 +00:00
|
|
|
if (self == [NSMutableDictionary class])
|
|
|
|
{
|
|
|
|
behavior_class_add_class (self, [NSMutableDictionaryNonCore class]);
|
|
|
|
behavior_class_add_class (self, [NSDictionaryNonCore class]);
|
|
|
|
}
|
1995-04-03 22:59:20 +00:00
|
|
|
}
|
|
|
|
|
1997-10-16 23:56:27 +00:00
|
|
|
+ allocWithZone: (NSZone*)z
|
1995-04-03 22:59:20 +00:00
|
|
|
{
|
1999-01-11 17:28:51 +00:00
|
|
|
if ([self class] == [NSMutableDictionary class])
|
|
|
|
return NSAllocateObject([self _mutableConcreteClass], 0, z);
|
|
|
|
return [super allocWithZone: z];
|
1995-04-03 22:59:20 +00:00
|
|
|
}
|
|
|
|
|
1999-04-08 12:17:15 +00:00
|
|
|
- copyWithZone: (NSZone*)z
|
|
|
|
{
|
|
|
|
/* a deep copy */
|
|
|
|
unsigned count = [self count];
|
|
|
|
id keys[count];
|
|
|
|
id objects[count];
|
|
|
|
NSDictionary *newDictionary;
|
|
|
|
unsigned i;
|
|
|
|
id key;
|
|
|
|
NSEnumerator *enumerator = [self keyEnumerator];
|
|
|
|
static SEL nxtSel = @selector(nextObject);
|
|
|
|
IMP nxtImp = [enumerator methodForSelector: nxtSel];
|
|
|
|
static SEL objSel = @selector(objectForKey:);
|
|
|
|
IMP objImp = [self methodForSelector: objSel];
|
|
|
|
|
|
|
|
for (i = 0; (key = (*nxtImp)(enumerator, nxtSel)); i++)
|
|
|
|
{
|
|
|
|
keys[i] = key;
|
|
|
|
objects[i] = (*objImp)(self, objSel, key);
|
|
|
|
objects[i] = [objects[i] copyWithZone: z];
|
|
|
|
}
|
|
|
|
newDictionary = [[[[self class] _concreteClass] allocWithZone: z]
|
|
|
|
initWithObjects: objects
|
|
|
|
forKeys: keys
|
|
|
|
count: count];
|
|
|
|
while (i > 0)
|
|
|
|
{
|
|
|
|
[objects[--i] release];
|
|
|
|
}
|
|
|
|
return newDictionary;
|
|
|
|
}
|
|
|
|
|
1995-05-05 18:31:51 +00:00
|
|
|
/* This is the designated initializer */
|
|
|
|
- initWithCapacity: (unsigned)numItems
|
1995-04-03 22:59:20 +00:00
|
|
|
{
|
1999-04-08 12:17:15 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
1995-04-04 16:15:31 +00:00
|
|
|
return 0;
|
1995-04-03 22:59:20 +00:00
|
|
|
}
|
|
|
|
|
1999-04-08 12:17:15 +00:00
|
|
|
- (void) setObject: anObject forKey: (id)aKey
|
1997-10-16 23:56:27 +00:00
|
|
|
{
|
1999-04-08 12:17:15 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
1997-10-16 23:56:27 +00:00
|
|
|
}
|
|
|
|
|
1999-04-08 12:17:15 +00:00
|
|
|
- (void) removeObjectForKey: (id)aKey
|
1997-10-16 23:56:27 +00:00
|
|
|
{
|
1999-04-08 12:17:15 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
1997-10-16 23:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSMutableDictionaryNonCore
|
|
|
|
|
|
|
|
+ dictionaryWithCapacity: (unsigned)numItems
|
|
|
|
{
|
1999-04-08 12:17:15 +00:00
|
|
|
return [[[self alloc] initWithCapacity: numItems]
|
1997-10-16 23:56:27 +00:00
|
|
|
autorelease];
|
|
|
|
}
|
|
|
|
|
1995-05-05 18:31:51 +00:00
|
|
|
/* Override superclass's designated initializer */
|
|
|
|
- initWithObjects: (id*)objects
|
1998-10-27 08:12:49 +00:00
|
|
|
forKeys: (id*)keys
|
1995-05-05 18:31:51 +00:00
|
|
|
count: (unsigned)count
|
|
|
|
{
|
1999-04-08 12:17:15 +00:00
|
|
|
[self initWithCapacity: count];
|
1995-05-05 18:31:51 +00:00
|
|
|
while (count--)
|
1999-04-08 12:17:15 +00:00
|
|
|
[self setObject: objects[count] forKey: keys[count]];
|
1995-05-05 18:31:51 +00:00
|
|
|
return self;
|
|
|
|
}
|
1995-04-03 22:59:20 +00:00
|
|
|
|
1995-05-05 18:31:51 +00:00
|
|
|
- (void) removeAllObjects
|
1995-04-03 22:59:20 +00:00
|
|
|
{
|
1995-05-05 18:31:51 +00:00
|
|
|
id k, e = [self keyEnumerator];
|
|
|
|
while ((k = [e nextObject]))
|
1999-04-08 12:17:15 +00:00
|
|
|
[self removeObjectForKey: k];
|
1995-04-03 22:59:20 +00:00
|
|
|
}
|
|
|
|
|
1995-05-05 18:31:51 +00:00
|
|
|
- (void) removeObjectsForKeys: (NSArray*)keyArray
|
1995-04-03 22:59:20 +00:00
|
|
|
{
|
1995-05-05 18:31:51 +00:00
|
|
|
int c = [keyArray count];
|
|
|
|
while (c--)
|
1999-04-08 12:17:15 +00:00
|
|
|
[self removeObjectForKey: [keyArray objectAtIndex: c]];
|
1995-04-03 22:59:20 +00:00
|
|
|
}
|
|
|
|
|
1995-05-05 18:31:51 +00:00
|
|
|
- (void) addEntriesFromDictionary: (NSDictionary*)other
|
1995-04-03 22:59:20 +00:00
|
|
|
{
|
1995-05-05 18:31:51 +00:00
|
|
|
id k, e = [other keyEnumerator];
|
|
|
|
while ((k = [e nextObject]))
|
1999-04-08 12:17:15 +00:00
|
|
|
[self setObject: [other objectForKey: k] forKey: k];
|
1995-04-03 22:59:20 +00:00
|
|
|
}
|
|
|
|
|
1998-01-19 15:20:15 +00:00
|
|
|
- (void) setDictionary: (NSDictionary*)otherDictionary
|
|
|
|
{
|
|
|
|
[self removeAllObjects];
|
|
|
|
[self addEntriesFromDictionary: otherDictionary];
|
|
|
|
}
|
1995-04-03 22:59:20 +00:00
|
|
|
|
|
|
|
@end
|