//
// Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: Mon Jul 18 11:23:42 PDT 2005
// Last Modified: Mon Jul 18 11:23:46 PDT 2005
// Filename:      ...sig/examples/all/extract2.cpp
// Web Address:   http://sig.sapp.org/examples/museinfo/humdrum/extract2.cpp
// Syntax:        C++; museinfo
//
// Description:   C++ implementation of the Humdrum Toolkit thru command.
//

#include "humdrum.h"

#include <stdlib.h>
#include <string.h>

#ifndef OLDCPP
   #include <iostream>
#else
   #include <iostream.h>
#endif


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

// global variables
Options      options;            // database for command-line arguments
int          exclude = 0;        // used with -x option
int          interpQ = 0;        // used with -i option
const char*  interps = "";       // used with -i option
int          debugQ  = 0;        // used with --debug option


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


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

   // 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();
      outfile.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));
      }

      // analyze the input file according to command-line options
      if (interpQ) {
         extractInterpretations(infile, interps);
      } else if (exclude <= 0) {
         cout << infile;
      } else {
         excludeSpine(infile, exclude);
      }
   }

   return 0;
}


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

//////////////////////////////
//
// excludeSpine -- 
//

void excludeSpine(HumdrumFile& infile, int exclude) {
   int i;
   int j;
   int start = 0;

   for (i=0; i<infile.getNumLines(); i++) {
      switch (infile[i].getType()) {
         case E_humrec_none:
         case E_humrec_empty:
         case E_humrec_global_comment:
         case E_humrec_bibliography:
            cout << infile[i] << '\n';
            break;
         case E_humrec_data_comment:
         case E_humrec_data_kern_measure:
         case E_humrec_interpretation:
         case E_humrec_data:
            start = 0;
            for (j=0; j<infile[i].getFieldCount(); j++) {
               if (infile[i].getPrimaryTrack(j) == exclude) {
                  continue;
               }
               if (start != 0) {
                  cout << '\t';
               }
               start = 1;
               cout << infile[i][j];
            }
            cout << endl;
            break;
         default:
            cout << "!!Line = UNKNOWN:" << infile[i] << endl;
            break;
      }
   }
}

//////////////////////////////
//
// extractInterpretation -- extract interpretations which match the
//    given list.
//

void extractInterpretations(HumdrumFile& infile, const char* interps) {
   int i;
   int j;
   int column = 0;

   for (i=0; i<infile.getNumLines(); i++) {
      if (debugQ) {
         cout << "Processing line " << i+1 << ": " << infile[i] << endl;
      }
      switch (infile[i].getType()) {
         case E_humrec_none:
         case E_humrec_empty:
         case E_humrec_global_comment:
         case E_humrec_bibliography:
            cout << infile[i] << '\n';
            break;
         case E_humrec_data_comment:
         case E_humrec_data_kern_measure:
         case E_humrec_interpretation:
         case E_humrec_data:
            column = 0;
            for (j=0; j<infile[i].getFieldCount(); j++) {
               if (strstr(interps, infile[i].getExInterp(j)) == NULL) {
                  continue;
               }
               if (column != 0) {
                  cout << '\t';
               }
               column++;
               cout << infile[i][j];
            }
            cout << endl;
            break;
         default:
            cout << "!!Line = UNKNOWN:" << infile[i] << endl;
            break;
      }
   }
}



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

void checkOptions(Options& opts, int argc, char* argv[]) {
   opts.define("x=i:0", "A field to remove from input");
   opts.define("i=s:", "Exclusive interpretation list to extract from input");

   opts.define("debug=b", "print debugging information");
   opts.define("author=b");                     // author of program
   opts.define("version=b");                    // compilation info
   opts.define("example=b");                    // example usages
   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, Feb 2008" << endl;
      exit(0);
   } else if (opts.getBoolean("version")) {
      cout << argv[0] << ", version: Feb 2008" << 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);
   }

   exclude = opts.getInteger("x");
   interpQ = opts.getBoolean("i");
   interps = opts.getString("i");

}



//////////////////////////////
//
// example -- example usage of the sonority program
//

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





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

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



// md5sum: 0c61bacdfce1312ffc1d8c7048d8a113 extractx.cpp [20090508]