src/HumdrumFileBase.cpp
HLp HumdrumFileBase::insertNullDataLine(HumNum timestamp) {
	// for now do a linear search for the insertion point, but later
	// do something more efficient.
	HumdrumFileBase& infile = *this;
	HumNum beforet(-1);
	HumNum aftert(-1);
	int beforei = -1;
	// int afteri = -1;
	HumNum current;
	for (int i=0; i<infile.getLineCount(); i++) {
		if (!infile[i].isData()) {
			continue;
		}
		current = infile[i].getDurationFromStart();
		if (current == timestamp) {
			return &infile[i];
		} else if (current < timestamp) {
			beforet = current;
			beforei = i;
		} else if (current > timestamp) {
			aftert = current;
			// afteri = i;
			break;
		}
	}

	if (beforei < 0) {
		return NULL;
	}
	HLp newline = new HumdrumLine;
	// copyStructure will add null tokens automatically
	newline->copyStructure(&infile[beforei], ".");

	infile.insertLine(beforei+1, newline);

	// Set the timestamp information for inserted line:
	HumNum delta = timestamp - beforet;
	HumNum durationFromStart = infile[beforei].getDurationFromStart() + delta;
	HumNum durationFromBarline = infile[beforei].getDurationFromBarline() + delta;
	HumNum durationToBarline = infile[beforei].getDurationToBarline() - delta;

	newline->m_durationFromStart = durationFromStart;
	newline->m_durationFromBarline = durationFromBarline;
	newline->m_durationToBarline = durationToBarline;

	newline->m_duration = infile[beforei].m_duration - delta;
	infile[beforei].m_duration = delta;

	for (int i=0; i<infile[beforei].getFieldCount(); i++) {
		HTp token = infile.token(beforei, i);
		HTp newtoken = newline->token(i);
		token->insertTokenAfter(newtoken);
	}

	return newline;
}
//////////////////////////////
//
// HumdrumFileBase::insertNullDataLine -- Add a null data line at
//     the given absolute quarter-note timestamp in the file.  If there
//     is already a data line at the given timestamp, then do not create
//     a line and instead return a pointer to the existing line.  Returns
//     NULL if there was a problem.
//
:insertNullDataLine -- Add a null data line at
the given absolute quarter-note timestamp in the file.  If there
is already a data line at the given timestamp, then do not create
a line and instead return a pointer to the existing line.  Returns
NULL if there was a problem.
: insertNullDataLine -- Add a null data line at the given absolute quarter - note timestamp in the file . If there is already a data line at the given timestamp then do not create a line and instead return a pointer to the existing line . Returns NULL if there was a problem .
HLp HumdrumFileBase::insertNullDataLine(HumNum timestamp) {
	HumdrumFileBase& infile = *this;
	HumNum beforet(-1);
	HumNum aftert(-1);
	int beforei = -1;
	HumNum current;
	for (int i=0; i<infile.getLineCount(); i++) {
		if (!infile[i].isData()) {
			continue;
		}
		current = infile[i].getDurationFromStart();
		if (current == timestamp) {
			return &infile[i];
		} else if (current < timestamp) {
			beforet = current;
			beforei = i;
		} else if (current > timestamp) {
			aftert = current;
			break;
		}
	}
	if (beforei < 0) {
		return NULL;
	}
	HLp newline = new HumdrumLine;
	newline->copyStructure(&infile[beforei], ".");
	infile.insertLine(beforei+1, newline);
	HumNum delta = timestamp - beforet;
	HumNum durationFromStart = infile[beforei].getDurationFromStart() + delta;
	HumNum durationFromBarline = infile[beforei].getDurationFromBarline() + delta;
	HumNum durationToBarline = infile[beforei].getDurationToBarline() - delta;
	newline->m_durationFromStart = durationFromStart;
	newline->m_durationFromBarline = durationFromBarline;
	newline->m_durationToBarline = durationToBarline;
	newline->m_duration = infile[beforei].m_duration - delta;
	infile[beforei].m_duration = delta;
	for (int i=0; i<infile[beforei].getFieldCount(); i++) {
		HTp token = infile.token(beforei, i);
		HTp newtoken = newline->token(i);
		token->insertTokenAfter(newtoken);
	}
	return newline;
}
HLp HumdrumFileBase :: insertNullDataLine ( HumNum timestamp ) { HumdrumFileBase & infile = * this ; HumNum beforet ( -1 ) ; HumNum aftert ( -1 ) ; int beforei = -1 ; HumNum current ; for ( int i = 0 ; i < infile . getLineCount ( ) ; i ++ ) { if ( ! infile [ i ] . isData ( ) ) { continue ; } current = infile [ i ] . getDurationFromStart ( ) ; if ( current == timestamp ) { return & infile [ i ] ; } else if ( current < timestamp ) { beforet = current ; beforei = i ; } else if ( current > timestamp ) { aftert = current ; break ; } } if ( beforei < 0 ) { return NULL ; } HLp newline = new HumdrumLine ; newline -> copyStructure ( & infile [ beforei ] , " " ) ; infile . insertLine ( beforei + 1 , newline ) ; HumNum delta = timestamp - beforet ; HumNum durationFromStart = infile [ beforei ] . getDurationFromStart ( ) + delta ; HumNum durationFromBarline = infile [ beforei ] . getDurationFromBarline ( ) + delta ; HumNum durationToBarline = infile [ beforei ] . getDurationToBarline ( ) - delta ; newline -> m_durationFromStart = durationFromStart ; newline -> m_durationFromBarline = durationFromBarline ; newline -> m_durationToBarline = durationToBarline ; newline -> m_duration = infile [ beforei ] . m_duration - delta ; infile [ beforei ] . m_duration = delta ; for ( int i = 0 ; i < infile [ beforei ] . getFieldCount ( ) ; i ++ ) { HTp token = infile . token ( beforei , i ) ; HTp newtoken = newline -> token ( i ) ; token -> insertTokenAfter ( newtoken ) ; } return newline ; }
HumdrumFileBase::insertNullDataLine -- Add a null data line at
the given absolute quarter-note timestamp in the file.
HumdrumFileBase :: insertNullDataLine -- Add a null data line at the given absolute quarter - note timestamp in the file .
// for now do a linear search for the insertion point, but later // do something more efficient. // int afteri = -1; // afteri = i; // copyStructure will add null tokens automatically // Set the timestamp information for inserted line: