#ifndef _TTABLE_
#define _TTABLE_

#include <CStr.h>
#include <TStream.h>
#include <TArray.h>


enum TableErr {
	teNoErr = 0,
	teTypeMismatch,
	teKeyNotFound,
	teBoundsErr,
	teDefRowErr,
	teOutOfMem,
	teMiscReadErr,
	teNoTable,
	teBadHeader,
	teNoStream,
	teEarlyEof,
	teEarlyEol
};

enum CellType {
	ctNoType,
	ctStr31,
	ctDouble_t
};

struct TableCell
{
	CStr31		str;
	double_t	d;
};

DEFINE_NOPOP_TARRAY(TableCell);
typedef TTableCellArray Column;

DEFINE_NOPOP_TARRAY(CellType);

#if THINK_C || __SC__
class TTable : public PascalObject 
#else
class TTable
#endif
{
		
	public:
		GS_API TTable();
		virtual Boolean GS_API Initialize(short numColsParam);
		 void GS_API Free();
		
		TableErr GS_API Load(TStream *stream);
		
		TableErr GS_API RowFromColumn(short column, char *key, short &row);
		TableErr GS_API RowFromColumn(short column, double_t &key, short &row);
		
		short	 GetNumRows()	{return numRows;}
		TableErr GS_API GetDouble(short row, short col, double_t &d);
		TableErr GS_API GetCStr31(short row, short col, CStr31 &s);
		
		double_t GetUnitsPerInch() {return unitsPerInch;}
		
		void GS_API Display();
		
	protected:
		Column				*data;
		CellType			*cType;
		short				numCols, numRows;
		double_t			unitsPerInch;

		Boolean GS_API ReadHeader(TStream *s);		// return TRUE if table found
		Boolean GS_API SkipToNext(TStream *s);		// return TRUE if EOL
		TableErr GS_API ReadCell(TStream *s, TableCell& c, CellType& ct);
		Boolean GS_API ReadDouble(TStream *s, char c, double_t& d);
		Boolean GS_API ReadString(TStream *s, char c, CStr31& cs);

	private:
		Boolean GS_API ReadToData(TStream* s);
		Boolean GS_API ReadToNextLine(TStream* s);
};

#endif
