/* Implementation for NXCoder
 *
 * Copyright (C)  1994  The Board of Trustees of  
 * The Leland Stanford Junior University.  All Rights Reserved.
 *
 * Authors: Paul Kunz
 *
 * This file is part of an Objective-C library
 *
 * NXCoder.m,v 1.6 1995/01/25 04:17:01 pfkeb Exp
 */
 
/* Based on 
 * (Preliminary Documentation) Copyright (c) 1994 by NeXT Computer, Inc.  
 * All Rights Reserved.
 *
 * An implementation of NSCoder that uses NXTypedStream without NSCoder
 *
 */
 
#include "NXCoder.h"

#include "NSString.h"
#include <stdarg.h>           /* for ANSI programs */
#include <stdio.h>
#include <gnuarchive/encoding.h>

@implementation NXCoder

// Encoding Data

- (void)encodeArrayOfObjCType:(const char *)types
                        count:(unsigned)count
                           at:(const void *)array
{
    NXWriteArray( stream, types, count, array);
    return;  
}
		
- (void)encodeBycopyObject:(id)anObject;
{
    [self encodeObject:anObject];
    return;
}

- (void)encodeConditionalObject:(id)anObject;
{
    [self encodeObject:anObject];
    return;
}
  
// - (void)encodeDataObject:(NSData *)data;
 
- (void)encodeObject:(id)anObject
{
    NXWriteObject( stream, anObject);
    return;
}
 
- (void)encodeObjectReference:(id)anObject
{
    NXWriteObjectReference( stream, anObject);
    return;
}
 
// - (void)encodePropertyList:(id)plist;
// - (void)encodePoint:(NSPoint)point;
// - (void)encodeRect:(NSRect)rect;
 
- (void)encodeRootObject:(id)rootObject
{
    NXWriteRootObject( stream, rootObject);
    return;
}

- (void)encodeSize:(NXSize)size
{
    NXWriteSize(stream,&size);
    return;
}
 
- (void) encodeColor:(NXColor *)aColor
{
 /* not supported in NSCoder */
 
    NXWriteColor(stream, *aColor);
    return;
}

- (void)encodeValueOfObjCType:(const char *)type
                           at:(const void *)address
{
    NXWriteType( stream, type, address );
    return;
}
	
// - (void)encodeValuesOfObjCTypes:(const char *)types, args, ...

// Decoding Data

- (void)decodeArrayOfObjCType:(const char *)types
                        count:(unsigned)count
                           at:(void *)address
{
    NXReadArray( stream, types, count, address );
    return;
}
		
// - (NSData *)decodeDataObject;
 
- (id)decodeObject
{
    id		object;
    
    object = NXReadObject( stream );
    return object;
}

// - (id)decodePropertyList;
// - (NSPoint)decodePoint;

- (NXRect)decodeRect
{
    NXRect	rect;

    NXReadRect(stream, &rect);
    return rect;
}

- (NXSize)decodeSize
{
    NXSize	size;

    NXReadSize( stream, &size);
    return size;
}
 
- (void)decodeValueOfObjCType:(const char *)type
                           at:(void *)address
{
    NXReadType( stream, type, address );
    return;
}

- (NXColor) decodeColor
{
 /* not supported in NSCoder */
 
    return NXReadColor(stream);
}

- (void)decodeValuesOfObjCTypes:(const char *)types, ...
{
#define CODER_MAX_VAR_ARGS 10
    va_list	args;
    const char	*c;
    char	*where[CODER_MAX_VAR_ARGS];
    int		i;

    va_start(args, types);
    for ( c = types, i = 0; *c; c = objc_skip_typespec(c) ) {
        if ( i < CODER_MAX_VAR_ARGS ) {
            where[i++] = va_arg(args, char*);
	} else {
	    printf( "[NXCoder decodeValuesOfObjCTypes]:"
	    	    " Maximum number of args exceeded\n");
	    return;
	}
    }
    va_end(args);
    NXReadTypes(stream, types, where[0], where[1], where[2], where[3],
	        where[4], where[5], where[6], where[7], where[8], where[9]);
    return;
}
 
// Managing Zones

// - (NSZone *)objectZone;
// - (void)setObjectZone:(NSZone *)zone;


// Getting a Version

// - (unsigned int)systemVersion;
 
- (unsigned int)versionForClassName:(NSString *)className
{
    const char	*class = [className cString];
    
    return NXTypedStreamClassVersion( stream, class );
}

@end
