Skip to content

Commit 5e30751

Browse files
committed
Replace last occurrence of strtok (closes Warzone2100#4241)
1 parent a4f0e48 commit 5e30751

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

src/seqdisp.cpp

+16-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This file is part of Warzone 2100.
33
Copyright (C) 1999-2004 Eidos Interactive
4-
Copyright (C) 2005-2020 Warzone 2100 Project
4+
Copyright (C) 2005-2025 Warzone 2100 Project
55
66
Warzone 2100 is free software; you can redistribute it and/or modify
77
it under the terms of the GNU General Public License as published by
@@ -847,15 +847,13 @@ bool seq_AddTextForVideo(const char *pText, SDWORD xOffset, SDWORD yOffset, doub
847847
return true;
848848
}
849849

850-
851850
static bool seq_AddTextFromFile(const char *pTextName, SEQ_TEXT_POSITIONING textJustification)
852851
{
853852
char aTextName[MAX_STR_LENGTH];
854-
char *pTextBuffer, *pCurrentLine, *pText;
853+
char *pTextBuffer;
855854
UDWORD fileSize;
856855
SDWORD xOffset, yOffset;
857856
double startTime, endTime;
858-
const char *seps = "\n";
859857

860858
// NOTE: The original game never had a fullscreen mode for FMVs on >640x480 screens.
861859
// They would just use double sized videos, and move the text to that area.
@@ -871,35 +869,33 @@ static bool seq_AddTextFromFile(const char *pTextName, SEQ_TEXT_POSITIONING text
871869
}
872870

873871
pTextBuffer = fileLoadBuffer;
874-
pCurrentLine = strtok(pTextBuffer, seps);
875-
while (pCurrentLine != nullptr)
872+
std::istringstream stream(pTextBuffer);
873+
std::string pCurrentLine;
874+
875+
while (std::getline(stream, pCurrentLine))
876876
{
877-
if (*pCurrentLine != '/')
877+
if (!pCurrentLine.empty() && pCurrentLine[0] != '/')
878878
{
879-
if (sscanf(pCurrentLine, "%d %d %lf %lf", &xOffset, &yOffset, &startTime, &endTime) == 4)
879+
std::istringstream lineStream(pCurrentLine);
880+
if (lineStream >> xOffset >> yOffset >> startTime >> endTime)
880881
{
881882
// Since all the positioning was hardcoded to specific values, we now calculate the
882883
// ratio of our screen, compared to what the game expects and multiply that to x, y.
883884
// This makes the text always take up the full screen, instead of original style.
884885
xOffset = static_cast<SDWORD>((double)pie_GetVideoBufferWidth() / 640. * (double)xOffset);
885886
yOffset = static_cast<SDWORD>((double)pie_GetVideoBufferHeight() / 480. * (double)yOffset);
886887
//get the text
887-
pText = strrchr(pCurrentLine, '"');
888-
ASSERT(pText != nullptr, "error parsing text file");
889-
if (pText != nullptr)
890-
{
891-
*pText = (UBYTE)0;
892-
}
893-
pText = strchr(pCurrentLine, '"');
894-
ASSERT(pText != nullptr, "error parsing text file");
895-
if (pText != nullptr)
888+
size_t firstQuote = pCurrentLine.find('"');
889+
size_t lastQuote = pCurrentLine.rfind('"');
890+
891+
ASSERT(firstQuote != std::string::npos && lastQuote != std::string::npos && firstQuote < lastQuote, "error parsing text file");
892+
if (firstQuote != std::string::npos && lastQuote != std::string::npos && firstQuote < lastQuote)
896893
{
897-
seq_AddTextForVideo(_(&pText[1]), xOffset, yOffset, startTime, endTime, textJustification);
894+
std::string text = pCurrentLine.substr(firstQuote + 1, lastQuote - firstQuote - 1);
895+
seq_AddTextForVideo(_(text.c_str()), xOffset, yOffset, startTime, endTime, textJustification);
898896
}
899897
}
900898
}
901-
//get next line
902-
pCurrentLine = strtok(nullptr, seps);
903899
}
904900
return true;
905901
}

0 commit comments

Comments
 (0)