2005-07-01 21:00:04 +00:00
|
|
|
|
/* Test/example program for the base library
|
|
|
|
|
|
|
|
|
|
Copyright (C) 2005 Free Software Foundation, Inc.
|
|
|
|
|
|
2005-07-15 22:51:23 +00:00
|
|
|
|
Copying and distribution of this file, with or without modification,
|
|
|
|
|
are permitted in any medium without royalty provided the copyright
|
|
|
|
|
notice and this notice are preserved.
|
|
|
|
|
|
2005-07-01 21:00:04 +00:00
|
|
|
|
This file is part of the GNUstep Base Library.
|
|
|
|
|
*/
|
1996-01-26 02:57:43 +00:00
|
|
|
|
/* A demonstration of writing and reading with NSArchiver */
|
|
|
|
|
|
1996-02-22 15:04:01 +00:00
|
|
|
|
#if 1
|
1996-02-13 01:57:17 +00:00
|
|
|
|
|
1996-01-26 02:57:43 +00:00
|
|
|
|
#include <Foundation/NSArchiver.h>
|
|
|
|
|
#include <Foundation/NSString.h>
|
|
|
|
|
#include <Foundation/NSAutoreleasePool.h>
|
|
|
|
|
#include <Foundation/NSSet.h>
|
|
|
|
|
#include <Foundation/NSUtilities.h>
|
2001-01-12 16:48:33 +00:00
|
|
|
|
#include <Foundation/NSValue.h>
|
1998-10-28 13:58:39 +00:00
|
|
|
|
#include <Foundation/NSDate.h>
|
1996-01-26 02:57:43 +00:00
|
|
|
|
|
2004-06-02 05:29:13 +00:00
|
|
|
|
@interface ClassOne : NSObject
|
|
|
|
|
@end
|
|
|
|
|
@implementation ClassOne
|
|
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
- (id) initWithCoder: (NSCoder*)aCoder
|
|
|
|
|
{
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
@interface ClassTwo : NSObject
|
|
|
|
|
@end
|
|
|
|
|
@implementation ClassTwo
|
|
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
- (id) initWithCoder: (NSCoder*)aCoder
|
|
|
|
|
{
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
2003-04-10 16:28:07 +00:00
|
|
|
|
typedef struct { char a; double b; char c;} tstruct;
|
1996-01-26 02:57:43 +00:00
|
|
|
|
int main()
|
|
|
|
|
{
|
2004-06-02 05:29:13 +00:00
|
|
|
|
id obj;
|
1996-01-26 02:57:43 +00:00
|
|
|
|
id set;
|
|
|
|
|
id arp;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
id arc;
|
|
|
|
|
id una;
|
|
|
|
|
id xxx;
|
|
|
|
|
id apl;
|
2003-04-10 16:28:07 +00:00
|
|
|
|
tstruct ss;
|
|
|
|
|
tstruct tt;
|
2005-02-22 11:22:44 +00:00
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
|
[NSAutoreleasePool enableDoubleReleaseCheck:YES];
|
2005-02-22 11:22:44 +00:00
|
|
|
|
|
1996-01-26 02:57:43 +00:00
|
|
|
|
arp = [[NSAutoreleasePool alloc] init];
|
|
|
|
|
|
|
|
|
|
/* Create a Set of int's */
|
|
|
|
|
set = [[NSSet alloc] initWithObjects:
|
2001-01-12 16:48:33 +00:00
|
|
|
|
@"apple", @"banana", @"carrot", @"dal", @"escarole", @"fava",
|
|
|
|
|
[NSValue valueWithPoint: NSMakePoint(1,1)],
|
|
|
|
|
[NSValue valueWithSize: NSMakeSize(11,11)],
|
|
|
|
|
[NSValue valueWithRange: NSMakeRange(10,2)],
|
|
|
|
|
[NSValue valueWithRect: NSMakeRect(11,11,22,22)],
|
|
|
|
|
nil];
|
1996-01-26 02:57:43 +00:00
|
|
|
|
|
|
|
|
|
/* Display the set */
|
|
|
|
|
printf("Writing:\n");
|
|
|
|
|
{
|
|
|
|
|
id o, e = [set objectEnumerator];
|
|
|
|
|
while ((o = [e nextObject]))
|
2005-02-22 11:22:44 +00:00
|
|
|
|
printf("%s\n", [[o description] cString]);
|
1996-01-26 02:57:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-04-10 16:28:07 +00:00
|
|
|
|
apl = [[NSAutoreleasePool alloc] init];
|
|
|
|
|
ss.a = 'A';
|
|
|
|
|
ss.b = 1.234;
|
|
|
|
|
ss.c = 'Z';
|
|
|
|
|
arc = [[NSArchiver new] autorelease];
|
|
|
|
|
[arc encodeValueOfObjCType: @encode(tstruct) at: &ss];
|
|
|
|
|
una = [[[NSUnarchiver alloc] initForReadingWithData: [arc archiverData]] autorelease];
|
|
|
|
|
[una decodeValueOfObjCType: @encode(tstruct) at: &tt];
|
|
|
|
|
[apl release];
|
|
|
|
|
if (ss.a != tt.a) printf("Encoded '%c' in 'a' but got '%c'\n", ss.a, tt.a);
|
|
|
|
|
if (ss.b != tt.b) printf("Encoded '%f' in 'a' but got '%f'\n", ss.b, tt.b);
|
|
|
|
|
if (ss.c != tt.c) printf("Encoded '%c' in 'a' but got '%c'\n", ss.c, tt.c);
|
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
|
apl = [[NSAutoreleasePool alloc] init];
|
|
|
|
|
arc = [[NSArchiver new] autorelease];
|
|
|
|
|
printf("%u\n", [arc retainCount]);
|
|
|
|
|
[arc retain];
|
|
|
|
|
printf("%u\n", [arc retainCount]);
|
|
|
|
|
[arc release];
|
|
|
|
|
printf("%u\n", [arc retainCount]);
|
|
|
|
|
[arc encodeRootObject: set];
|
1998-10-03 04:49:01 +00:00
|
|
|
|
una = [[[NSUnarchiver alloc] initForReadingWithData: [arc archiverData]] autorelease];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
xxx = [una decodeObject];
|
1998-10-28 13:58:39 +00:00
|
|
|
|
if ([xxx isEqual: set] == NO)
|
|
|
|
|
printf("Argh\n");
|
|
|
|
|
printf("%s\n", [[xxx description] cString]);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
[apl release];
|
|
|
|
|
|
1998-10-28 13:58:39 +00:00
|
|
|
|
|
1996-01-26 02:57:43 +00:00
|
|
|
|
/* Write it to a file */
|
|
|
|
|
[NSArchiver archiveRootObject: set toFile: @"./nsarchiver.dat"];
|
|
|
|
|
|
|
|
|
|
/* Release the object that was coded */
|
|
|
|
|
[set release];
|
|
|
|
|
|
|
|
|
|
/* Read it back in from the file */
|
1996-02-13 01:57:17 +00:00
|
|
|
|
#if 1
|
|
|
|
|
{
|
|
|
|
|
id d = [[NSData alloc] initWithContentsOfFile:@"./nsarchiver.dat"];
|
|
|
|
|
id a = [NSUnarchiver alloc];
|
|
|
|
|
a = [a initForReadingWithData:d];
|
|
|
|
|
set = [a decodeObject];
|
|
|
|
|
}
|
|
|
|
|
#else
|
1996-01-26 19:49:12 +00:00
|
|
|
|
set = [NSUnarchiver unarchiveObjectWithFile: @"./nsarchiver.dat"];
|
1996-02-13 01:57:17 +00:00
|
|
|
|
#endif
|
1996-01-26 02:57:43 +00:00
|
|
|
|
|
|
|
|
|
/* Display what we read, to make sure it matches what we wrote */
|
|
|
|
|
printf("\nReading:\n");
|
|
|
|
|
{
|
|
|
|
|
id o, e = [set objectEnumerator];
|
1996-04-16 22:05:21 +00:00
|
|
|
|
while ((o = [e nextObject]))
|
2005-02-22 11:22:44 +00:00
|
|
|
|
printf("%s\n", [[o description] cString]);
|
1996-01-26 02:57:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-06-02 05:29:13 +00:00
|
|
|
|
obj = [ClassOne new];
|
|
|
|
|
[NSArchiver archiveRootObject: obj toFile: @"./nsarchiver.dat"];
|
|
|
|
|
RELEASE(obj);
|
|
|
|
|
[NSUnarchiver decodeClassName: @"ClassOne" asClassName: @"ClassTwo"];
|
|
|
|
|
obj = [NSUnarchiver unarchiveObjectWithFile: @"./nsarchiver.dat"];
|
|
|
|
|
if ([obj isKindOfClass: [ClassTwo class]] == NO)
|
|
|
|
|
NSLog(@"ERROR: ClassOne decoded as %@", NSStringFromClass([obj class]));
|
|
|
|
|
|
1998-10-28 15:14:03 +00:00
|
|
|
|
#if 0
|
|
|
|
|
/*
|
|
|
|
|
* Benchmark use of very lightwight archiving - a single
|
|
|
|
|
* archiver/unarchiver pair using a single mutable data object to
|
|
|
|
|
* archive and unarchive many times.
|
|
|
|
|
*/
|
1998-10-28 13:58:39 +00:00
|
|
|
|
{
|
|
|
|
|
NSDate *start = [NSDate date];
|
|
|
|
|
NSAutoreleasePool *arp = [NSAutoreleasePool new];
|
|
|
|
|
int i;
|
|
|
|
|
NSUnarchiver *u = nil;
|
1998-10-28 15:14:03 +00:00
|
|
|
|
NSMutableData *d;
|
|
|
|
|
NSArchiver *a;
|
|
|
|
|
|
|
|
|
|
d = [NSMutableData data];
|
|
|
|
|
a = [[NSArchiver alloc] initForWritingWithMutableData: d];
|
1998-10-28 13:58:39 +00:00
|
|
|
|
|
|
|
|
|
[NSAutoreleasePool enableDoubleReleaseCheck:NO];
|
1998-10-28 15:14:03 +00:00
|
|
|
|
for (i = 0; i < 10000; i++)
|
|
|
|
|
{
|
1998-10-28 13:58:39 +00:00
|
|
|
|
id o;
|
|
|
|
|
|
|
|
|
|
[a encodeRootObject: set];
|
1998-10-28 15:14:03 +00:00
|
|
|
|
if (u == nil)
|
|
|
|
|
{
|
1998-10-28 13:58:39 +00:00
|
|
|
|
u = [[NSUnarchiver alloc] initForReadingWithData: d];
|
1998-10-28 15:14:03 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
1998-10-28 13:58:39 +00:00
|
|
|
|
[u resetUnarchiverWithData: d atIndex: 0];
|
1998-10-28 15:14:03 +00:00
|
|
|
|
}
|
1998-10-28 13:58:39 +00:00
|
|
|
|
o = [u decodeObject];
|
|
|
|
|
[d setLength: 0];
|
|
|
|
|
[a resetArchiver];
|
1998-10-28 15:14:03 +00:00
|
|
|
|
}
|
1998-10-28 13:58:39 +00:00
|
|
|
|
[a release];
|
|
|
|
|
[u release];
|
|
|
|
|
[arp release];
|
|
|
|
|
printf("Time: %f\n", -[start timeIntervalSinceNow]);
|
|
|
|
|
}
|
1998-10-28 15:14:03 +00:00
|
|
|
|
#endif
|
1998-10-28 13:58:39 +00:00
|
|
|
|
|
1996-01-26 02:57:43 +00:00
|
|
|
|
/* Do the autorelease. */
|
|
|
|
|
[arp release];
|
2005-02-22 11:22:44 +00:00
|
|
|
|
|
1996-01-26 02:57:43 +00:00
|
|
|
|
exit(0);
|
|
|
|
|
}
|
1996-02-13 01:57:17 +00:00
|
|
|
|
|
1996-05-07 01:20:44 +00:00
|
|
|
|
|
|
|
|
|
/* An old, unused test. */
|
1996-02-13 01:57:17 +00:00
|
|
|
|
#else
|
|
|
|
|
|
2003-07-31 23:49:32 +00:00
|
|
|
|
#include <GNUstepBase/all.h>
|
1996-02-13 01:57:17 +00:00
|
|
|
|
#include <Foundation/NSArchiver.h>
|
|
|
|
|
#include <Foundation/NSAutoreleasePool.h>
|
|
|
|
|
|
|
|
|
|
@interface TestClass : NSObject
|
|
|
|
|
{
|
|
|
|
|
id next_responder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)setNextResponder: anObject;
|
|
|
|
|
- nextResponder;
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation TestClass
|
|
|
|
|
|
|
|
|
|
- (void)setNextResponder: anObject
|
|
|
|
|
{
|
|
|
|
|
next_responder = anObject;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- nextResponder
|
|
|
|
|
{
|
|
|
|
|
return next_responder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NSCoding protocol
|
|
|
|
|
- (void)encodeWithCoder:aCoder
|
|
|
|
|
{
|
|
|
|
|
[super encodeWithCoder:aCoder];
|
|
|
|
|
[aCoder encodeObjectReference:next_responder withName:@"Next Responder"];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- initWithCoder:aDecoder
|
|
|
|
|
{
|
|
|
|
|
id d;
|
|
|
|
|
[super initWithCoder:aDecoder];
|
|
|
|
|
[aDecoder decodeObjectAt:&next_responder withName:&d];
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
id arp;
|
|
|
|
|
id r1, r2;
|
|
|
|
|
|
|
|
|
|
arp = [[NSAutoreleasePool alloc] init];
|
|
|
|
|
|
|
|
|
|
// Create a simple loop
|
|
|
|
|
r1 = [[TestClass alloc] init];
|
|
|
|
|
r2 = [[TestClass alloc] init];
|
|
|
|
|
[r1 setNextResponder: r2];
|
|
|
|
|
[r2 setNextResponder: r1];
|
|
|
|
|
|
|
|
|
|
printf("Writing\n");
|
|
|
|
|
printf("%d\n", [r1 hash]);
|
|
|
|
|
printf("%d\n", [r2 hash]);
|
|
|
|
|
printf("%d\n", [[r1 nextResponder] hash]);
|
|
|
|
|
printf("%d\n", [[r2 nextResponder] hash]);
|
|
|
|
|
|
|
|
|
|
/* Write it to a file */
|
|
|
|
|
{
|
|
|
|
|
id d = [[NSMutableData alloc] init];
|
1996-02-22 15:04:01 +00:00
|
|
|
|
id a = [[Archiver alloc] initForWritingWithMutableData: d];
|
1996-02-13 01:57:17 +00:00
|
|
|
|
|
|
|
|
|
[a startEncodingInterconnectedObjects];
|
|
|
|
|
[a encodeObject: r1 withName:@"one"];
|
|
|
|
|
[a encodeObject: r2 withName:@"another"];
|
|
|
|
|
[a finishEncodingInterconnectedObjects];
|
|
|
|
|
|
|
|
|
|
[d writeToFile: @"./nsarchiver.dat" atomically:NO];
|
|
|
|
|
|
|
|
|
|
[d release];
|
|
|
|
|
[a release];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Release the object that was coded */
|
|
|
|
|
[r1 release];
|
|
|
|
|
[r2 release];
|
|
|
|
|
|
|
|
|
|
/* Read it back in from the file */
|
|
|
|
|
printf("\nReading:\n");
|
|
|
|
|
{
|
|
|
|
|
id d;
|
1996-02-22 15:04:01 +00:00
|
|
|
|
id a = [Unarchiver newReadingFromFile:@"./nsarchiver.dat"];
|
1996-02-13 01:57:17 +00:00
|
|
|
|
|
|
|
|
|
[a startDecodingInterconnectedObjects];
|
|
|
|
|
[a decodeObjectAt: &r1 withName:&d];
|
|
|
|
|
[a decodeObjectAt: &r2 withName:&d];
|
|
|
|
|
[a finishDecodingInterconnectedObjects];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Display what we read, to make sure it matches what we wrote */
|
|
|
|
|
{
|
|
|
|
|
printf("%d\n", [r1 hash]);
|
|
|
|
|
printf("%d\n", [r2 hash]);
|
|
|
|
|
printf("%d\n", [[r1 nextResponder] hash]);
|
|
|
|
|
printf("%d\n", [[r2 nextResponder] hash]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Do the autorelease. */
|
|
|
|
|
[arp release];
|
|
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|