/* Generated by Interface Builder */

#import "Logger.h"
#import "GraphView.h"
#import <appkit/Text.h>
#import <appkit/ScrollView.h>
#import <appkit/Control.h>
#import <NXCType.h>
#import <math.h>
#import <dpsclient/psops.h>
#import <dpsclient/wraps.h>
#import <stdlib.h>
#import <string.h>

#define MAXNUMLENGTH 50

@interface Logger (Private)
-registerData; // opens a stream from text and parse into NXDPoints.
@end

extern int getSeparator(NXStream *stream);
extern double getNumber(NXStream *stream);

@implementation Logger (Private)
-registerData
{
    int		c, sign, retval;
    double	aNum;
    NXDPoint	aPoint;
    NXStream	*stream;
    BOOL	processedLine = NO, gotFirst = NO, gotSecond = NO; 
    stream=[theText stream];
    NXSeek(stream, 0, NX_FROMSTART);
    while ((c = NXGetc(stream)) != EOF)
    {
    	sign = 1;
	retval = getSeparator(stream);
    	if (retval == 1)
	{
		c = NXGetc(stream);
	}
	else if (retval == -1)
    	{
    		break;
    	}
	
	if (!NXIsDigit(c))
	{
		if ((c =='-') && NXIsDigit(c = NXGetc(stream)))
		{
			sign = -1;
		}
		else
		{
			while ((c!='\n') && (c!= EOF))
			{
				c = NXGetc(stream);
				processedLine = YES;
			}
		}
	}
	
	if (c == EOF) break;
	else if (processedLine)
	{
		processedLine = NO;
		continue;
	}	
	
	aNum = getNumber(stream);
	
	if (!gotFirst)
	{
		aPoint.x = sign * aNum;
		gotFirst = YES;
	}
	else if (!gotSecond)
	{
		aPoint.y = sign * aNum;
		[thePlotView addThePoint:aPoint];
	        gotFirst = gotSecond = NO;
	}
    }
    return self;

}
int getSeparator(NXStream *stream)
{
	int	c, firstChar;
	
	NXUngetc(stream);
	c = firstChar = NXGetc(stream);
	while (NXIsSpace(c) || (c == ',') || (c == '+'))
		c = NXGetc(stream);
	
	if (c == firstChar) 
		return 0;
	else if (c == EOF)
		return -1;
	NXUngetc(stream);
	return 1;		
}
double getNumber(NXStream *stream)
{
	int c, i=0;
	char *end;
	char temp[MAXNUMLENGTH];
	
	NXUngetc(stream);
	c=NXGetc(stream);
	while ((NXIsDigit(c) || (c == '.')) && (c != '\n') && (c != EOF))
	{
		temp[i++] = c;
		c = NXGetc(stream);
	}
	NXUngetc(stream);
	temp[i] = 0;	
	return (strtod(temp,&end));
}							
@end
@implementation Logger
- appDidInit:sender
{
[[[thePlotView manualXMin:0.0] manualXMax:100.0] setXMajorTickWidthTo:20.0];
[[[thePlotView manualYMin:-1.0] manualYMax:1.0] setYMajorTickWidthTo:0.2];
[thePlotView setCurrentPenColorToRGBColor:0:0:0];
[thePlotView setPointRadiusTo:0.01];
theText=[theTextView docView];
autoX=NO;
autoY=NO;
return self;
}

-clear:sender
{
char buffer[100];
sprintf(buffer,"");
[theText selectAll:self];
[theText replaceSel:buffer];
[thePlotView clearDataInfo:self];
return self;
}
- plot:sender
{
   [thePlotView clearDataInfo:self];
   if  ([autoX intValue]) [thePlotView setXAutoscale:NO]; else [thePlotView setXAutoscale:YES];
    if  ([autoY intValue]) [thePlotView setYAutoscale:NO]; else [thePlotView setYAutoscale:YES];
   if([connected intValue]) 
     [thePlotView setCurrentPenStyleTo:SLINE]; 
     else [thePlotView setCurrentPenStyleTo:CIRCLE];
   [self registerData];    
    return self;
}

@end
