 /* HTuple.m	by Paul Kunz	December 1991
 * Object used to store information on open Hippo Tuple and methods
 * to archive it to NXTypedStream.
 *
 * $Id: HTuple.m,v 1.8 1992/04/13 20:43:55 pfkeb Rel $
 *
 * Copyright (C)  1991  The Board of Trustees of
 * The Leland Stanford Junior University.  All Rights Reserved.
 */ 

#import "HTuple.h"

#import <appkit/nextstd.h>
#import "strings.h"

#define INDEX_VERSION 1
#define CURRENT_VERSION INDEX_VERSION

@implementation HTuple

+ initialize
{
    [self setVersion:CURRENT_VERSION];
    return self;
}

- initTuple:(ntuple) nt file:(const char *)path
         by:(BOOL)refFlag mode:(BOOL) binFlag index:(int) iValue
{
    [super init];
    tuple = nt;
    isRef = refFlag;
    isBinary = binFlag;
    if ( filename == NULL ) {
        NX_ZONEMALLOC( [self zone], filename, char, strlen(path)+1 );
    } else {
        NX_ZONEREALLOC( [self zone], filename, char, strlen(path)+1 );
    }
    strcpy( filename, path );
    [self setFakeFilename:NO];
    ntindex = iValue;
    return self;
}
- (ntuple) ntuple
{
    return tuple;
}
- setNtuple:(ntuple) aTuple;
{
    tuple = aTuple;
    return self;
}
- (BOOL) isRef
{
    return isRef;
}
- setIsRef:(BOOL) refFlag
{
    isRef = refFlag;
    return self;
}
- setIsBinary:(BOOL) binFlag
{
    isBinary = binFlag;
    return self;
}
- (const char *)filename
{
    return filename;
}
- setFilename:(const char *)path
{
    if ( filename == NULL ) {
        NX_ZONEMALLOC( [self zone], filename, char, strlen(path)+1 );
    } else {
        NX_ZONEREALLOC( [self zone], filename, char, strlen(path)+1 );
    }
    strcpy( filename, path );
    return self;
}
- (int) index
{
    return ntindex;
}
- setIndex:(int) value
{
    ntindex = value;
    return self;
}
- setAltFilename:(const char *)path
{
    if ( altfilename == NULL ) {
        NX_ZONEMALLOC( [self zone], altfilename, char, strlen(path)+1 );
    } else {
        NX_ZONEREALLOC( [self zone], altfilename, char, strlen(path)+1 );
    }
    strcpy( altfilename, path );
    return self;
}
- (const char *)altfilename
{
    return altfilename;
}
- (BOOL) isFakeFilename
{
    return fakeFilename;
}
- setFakeFilename:(BOOL) bValue
{
    fakeFilename = bValue;
    return self;
}
- (BOOL) isSameAs:aTuple
{
    if ( strcmp( filename, [aTuple filename] ) ) {
        return NO;
    }
    if ( [aTuple index] != ntindex ) {
        return NO;
    }
    return YES;
}
- (const char *) title
{
    if ( tuple ) {
       return h_getNtTitle( tuple );
    } else {
       return "not available";
    }
}
- write:(NXTypedStream *) stream
{
    ntuple	ntlist[] = {NULL, NULL};
    char	*buffer;
    int		nt_size, len_fn;
    
    [super write:stream];
    len_fn = strlen( filename );
    NXWriteTypes( stream, "cci", &isRef, &isBinary, &len_fn );
    NXWriteTypes( stream, "*", &filename );
    if ( [HTuple version] >= INDEX_VERSION ) {
        NXWriteTypes( stream, "i", &ntindex );
    }
    if ( !isRef ) {
	nt_size = h_ntSize( tuple );
        NX_ZONEMALLOC( [self zone], buffer, char, nt_size );
	ntlist[0] = tuple;
	h_writeMem( buffer, nt_size, NULL, ntlist );
	NXWriteType( stream, "i", &nt_size );
	NXWriteArray( stream, "c", nt_size, buffer );
	NX_FREE(buffer);
    }
    return self;
}
- read:(NXTypedStream *) stream
{
    ntuple	*ntlist;
    display	*dlist;
    char	*buffer;
    int		nt_size, len_fn;
    
    [super read:stream];
    NXReadTypes( stream, "cci", &isRef, &isBinary, &len_fn );
    NX_ZONEMALLOC( [self zone], filename, char, len_fn+1 );
    NXReadTypes( stream, "*", &filename );
    if ( NXTypedStreamClassVersion(stream, "HTuple") >= INDEX_VERSION ) {
	NXReadType( stream, "i", &ntindex );
    }
    if ( !isRef ) {
        NXReadType( stream, "i", &nt_size );
        NX_ZONEMALLOC( [self zone], buffer, char, nt_size );
	NXReadArray( stream, "c", nt_size, buffer );
	h_readMem( buffer, nt_size, &dlist, &ntlist );
	tuple = ntlist[0];
	NX_FREE(buffer);
    }
    return self;
}
- free
{
    h_freeNt( tuple );
    NXZoneFree([self zone], filename );
    NXZoneFree([self zone], altfilename );
    return [super free];
}

@end
