/*

  CLNormalize

  Overdue checklist normalizer

  haleyjd 02/27/08

*/

#ifdef _MSC_VER
#pragma warning( disable : 4786 )
#endif

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

static vector<string> lines;

//
// ReadInput
//
// Get the input from the file and put the lines into the lines vector.
//
static void ReadInput(ifstream &infile)
{
   string str;
   char c;

   while(infile.read(&c, 1))
   {
      switch(c)
      {
      case '\n': // newline
         lines.push_back(str);
         str.erase();
         break;
      default:
         str.append(1, c);
         break;
      }
   }
}

typedef vector<string>::iterator vsiterator;

//
// lfstrip
//
// Strip spaces from beginning of lines.
//
static void lfstrip(void)
{
   vsiterator str;

   for(str = lines.begin(); str != lines.end(); ++str)
   {
      size_t nspacepos = str->find_first_not_of(' ');

      if(nspacepos != string::npos)
         str->erase(0, nspacepos);
   }
}

//
// DelBadLines
//
// Removes bad lines from the file.
//
static void DelBadLines(void)
{
   bool done = false;
   vsiterator str;

   do
   {
      for(str = lines.begin(); str != lines.end(); ++str)
      {
         if(str->length() <= 24)
         {
            lines.erase(str);
            break; // iterator is invalid, must restart loop
         }
      }

      if(str == lines.end())
         done = true;
   }
   while(!done);
}

//
// RemoveParenNames
//
// Deletes parenthetized names.
//
static void RemoveParenNames(void)
{
   vsiterator str;
   char c;

   for(str = lines.begin(); str != lines.end(); ++str)
   {
      size_t lppos = str->find_first_of('(');
      size_t rppos = str->find_first_of(")0123456789");

      if(rppos != string::npos)
      {
         c = str->at(rppos);
         if(c >= '0' && c <= '9')
            rppos -= 1;
      }

      if(lppos != string::npos && rppos != string::npos && rppos > lppos)
         str->replace(lppos, rppos - lppos + 1, rppos - lppos + 1, ' ');
   }
}

//
// WriteOutput
//
static void WriteOutput(ofstream &outfile)
{
   vsiterator str;

   for(str = lines.begin(); str != lines.end(); ++str)
      outfile << str->c_str() << endl;
}

//
// Main Program
//
int main(int argc, char *argv[])
{
   ifstream infile;
   ofstream outfile;
   const char *infn;

   if(argc < 2)
   {
      cout << "Usage: CLNormalize <infile>" << endl;
      return 0;
   }

   infn = argv[1];

   // open input file
   infile.open(infn);

   if(!infile || infile.bad())
   {
      cout << "Error: couldn't open input file " << infn << endl;
      return 1;
   }

   // parse input file
   ReadInput(infile);

   // done with input
   infile.close();

   // strip spaces
   lfstrip();

   // delete bad lines
   DelBadLines();

   // remove parenthesized names
   RemoveParenNames();

   // open file for output
   outfile.open(infn);

   if(!outfile || outfile.bad())
   {
      cout << "Error: couldn't open output file " << infn << endl;
      return 1;
   }

   // write output
   WriteOutput(outfile);

   // close output
   outfile.close();

   return 0;
}

// EOF