//----------------------------------------------------------------------------------------------------
//
//	ObjInspector
//
//	Inherits From:		WMInspector
//
//	Declared In:		ObjInspector.h
//
//	Disclaimer
//
//		You may freely copy, distribute and reuse this software and its
//		associated documentation. I disclaim any warranty of any kind, 
//		expressed or implied, as to its fitness for any particular use.
//
//----------------------------------------------------------------------------------------------------

#import "ObjInspector.h"

#define NM		"nm"
#define AWK_TR		"| awk '$2 == \"t\" || $2 == \"T\" {print $3,$4}' | tr -d '[]'"
#define OUT_OF_MEMORY	"Out of Memory!"
#define PROCESS_ERROR	"Unable to open process stream!"

@implementation ObjInspector

static id 	_SELF = nil;

//----------------------------------------------------------------------------------------------------
//  Private Methods
//----------------------------------------------------------------------------------------------------
- _alert: (STR) errorMessage
{
	NXRunAlertPanel (NULL, "%s", NULL, NULL, NULL, errorMessage);
	return self;
}

	
- _getTableOfContents
{
	//  Get Objects text symbols by sending NM, AWK_TR command.
	//  Response stream will be read to populate browser (in brower:fillMatrix:inColumn).
	
	STR	command;
	FILE    *responsePipe;
	NXStream *responseStream;
	char 	currentPath [MAXPATHLEN+1];
	id	font;
		
	[self selectionPathsInto:currentPath separator:'\0'];
	
	if (! (command = malloc (strlen (NM) + strlen (currentPath) + strlen (AWK_TR) + 3)))
		{
		[self _alert: OUT_OF_MEMORY];
		return nil;
		}
		
	sprintf (command, "%s %s %s", NM, currentPath, AWK_TR);

	if (! (responsePipe = popen (command, "r")))
		{
		if (command) free (command);
		[self _alert: PROCESS_ERROR];
		return nil;
		}		

	if (command) free (command);

	//  Populate Table Of Contents ScrollView text with formatted command response.
	//  Set the font here (fixed font for even column alignment).

 	font = [Font newFont: "Courier" size: 10];
	responseStream = NXOpenFile (fileno (responsePipe), NX_READONLY);
	[textView readText:responseStream];
	NXClose (responseStream);
	pclose (responsePipe);
	if (font) [font free];
	return self;
}


//----------------------------------------------------------------------------------------------------
//  WMInspector Methods
//----------------------------------------------------------------------------------------------------
+ new
{
	//  Required/called by WM.  Only allow one instance...
	
    	char 	path[MAXPATHLEN+1];
    	id 		bundle;

    	if (_SELF) return _SELF;

        _SELF = self = [super new];

    	if ( ! (bundle = [NXBundle bundleForClass:[ObjInspector class]] ) ) return nil;          
	if ( ! [bundle getPath:path forResource:"ObjInspector" ofType:"nib"] ) return nil;
    	[NXApp loadNibFile:path owner:self  withNames:NO fromZone:[self zone]];
 
 	return _SELF;
}


- revert: sender
{
	//  Called by WM when file selection is type we inspect.	
	[self _getTableOfContents];
	return [super revert: sender];
}

@end