Listing 2: A function that uses three chronographs

void getcmt(istream &in)
{
   int  i, j, len;
   long linecount = 0l;

   double hours, minutes, seconds;

   // Flag: 0 = not in comment, 
   //       1 = in C comment
   //       2 = in C++ comment

   int incomment = 0;

   // Flag: 0 = no comment on this line
   //       1 = comment on this line

   int commentline = 0;

   char inbuff[MAXLINE + 1]; // I/O buffer

   double elapsed_time;
   double elapsed_time_inside;
   double elapsed_time_outside;

   // One Chronograph to count the time
   // spent in this function.

   Chronograph chrono;

   // Two Chronographs, to count the time
   // spent inside and outside of comments.

   Chronograph inside;
   Chronograph outside;

   // --------------------------------------

   // reset the "inside comment" timer to
   // zero and stop it

   inside.reset();

   cout << endl;

   in.getline(inbuff, MAXLINE);

   while(in.eof() == 0)
   {
      linecount++;

      //demonstrate use of lap time
      if(linecount % 100 == 0)
      {
         cout << pgm;
         cout << " getcmt()";
         cout << ": 100 lines in ";
         cout.precision(2);
         cout << chrono.lap() << " seconds.";
         cout << endl;
      }

      len = strlen(inbuff);
      i = 0;

      // while chars are left on the line...
      while(i < len)
      {
         // process depending on if we are
         // in a comment and what type it is
         switch(incomment)
         {
            case 0:
               // not inside a comment
               // C comment detection
               if(inbuff[i] == '/' &&  
                  inbuff[i+1] == '*')
               {
                  outside.stop();
                  inside.start();
                  incomment = 1;
                  commentline = 1;
                  i += 2;
               }


               // C++ comment detection
               // details omitted
               // ...

            case 1:
               // inside a C comment

               // Nested C or C++ comment 
               // detection details omitted
               // ...

               // end of C comment detected
               else if(inbuff[i] == '*' &&
                       inbuff[i+1] == '/')
               {
                  inside.stop();
                  outside.start();

                  incomment = 0;

                  i += 2;
               }
               else
                  i++;

               break;

            case 2:
               // inside a C++ comment
               // details omitted
               // ...

            default:
               // error, bad "incomment"
               // shouldn't happen
               cerr << "Error!" << endl;

            return;

         }
         // end of switch()
      }
      // end of while()

      if(commentline == 1)
         cout << inbuff << endl;

      if(incomment == 1)
         commentline = 1;
      else
         commentline = 0;

      in.getline(inbuff, MAXLINE);
   }

   // stop all three of the timers
   // and get the elapsed times
   elapsed_time         = chrono.stop();
   elapsed_time_inside  = inside.stop();
   elapsed_time_outside = outside.stop();

   // show times spent inside and outside
   // of comments.  Details omitted
   // ...

   return;
}
//End of File