From 2ebcea7fd08e1e934ac399715bb66cf682a0c079 Mon Sep 17 00:00:00 2001
From: Matthias Neeracher <neeri@users.sourceforge.net>
Date: Sat, 5 Apr 2008 18:58:42 +0000
Subject: [PATCH] Fix coda handling

---
 Sources/VLModel.cpp | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/Sources/VLModel.cpp b/Sources/VLModel.cpp
index 9c06b5e..436c3cd 100644
--- a/Sources/VLModel.cpp
+++ b/Sources/VLModel.cpp
@@ -997,7 +997,7 @@ static const uint8_t * sDiv4[] = {
 	NULL,     sDiv4_8,  NULL,     NULL,     NULL,     sDiv4_12};
 
 static const uint8_t sDiv6_2[] = {0, 0, 0, 1, 1, 1};
-static const uint8_t sDiv6_3[] = {0, 0, 1, 1, 2, 2};
+static const uint8_t sDiv6_3[] = {0, 0, 1, 2, 2, 2};
 static const uint8_t sDiv6_4[] = {0, 1, 2, 2, 3, 3};
 static const uint8_t sDiv6_8[] = {0, 2, 3, 4, 6, 7};
 static const uint8_t sDiv6_12[]= {0, 2, 4, 6, 8,10};
@@ -1851,7 +1851,13 @@ VLSong::iterator::iterator(const VLSong & song, bool end)
 	if (end) {
 		fMeasure	= fSong.CountMeasures()-fSong.EmptyEnding();
 	} else {
+		bool 	repeat;
 		fMeasure	= 0;
+		if (fSong.fCoda > 0 
+		 && !fSong.DoesEndRepeat(fMeasure)
+		 && !fSong.DoesEndEnding(fMeasure, &repeat)
+		)
+			fStatus.push_back(Repeat(fMeasure, 2));
 		AdjustStatus();
 	}
 }
@@ -1868,7 +1874,7 @@ void VLSong::iterator::AdjustStatus()
 {
 	int 	times;
 	size_t	volta;
-	bool 	repeat;
+	bool 	repeat = true;
 	if (fSong.DoesEndRepeat(fMeasure)
 	 || (fSong.DoesEndEnding(fMeasure, &repeat) && repeat)
 	) {
@@ -1880,6 +1886,8 @@ void VLSong::iterator::AdjustStatus()
 			
 			return;
 		} 
+	} else if (!repeat) {
+		fStatus.pop_back();
 	}
 	if (fSong.fCoda > 0 && fMeasure==fSong.fGoToCoda)
 		if (fStatus.size() && fStatus.back().fVolta == fStatus.back().fTimes-1) {
@@ -1887,7 +1895,9 @@ void VLSong::iterator::AdjustStatus()
 			
 			return;
 		}
-	if (fMeasure == fSong.CountMeasures()-fSong.EmptyEnding())
+	if (fMeasure == fSong.CountMeasures()-fSong.EmptyEnding() 
+		|| (fSong.fCoda > 0 && fMeasure == fSong.fCoda)
+	)
 		while (fStatus.size())
 			if (++fStatus.back().fVolta < fStatus.back().fTimes) {
 				fMeasure = fStatus.back().fBegin;