/* Implementation of NXArchiver and NXUnarchiver class
 *
 * Copyright (C)  1993  The Board of Trustees of  
 * The Leland Stanford Junior University.  All Rights Reserved.
 *
 * Authors: Paul Kunz
 *
 * This file is part of an Objective-C class library
 *
 * NXArchiver.m,v 1.3 1995/05/11 17:42:23 mfg Exp
 */
 
 /*
  * Based on (Preliminary Documentation) Copyright (c) 1994 by NeXT Computer,
  * Inc.  All Rights Reserved. 
  *
  * This NXCoder subclass reads and writes NeXTSTEP 3.2 compatible
  * NXTypedStreams without use of a NSCoder
  */
 
#include "NXArchiver.h"

#include <appkit/Application.h>
#include "NSString.h"
 
@implementation NXArchiver

// private methods

- (BOOL) _initForWritingRootObject:(id)rootObject toFile:(NSString *)path
{
    const char	*filename = [path cString];
    
    stream = NXOpenTypedStreamForFile(filename, NX_WRITEONLY );
    if ( stream == NULL ) {
        return NO;
    }
    [super encodeRootObject:rootObject];
    NXFlushTypedStream(stream);
    NXCloseTypedStream(stream);
    return YES;
}

// Initializing an NSArchiver

// - (id)initForWritingWithMutableData:(NSMutableData *)mdata
 
// Archiving data 

// + (NSData *)archivedDataWithRootObject:(id)rootObject

+ (BOOL)archiveRootObject:(id)rootObject
                   toFile:(NSString *)path;
{
    return [[self alloc] _initForWritingRootObject:rootObject toFile:path];
}

// 	  intoClassName:(NSString *)inArchiveName;


@end

@implementation NXUnarchiver

- (id) _initForReadingObjectFromFile:(NSString *)path
{
    id		object;
    const char	*filename = [path cString];
    
    stream = NXOpenTypedStreamForFile( filename, NX_READONLY );
    NXActive_Archiver = self;
    object = [self decodeObject];
    NXActive_Archiver = nil;
    return object;
    
}

- initForReadingWithStream:(NXStream *)untypedstream
{
    stream = NXOpenTypedStream(untypedstream, NX_READONLY);
    NXActive_Archiver = self;
    return self;
}

- initForReadingFromFile:(char *)filename
{
    stream = NXOpenTypedStreamForFile(filename, NX_READONLY);
    NXActive_Archiver = self;
    return self;
}

- free
{
    NXCloseTypedStream(stream);
    NXActive_Archiver = nil;
    [super dealloc];
    return self;
}
    
- (void)setObjectZone:(NXZone *)zone
{
    NXSetTypedStreamZone(stream, zone);
    return;
}

+ (id)unarchiveObjectWithFile:(NSString *)path;
{
    return [[ self alloc] _initForReadingObjectFromFile:path];
} 

@end
