Skip to content

Commit cf55e87

Browse files
authored
refactor: Simplify path string formatting in World Builder (#1759)
1 parent 5dff203 commit cf55e87

File tree

6 files changed

+18
-59
lines changed

6 files changed

+18
-59
lines changed

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

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -118,21 +118,12 @@ void OpenMap::populateMapListbox( Bool systemMaps )
118118
strcpy(dirBuf, "Maps\\");
119119
else
120120
{
121-
strcpy(dirBuf, TheGlobalData->getPath_UserData().str());
122-
strlcat(dirBuf, "Maps\\", ARRAY_SIZE(dirBuf));
123-
}
124-
125-
int len = strlen(dirBuf);
126-
127-
if (len > 0 && dirBuf[len - 1] != '\\') {
128-
dirBuf[len++] = '\\';
129-
dirBuf[len] = 0;
121+
snprintf(dirBuf, ARRAY_SIZE(dirBuf), "%sMaps\\", TheGlobalData->getPath_UserData().str());
130122
}
131123
CListBox *pList = (CListBox *)this->GetDlgItem(IDC_OPEN_LIST);
132124
if (pList == NULL) return;
133125
pList->ResetContent();
134-
strcpy(findBuf, dirBuf);
135-
strlcat(findBuf, "*.*", ARRAY_SIZE(findBuf));
126+
snprintf(findBuf, ARRAY_SIZE(findBuf), "%s*.*", dirBuf);
136127

137128
Bool found = false;
138129

@@ -145,11 +136,7 @@ void OpenMap::populateMapListbox( Bool systemMaps )
145136
continue;
146137
}
147138

148-
strcpy(fileBuf, dirBuf);
149-
strlcat(fileBuf, findData.cFileName, ARRAY_SIZE(findBuf));
150-
strlcat(fileBuf, "\\", ARRAY_SIZE(findBuf));
151-
strlcat(fileBuf, findData.cFileName, ARRAY_SIZE(findBuf));
152-
strlcat(fileBuf, ".map", ARRAY_SIZE(findBuf));
139+
snprintf(fileBuf, ARRAY_SIZE(fileBuf), "%s%s\\%s.map", dirBuf, findData.cFileName, findData.cFileName);
153140
try {
154141
CFileStatus status;
155142
if (CFile::GetStatus(fileBuf, status)) {

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ void SaveMap::populateMapListbox( Bool systemMaps )
138138
CListBox *pList = (CListBox *)this->GetDlgItem(IDC_SAVE_LIST);
139139
if (pList == NULL) return;
140140
pList->ResetContent();
141-
strcpy(findBuf, dirBuf);
142-
strlcat(findBuf, "*.*", ARRAY_SIZE(findBuf));
141+
snprintf(findBuf, ARRAY_SIZE(findBuf), "%s*.*", dirBuf);
143142

144143
hFindFile = FindFirstFile(findBuf, &findData);
145144
if (hFindFile != INVALID_HANDLE_VALUE) {
@@ -149,11 +148,7 @@ void SaveMap::populateMapListbox( Bool systemMaps )
149148
if ((findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
150149
continue;
151150
}
152-
strcpy(fileBuf, dirBuf);
153-
strlcat(fileBuf, findData.cFileName, ARRAY_SIZE(fileBuf));
154-
strlcat(fileBuf, "\\", ARRAY_SIZE(fileBuf));
155-
strlcat(fileBuf, findData.cFileName, ARRAY_SIZE(fileBuf));
156-
strlcat(fileBuf, ".map", ARRAY_SIZE(fileBuf));
151+
snprintf(fileBuf, ARRAY_SIZE(fileBuf), "%s%s\\%s.map", dirBuf, findData.cFileName, findData.cFileName);
157152
try {
158153
CFileStatus status;
159154
if (CFile::GetStatus(fileBuf, status)) {
@@ -169,7 +164,7 @@ void SaveMap::populateMapListbox( Bool systemMaps )
169164
}
170165
CEdit *pEdit = (CEdit*)GetDlgItem(IDC_SAVE_MAP_EDIT);
171166
if (pEdit != NULL) {
172-
strcpy(fileBuf, m_pInfo->filename);
167+
strlcpy(fileBuf, m_pInfo->filename, ARRAY_SIZE(fileBuf));
173168
Int len = strlen(fileBuf);
174169
if (len>4 && stricmp(".map", fileBuf+(len-4)) == 0) {
175170
// strip of the .map

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -414,20 +414,18 @@ void WorldHeightMapEdit::loadBaseImages(void)
414414
void WorldHeightMapEdit::loadDirectoryOfImages(const char *pFilePath)
415415
{
416416
char dirBuf[_MAX_PATH];
417-
char findBuf[_MAX_PATH];
418417
char fileBuf[_MAX_PATH];
419418

420-
strcpy(dirBuf, pFilePath);
419+
strlcpy(dirBuf, pFilePath, ARRAY_SIZE(dirBuf));
421420
int len = strlen(dirBuf);
422421

423422
if (len > 0 && dirBuf[len - 1] != '\\') {
424423
dirBuf[len++] = '\\';
425424
dirBuf[len] = 0;
426425
}
427-
strcpy(findBuf, dirBuf);
428426

429427
FilenameList filenameList;
430-
TheFileSystem->getFileListInDirectory(AsciiString(findBuf), AsciiString("*.*"), filenameList, TRUE);
428+
TheFileSystem->getFileListInDirectory(AsciiString(dirBuf), AsciiString("*.*"), filenameList, TRUE);
431429

432430
if (filenameList.size() == 0) {
433431
return;
@@ -436,8 +434,7 @@ void WorldHeightMapEdit::loadDirectoryOfImages(const char *pFilePath)
436434
do {
437435
AsciiString filename = *it;
438436

439-
strcpy(fileBuf, dirBuf);
440-
strcat(fileBuf, filename.str());
437+
snprintf(fileBuf, ARRAY_SIZE(fileBuf), "%s%s", dirBuf, filename.str());
441438
loadBitmap(fileBuf, filename.str());
442439

443440
++it;

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

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -118,21 +118,12 @@ void OpenMap::populateMapListbox( Bool systemMaps )
118118
strcpy(dirBuf, "Maps\\");
119119
else
120120
{
121-
strcpy(dirBuf, TheGlobalData->getPath_UserData().str());
122-
strlcat(dirBuf, "Maps\\", ARRAY_SIZE(dirBuf));
123-
}
124-
125-
int len = strlen(dirBuf);
126-
127-
if (len > 0 && dirBuf[len - 1] != '\\') {
128-
dirBuf[len++] = '\\';
129-
dirBuf[len] = 0;
121+
snprintf(dirBuf, ARRAY_SIZE(dirBuf), "%sMaps\\", TheGlobalData->getPath_UserData().str());
130122
}
131123
CListBox *pList = (CListBox *)this->GetDlgItem(IDC_OPEN_LIST);
132124
if (pList == NULL) return;
133125
pList->ResetContent();
134-
strcpy(findBuf, dirBuf);
135-
strlcat(findBuf, "*.*", ARRAY_SIZE(findBuf));
126+
snprintf(findBuf, ARRAY_SIZE(findBuf), "%s*.*", dirBuf);
136127

137128
Bool found = false;
138129

@@ -145,11 +136,7 @@ void OpenMap::populateMapListbox( Bool systemMaps )
145136
continue;
146137
}
147138

148-
strcpy(fileBuf, dirBuf);
149-
strlcat(fileBuf, findData.cFileName, ARRAY_SIZE(findBuf));
150-
strlcat(fileBuf, "\\", ARRAY_SIZE(findBuf));
151-
strlcat(fileBuf, findData.cFileName, ARRAY_SIZE(findBuf));
152-
strlcat(fileBuf, ".map", ARRAY_SIZE(findBuf));
139+
snprintf(fileBuf, ARRAY_SIZE(fileBuf), "%s%s\\%s.map", dirBuf, findData.cFileName, findData.cFileName);
153140
try {
154141
CFileStatus status;
155142
if (CFile::GetStatus(fileBuf, status)) {

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ void SaveMap::populateMapListbox( Bool systemMaps )
138138
CListBox *pList = (CListBox *)this->GetDlgItem(IDC_SAVE_LIST);
139139
if (pList == NULL) return;
140140
pList->ResetContent();
141-
strcpy(findBuf, dirBuf);
142-
strlcat(findBuf, "*.*", ARRAY_SIZE(findBuf));
141+
snprintf(findBuf, ARRAY_SIZE(findBuf), "%s*.*", dirBuf);
143142

144143
hFindFile = FindFirstFile(findBuf, &findData);
145144
if (hFindFile != INVALID_HANDLE_VALUE) {
@@ -149,11 +148,7 @@ void SaveMap::populateMapListbox( Bool systemMaps )
149148
if ((findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
150149
continue;
151150
}
152-
strcpy(fileBuf, dirBuf);
153-
strlcat(fileBuf, findData.cFileName, ARRAY_SIZE(fileBuf));
154-
strlcat(fileBuf, "\\", ARRAY_SIZE(fileBuf));
155-
strlcat(fileBuf, findData.cFileName, ARRAY_SIZE(fileBuf));
156-
strlcat(fileBuf, ".map", ARRAY_SIZE(fileBuf));
151+
snprintf(fileBuf, ARRAY_SIZE(fileBuf), "%s%s\\%s.map", dirBuf, findData.cFileName, findData.cFileName);
157152
try {
158153
CFileStatus status;
159154
if (CFile::GetStatus(fileBuf, status)) {
@@ -169,7 +164,7 @@ void SaveMap::populateMapListbox( Bool systemMaps )
169164
}
170165
CEdit *pEdit = (CEdit*)GetDlgItem(IDC_SAVE_MAP_EDIT);
171166
if (pEdit != NULL) {
172-
strcpy(fileBuf, m_pInfo->filename);
167+
strlcpy(fileBuf, m_pInfo->filename, ARRAY_SIZE(fileBuf));
173168
Int len = strlen(fileBuf);
174169
if (len>4 && stricmp(".map", fileBuf+(len-4)) == 0) {
175170
// strip of the .map

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -415,20 +415,18 @@ void WorldHeightMapEdit::loadBaseImages(void)
415415
void WorldHeightMapEdit::loadDirectoryOfImages(const char *pFilePath)
416416
{
417417
char dirBuf[_MAX_PATH];
418-
char findBuf[_MAX_PATH];
419418
char fileBuf[_MAX_PATH];
420419

421-
strcpy(dirBuf, pFilePath);
420+
strlcpy(dirBuf, pFilePath, ARRAY_SIZE(dirBuf));
422421
int len = strlen(dirBuf);
423422

424423
if (len > 0 && dirBuf[len - 1] != '\\') {
425424
dirBuf[len++] = '\\';
426425
dirBuf[len] = 0;
427426
}
428-
strcpy(findBuf, dirBuf);
429427

430428
FilenameList filenameList;
431-
TheFileSystem->getFileListInDirectory(AsciiString(findBuf), AsciiString("*.*"), filenameList, TRUE);
429+
TheFileSystem->getFileListInDirectory(AsciiString(dirBuf), AsciiString("*.*"), filenameList, TRUE);
432430

433431
if (filenameList.size() == 0) {
434432
return;
@@ -437,7 +435,7 @@ void WorldHeightMapEdit::loadDirectoryOfImages(const char *pFilePath)
437435
do {
438436
AsciiString filename = *it;
439437
//strcpy(fileBuf, dirBuf);
440-
strcpy(fileBuf, filename.str());
438+
strlcpy(fileBuf, filename.str(), ARRAY_SIZE(fileBuf));
441439
loadBitmap(fileBuf, filename.str());
442440

443441
++it;

0 commit comments

Comments
 (0)