/******************************************************************************
 GMScanner.cc

	BASE CLASS = public yyFlexLexer

	Copyright © 1997 by Glenn W. Bach.  All rights reserved.
	
	Base code generated by Codemill v0.1.0

 *****************************************************************************/

//Class Header
#include <GMScanner.h>

#include <JRegex.h>

#include <jStreamUtil.h>

#include <iostream.h>
#include <jAssert.h>

/******************************************************************************
 Constructor

 *****************************************************************************/

GMScanner::GMScanner
	(
	istream* is
	)
{
	itsIs = is;
	itsCurrentHeaderStart = 0;
	itsCurrentPosition = 0;
}

/******************************************************************************
 Destructor

 *****************************************************************************/

GMScanner::~GMScanner()
{
}

/******************************************************************************
 yylex

 *****************************************************************************/

int
GMScanner::yylex()
{
	if (itsCurrentPosition == 0)
		{
		itsIs >> ws;
		itsCurrentPosition = JTellg(*itsIs);
		itsCurrentHeaderStart = itsCurrentPosition;
		itsText = JReadLine(*itsIs);
		JRegex regex;
		JBoolean matched;
		JArray<JStringRange>* subList = new JArray<JStringRange>;
		assert(subList != NULL);
		err = regex.SetPattern("^From[[:space:]]+.*.{3}[[:space:]]+.{3}[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+:[[:digit:]]+:[[:digit:]]+[[:space:]]+[[:digit:]]{4}");
		assert(err.OK());
		matched = regex.Match(itsText, subList);
		delete subList;
		if (matched)
			{
			itsState = kHeaderStart;
			return itsState;
			}
		}
	else if (itsState == kHeaderStart)
		{
		itsCurrentPosition = JTellg(*itsIs);
		itsText = JReadLine(*itsIs);
		if (itsText.BeginsWith("Subject:"))
			{
			itsText.RemoveSubstring(1,9);
			return kSubject;
			}
		else if (itsText.BeginsWith("From:"))
			{
			itsText.RemoveSubstring(1,6);
			return kFrom;
			}
		else if (itsText.BeginsWith("Date:"))
			{
			itsText.RemoveSubstring(1,6);
			return kDate;
			}
		else if (itsText.BeginsWith("Cc:"))
			{
			itsText.RemoveSubstring(1,4);
			return kCC;
			}
		else if (itsText.BeginsWith("Reply-To:"))
			{
			itsText.RemoveSubstring(1,10);
			return kReplyTo;
			}
		else if (itsText.BeginsWith("Status:"))
			{
			itsText.RemoveSubstring(1,8);
			return kStatus;
			}
		}
}
