//
//	MiscStringNEXTSTEP.m
//		Written by Don Yacktman (c) 1993 by Don Yacktman.
//				Version 1.95  All rights reserved.
//		This notice may not be removed from this source code.
//
//	This object is included in the MiscKit by permission from the author
//	and its use is governed by the MiscKit license, found in the file
//	"LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
//	for a list of all applicable permissions and restrictions.
//	

#import <misckit/MiscString.h>

@implementation MiscString(NEXTSTEP)

// This category includes methods to support various NEXTSTEP features,
// such as InterfaceBuilder (palettes), Distributed Objects (NXTransport),
// and object archiving.

- read:(NXTypedStream *)stream
{
	int tempLength; char *tBuf;
	[super read:stream];
	
	// find out how much space we will need
	NXReadType(stream, "i", &tempLength);
	
	// note that -allocateBuffer:fromZone: will set _length for
	// us -- it is important to not smash _length since it tracks
	// the actual size of the current buffer!  (See the notes for
	// -allocateBuffer:fromZone: for more info on why no smashing.)
	[self allocateBuffer:tempLength fromZone:[self zone]]; // make sure
	// we have a buffer and it is big enough.
	
	// Get the string off the stream and into the buffer
	NXReadType(stream, "*", &tBuf);
	if (tBuf) strcpy(buffer, tBuf);
	if (!tBuf && buffer) buffer[0] = '\0'; // NULL string?
	
	// now make sure that we have the right length for the string stored.
	if (buffer) length = strlen(buffer);
	else length = 0;
	
	// clean up and blow this joint
	if (tBuf) free(tBuf);
	return self;
}

- write:(NXTypedStream *)stream
{
	[super write:stream];
	// put the string and it's length on the stream.  The length is sort
	// of redundant, but this assures that the string really is like the
	// one that was archived -- if the stored instance had an oversize
	// buffer, then read will restore an oversize buffer.  Of course,
	// crud after the string won't be the same, so the new instance isn't
	// quite the same, but...
	NXWriteType(stream, "i", &_length);
	NXWriteType(stream, "*", &buffer);
	return self;
}

// NXTransport protocol implementation (next three methods):
- encodeUsing:(id <NXEncoding>)portal
{
	[portal encodeData:&_length ofType:"i"];
	[portal encodeData:&length ofType:"i"];
	[portal encodeData:&buffer ofType:"*"];
	return self;
}

- decodeUsing:(id <NXDecoding>)portal
{
	int newLen;
	[self freeString];
	[portal decodeData:&newLen ofType:"i"];
	[self allocateBuffer:newLen];
	[portal decodeData:&length ofType:"i"];
	[portal decodeData:&buffer ofType:"*"];
	return self;
}

- encodeRemotelyFor:(NXConnection *)connection
	freeAfterEncoding:(BOOL *)flagp isBycopy:(BOOL)isByCopy
{
	if (isByCopy) {
		*flagp = NO; // object will not be freed (ie, copy,  not move)
		return self; //encode object (copy it)
	}
	*flagp = NO;
	// super will encode the proxy otherwise
	return [super encodeRemotelyFor:connection
				freeAfterEncoding:flagp isBycopy:isByCopy];
}

// Interface Builder support
- (const char *)getInspectorClassName { return "MiscStringInspector"; }
- (NXImage *)getIBImage { return [NXImage findImageNamed:"MiscStringObj"]; }

@end
