//
// Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: Mon Mar 25 10:24:23 PST 2002
// Last Modified: Mon Mar 25 10:24:27 PST 2002
// Filename:      ...sig/examples/all/extracttext.cpp
// Web Address:   http://sig.sapp.org/examples/museinfo/score/extracttext.cpp
// Syntax:        C++; museinfo
//
// Description:   Extract text fields from SCORE data files.
//

#include "ScorePage.h"
#include "Options.h"

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

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


void extractText(ScorePage& score, int page, int pageQ, int staffQ, 
      int hposQ, int vposQ);
void printTextLine(const char* string);

// interface variables:
Options options;
int     staffQ    = 0;    // used with the -s option
int     hposQ     = 0;    // used with the -h option
int     vposQ     = 0;    // used with the -v option
int     pageQ     = 0;    // used with the -p option
int     verboseQ  = 0;    // not accessible from command line
int     fontQ     = 0;    // used with the -f option


int main(int argc, char** argv) {
   options.define("s|staff=b",     "print staff of text");
   options.define("h|hpos=b",      "print horizontal position of text");
   options.define("v|vpos=b",      "print vertical position of text");
   options.define("p|page=b",      "print page number of text");
   options.define("f|F|no-font=b", "remove font information from text");
   options.process(argc, argv);
   vposQ     = options.getBoolean("vpos");
   hposQ     = options.getBoolean("hpos");
   staffQ    = options.getBoolean("staff");
   pageQ     = options.getBoolean("page");
   fontQ     = options.getBoolean("no-font");

   if (options.getArgCount() == 0) {
      cout << "Usage: " << argv[0] << " input.mus " << endl;
      exit(1);
   }

   ScorePage score;
   for (int i=1; i<=options.getArgCount(); i++) {
      score.clear();
      score.readFile(options.getArg(i), verboseQ); 
      extractText(score, i, pageQ, staffQ, hposQ, vposQ);
   }

   cout << flush;
   return 0;
}



//////////////////////////////
//
// extractText --
//

void extractText(ScorePage& score, int page, int pageQ, int staffQ, 
      int hposQ, int vposQ) {
   int i;
   for (i=0; i<score.getSize(); i++) {
      if (score[i].isTextItem()) {
         if (pageQ) {
            cout << page << '\t';
         }
         if (staffQ) {
            cout << score[i].getPValue(2) << '\t';
         }
         if (hposQ) {
            cout << score[i].getPValue(3) << '\t';
         }
         if (vposQ) {
            cout << score[i].getPValue(4) << '\t';
         }
         printTextLine(score[i].getTextData());
         cout << '\n';
      }
   }
}



//////////////////////////////
//
// printTextLine -- extract the font information if necessary.
//

void printTextLine(const char* string) {
   if (!fontQ) {
      cout << string;
   } else {
      int i;
      int length = strlen(string);
      for (i=0; i<length; i++) {
         if (string[i] == '_' && isdigit(string[i+1]) && isdigit(string[i+2])) {
            i += 2;
         } else {
            cout << string[i];
         }
      }

   }
}








// md5sum: 6c17cbe8f33d4cd0d83a869595fdb2f9 extracttext.cpp [20050403]