//
// Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: Wed Apr 20 20:47:40 PDT 2011
// Last Modified: Wed Apr 20 20:47:43 PDT 2011
// Filename:      ...sig/examples/all/noficta.cpp
// Web Address:   http://sig.sapp.org/examples/museinfo/humdrum/noficta.cpp
// Syntax:        C++; museinfo
//
// Description:   Remove ficta marks from **kern data
//

#include "humdrum.h"
#include "PerlRegularExpression.h"

#include <string.h>

#ifndef OLDCPP
   #include <iostream>
   using namespace std;
#else
   #include <iostream.h>
#endif


// function declarations
void      checkOptions(Options& opts, int argc, char* argv[]);
void      example(void);
void      usage(const char* command);
void      getFictaChar(char* ficta, HumdrumFile& infile);
void      processFile(HumdrumFile& infile);
void      removeFicta(HumdrumFile& infile, int line, int spine, 
                              const char* ficta);

// global variables:
Options   options;            // database for command-line arguments
int       numinputs;          // the total number of input files

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

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
   numinputs = options.getArgCount();
 
   const char* filename = "";

   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) {
         filename = "";
         infile.read(cin);
      } else {
         filename = options.getArg(i+1);
         infile.read(filename);
      }
      processFile(infile);
     
   }

   return 0;
}

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



//////////////////////////////
//
// processFile
//

void processFile(HumdrumFile& infile) {
   int i, j;
   char ficta[1024] = {0};
   getFictaChar(ficta, infile);
   PerlRegularExpression pre;
   
   for (i=0; i<infile.getNumLines(); i++) {
      if (!infile[i].isData()) {
         continue;
      }
      for (j=0; j<infile[i].getFieldCount(); j++) {
         if (!infile[i].isExInterp(j, "**kern")) {
            continue;
         }
         if (strstr(infile[i][j], ficta) == NULL) {
            continue;
         }
         removeFicta(infile, i, j, ficta);
      }
   }

   for (i=0; i<infile.getNumLines(); i++) {
      if (!infile[i].isBibliographic()) {
         cout << infile[i] << "\n";
         continue;
      }
      if (!pre.search(infile[i][0], "^!!!RDF\\*\\*kern\\s*:.*ficta", "")) {
         cout << infile[i] << "\n";
      }
   }

}



//////////////////////////////
//
// removeFicta --
//

void removeFicta(HumdrumFile& infile, int line, int spine, const char* ficta) {
   Array<char> temp;
   temp.setSize(strlen(infile[line][spine])+1);
   strcpy(temp.getBase(), infile[line][spine]);

   PerlRegularExpression pre;
   int hasflat    = pre.search(temp, "-", "");
   int hassharp   = pre.search(temp, "#", "");
   int hasnatural = pre.search(temp, "n", "");

   pre.sar(temp, ficta, "", "g");
 
   if (hasflat) {
      pre.sar(temp, "-", "", "g");
   } else if (hassharp) {
      pre.sar(temp, "#", "", "g");
   } else {
      if (hasnatural) {
         pre.sar(temp, "n", "", "g");
      }
      char buffer[1024] = {0};
      pre.search(temp, "^(.*)([a-g]+)(.*)$", "i");
      strcpy(buffer, pre.getSubmatch(1));
      strcat(buffer, pre.getSubmatch(2));
      PerlRegularExpression pre2;
      if (pre2.search(pre.getSubmatch(), "[bead]+", "")) {
         strcat(buffer, "-");
      } else if (pre2.search(pre.getSubmatch(), "[fcg]+", "")) {
         strcat(buffer, "#");
      }
      strcat(buffer, pre.getSubmatch(3));
      temp.setSize(strlen(buffer)+1);
      strcpy(temp.getBase(), buffer);
   }

   infile[line].changeField(spine, temp.getBase());
}



//////////////////////////////
//
// getFictaChar --
//

void getFictaChar(char* ficta, HumdrumFile& infile) {
   int i;
   PerlRegularExpression pre;
   for (i=infile.getNumLines()-1; i>=0; i--) {
      if (!infile[i].isBibliographic()) {
         continue;
      }

      if (pre.search(infile[i][0], 
            "^!!!RDF\\*\\*kern\\s*:\\s*([^\\s=]+)\\s*=.*ficta", "")) {
         strcpy(ficta, pre.getSubmatch(1));
         return;
      }
   }

   // presume "i" as ficta if no RDF marker
   strcpy(ficta, "i");
}



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

void checkOptions(Options& opts, int argc, char* argv[]) {
   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, April 2011" << endl;
      exit(0);
   } else if (opts.getBoolean("version")) {
      cout << argv[0] << ", version: 20 April 2011" << 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);
   }
}
  




//////////////////////////////
//
// example -- example usage of the quality program
//

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



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

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



// md5sum: 72a1c7356836a553e1044872717ef3d0 noficta.cpp [20110427]