//
// Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: Mon Feb 10 08:12:05 PST 2003
// Last Modified: Mon Feb 10 08:12:09 PST 2003
// Filename:      ...sig/examples/all/vlcontext/vlcontext.cpp
// Web Address:   http://sig.sapp.org/examples/museinfo/humdrum/vlcontext.cpp
// Syntax:        C++; museinfo
//
// Description:   Voice leading context.  Identifies the note before
//                and after the current note.
//
// Note:          Spine changes are not allowed for this program.
//  
//

#include "humdrum.h"

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

// option variables:
Options      options;            // database for command-line arguments
int          debugQ  = 0;        // for debugging
int          appendQ = 0;        // for appending analysis data to input data
int          spine   = 0;        // which spine to analyze (0 offset)

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

int main(int argc, char* argv[]) {
   HumdrumFile infile;
   checkOptions(options, argc, argv); // process the command-line options

   // if no command-line arguments read data file from standard input
   if (options.getArgCount() < 1) {
      infile.read(cin);
   } else {
      infile.read(options.getArg(1));
   }
   infile.analyzeRhythm();
 
   printAnalysis(infile, spine);


   return 0;
}


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



//////////////////////////////
//
// printAnalysis -- print the analysis of the given spine.
//


void printAnalysis(HumdrumFile& infile, int spine) {
   int i;
   const char* lastptr = "";
   const char* nextptr = "";
   for (i=0; i<infile.getNumLines(); i++) {
      switch (infile[i].getType()) {
      case E_humrec_global_comment:
      case E_humrec_bibliography:
      case E_humrec_none:
      case E_humrec_empty:
         cout << infile[i] << "\n";
         break;
      case E_humrec_data:
         lastptr = infile.getLastDatum(i, spine); 
         nextptr = infile.getNextDatum(i, spine); 
         cout << infile[i][spine] << "\t";
         if (strcmp(lastptr, "") == 0) {
            cout << ".\t";
         } else {
            cout << lastptr << "\t";
         }
         if (strcmp(nextptr, "") == 0) {
            cout << ".\n";
         } else {
            cout << nextptr << "\n";
         }
         break;
      case E_humrec_data_comment:
         cout << infile[i][spine] << "\t!\t!\n";
         break;
      case E_humrec_data_measure:
         cout << infile[i][spine] << "\t"
              << infile[i][spine] << "\t"
              << infile[i][spine] << "\n";
         break;
      case E_humrec_data_interpretation:
         if (strncmp(infile[i][0], "**", 2) == 0) {
            cout << infile[i][spine] << "\t"
                 << infile[i][spine] << "\t"
                 << infile[i][spine] << "\n";
            cout << "!\t!last\t!next\n";
         } else if (strcmp(infile[i][0], "*-") == 0) {
            cout << "*-\t*-\t*-\n";
         } else {
            cout << infile[i][spine] << "\t*\t*\n";
         }
         break;
      }
   }
}



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

void checkOptions(Options& opts, int argc, char* argv[]) {
   opts.define("s|spine|f|field=i:1", "spine to analyze");
   opts.define("a|append=b",          "append analysis to data in output");   
   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, Feb 2003" << endl;
      exit(0);
   } else if (opts.getBoolean("version")) {
      cout << argv[0] << ", version: 10 Feb 2003" << 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");
   appendQ  = opts.getBoolean("append");
   spine    = opts.getInteger("spine") - 1;

}



//////////////////////////////
//
// example -- example usage of the melstep program
//

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



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

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


// md5sum: 6c988a3b023e7d8b6f87accbc4be38b9 vlcontext.cpp [20050403]