// tokens.cpp -- Reading tokens

#include <iostream>
#include <string>

using namespace std;

#include <string.h>

// compares two strings , capitalized
int mycmp(const char s1[], const char s2[])
{
        int cursor;
	for (cursor = 0; 
	    s1[cursor] != '\0'
		  && s2[cursor] != '\0' 
		  && toupper(s1[cursor]) == toupper(s2[cursor]); 
	    cursor++);
	if (s1[cursor] == '\0')
		if (s2[cursor] == '\0')
			return 0;
		else
			return -1;
	else if (s2[cursor] == '\0')
		return 1;
	else if (toupper(s1[cursor]) < toupper(s2[cursor]))
		return -1;
	else 
		return 1;
}

// It skips initial spaces.
// It reads up to maxsize-1 consecutive nonspace characters into
// buffer. It then skips until one CR has been read.

void getjusttoken(char buffer[], int maxsize)
{
	char c;           // Character read from input
	int counter = 0;  // Cursor
	enum {before, during, after} state = before;  // It is before until
		// we get to the first token, then it becomes during. 
	    // After the token it becomes after.
	for(;;)
	{
		cin.get(c);
		if (c == '\n')
		{
			buffer[counter] = '\0';
			return;
		}
		if (isspace(c))
		{
			if (state == during)
				state = after;
			continue;
		}
		if (state == before)
			state = during;
		if ((state == during) && (counter < maxsize-1))
			buffer[counter++] = c;
	}
}

// From current position of cursor, it skips initial spaces.
// It reads up to maxsize-1 consecutive nonspace characters into
// buffer. If any consecutive nonspace characters are still left,
// they are skipped until a space character has been read.

void gettoken(char buffer[], int maxsize)
{
	char c;                   // Character read from input
	int previousNotSpace = 0; // True if previous character 
	                          // read was not a space
	int counter = 0;          // Position in buffer
	for(;;)
	{
		cin.get(c);
		if (isspace(c)) 
		{
			if (previousNotSpace)
			{
				buffer[counter] = '\0';
				return;
			}
		}
		else if (counter < maxsize-1)
		{
			buffer[counter++] = c;
		}
		previousNotSpace = !isspace(c);
	}
}


void main(void)
{
	enum {NAMESIZE=12};
	char s1[NAMESIZE];
	char s2[NAMESIZE];

	cout << "Using getjusttoken" << endl;
	for (;;)
	{
		cout << "Enter first string: ";
		getjusttoken(s1, NAMESIZE);
		cout << "Enter second string: ";
		getjusttoken(s2, NAMESIZE);
		if ((strcmp(s1, "") == 0) 
			&& (strcmp(s2, "") == 0)) break;
		int cmp = mycmp(s1, s2);
		if (cmp < 0)
			cout << s1 << " is less than " << s2 << endl;
		else if (cmp > 0)
			cout << s2 << " is less than " << s1 << endl;
		else
			cout << s1 << " and " << s2 << " are equal" << endl;
	}

	cout << "\n\nUsing gettoken: gearing ten tokens" << endl;
	for (int k = 0; k < 10; k++)
	{
		gettoken(s1, NAMESIZE);
		cout << k << ". " << s1 << endl;
	}

}

/* Here is a run of this program
% a.out
Using getjusttoken
Enter first string: sam
Enter second string: sally
sally is less than sam
Enter first string: anN
Enter second string: ANN
anN and ANN are equal
Enter first string: ann
Enter second string: aNNA
ann is less than aNNA
Enter first string: 
Enter second string: 


Using gettoken: gearing ten tokens
roses are red 1234567891012345 and
0. roses
1. are
2. red
3. 12345678901
4. and
violets are blue and so are you
5. violets
6. are
7. blue
8. and
9. so
% exit
% 
 */
