//
// AppDelegate.m -- Example of using category of MiscTree that allows a
//		MiscTree to be a delegate for an NXBrowser
//		Written by Scott Anguish Copyright (c) 1994 by Scott Anguish.
//				Version 1.0.  All rights reserved.
//
//		This notice may not be removed from this source code.
//
//	This program 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 "AppDelegate.h"

@implementation AppDelegate

	NXStream	*localStream;
		id	theLine;
		id	root;


-appDidInit:sender
{
	[self loadTree:self];
	[theBrowser setDelegate:root];
	[theBrowser setTarget:self];
	[theBrowser setAction:@selector(didClick:)];
	[theBrowser loadColumnZero];

	return self;
}

- didClick:sender
{
	id	theNode;
	id	pathString;
	
	theNode=[root returnSelectedNode:sender];
	[selectedLabelNode setStringValue:[theNode label]];
	pathString=[root returnSelectedPath:sender];
	[selectedLabelPath setStringValue:[pathString stringValue]];
	
	return self;
}

	int	ParseTree(id lastLimb, int currentLevel, id lastPlanting);

- loadTree:sender
	{
		id	applicationLocation;
		char path[MAXPATHLEN+1];
		
		applicationLocation=[NXBundle mainBundle];
		if ([applicationLocation getPath:path forResource:"MiscKit" ofType:".tree"])
			{
			localStream=NXMapFile(path,NX_READONLY);
			root = [[MiscTree alloc] init];
			ParseTree(root,-1,root);
			
			NXClose(localStream);
			}
		return self;
	};




	char *NXGets(char *s, int n, NXStream *stream)
	{
		int c, i = 0;
		
		while (((c = NXGetc(stream)) != EOF) && (i < n-1)) {
			s[i++] = c;
			if (c == '\n')
				break;
		}
		s[i++] = 0;
		
		return i > 1 ? s : NULL;
	}

	id NXGetsString(NXStream *stream)
	{
		char	theLine[BUFSIZ];
		id	theString;

		theString=[[MiscString alloc] init];
		[theString setStringValue:NXGets(theLine,BUFSIZ, stream)];
		return (theString);
	}
	
	
	
	int	ParseTree(id lastLimb, int currentLevel, id lastPlanting)
	{
		int	indentLevel;
		id	newLeaf;
		id	lastLeaf;
		int	returnLevel;
		
		lastLeaf=lastPlanting;
		while (YES)
			{
			
			theLine=NXGetsString(localStream);
			if ((!theLine) || ([theLine emptyString])) {return (-1);};
			indentLevel=[theLine numOfChar:'\t'];
			[theLine replaceEveryOccurrenceOfChars:"\t" with:""];
			[theLine replaceEveryOccurrenceOfChars:"\n" with:""];
			if (indentLevel < currentLevel) {return (indentLevel);};
			if (indentLevel > currentLevel)
				{
					newLeaf=[[MiscTree alloc] initLabelString:theLine];
					[lastLeaf addBranch:newLeaf];
					returnLevel=ParseTree(lastLeaf,indentLevel,newLeaf);
					if (returnLevel != currentLevel) {return (returnLevel);};
					if ((!theLine) || ([theLine emptyString])) {return (-1);};
					//if we got here, then we need to take the currentLine
					//and add it anyways, so we fall through
					// My specific question is what you would suggest here... I'm using 
					// theLine as a variable in this class only... I have the other option
					// of passing the MiscString as a param in ParseTree, so I could trash that 
					// variable, and make it part of the MiscTree without having a 'global-ish' 
					// variable on my mind
				}
			newLeaf=[[MiscTree alloc] initLabelString:theLine];
			[lastLimb addBranch:newLeaf];
			lastLeaf=newLeaf;
			} // while(YES)
	};











@end
