//-------------------------------------------------
// Person.m - implementation for the Person object.
//-------------------------------------------------

#import <stdio.h>
#import <stdlib.h>
#import <strings.h>
#import "Person.h"

@implementation Person

//-------------------------------------------------
// Instance methods.
//-------------------------------------------------
- initWithName:(const char*)aName
  age:(int)anAge
//
// Initialize newly created instance.
//
{
[super init];
[[self setName:aName] setAge:anAge];
return self;
}

//-------------------------------------------------
- free
//
// Tidy up the instance. Free malloced storage.
//
{
if( name )
	free( name );

return [super free];
}

//-------------------------------------------------
// Set instance variable values.
//-------------------------------------------------
- setName:(const char*)aName
{
int	len = strlen(aName);

if( name )
	free( name );
	
name = (char*)malloc( len + 1 );
strcpy( name, aName );

return self;
}

//-------------------------------------------------
- setAge:(int)anAge
{
age = anAge;
return self;
}

//-------------------------------------------------
// Get instance variable values.
//-------------------------------------------------
- (const char*)name { return (const char*)name; }

//-------------------------------------------------
- (int)age { return age; }

//-------------------------------------------------
// General instance methods.
//-------------------------------------------------
- printInfo
//
// Print info for this instance of Person.
//
{
printf( "%s info: name %s, age %d \n",
	[[self class] name], name, age );

return self;
}

//-------------------------------------------------
// Archiving methods.
//-------------------------------------------------
- read:(NXTypedStream*)stream
//
// Unarchive this instance from a typed stream.
//
{
int	namelen;

// Read our superclass.
[super read:stream];

// Read our instance variables.
NXReadTypes( stream, "ii", &age, &namelen );
name = malloc( namelen + 1 );
NXReadArray( stream, "c", namelen, name );

return self;
}

//-------------------------------------------------
- write:(NXTypedStream*)stream
//
// Archive this instance to a typed stream.
//
{
int	namelen;

// Write our superclass.
[super write:stream];

// Write our instance variables.
namelen = strlen(name);
NXWriteTypes( stream, "ii", &age, &namelen );
NXWriteArray( stream, "c", namelen, name );

return self;
}

//-------------------------------------------------
@end

