1995-04-09 02:42:37 +00:00
|
|
|
|
/* archiving class for serialization and persistance.
|
1997-05-03 20:10:45 +00:00
|
|
|
|
Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
|
1995-04-09 02:42:37 +00:00
|
|
|
|
|
1996-04-17 20:17:45 +00:00
|
|
|
|
Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
|
1996-01-26 03:15:14 +00:00
|
|
|
|
Created: March 1995
|
1995-04-09 02:42:37 +00:00
|
|
|
|
|
1996-05-12 00:56:10 +00:00
|
|
|
|
This file is part of the GNUstep Base Library.
|
1995-04-09 02:42:37 +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.
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
|
|
|
|
|
1997-11-06 00:51:23 +00:00
|
|
|
|
#include <config.h>
|
1996-04-17 15:34:35 +00:00
|
|
|
|
#include <gnustep/base/preface.h>
|
1995-04-17 21:13:20 +00:00
|
|
|
|
#include <Foundation/NSArchiver.h>
|
|
|
|
|
#include <Foundation/NSGArchiver.h>
|
1995-08-02 15:40:18 +00:00
|
|
|
|
#include <Foundation/NSData.h>
|
1996-04-17 15:23:00 +00:00
|
|
|
|
#include <gnustep/base/Coder.h>
|
1995-04-09 02:42:37 +00:00
|
|
|
|
|
|
|
|
|
@implementation NSArchiver
|
|
|
|
|
|
1995-07-03 22:20:40 +00:00
|
|
|
|
static Class NSArchiver_concrete_class;
|
|
|
|
|
|
|
|
|
|
+ (void) _setConcreteClass: (Class)c
|
|
|
|
|
{
|
|
|
|
|
NSArchiver_concrete_class = c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (Class) _concreteClass
|
|
|
|
|
{
|
|
|
|
|
return NSArchiver_concrete_class;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
|
{
|
1996-03-18 13:54:00 +00:00
|
|
|
|
if (self == [NSArchiver class])
|
1996-01-26 19:53:15 +00:00
|
|
|
|
NSArchiver_concrete_class = [NSGArchiver class];
|
1995-07-03 22:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-01-26 03:15:14 +00:00
|
|
|
|
|
|
|
|
|
/* Allocating and Initializing an archiver */
|
|
|
|
|
|
|
|
|
|
+ allocWithZone:(NSZone *)zone
|
|
|
|
|
{
|
|
|
|
|
return NSAllocateObject([self _concreteClass], 0, zone);
|
|
|
|
|
}
|
1995-04-15 19:38:52 +00:00
|
|
|
|
|
|
|
|
|
/* This is the designated initializer */
|
|
|
|
|
- (id) initForWritingWithMutableData: (NSMutableData*)mdata
|
|
|
|
|
{
|
1996-01-26 03:15:14 +00:00
|
|
|
|
[self subclassResponsibility:_cmd];
|
|
|
|
|
return self;
|
1995-04-15 19:38:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
1997-05-03 20:16:26 +00:00
|
|
|
|
|
|
|
|
|
|
1996-01-26 03:15:14 +00:00
|
|
|
|
|
|
|
|
|
/* Archiving Data */
|
1995-04-15 19:38:52 +00:00
|
|
|
|
|
1997-05-03 20:16:26 +00:00
|
|
|
|
- init
|
|
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
|
[self subclassResponsibility:_cmd];
|
|
|
|
|
return self;
|
1997-05-03 20:16:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
1995-04-15 19:38:52 +00:00
|
|
|
|
+ (NSData*) archivedDataWithRootObject: (id)rootObject
|
|
|
|
|
{
|
1995-07-03 22:20:40 +00:00
|
|
|
|
id d = [[NSMutableData alloc] init];
|
|
|
|
|
id a = [[NSArchiver alloc] initForWritingWithMutableData:d];
|
1995-04-15 19:38:52 +00:00
|
|
|
|
[a encodeRootObject:rootObject];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
[a release]; /* Done with archiver. */
|
1995-04-15 19:38:52 +00:00
|
|
|
|
return [d autorelease];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (BOOL) archiveRootObject: (id)rootObject toFile: (NSString*)path
|
|
|
|
|
{
|
1996-01-23 22:55:55 +00:00
|
|
|
|
/* xxx fix this return value */
|
1995-04-15 19:38:52 +00:00
|
|
|
|
id d = [self archivedDataWithRootObject:rootObject];
|
|
|
|
|
[d writeToFile:path atomically:NO];
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
|
1996-01-26 03:15:14 +00:00
|
|
|
|
- (unsigned int) versionForClassName: (NSString*)className;
|
|
|
|
|
{
|
1996-01-26 19:53:15 +00:00
|
|
|
|
[self subclassResponsibility:_cmd];
|
1996-01-26 03:15:14 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Getting data from the archiver */
|
|
|
|
|
|
|
|
|
|
+ unarchiveObjectWithData: (NSData*) data
|
|
|
|
|
{
|
|
|
|
|
return [[self _concreteClass] unarchiveObjectWithData: data];
|
|
|
|
|
}
|
1995-04-15 19:38:52 +00:00
|
|
|
|
|
1996-01-26 03:15:14 +00:00
|
|
|
|
+ unarchiveObjectWithFile: (NSString*) path
|
|
|
|
|
{
|
|
|
|
|
return [[self _concreteClass] unarchiveObjectWithFile: path];
|
|
|
|
|
}
|
1995-04-15 19:38:52 +00:00
|
|
|
|
|
|
|
|
|
- (NSMutableData*) archiverData
|
|
|
|
|
{
|
1996-01-26 03:15:14 +00:00
|
|
|
|
[self subclassResponsibility:_cmd];
|
1995-04-15 19:38:52 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
1996-01-26 03:15:14 +00:00
|
|
|
|
|
|
|
|
|
/* Substituting Classes */
|
1995-04-15 19:38:52 +00:00
|
|
|
|
|
|
|
|
|
+ (NSString*) classNameEncodedForTrueClassName: (NSString*)trueName
|
|
|
|
|
{
|
1996-01-26 03:15:14 +00:00
|
|
|
|
return [[self _concreteClass] classNameEncodedForTrueClassName: trueName];
|
1995-04-15 19:38:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
1997-05-03 20:10:45 +00:00
|
|
|
|
- (void) encodeClassName: (NSString*)trueName
|
1995-04-15 19:38:52 +00:00
|
|
|
|
intoClassName: (NSString*)inArchiveName
|
1996-01-26 03:15:14 +00:00
|
|
|
|
{
|
|
|
|
|
[self subclassResponsibility:_cmd];
|
|
|
|
|
}
|
|
|
|
|
|
1996-01-26 19:53:15 +00:00
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@implementation NSUnarchiver
|
|
|
|
|
|
|
|
|
|
static Class NSUnarchiver_concrete_class;
|
|
|
|
|
|
|
|
|
|
+ (void) _setConcreteClass: (Class)c
|
|
|
|
|
{
|
|
|
|
|
NSUnarchiver_concrete_class = c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (Class) _concreteClass
|
|
|
|
|
{
|
|
|
|
|
return NSUnarchiver_concrete_class;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
|
{
|
1996-04-19 23:13:51 +00:00
|
|
|
|
if (self == [NSUnarchiver class])
|
|
|
|
|
NSUnarchiver_concrete_class = [NSGUnarchiver class];
|
1996-01-26 19:53:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initializing an unarchiver
|
|
|
|
|
|
1996-02-13 01:50:29 +00:00
|
|
|
|
+ allocWithZone:(NSZone *)zone
|
|
|
|
|
{
|
|
|
|
|
return NSAllocateObject([self _concreteClass], 0, zone);
|
|
|
|
|
}
|
|
|
|
|
|
1996-01-26 19:53:15 +00:00
|
|
|
|
- (id) initForReadingWithData: (NSData*)data
|
|
|
|
|
{
|
|
|
|
|
[self subclassResponsibility:_cmd];
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Decoding objects
|
|
|
|
|
|
|
|
|
|
+ (id) unarchiveObjectWithData: (NSData*)data
|
|
|
|
|
{
|
|
|
|
|
return [[self _concreteClass] unarchiveObjectWithData: data];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (id) unarchiveObjectWithFile: (NSString*)path
|
|
|
|
|
{
|
|
|
|
|
return [[self _concreteClass] unarchiveObjectWithFile: path];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Managing */
|
|
|
|
|
|
|
|
|
|
- (BOOL) isAtEnd
|
|
|
|
|
{
|
|
|
|
|
[self subclassResponsibility:_cmd];
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSZone*) objectZone
|
|
|
|
|
{
|
|
|
|
|
[self subclassResponsibility:_cmd];
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) setObjectZone: (NSZone*)zone
|
|
|
|
|
{
|
|
|
|
|
[self subclassResponsibility:_cmd];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unsigned int) systemVersion
|
|
|
|
|
{
|
|
|
|
|
[self subclassResponsibility:_cmd];
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Substituting Classes */
|
|
|
|
|
|
|
|
|
|
+ (NSString*) classNameDecodedForArchiveClassName: (NSString*)nameInArchive
|
1996-01-26 03:15:14 +00:00
|
|
|
|
{
|
|
|
|
|
return [[self _concreteClass]
|
1996-01-26 19:53:15 +00:00
|
|
|
|
classNameDecodedForArchiveClassName: nameInArchive];
|
|
|
|
|
}
|
|
|
|
|
+ (void) decodeClassName: (NSString*)nameInArchive
|
|
|
|
|
asClassName: (NSString*)trueName
|
|
|
|
|
{
|
|
|
|
|
return [[self _concreteClass]
|
|
|
|
|
decodeClassName: nameInArchive asClassName: trueName];
|
1996-01-26 03:15:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-01-26 19:53:15 +00:00
|
|
|
|
- (NSString*) classNameDecodedForArchiveClassName: (NSString*)nameInArchive
|
1995-04-15 19:38:52 +00:00
|
|
|
|
{
|
1996-01-26 19:53:15 +00:00
|
|
|
|
[self subclassResponsibility:_cmd];
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) decodeClassName: (NSString*)nameInArchive
|
|
|
|
|
asClassName: (NSString*)trueName
|
|
|
|
|
{
|
|
|
|
|
[self subclassResponsibility:_cmd];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* NSObject extensions for archiving */
|
|
|
|
|
|
|
|
|
|
@implementation NSObject (NSArchiver)
|
|
|
|
|
|
|
|
|
|
- (Class) classForArchiver
|
|
|
|
|
{
|
|
|
|
|
return [self classForCoder];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- replacementObjectForArchiver: (NSArchiver*) archiver
|
|
|
|
|
{
|
|
|
|
|
return [self replacementObjectForCoder: archiver];
|
1995-04-15 19:38:52 +00:00
|
|
|
|
}
|
1995-04-09 02:42:37 +00:00
|
|
|
|
|
|
|
|
|
@end
|
1996-01-26 19:53:15 +00:00
|
|
|
|
|