ftp.nice.ch/pub/next/games/fun/cookie.s.tar.gz#/Cookie/String.m

This is String.m in view mode; [Download] [Up]

/* Generated by Interface Builder */

#import "String.h"
#import <strings.h>
#import <stdlib.h>
#import <stdio.h>
#import <NXCType.h>
#import <c.h>

@implementation String

- setString:(char *)AString
{
	// If it's outta bounds theres nothing to do
	if(!AString)
		return self;

	// First see if we have to allocate space to the string
	[self preAlloc :strlen(AString)+1];
	strcpy(buffer,AString);			/* Copy in string */

	return self;
}

- (char *)string
{
	return buffer;
}

- freeString
{
	if(!buffer)
		return self;	/* Get out if nothing to free */

	/* Free up string here */
	free(buffer);
	/* Blank buffer and length so we know it's blank */
	buffer = 0;
	length = 0;

	return self;
}

- (BOOL)writeString :(NXStream *)MyStream
{
	if(length)
		{
			int strlength = strlen(buffer);

			if(NXWrite(MyStream,buffer,strlength) != strlength)
				return FALSE;	/* A Problem */
		}

	return TRUE;
}

- (BOOL)writeWithTerminator :(NXStream *)MyStream :(char)terminator
{
	return [self writeWithTerminator :MyStream :&terminator :1];
}

- (BOOL)writeWithTerminator :(NXStream *)MyStream :(char *)terminator :(int)mylength
{
	if(![self writeString :MyStream])
		return FALSE;

	if(NXWrite(MyStream,terminator,mylength) != mylength)
		return FALSE;

	return TRUE;
}

- (int)readString: (NXStream *)MyStream
{
	char TempBuffer[2024];
	int mylength;

	mylength = 0;

	do
		{
		if(NXRead(MyStream,&TempBuffer[mylength],1) != 1)
			break;	/* Problem!! */
		if((!NXIsPrint(TempBuffer[mylength])) && (TempBuffer[mylength] != 0xA))
			continue;
		mylength++;
		}
	while(TempBuffer[mylength-1] != 0xA);

	if(mylength == 0) // Didn't read anything
		return 0;

	TempBuffer[mylength-1] = 0; /* Strip off linefeed */

	/* Allocate some memory for our buffer */
	[self setString:TempBuffer];

	return strlen(TempBuffer)+1;
}

- (void)free
{
	 [self freeString];
	 [super free];
}

- init
{
	 [super init];

	 /* Do my initialisation */
	 buffer = 0;
	 length = 0;
	 
	 return self;
}

- preAlloc :(int)size
{
	if(size <= length)
		return self;	// we already have that size available

	[self freeString];
	buffer = malloc(size);
	buffer[0] = 0;
	length = size;

	return self;
}

@end

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Marcel Waldvogel and Netfuture.ch.