/* parseTest.cpp

   A quick little test program to illustrate line parsing 
   
   Written March 2013 by John Bell for CS 440
   
*/

#include <cstdlib>
#include <iostream>
#include <cstring>

using namespace std;

int main( void )
{
    // The following is separated by multiple  spaces and tabs
    
    char input[ ] = "   12      34          56           This is the rest";
    cout << "\nInput string = \"" << input << "\"\n\n";
    
    // First use strtok to extract the numbers,
    // and atoi to convert the strings to integers
    
    char *args[ 3 ];
    int i1, i2, i3;
    
    args[ 0 ] = strtok( input, " \t" ); // First token on line
    
    for( int i = 1; i < 3; i++ )  // Next 2 tokens.  Note loop starts at 1
         args[ i ] = strtok( NULL, " \t" );
    
    // The next loop prints the first 3 strings, lengths, and integers
    
    for( int i = 0; i < 3; i++ )
         cout << "args[ " << i << " ] = " << args[ i ] 
              << ". Length = " << strlen( args[ i ] ) 
              << ".  Integer = " << atoi( args[ i ] ) << endl;
    
    // The rest of the line starts after the last number string.
              
    char * rest = args[ 2 ] + strlen( args[ 2 ] ) + 1;
    
    // Move the pointer past spaces and tabs.
    
    while( *rest == ' ' || *rest == '\t' )
           rest++;
    
    cout << "\nrest = \"" << rest << "\". Length = " << strlen( rest ) 
         << endl << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
