

//	Copyright H. Giesen, University of Koblenz-Landau 1996


#import "BibTeXObject.h"


@implementation BibTeXObject


- initFromStream:(NXStream *)aStream owner:myFile
{	
	[super init];
	theStream = aStream;
	owner = myFile;
	c = NXGetc( theStream );
	// we could first check for Header
	// this could be a LaTeX comment before the first entry
	// now comments are skipped in the parser:
	// 	while( (c!='@') && (c!=EOF) ) NextByte;

	if( [self pickUpItem] ) return self;
	//if( [self parseSelf] ) return self;	// works
	return [self free];
}


- fileObject
{
	return owner;
}


- (NXStream	*)stream
{
	return theStream;
}


- setRange:(locType)range
{
	
	locInput = range;
	NXSeek(theStream, locInput.start-1, NX_FROMSTART);
	c = NXGetc( theStream );
	[self pickUpItem];
	return self;
}


- (int)line
{
	return firstLine;
}


- (locType)range
{
	return locInput;
}


- (locType)key
{
	return myKey;
}


- (int)entryType
{
	return myType;
}


- (char *)buffer
{
	char *theBuffer;
	int	length, maxLength;
	
	NXGetMemoryBuffer(theStream, &theBuffer, &length, &maxLength);
	return theBuffer;
}

// !! the calling object has to guarantee the buffer of
// appropriate size !!
- (char *)copy:(int)start :(int)length toBuffer:(char *)buffer
{
	int i;
	
	NXSeek(theStream, start, NX_FROMSTART);
	for( i=0; i<length; i++ ){
		buffer[i] = NXGetc( theStream );
	}
	buffer[i] = '\0';
	return buffer;
}


- (char *)copyRange:(locType)r toBuffer:(char *)buffer
{
	return [self copy:r.start :r.length toBuffer:buffer];
}


- copyRange:(locType)r toStream:(NXStream *)str
{
	char *theBuffer;
	int	length, maxLength;
	int	count;
	
	NXGetMemoryBuffer( [self stream], &theBuffer, &length, &maxLength);

	for( count=0; count<r.length; count++ ){
		NXPutc( str, theBuffer[ r.start + count ] );
	}
	return self;
}


- setKeyTo:(Cell *)strField
{
	char *theBuffer;
	char ccc;
	locType r = [self key];
	int	inx = r.start + r.length;
	int	length, maxLength;
	
	NXGetMemoryBuffer(theStream, &theBuffer, &length, &maxLength);
	ccc = theBuffer[ inx ];
	theBuffer[ inx ] = '\0';	// overwrite	

	[strField setStringValue:&theBuffer[ r.start ]];
	theBuffer[ inx ] = ccc;	// old value
	return self;
}


- setStringOf:(locType)r to:strField
{
	char *theBuffer;
	char ccc;
	int	inx = r.start + r.length;
	int	length, maxLength;
	
	NXGetMemoryBuffer(theStream, &theBuffer, &length, &maxLength);
	ccc = theBuffer[ inx ];
	theBuffer[ inx ] = '\0';	// overwrite	

	[strField setStringValue:&theBuffer[ r.start ]];
	theBuffer[ inx ] = ccc;	// old value
	return self;
}


- (locType)fieldFor:(int)fieldType
{
	return [BibTexParser fieldFor:fieldType];
}


- copyToStream:(NXStream *)str
{
	char *theBuffer;
	int	length, maxLength;
	int	count;
	
	NXGetMemoryBuffer( [self stream], &theBuffer, &length, &maxLength);

	for( count=0; count<locInput.length; count++ ){
		NXPutc( str, theBuffer[ locInput.start + count ] );
	}
	return self;
}


- copyToFile:(FILE *)str
{
	char *theBuffer;
	char ch;
	int	i = 0;
	int	length, maxLength;
	
	NXGetMemoryBuffer( [self stream], &theBuffer, &length, &maxLength);

	while( (ch=theBuffer[locInput.start + i])!='@' ){
		fputc( ch, str );
		i++;
	}
	fputc( ch, str );	// '@'
	i++;
	fputs( [self entryNameString:myType], str );	// the entryType
	ch = theBuffer[locInput.start + i++];
	while( (ch!=LPARA) && (ch!=LBRACE) ) {	// skip the entryName
		ch = theBuffer[locInput.start + i++];
	}
	i--;	// position of LPARA or LBRACE
	fwrite( &theBuffer[locInput.start+i],
		sizeof(char), locInput.length-i, str );
	return self;
}


@end
