Skip to content

Commit 27463b1

Browse files
derwin12dkulp
authored andcommitted
Make text effect from file thread safe
1 parent 7cd426f commit 27463b1

1 file changed

Lines changed: 41 additions & 29 deletions

File tree

xLights/effects/TextEffect.cpp

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -459,25 +459,29 @@ void TextEffect::Render(Effect *effect, const SettingsMap &SettingsMap, RenderBu
459459

460460
if (text.IsEmpty())
461461
{
462-
if (FileExists(filename))
463-
{
464-
wxTextFile f(filename);
465-
f.Open();
466-
int i = 0;
467-
text = f.GetFirstLine() + "\n";
468-
while (!f.Eof() && i < MAXTEXTLINES)
469-
{
470-
text += f.GetNextLine() + "\n";
471-
i++;
472-
}
473-
if (!text.IsEmpty())
474-
{
475-
while (!text.IsEmpty() && text.Last() == '\n' )
476-
{
477-
text = text.BeforeLast('\n');
462+
if (FileExists(filename)) {
463+
wxFile file(filename);
464+
if (file.IsOpened()) {
465+
wxString fileContent;
466+
if (file.ReadAll(&fileContent)) {
467+
wxArrayString lines = wxSplit(fileContent, '\n');
468+
469+
text.Clear();
470+
int lineCount = std::min((int)lines.GetCount(), MAXTEXTLINES);
471+
472+
for (int i = 0; i < lineCount; i++) {
473+
if (i > 0) {
474+
text += "\n";
475+
}
476+
text += lines[i];
477+
}
478+
479+
while (!text.IsEmpty() && text.Last() == '\n') {
480+
text.RemoveLast();
481+
}
478482
}
483+
file.Close();
479484
}
480-
f.Close();
481485
}
482486
else
483487
{
@@ -1537,20 +1541,28 @@ void TextEffect::RenderXLText(Effect* effect, const SettingsMap& settings, Rende
15371541

15381542
if (text == "") {
15391543
if (FileExists(filename)) {
1540-
wxTextFile f(filename);
1541-
f.Open();
1542-
int i = 0;
1543-
text = f.GetFirstLine() + "\n";
1544-
while (!f.Eof() && i < MAXTEXTLINES) {
1545-
text += f.GetNextLine() + "\n";
1546-
i++;
1547-
}
1548-
if (text != "") {
1549-
while (text.Last() == '\n') {
1550-
text = text.BeforeLast('\n');
1544+
wxFile file(filename);
1545+
if (file.IsOpened()) {
1546+
wxString fileContent;
1547+
if (file.ReadAll(&fileContent)) {
1548+
wxArrayString lines = wxSplit(fileContent, '\n');
1549+
1550+
text.Clear();
1551+
int lineCount = std::min((int)lines.GetCount(), MAXTEXTLINES);
1552+
1553+
for (int i = 0; i < lineCount; i++) {
1554+
if (i > 0) {
1555+
text += "\n";
1556+
}
1557+
text += lines[i];
1558+
}
1559+
1560+
while (!text.IsEmpty() && text.Last() == '\n') {
1561+
text.RemoveLast();
1562+
}
15511563
}
1564+
file.Close();
15521565
}
1553-
f.Close();
15541566
}
15551567
else if (lyricTrack != "") {
15561568
Element* t = nullptr;

0 commit comments

Comments
 (0)