//
// Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: Tue Oct  7 15:36:24 PDT 2008
// Last Modified: Tue Oct  7 15:36:34 PDT 2008
// Filename:      ...sig/examples/all/msubtract.cpp
// Web Address:   http://sig.sapp.org/examples/museinfo/msubtract/msubtract.cpp
// Syntax:        C++; museinfo
//
// Description:   Subtract two matrices from each other.
//                Second one is subtracted from the first file on
//                command-line.
//

#include <stdlib.h>             /* for qsort and bsearch functions */
#include <time.h>               /* for random number seeding */

#include "museinfo.h"		/* for humdrum file class */

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


// function declarations:

void     checkOptions(Options& opts, int argc, char** argv);
void     example(void);
void     usage(const char* command);
void     getDataLines(Array<int>& lines, HumdrumFile& infile);

// User interface variables:
Options     options;

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


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

   HumdrumFile file1;
   HumdrumFile file2;

   file1.read(options.getArg(1));
   file2.read(options.getArg(2));
     
   Array<int> lines1;
   Array<int> lines2;

   getDataLines(lines1, file1);
   getDataLines(lines2, file2);

   int linecount = lines1.getSize() < lines2.getSize() ?
         lines1.getSize() : lines2.getSize();

   Array<Array<double> > outputdata;
   outputdata.setSize(linecount);

   int i, j;
   int colcount;
   int col1;
   int col2;
   int i1;
   int i2;
   double num1;
   double num2;
   int flag;

   for (i=0; i<linecount; i++) {
      i1 = lines1[i];
      i2 = lines2[i];
      col1 = file1[i1].getFieldCount();
      col2 = file2[i2].getFieldCount();
      colcount = col1 < col2 ? col1 : col2;
      outputdata[i].setSize(colcount);
      for (j=0; j<colcount; j++) {
         if (strcmp(file1[i1].getExInterp(j), "**kern") == 0) {
            // ignore procesing of musical data
            continue;
         }
         if (strcmp(file1[i2].getExInterp(j), "**text") == 0) {
            // ignore procesing of text data
            continue;
         }
         // add user-controlled ignore list here?
         flag = sscanf(file1[i1][j], "%lf", &num1);
         if (flag != 1) {
            cout << "ERROR in reading number from first file" << endl;
            exit(1);
         }
         flag = sscanf(file2[i2][j], "%lf", &num2);
         if (flag != 1) {
            cout << "ERROR in reading number from second file" << endl;
            exit(1);
         }
         outputdata[i][j] = num1 - num2;
      }
   }

   colcount = outputdata[0].getSize();
   int ocounter = 0;
   for (i=0; i<file1.getNumLines(); i++) {
      if (file1[i].getType() == E_humrec_data) {
         for (j=0; j<colcount; j++) {
            cout << outputdata[ocounter][j];
            if (j < colcount - 1) {
               cout << "\t";
            }
         }
         cout << "\n";
         ocounter++;
      } else if (((file1[i].getType()) & 0x0f0000) == E_humrec_data) {
         for (j=0; j<colcount; j++) {
            cout << file1[i][j];
            if (j < colcount - 1) {
               cout << "\t";
            }
         }
         cout << "\n";
      } else {
         cout << file1[i] << "\n";
      }
   }

    return 0;
}



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


//////////////////////////////
//
// getDataLines --
//

void getDataLines(Array& lines, HumdrumFile& infile) {
   int i;
   lines.setSize(infile.getNumLines());
   lines.setSize(0);
   for (i=0; i<infile.getNumLines(); i++) {
      if (infile[i].getType() == E_humrec_data) {
         lines.append(i);
      }
   }
}



//////////////////////////////
//
// checkOptions --
//

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("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, October 2008" << std::endl;
      exit(0);
   } else if (opts.getBoolean("version")) {
      cout << argv[0] << ", version: October 2008" << std::endl;
      cout << "compiled: " << __DATE__ << std::endl;
      exit(0);
   } else if (opts.getBoolean("help")) {
      usage(opts.getCommand());
      exit(0);
   } else if (opts.getBoolean("example")) {
      example();
      exit(0);
   }


   if (options.getArgCount() != 2) {
      usage(options.getCommand());
      exit(1);
   }
	    

}



//////////////////////////////
//
// example --
//

void example(void) {


}



//////////////////////////////
//
// usage --
//

void usage(const char* command) {

}


// md5sum: 4e70d1d7ee9f417b4b854bea8e5d3407 msubtract.cpp [20081011]