Skip to content

Commit 10f130a

Browse files
authored
refactor: Replace custom strrchr implementations with standard function (#1782)
1 parent 38a4202 commit 10f130a

File tree

21 files changed

+50
-191
lines changed

21 files changed

+50
-191
lines changed

Core/GameEngine/Source/Common/System/Debug.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -376,15 +376,9 @@ void DebugInit(int flags)
376376

377377
char dirbuf[ _MAX_PATH ];
378378
::GetModuleFileName( NULL, dirbuf, sizeof( dirbuf ) );
379-
char *pEnd = dirbuf + strlen( dirbuf );
380-
while( pEnd != dirbuf )
379+
if (char *pEnd = strrchr(dirbuf, '\\'))
381380
{
382-
if( *pEnd == '\\' )
383-
{
384-
*(pEnd + 1) = 0;
385-
break;
386-
}
387-
pEnd--;
381+
*(pEnd + 1) = 0;
388382
}
389383

390384
strcpy(theLogFileNamePrev, dirbuf);

Core/GameEngine/Source/Common/System/GameMemoryInit.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,9 @@ void userMemoryManagerInitPools()
113113
// we expect. so do it the hard way.
114114
char buf[_MAX_PATH];
115115
::GetModuleFileName(NULL, buf, sizeof(buf));
116-
char* pEnd = buf + strlen(buf);
117-
while (pEnd != buf)
116+
if (char* pEnd = strrchr(buf, '\\'))
118117
{
119-
if (*pEnd == '\\')
120-
{
121-
*pEnd = 0;
122-
break;
123-
}
124-
--pEnd;
118+
*pEnd = 0;
125119
}
126120
strlcat(buf, "\\Data\\INI\\MemoryPools.ini", ARRAY_SIZE(buf));
127121

Core/Tools/MapCacheBuilder/Source/WinMain.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,8 @@ Int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
224224
// Set the current directory to the app directory.
225225
char buf[_MAX_PATH];
226226
GetModuleFileName(NULL, buf, sizeof(buf));
227-
char *pEnd = buf + strlen(buf);
228-
while (pEnd != buf) {
229-
if (*pEnd == '\\') {
230-
*pEnd = 0;
231-
break;
232-
}
233-
pEnd--;
227+
if (char *pEnd = strrchr(buf, '\\')) {
228+
*pEnd = 0;
234229
}
235230
::SetCurrentDirectory(buf);
236231

Generals/Code/GameEngine/Source/Common/MiniLog.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,9 @@ LogClass::LogClass(const char *fname)
3636
{
3737
char buffer[ _MAX_PATH ];
3838
GetModuleFileName( NULL, buffer, sizeof( buffer ) );
39-
char *pEnd = buffer + strlen( buffer );
40-
while( pEnd != buffer )
39+
if (char *pEnd = strrchr(buffer, '\\'))
4140
{
42-
if( *pEnd == '\\' )
43-
{
44-
*pEnd = 0;
45-
break;
46-
}
47-
pEnd--;
41+
*pEnd = 0;
4842
}
4943
// TheSuperHackers @fix Caball009 03/06/2025 Don't use AsciiString here anymore because its memory allocator may not have been initialized yet.
5044
const std::string fullPath = std::string(buffer) + "\\" + fname;

Generals/Code/GameEngineDevice/Source/W3DDevice/GameClient/Drawable/Draw/W3DModelDraw.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,9 @@ LogClass::LogClass(const char *fname)
9999
{
100100
char buffer[ _MAX_PATH ];
101101
GetModuleFileName( NULL, buffer, sizeof( buffer ) );
102-
char *pEnd = buffer + strlen( buffer );
103-
while( pEnd != buffer )
102+
if (char *pEnd = strrchr(buffer, '\\'))
104103
{
105-
if( *pEnd == '\\' )
106-
{
107-
*pEnd = 0;
108-
break;
109-
}
110-
pEnd--;
104+
*pEnd = 0;
111105
}
112106
// TheSuperHackers @fix Caball009 03/06/2025 Don't use AsciiString here anymore because its memory allocator may not have been initialized yet.
113107
const std::string fullPath = std::string(buffer) + "\\" + fname;

Generals/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,9 @@ StatDumpClass::StatDumpClass( const char *fname )
147147
{
148148
char buffer[ _MAX_PATH ];
149149
GetModuleFileName( NULL, buffer, sizeof( buffer ) );
150-
char *pEnd = buffer + strlen( buffer );
151-
while( pEnd != buffer )
150+
if (char *pEnd = strrchr(buffer, '\\'))
152151
{
153-
if( *pEnd == '\\' )
154-
{
155-
*pEnd = 0;
156-
break;
157-
}
158-
pEnd--;
152+
*pEnd = 0;
159153
}
160154
// TheSuperHackers @fix Caball009 03/06/2025 Don't use AsciiString here anymore because its memory allocator may not have been initialized yet.
161155
const std::string fullPath = std::string(buffer) + "\\" + fname;

Generals/Code/Main/WinMain.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -774,15 +774,9 @@ Int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
774774
/// @todo remove this force set of working directory later
775775
Char buffer[ _MAX_PATH ];
776776
GetModuleFileName( NULL, buffer, sizeof( buffer ) );
777-
Char *pEnd = buffer + strlen( buffer );
778-
while( pEnd != buffer )
777+
if (Char *pEnd = strrchr(buffer, '\\'))
779778
{
780-
if( *pEnd == '\\' )
781-
{
782-
*pEnd = 0;
783-
break;
784-
}
785-
pEnd--;
779+
*pEnd = 0;
786780
}
787781
::SetCurrentDirectory(buffer);
788782

Generals/Code/Tools/GUIEdit/Source/WinMain.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,9 @@ Int APIENTRY WinMain(HINSTANCE hInstance,
187187
/// @todo remove this force set of working directory later
188188
Char buffer[ _MAX_PATH ];
189189
GetModuleFileName( NULL, buffer, sizeof( buffer ) );
190-
Char *pEnd = buffer + strlen( buffer );
191-
while( pEnd != buffer )
190+
if (Char *pEnd = strrchr(buffer, '\\'))
192191
{
193-
if( *pEnd == '\\' )
194-
{
195-
*pEnd = 0;
196-
break;
197-
}
198-
pEnd--;
192+
*pEnd = 0;
199193
}
200194
::SetCurrentDirectory(buffer);
201195

Generals/Code/Tools/WorldBuilder/src/BuildList.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -739,15 +739,9 @@ void BuildList::OnExport()
739739
try {
740740
char dirbuf[ _MAX_PATH ];
741741
::GetModuleFileName( NULL, dirbuf, sizeof( dirbuf ) );
742-
char *pEnd = dirbuf + strlen( dirbuf );
743-
while( pEnd != dirbuf )
742+
if (char *pEnd = strrchr(dirbuf, '\\'))
744743
{
745-
if( *pEnd == '\\' )
746-
{
747-
*(pEnd + 1) = 0;
748-
break;
749-
}
750-
pEnd--;
744+
*(pEnd + 1) = 0;
751745
}
752746

753747
char curbuf[ _MAX_PATH ];

Generals/Code/Tools/WorldBuilder/src/ScriptDialog.cpp

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,13 +1140,8 @@ void ScriptDialog::OnSave()
11401140
// change it back.
11411141
char buf[_MAX_PATH];
11421142
::GetModuleFileName(NULL, buf, sizeof(buf));
1143-
char *pEnd = buf + strlen(buf);
1144-
while (pEnd != buf) {
1145-
if (*pEnd == '\\') {
1146-
*pEnd = 0;
1147-
break;
1148-
}
1149-
pEnd--;
1143+
if (char *pEnd = strrchr(buf, '\\')) {
1144+
*pEnd = 0;
11501145
}
11511146
::SetCurrentDirectory(buf);
11521147
if (IDCANCEL==result) {
@@ -1357,13 +1352,8 @@ void ScriptDialog::OnLoad()
13571352
// change it back.
13581353
char buf[_MAX_PATH];
13591354
::GetModuleFileName(NULL, buf, sizeof(buf));
1360-
char *pEnd = buf + strlen(buf);
1361-
while (pEnd != buf) {
1362-
if (*pEnd == '\\') {
1363-
*pEnd = 0;
1364-
break;
1365-
}
1366-
pEnd--;
1355+
if (char *pEnd = strrchr(buf, '\\')) {
1356+
*pEnd = 0;
13671357
}
13681358
CWorldBuilderDoc* pDoc = CWorldBuilderDoc::GetActiveDoc();
13691359
::SetCurrentDirectory(buf);

0 commit comments

Comments
 (0)