
/* Generated by Interface Builder, modified by the author */

/* File: ClassTable.m */

#include <stdio.h>
#include <string.h>

#include <sys/types.h>
#include <sys/dir.h>

#import "ClassTable.h"

#import <streams/streams.h>
#import <NXCType.h>

#import <objc/Storage.h>

#import <appkit/Text.h>
#import <appkit/Matrix.h>

@implementation ClassTable

typedef struct _classinfo
{
	char className[128];
	char path[1024];
} CLASSINFO;

- init /* init object */
{
	[super init];
	
	table= [[Storage alloc] initCount:100 elementSize:sizeof( CLASSINFO) description:NULL];
	numberOfElements= 0;
	
	return self;
}

- reset /* reset object */
{
	[table empty];
	numberOfElements= 0;
	
	return self;
}

- (BOOL)readDirectory:(char *)aName /* read the object definition in the directory */
{
	int position;
	CLASSINFO classinfo;
	char *pointer;
	DIR *directory;
	struct direct *entry;
	
	if( (directory= opendir( aName)) != NULL)
	{
		while( (entry= readdir( directory)) != NULL)
		{
			if( strcmp( entry->d_name, ".") && strcmp( entry->d_name, ".."))
			{
				strcpy( classinfo.path, aName);
				strcat( classinfo.path, "/");
				strcat( classinfo.path, entry->d_name);
				if( ![self readDirectory:classinfo.path]) /* is directory ? */
				{
					if( NXIsUpper( entry->d_name[0]) &&
						((pointer= rindex( entry->d_name, '.')) != NULL) &&
						!strcmp( pointer+1, "h"))
					{
						*pointer= (char)0;
						strcpy( classinfo.className, entry->d_name);
						position= 0; /* search position to insert */
						while( (position < numberOfElements) && 
							(NXOrderStrings(
							(unsigned char *)((CLASSINFO *)[table elementAt:position])->className, 
							(unsigned char *)classinfo.className, YES, -1, NULL) < 0))
						{
							position++;
						}
						[table insert:&classinfo at:position]; /* insert */
						numberOfElements++;
					}
				}
			}
		}
		
		closedir( directory);
	}
	
	return (directory != NULL);
}

- readClassesAtPath:(char *)aPath /* read the object definition at the path */
{
	char *start, *end;
	
	end= aPath;
	do
	{
		start= end;
		if( (end= index( start, ':')) != NULL)
		{
			*end= (char)0;
		}
		
		[self readDirectory:start];
		
		if( end != NULL)
		{
			*end++= ':';
		}
	}
	while( end != NULL);
	
	return self;
}

- (int)fillMatrix:aMatrix; /* fill a matrix */
{
	int count;
	
	for( count= 0; count < numberOfElements; count++)
	{
		[[aMatrix addRow] setTitle:((CLASSINFO *)[table elementAt:count])->className at:count:0];
	}
	
	return numberOfElements;
}

- (BOOL)fillFile:(char *)aFilename /* fill a file */
{
	char class[256];
	int count;
	BOOL ok;
	NXStream *stream;
	
	if( ok= ((stream= NXOpenMemory( NULL, 0, NX_WRITEONLY)) != NULL)) /* open stream */
	{
		for( count= 0; count < numberOfElements; count++)
		{
			strcpy( class, ((CLASSINFO *)[table elementAt:count])->className);
			strcat( class, "\n");
			NXWrite( stream, class, strlen( class)); /* write class */
		}
		
		ok= (NXSaveToFile( stream, aFilename) == 0); /* save to file */
		NXCloseMemory( stream, NX_TRUNCATEBUFFER); /* close stream */
	}
	
	return ok;
}

- (char *)getPathAtPosition:(int)aPosition /* get class's path */
{
	return ((CLASSINFO *)[table elementAt:aPosition])->path;
}

- (int)getPositionOfClass:(char *)aClassName /* get position of class */
{
	int i, position;
	
	for( i= 0, position= -1; i < numberOfElements; i++)
	{
		if( !strcmp( ((CLASSINFO *)[table elementAt:i])->className, aClassName))
		{
			position= i;
			i= numberOfElements;
		}
	}
	
	return position;
}

@end
