Skip to content

Commit 03d4df1

Browse files
committed
refactor: combine lightweight excerpt detection and reading
1 parent 286d8ac commit 03d4df1

File tree

1 file changed

+15
-41
lines changed

1 file changed

+15
-41
lines changed

src/engraving/rw/mscloader.cpp

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -53,47 +53,15 @@ using namespace muse::io;
5353
using namespace mu::engraving;
5454
using namespace mu::engraving::rw;
5555

56-
//---------------------------------------------------------
57-
// isLightweightExcerpt
58-
/// Check if excerpt data represents a lightweight excerpt
59-
/// by looking for the <lightweight>true</lightweight> element
60-
//---------------------------------------------------------
61-
62-
static bool isLightweightExcerpt(const ByteArray& excerptData)
63-
{
64-
XmlReader xml(excerptData);
65-
while (xml.readNextStartElement()) {
66-
if (xml.name() == "museScore") {
67-
while (xml.readNextStartElement()) {
68-
if (xml.name() == "Score") {
69-
while (xml.readNextStartElement()) {
70-
if (xml.name() == "lightweight") {
71-
return xml.readText().toInt() != 0;
72-
} else {
73-
xml.skipCurrentElement();
74-
}
75-
}
76-
} else {
77-
xml.skipCurrentElement();
78-
}
79-
}
80-
} else {
81-
xml.skipCurrentElement();
82-
}
83-
}
84-
return false;
85-
}
86-
8756
//---------------------------------------------------------
8857
// readLightweightExcerpt
89-
/// Read a lightweight excerpt that has no full excerptScore
90-
/// Restores name, parts references, and tracks mapping
58+
/// Read a lightweight excerpt (no full excerptScore).
59+
/// Returns nullptr if not a lightweight excerpt.
9160
//---------------------------------------------------------
9261

9362
static Excerpt* readLightweightExcerpt(MasterScore* masterScore, const ByteArray& excerptData, const String& fileName)
9463
{
95-
Excerpt* excerpt = new Excerpt(masterScore);
96-
excerpt->setFileName(fileName);
64+
Excerpt* excerpt = nullptr;
9765
TracksMap tracksMap;
9866

9967
XmlReader xml(excerptData);
@@ -104,7 +72,15 @@ static Excerpt* readLightweightExcerpt(MasterScore* masterScore, const ByteArray
10472
while (xml.readNextStartElement()) {
10573
const AsciiStringView tag = xml.name();
10674
if (tag == "lightweight") {
107-
xml.readText(); // consume the value
75+
if (xml.readText().toInt() != 0) {
76+
excerpt = new Excerpt(masterScore);
77+
excerpt->setFileName(fileName);
78+
} else {
79+
return nullptr;
80+
}
81+
} else if (!excerpt) {
82+
// Not lightweight - first element must be <lightweight>
83+
return nullptr;
10884
} else if (tag == "name") {
10985
excerpt->setName(xml.readText(), false);
11086
} else if (tag == "Tracklist") {
@@ -118,7 +94,6 @@ static Excerpt* readLightweightExcerpt(MasterScore* masterScore, const ByteArray
11894
excerpt->setInitialPartId(ID(xml.readText().toStdString()));
11995
} else if (tag == "Part") {
12096
ID partId(static_cast<uint64_t>(xml.intAttribute("id", 0)));
121-
// Find the part in master score by ID
12297
for (Part* part : masterScore->parts()) {
12398
if (part->id() == partId) {
12499
excerpt->parts().push_back(part);
@@ -139,7 +114,7 @@ static Excerpt* readLightweightExcerpt(MasterScore* masterScore, const ByteArray
139114
}
140115
}
141116

142-
if (!tracksMap.empty()) {
117+
if (excerpt && !tracksMap.empty()) {
143118
excerpt->setTracksMapping(tracksMap);
144119
}
145120

@@ -255,9 +230,8 @@ Ret MscLoader::loadMscz(MasterScore* masterScore, const MscReader& mscReader, Se
255230
for (const String& excerptFileName : excerptFileNames) {
256231
ByteArray excerptData = mscReader.readExcerptFile(excerptFileName);
257232

258-
// Check if this is a lightweight excerpt (potential excerpt without full score)
259-
if (isLightweightExcerpt(excerptData)) {
260-
Excerpt* ex = readLightweightExcerpt(masterScore, excerptData, excerptFileName);
233+
// Try to read as lightweight excerpt first
234+
if (Excerpt* ex = readLightweightExcerpt(masterScore, excerptData, excerptFileName)) {
261235
masterScore->addLightweightExcerpt(ex);
262236
continue;
263237
}

0 commit comments

Comments
 (0)