#include	"global.h"
#include	"source.h"

// Input for the source handler is specified as a parameter to the
// constructor function.  The filename "-" is used to denote the
// standard input stream.

Source::Source(char* fn) : Buffer()
{
	_currentLine = 0;
	if (strcmp(fn, "-") == 0) {
		fd = stdin;
		_fileName = "stdin";
	} else {
		fd = fopen(fn, "r");
		_fileName = allocate(strlen(fn)+1);
		strcpy(_fileName, fn);
	}
	_status = (fd ? OK : CANNOTOPEN);
}

Source::~Source()
{
	if (fd != stdin) {
		free(_fileName);
		fclose(fd);
	}
}

char Source::nextChar()
{
	if (empty()) {
		char *p = fill();
		if (fgets(p, MAXBUFFERSPACE, fd)) {
			_currentLine++;
			//fprintf(stderr, "%6d    %s", _currentLine, p);
		} else {
			*p++ = (char)EOF;
			*p = '\0';
			_status = SAWEOF;
		}
	}
	return(get());
	
}


syntax highlighted by Code2HTML, v. 0.9.1