// // Programmer: Craig Stuart Sapp // Creation Date: Thu Dec 13 14:58:53 PST 2012 // Last Modified: Thu Dec 13 22:56:07 PST 2012 // Filename: ...sig/examples/all/humsplit.cpp // Web Address: http://sig.sapp.org/examples/museinfo/humdrum/humsplit.cpp // Syntax: C++; museinfo // // Description: Splits multiple-segment Humdrum streams into separate files. // #include #include #include /* for stat function in fileExists */ #include #include "humdrum.h" #include "PerlRegularExpression.h" using namespace std; // function declarations void checkOptions (Options& opts, int argc, char* argv[]); void example (void); void usage (const string& command); int getHumdrumSegmentCount (HumdrumStream& streamer); void extractSegment (HumdrumFile& infile, HumdrumStream& streamer, int extractNum); void saveToDisk (HumdrumFile& infile, int count); int fileExists (const string& filename); // global variables Options options; // database for command-line arguments int countQ = 0; // used with -c option int extractQ = 0; // used with -x option int extractNum = 0; // used with -x option int segmentLabelQ = 0; // used with -s option int overwriteQ = 0; // used with -O option (capital O) int directoryQ = 0; // used with -d option string Directory; // used with -d option int extensionQ = 0; // used with -e option string Extension; // used with -e option int prefixQ = 0; // used with -p option string Prefix; // used with -p option int Width = 5; // used with -w option int Start = 1; // used with -n option /////////////////////////////////////////////////////////////////////////// int main(int argc, char* argv[]) { checkOptions(options, argc, argv); HumdrumStream streamer(options); HumdrumFile infile; if (countQ) { int count = getHumdrumSegmentCount(streamer); cout << count << endl; exit(0); } if (extractQ) { extractSegment(infile, streamer, extractNum); if (segmentLabelQ) { infile.printSegmentLabel(cout); } cout << infile; exit(0); } int filecounter = Start; while (streamer.read(infile)) { saveToDisk(infile, filecounter++); } return 0; } /////////////////////////////////////////////////////////////////////////// ////////////////////////////// // // saveToDisk -- // void saveToDisk(HumdrumFile& infile, int count) { string writename; writename = infile.getFilename(); PerlRegularExpression pre; pre.sar(writename, ".*/", ""); // remove old directory path (or URI) if (writename.size() == 0) { // There is no file, so create a synthetic one: count.humsplit char filename[1024] = {0}; char format[32] = {0}; sprintf(format, "%s0%d.humsplit", "%", Width); sprintf(filename, format, count); writename = filename; } if (extensionQ) { // remove old filename extension and replace with new one pre.sar(writename, "\\.[^.]*$", ""); pre.sar(writename, "$", Extension); } if (prefixQ) { // prefix to give to filename (useful for auto-naming). pre.sar(writename, "^", Prefix); } if (directoryQ) { // place in new directory if requested pre.sar(Directory, "/*$", "/"); pre.sar(writename, "^", Directory); } if (!overwriteQ && fileExists(writename)) { // print warning if trying to overwrite an existing file cerr << "Warning: " << writename << " already exists. Skipping..." << endl; return; } ofstream output; output.open(writename.c_str()); if (!output.is_open()) { cerr << "Warning: " << writename << " could not be written. Skipping..." << endl; } if (segmentLabelQ) { // preserve segment label infile.printSegmentLabel(output); } int i; int start = 0; if (strncmp(infile[0][0], "!!!!SEGMENT", strlen("!!!!SEGMENT")) == 0) { start = 1; } for (i=start; i