//
// Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: Sun May 26 21:42:21 PDT 2002
// Last Modified: Sun May 26 21:42:24 PDT 2002
// Filename:      ...sig/examples/all/plainkern.cpp
// Web Address:   http://sig.sapp.org/examples/museinfo/humdrum/plainkern.cpp
// Syntax:        C++; museinfo
//

#include "humdrum.h"

#include <string.h>


// function declarations
void   checkOptions(Options& opts, int argc, char* argv[]);
void   example(void);
void   printAnalysis(HumdrumFile& infile);
void   usage(const char* command);

// global variables
Options      options;            // database for command-line arguments
int          debugQ     = 0;     // used with the --debug option

///////////////////////////////////////////////////////////////////////////

int main(int argc, char* argv[]) {
   HumdrumFile infile;

   // process the command-line options
   checkOptions(options, argc, argv);

   // figure out the number of input files to process
   int numinputs = options.getArgCount();

   for (int i=0; i<numinputs || i==0; i++) {
      infile.clear();

      // if no command-line arguments read data file from standard input
      if (numinputs < 1) {
         infile.read(cin);
      } else {
         infile.read(options.getArg(i+1));
      }

      printAnalysis(infile);
   }

   return 0;
}


///////////////////////////////////////////////////////////////////////////


//////////////////////////////
//
// checkOptions -- validate and process command-line options.
//

void checkOptions(Options& opts, int argc, char* argv[]) {

   opts.define("debug=b",  "trace input parsing");   
   opts.define("author=b",  "author of the program");   
   opts.define("version=b", "compilation information"); 
   opts.define("example=b", "example usage"); 
   opts.define("h|help=b",  "short description"); 
   opts.process(argc, argv);
   
   // handle basic options:
   if (opts.getBoolean("author")) {
      cout << "Written by Craig Stuart Sapp, "
           << "craig@ccrma.stanford.edu, May 2002" << endl;
      exit(0);
   } else if (opts.getBoolean("version")) {
      cout << argv[0] << ", version: 26 May 2002" << endl;
      cout << "compiled: " << __DATE__ << endl;
      cout << MUSEINFO_VERSION << endl;
      exit(0);
   } else if (opts.getBoolean("help")) {
      usage(opts.getCommand());
      exit(0);
   } else if (opts.getBoolean("example")) {
      example();
      exit(0);
   }

   debugQ = opts.getBoolean("debug");

}



//////////////////////////////
//
// example -- example usage of the plainkern program
//

void example(void) {
   cout <<
   "                                                                        \n"
   << endl;
}



//////////////////////////////
//
// printAnalysis -- 
//

void printAnalysis(HumdrumFile& infile) {
   int i, j, k;
   int length;
   for (i=0; i<infile.getNumLines(); i++) {
      switch (infile[i].getType()) {
         case E_humrec_data:
            for (j=0; j<infile[i].getFieldCount(); j++) {
               if (strcmp(infile[i].getExInterp(j), "**kern") != 0) {
                  cout << infile[i][j];
               } else {
                  length = strlen(infile[i][j]);
                  for (k=0; k<length; k++) {
                     switch (infile[i][j][k]) {
                        case '\\': break;
                        case '/': break;
                        case 'L': break;
                        case 'J': break;
                        default:
                           cout << infile[i][j][k];
                     }
                  }
               }
               if (j<infile[i].getFieldCount()-1) {
                  cout << "\t";
               }
            }
            cout << "\n";
            break;
         case E_humrec_global_comment:
         case E_humrec_bibliography:
         case E_humrec_none:
         case E_humrec_empty:
         case E_humrec_data_comment:
         case E_humrec_data_measure:
         case E_humrec_data_interpretation:
         default:
            cout << infile[i].getLine() << "\n";
            break;
      }
   }
}



//////////////////////////////
//
// usage -- gives the usage statement for the quality program
//

void usage(const char* command) {
   cout <<
   "                                                                        \n"
   << endl;
}


// md5sum: bef5165261e263fcb7e289f29a129bc8 plainkern.cpp [20050403]