// example program to read font file
// readFontFile.cc   April 20, 2004

#include <iostream>
using std::cerr;
using std::endl;
using std::cout;

#include <fstream>
using std::ifstream;

#include <cstdlib> // for exit(), and anything else we may need.

enum StrokeType 
  {
    ST_CONTINUE = 0,
    ST_LIFT = 1,
    ST_DONE = 2
  };

struct Point
{
  int x;
  int y;
  StrokeType s;
  Point *next;
};

void readPoint(int *x, int *y, StrokeType *s, ifstream *file);
void reportBadChar(char testChar, char expectedChar, int lineno);

int main(void)
{
  
  

  ifstream fontData("font.txt");
  
  if (!fontData)
    {
      cerr << "Could not open font.txt" << endl;
      exit (-1);
    }

  char letter; // letter read from the font file
  char dummy = 0; // same as char dummy = '\0';
                  // dummy= 65; is the same as dummy='A';

  int x,y;
  StrokeType s;

  fontData >> letter; // read the first letter in the file 
  while ( !fontData.eof()   )
    {
      // Process the data for this letter

      cout << "Here is the data for the letter " << letter<< ":" << endl;

      // read the rest of the line for this letter of the alphabet
      do 
	{
	  readPoint(&x, &y, &s, &fontData);
      
	  // This is the point where we would create a new node in the 
	  // linked list with the point we just read... 
	  
	  cout << "\tx =" << x << " y=" << y << " s=" << s << endl;
	} while ( s != ST_DONE);
      
      // read the first letter on the next line of the file.
      
      fontData >> letter;
    }

  return 0;
}


void readPoint(int *x, int *y, StrokeType *s, ifstream *file)
{
  char dummy;

  (*file) >> (*x);
  (*file) >> dummy ; // read the comma;

  if (dummy!=',')
    reportBadChar(dummy, ',', __LINE__);
  
  (*file) >> (*y);
  (*file) >> dummy ; // read the comma;
  
  if (dummy!=',')
    reportBadChar(dummy, ',', __LINE__);

  
  int strokeTypeAsInteger;
  
  (*file) >> (strokeTypeAsInteger);
  
  switch(strokeTypeAsInteger)
    {
    case 0:
      (*s) = ST_CONTINUE;
      break;
    case 1:
      (*s) = ST_LIFT;
      break;
    case 2:
      (*s) = ST_DONE;
      break;
    default:
      cerr << "Unknown StrokeType in input file: " 
	   << strokeTypeAsInteger << endl;
      exit(-1);
    }

#if(0)
  (*file) >> dummy; // should be either a space or a newline

  switch(strokeTypeAsInteger)
    {
    case ST_CONTINUE: // fall thru to ST_LIFT case
    case ST_LIFT:
      reportBadChar(dummy, ' ', __LINE__);
      break;
    case ST_DONE:
      reportBadChar(dummy, '\n', __LINE__);
      break;
      
    }
#endif
	
  return ;
  
}





void reportBadChar(char testChar, char expectedChar, int lineno)
{
  if (testChar != expectedChar)
    {
      cerr << "At line number " << lineno
	   << " I expected" << expectedChar 
	   << " but saw " << testChar << " instead!" << endl;
      exit (-1);
    }
}

