// MyLine.C
// (c) 1996 Burt Rosenberg
// The solution to homework 7.
// Revised to include nextWord method

// This is just the MyLine object and it's methods.
// Compile this using the -c option:
//   CC -c MyLine.C
// in the same directory as the file MyLine.h.
// It will result in a MyLine.o file which can be
// combined with other programs.

// The MyLine.h file commnicates among the program modules
// the types and names of methods in the MyLine object.

#include <iostream.h>
#include "MyLine.h"

MyLine::MyLine( void )
{
   buffer[0] = '\0' ;
}

void MyLine::
copyIn ( char m[] )
{
   int i ;
   for ( i = 0; i<size ; i++ )
   {
      buffer[i] = m[i] ;
      if ( buffer[i] == '\0' ) break ;
   }
   buffer[size-1] = '\0' ;
}

void MyLine::
print( void ) 
{
   cout << buffer << endl ;
}

int MyLine::
countChar( char t )
{
   int count = 0 ;
   int i = 0 ;
   while ( buffer[i] != '\0' )
   {
       if ( buffer[i] == t ) count++ ;
       i++ ;
   }
   return count ;   
}

int MyLine::
countChars( char match[] )
{
   int count = 0 ;
   int j, i = 0 ;
   while ( buffer[i] != '\0' )
   {
       j = 0 ;
       while ( match[j] != '\0' ) 
       {
          if ( buffer[i] == match[j] ) count++ ;
          j++ ;
       }
       i++ ;
   }
   return count ;  
}

void MyLine::
translate( char match[], char subst[] )
{
   int i, j ;

   // ASSERT length of match string must equal that of subst string
   i = 0 ;
   while ( buffer[i] != '\0' )
   {
      j = 0 ;
      while ( match[j] != '\0' )
      {
         if ( match[j] == buffer[i] )
            buffer[i] = subst[j] ;
         j++ ;
      }
      i++ ;
   }
}

int MyLine::
isEmpty( void )
{
   return ( buffer[0] == '\0' ) ;
}

int MyLine::
index( char match[], int mode )
{
   int i, j, found ;
   i = 0 ;
   while ( buffer[i]!='\0' ) 
   {
      j = 0 ;
      found = 0 ;
      while ( match[j]!='\0' )
      {
          switch ( mode ) 
          {
              case +1:
                 if ( match[j] == buffer[i] ) return i ;
                 break ;
              case -1:
                 if ( match[j] == buffer[i] ) found = 1 ;
                 break ;
          }
          j++ ;
      }
      if ( mode==-1 && found==0 ) return i ;
      i++ ;
   }
   return i ;
}

char * MyLine::
prefix( char rb[], int n )
{
   int i ;
 
   // verify input
   if ( n >= size )
   {
      n = size-1 ;
   }
   if ( n < 0 ) 
   {
      n = 0 ;
   }

   // copy
   for ( i=0; i<n ; i++ ) rb[i] = buffer[i] ;
   //ASSERT i <= size-1
   rb[i] = '\0' ; 

   // move forward
   i = 0 ;
   while ( buffer[n] != '\0' )
   {
       buffer[i] = buffer[n] ;
       i++ ;
       n++ ;
   }
   buffer[i] = '\0' ;
   
   return rb ;
}

#define DELIMITER " ,.!?;:"

char * MyLine::
nextWord( char m [] ) {
   const int linesize = 100 ;
   char line[linesize] ;

   do {
       if ( isEmpty() ) {
          cin.getline(line, linesize) ;
          if ( line[0] == '\0' ) return NULL ;
          copyIn(line) ;
       }
       prefix( m, index( DELIMITER, -1 ) ) ;
   } while ( isEmpty() ) ;

   prefix( m, index( DELIMITER, +1 ) ) ;
   return m ;

}

