Skip to content

Commit 727502e

Browse files
committed
refactor: continue refactoring to use strrchr
1 parent 2eda916 commit 727502e

File tree

11 files changed

+45
-111
lines changed

11 files changed

+45
-111
lines changed

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -376,15 +376,10 @@ 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+
char *pEnd = strrchr(dirbuf, '\\');
380+
if (pEnd != NULL)
381381
{
382-
if( *pEnd == '\\' )
383-
{
384-
*(pEnd + 1) = 0;
385-
break;
386-
}
387-
pEnd--;
382+
*(pEnd + 1) = 0;
388383
}
389384

390385
strcpy(theLogFileNamePrev, dirbuf);

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,10 @@ 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+
char* pEnd = strrchr(buf, '\\');
117+
if (pEnd != NULL)
118118
{
119-
if (*pEnd == '\\')
120-
{
121-
*pEnd = 0;
122-
break;
123-
}
124-
--pEnd;
119+
*pEnd = 0;
125120
}
126121
strlcat(buf, "\\Data\\INI\\MemoryPools.ini", ARRAY_SIZE(buf));
127122

Core/Tools/MapCacheBuilder/Source/WinMain.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,9 @@ 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+
char *pEnd = strrchr(buf, '\\');
228+
if (pEnd != NULL) {
229+
*pEnd = 0;
234230
}
235231
::SetCurrentDirectory(buf);
236232

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -739,15 +739,10 @@ 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+
char *pEnd = strrchr(dirbuf, '\\');
743+
if (pEnd != NULL)
744744
{
745-
if( *pEnd == '\\' )
746-
{
747-
*(pEnd + 1) = 0;
748-
break;
749-
}
750-
pEnd--;
745+
*(pEnd + 1) = 0;
751746
}
752747

753748
char curbuf[ _MAX_PATH ];

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

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,13 +1140,9 @@ 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+
char *pEnd = strrchr(buf, '\\');
1144+
if (pEnd != NULL) {
1145+
*pEnd = 0;
11501146
}
11511147
::SetCurrentDirectory(buf);
11521148
if (IDCANCEL==result) {
@@ -1357,13 +1353,9 @@ void ScriptDialog::OnLoad()
13571353
// change it back.
13581354
char buf[_MAX_PATH];
13591355
::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--;
1356+
char *pEnd = strrchr(buf, '\\');
1357+
if (pEnd != NULL) {
1358+
*pEnd = 0;
13671359
}
13681360
CWorldBuilderDoc* pDoc = CWorldBuilderDoc::GetActiveDoc();
13691361
::SetCurrentDirectory(buf);

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,9 @@ BOOL CWorldBuilderApp::InitInstance()
316316
// Set the current directory to the app directory.
317317
char buf[_MAX_PATH];
318318
GetModuleFileName(NULL, buf, sizeof(buf));
319-
char *pEnd = buf + strlen(buf);
320-
while (pEnd != buf) {
321-
if (*pEnd == '\\') {
322-
*pEnd = 0;
323-
break;
324-
}
325-
pEnd--;
319+
char *pEnd = strrchr(buf, '\\');
320+
if (pEnd != NULL) {
321+
*pEnd = 0;
326322
}
327323
::SetCurrentDirectory(buf);
328324

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

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,13 +1359,9 @@ BOOL CWorldBuilderDoc::OnOpenDocument(LPCTSTR lpszPathName)
13591359

13601360
WbApp()->setCurrentDirectory(AsciiString(buf));
13611361
::GetModuleFileName(NULL, buf, sizeof(buf));
1362-
char *pEnd = buf + strlen(buf);
1363-
while (pEnd != buf) {
1364-
if (*pEnd == '\\') {
1365-
*pEnd = 0;
1366-
break;
1367-
}
1368-
pEnd--;
1362+
char *pEnd = strrchr(buf, '\\');
1363+
if (pEnd != NULL) {
1364+
*pEnd = 0;
13691365
}
13701366
::SetCurrentDirectory(buf);
13711367

@@ -2105,15 +2101,10 @@ void CWorldBuilderDoc::OnDumpDocToText(void)
21052101
try {
21062102
char dirbuf[ _MAX_PATH ];
21072103
::GetModuleFileName( NULL, dirbuf, sizeof( dirbuf ) );
2108-
char *pEnd = dirbuf + strlen( dirbuf );
2109-
while( pEnd != dirbuf )
2104+
char *pEnd = strrchr(dirbuf, '\\');
2105+
if (pEnd != NULL)
21102106
{
2111-
if( *pEnd == '\\' )
2112-
{
2113-
*(pEnd + 1) = 0;
2114-
break;
2115-
}
2116-
pEnd--;
2107+
*(pEnd + 1) = 0;
21172108
}
21182109

21192110
char curbuf[ _MAX_PATH ];

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -739,15 +739,10 @@ 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+
char *pEnd = strrchr(dirbuf, '\\');
743+
if (pEnd != NULL)
744744
{
745-
if( *pEnd == '\\' )
746-
{
747-
*(pEnd + 1) = 0;
748-
break;
749-
}
750-
pEnd--;
745+
*(pEnd + 1) = 0;
751746
}
752747

753748
char curbuf[ _MAX_PATH ];

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

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,13 +1295,9 @@ void ScriptDialog::OnSave()
12951295
// change it back.
12961296
char buf[_MAX_PATH];
12971297
::GetModuleFileName(NULL, buf, sizeof(buf));
1298-
char *pEnd = buf + strlen(buf);
1299-
while (pEnd != buf) {
1300-
if (*pEnd == '\\') {
1301-
*pEnd = 0;
1302-
break;
1303-
}
1304-
pEnd--;
1298+
char *pEnd = strrchr(buf, '\\');
1299+
if (pEnd != NULL) {
1300+
*pEnd = 0;
13051301
}
13061302
::SetCurrentDirectory(buf);
13071303
if (IDCANCEL==result) {
@@ -1519,13 +1515,9 @@ void ScriptDialog::OnLoad()
15191515
// change it back.
15201516
char buf[_MAX_PATH];
15211517
::GetModuleFileName(NULL, buf, sizeof(buf));
1522-
char *pEnd = buf + strlen(buf);
1523-
while (pEnd != buf) {
1524-
if (*pEnd == '\\') {
1525-
*pEnd = 0;
1526-
break;
1527-
}
1528-
pEnd--;
1518+
char *pEnd = strrchr(buf, '\\');
1519+
if (pEnd != NULL) {
1520+
*pEnd = 0;
15291521
}
15301522
CWorldBuilderDoc* pDoc = CWorldBuilderDoc::GetActiveDoc();
15311523
::SetCurrentDirectory(buf);

GeneralsMD/Code/Tools/WorldBuilder/src/WorldBuilder.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -328,13 +328,9 @@ BOOL CWorldBuilderApp::InitInstance()
328328
// Set the current directory to the app directory.
329329
char buf[_MAX_PATH];
330330
GetModuleFileName(NULL, buf, sizeof(buf));
331-
char *pEnd = buf + strlen(buf);
332-
while (pEnd != buf) {
333-
if (*pEnd == '\\') {
334-
*pEnd = 0;
335-
break;
336-
}
337-
pEnd--;
331+
char *pEnd = strrchr(buf, '\\');
332+
if (pEnd != NULL) {
333+
*pEnd = 0;
338334
}
339335
::SetCurrentDirectory(buf);
340336

0 commit comments

Comments
 (0)