/* from
 * (Preliminary Documentation) Copyright (c) 1994 by NeXT Computer, Inc.  
 * All Rights Reserved.
 *
 * part of Foundation Kit
 *
 * a NSArchiver for GNU archiving
 *
 */
 
#include "GSArchiver.h"

#include <Foundation/NSString.h>
#include <gnuarchive/objc/typedstream.h>
 
id	NXActive_Archiver;

@implementation GSArchiver

// Private methods not declared in interface

- (BOOL)_initForWritingToFile:(NSString *)path
{
    const char	*filename = [path cString];
    
    stream = objc_open_typed_stream_for_file(filename, OBJC_WRITEONLY );
    if ( stream == NULL ) {
        return NO;
    }
    NXActive_Archiver = self;
    return YES;
}

- _finishWriting
{
    objc_flush_typed_stream(stream);
    objc_close_typed_stream(stream);
    NXActive_Archiver = nil;
    return self;
}  
- (BOOL) _initForWritingRootObject:(id)rootObject toFile:(NSString *)path
{
    [self _initForWritingToFile:path];
    [super encodeRootObject:rootObject];
    [self _finishWriting];
    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];
}

- initForWritingToFile:(NSString *)path
{
    [self _initForWritingToFile:path];
    return self;
}

- finishWriting
{
    [self _finishWriting];
    return self;
}
// - (void)encodeArrayOfObjCType:(const char *)type
//                        count:(unsigned int)count
//			   at:(const void *)array
// Note: done by superclass

// - (void)encodeConditionalObject:(id)object
// Note: done by superclass

// - (void)encodeRootObject:(id)rootObject
// Note: done by superclass

// Getting data from the archiver

// - (NSMutableData *)archiverData;

// Substituting One Class for Another 

// + (NSString *)classNameEncodedForTrueClassName:(NSString *)trueName;

// - (void)encodeClassName:(NSString *)trueName
//	  intoClassName:(NSString *)inArchiveName;


@end

@implementation GSUnarchiver

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

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

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

- free
{
    objc_close_typed_stream(stream);
    NXActive_Archiver = nil;
    [super free];
    return self;
}
    
- (void)setObjectZone:(NSZone *)zone
{
    return;
}

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

@end
