// // Copyright 2010 by Craig Stuart Sapp, All Rights Reserved. // Programmer: Craig Stuart Sapp // Creation Date: Thu Jun 17 13:27:39 PDT 2010 // Last Modified: Thu Jun 17 13:27:46 PDT 2010 // Filename: ...sig/src/sigInfo/MuseDataSet.cpp // Web Address: http://sig.sapp.org/src/sigInfo/MuseDataSet.cpp // Syntax: C++ // // Description: A class that stores a collection of MuseDataSet files // representing parts for the same score. // #include "MuseDataSet.h" #include "PerlRegularExpression.h" #include #ifndef OLDCPP #include #include #define SSTREAM stringstream #define CSTRING str().c_str() using namespace std; #else #ifdef VISUAL #include /* for Windows 95 */ #else #include #endif #include #define SSTREAM strstream #define CSTRING str() #endif /////////////////////////////////////////////////////////////////////////// // // MuseDataSet class functions -- // ////////////////////////////// // // MuseDataSet::MuseDataSet -- // MuseDataSet::MuseDataSet (void) { part.setSize(100); part.setSize(0); part.setGrowth(1000); } ////////////////////////////// // // MuseDataSet::clear -- Remove contents of object. // void MuseDataSet::clear(void) { int i; for (i=0; iread(filename); md->setFilename(filename); return appendPart(md); } int MuseDataSet::readPart(istream& input) { MuseData* md = new MuseData; md->read(input); return appendPart(md); } ////////////////////////////// // // MuseDataSet::read -- read potentially Multiple parts from a single file. // First clear the contents of any previous part data. // void MuseDataSet::read(const char* filename) { MuseDataSet::clear(); #ifndef OLDCPP ifstream infile(filename, ios::in); #else ifstream infile(filename, ios::in | ios::nocreate); #endif MuseDataSet::read(infile); } void MuseDataSet::read(istream& infile) { Array datalines; datalines.setSize(1000000); datalines.setGrowth(5000000); datalines.setSize(0); string thing; while (!infile.eof()) { getline(infile, thing); if (infile.eof() && (thing.length() == 0)) { // last line was not terminated by a newline character break; } datalines.append(thing); } Array startindex; Array stopindex; startindex.setSize(0); stopindex.setSize(0); analyzePartSegments(startindex, stopindex, datalines); int i, j; SSTREAM* sstream; MuseData* md; for (i=0; iread(*sstream); appendPart(md); delete sstream; } } ////////////////////////////// // // MuseDataSet::appendPart -- append a MuseData pointer to the end of the // parts list and return the index number of the new part. // int MuseDataSet::appendPart(MuseData* musedata) { int index = part.getSize(); part.setSize(part.getSize()+1); part[index] = musedata; return index; } ////////////////////////////// // // MuseData::analyzePartSegments -- Calculate the starting line index // and the ending line index for each part in the input. // void MuseDataSet::analyzePartSegments(Array& startindex, Array& stopindex, Array lines) { startindex.setSize(1000); startindex.setSize(0); startindex.setGrowth(1000); stopindex.setSize(1000); stopindex.setSize(0); stopindex.setGrowth(1000); Array types; // MuseData& thing = *this; int i, j; types.setSize(lines.getSize()); types.allowGrowth(0); types.setAll(E_muserec_unknown); // first identify lines which are multi-line comments so they will // not cause confusion in the next step int commentstate = 0; for (i=0; i groupmemberships(1000); groupmemberships.setSize(0); groupmemberships.setGrowth(1000); int len = strlen("Group memberships:"); for (i=0; i=0; j--) { if (j < 0) { break; } if ((types[j] == E_muserec_comment_line) || (types[j] == E_muserec_comment_toggle)) { j--; continue; } if (j < 0) { break; } headerline--; if (headerline == 0) { while ((j>= 0) && (lines[j][0] == '@')) { j--; } value = j+1; //value = j+2; found = 1; startindex.append(value); break; } if ((j >= 0) && (headerline == 0)) { value = j+1; found = 1; startindex.append(value); break; } if (j<0) { value = 0; found = 1; startindex.append(value); continue; } switch (headerline) { case 11: types[j] = E_muserec_header_11; break; case 10: types[j] = E_muserec_header_10; break; case 9: types[j] = E_muserec_header_9; break; case 8: types[j] = E_muserec_header_8; break; case 7: types[j] = E_muserec_header_7; break; case 6: types[j] = E_muserec_header_6; break; case 5: types[j] = E_muserec_header_5; break; case 4: types[j] = E_muserec_header_4; break; case 3: types[j] = E_muserec_header_3; break; case 2: types[j] = E_muserec_header_2; break; case 1: types[j] = E_muserec_header_1; break; } } if (!found) { value = 0; startindex.append(value); } } // now calculate the stopindexes: stopindex.setSize(startindex.getSize()); stopindex[stopindex.getSize()-1] = lines.getSize()-1; for (i=0; i part.getSize()-1) { cerr << "Trying to delete a non-existent part" << endl; exit(1); } delete part[index]; int i; for (i=index+1; icleanLineEndings(); } } /////////////////////////////////////////////////////////////////////////// ////////////////////////////// // // operator<< -- print out all parts in sequential order // ostream& operator<<(ostream& out, MuseDataSet& musedataset) { int i, j; for (i=0; i