/*
 * @(#)StreamTokenizer.cpp
 *
 * This file is part of webCDwriter - Network CD Writing.
 *
 * Copyright (C) 2002 Jörg P. M. Haeger
 *
 * webCDwriter is free software. See CDWserver.cpp for details.
 *
 * Jörg Haeger, 19.04.2002
 */

#include "BufferedReader.h"
#include "StreamTokenizer.h"

StreamTokenizer::StreamTokenizer(class BufferedReader &r): reader(r) {
}

StreamTokenizer::~StreamTokenizer() {
}

int StreamTokenizer::nextToken() {
	sval = "";

	while (1) {
		reader.mark(1);
		int ch = reader.read();
		if (ch == 13) {
			reader.mark(1);
			if (reader.read() == 10)
				return ttype = TT_EOL;
			reader.reset();
			return TT_EOL;
		}

		if (ch == 10)
			return TT_EOL;

		if (ch > 32) {
			reader.reset();
			break;
		}

		if (ch < 0)
			return ttype = TT_EOF;
	}

	for (int i = 0; i < 1024; i++) {
		reader.mark(1);
		int ch = reader.read();
		if (ch < 0)
			return ttype = TT_EOF;

		if (ch <= 32) {
			reader.reset();
			break;
		}

		if (ch == ':' || ch == ',') {
			if (sval.length() == 0)
				sval = sval + (char)ch;
			else
				reader.reset();
			break;
		}

		sval = sval + (char)ch;
	}

	return ttype = TT_WORD;
}
