Skip to content

Commit

Permalink
Utilize new helper methods for finding and replacing characters.
Browse files Browse the repository at this point in the history
- Utilize CTString::FindSubstr() wherever appropriate.
  • Loading branch information
DreamyCecil committed May 1, 2023
1 parent 504f010 commit bfa8bcc
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 48 deletions.
8 changes: 6 additions & 2 deletions Sources/DecodeReport/DecodeReport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,10 @@ void SubMain( int argc, char *argv[])
strmSrc.GetLine_t(strLine);

// try to find address marker in it
const char *strAdr = strstr(strLine.ConstData(), "$adr:");
const INDEX iAddr = strLine.FindSubstr("$adr:");

// if there is no marker
if (strAdr==NULL) {
if (iAddr == -1) {
// just copy the line
strmDst.PutLine_t(strLine.ConstData());

Expand All @@ -129,7 +130,10 @@ void SubMain( int argc, char *argv[])
strImage[0]=0;
ULONG ulSegment=-1;
ULONG ulOffset=-1;

const char *strAdr = strLine.ConstData() + iAddr;
sscanf(strAdr, "$adr: %s %x:%x", strImage, &ulSegment, &ulOffset);

// find the function
CTString strFunction;
SLONG slDelta;
Expand Down
5 changes: 2 additions & 3 deletions Sources/Depend/Dependency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
void AdjustFilePath_t(CTFileName &fnm)
{
// if filename contains a colon or double backslash
if (strchr(fnm.ConstData(), ':') != NULL
|| strstr(fnm.ConstData(), "\\\\") != NULL) {
if (fnm.FindChar(':') != NULL || fnm.FindSubstr("\\\\") != -1) {
// it must be prefixed with application path
fnm.RemoveApplicationPath_t();
}
Expand Down Expand Up @@ -378,7 +377,7 @@ static void AddStringForTranslation(const CTString &str)
INDEX ct = _atpPairs.Count();
for(INDEX i=0; i<ct; i++) {
// if it is that one
if (strcmp(_atpPairs[i].tp_strSrc.ConstData(), str.ConstData()) == 0) {
if (_atpPairs[i].tp_strSrc == str) {
// just mark it as used
_atpPairs[i].m_bUsed = TRUE;
// don't search any more
Expand Down
7 changes: 2 additions & 5 deletions Sources/Engine/Base/Shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,16 +488,13 @@ CTString RemoveSubstring(const CTString &strFull, const CTString &strSub)
CTString strFullL = strFull.ToLower();
CTString strSubL = strSub.ToLower();

const char *pchFullL = strFullL.ConstData();
const char *pchSubL = strSubL.ConstData();
const char *pchFound = strstr(pchFullL, pchSubL);
const INDEX iOffset = strFullL.FindSubstr(strSubL);
INDEX iLenSub = strSub.Length();

if (pchFound == NULL || iLenSub == 0) {
if (iOffset == -1 || iLenSub == 0) {
return strFull;
}

INDEX iOffset = pchFound-pchFullL;
INDEX iLenFull = strFull.Length();

CTString strLeft = strFull;
Expand Down
17 changes: 4 additions & 13 deletions Sources/Engine/Base/Unzip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,17 +205,6 @@ static CStaticStackArray<CZipHandle> _azhHandles;
// filenames of all archives
static CStaticStackArray<CTFileName> _afnmArchives;

// convert slashes to backslashes in a file path
void ConvertSlashes(char *p)
{
while (*p!=0) {
if (*p=='/') {
*p = '\\';
}
p++;
}
}

// read directory of a zip archive and add all files in it to active set
void ReadZIPDirectory_t(CTFileName *pfnmZip)
{
Expand Down Expand Up @@ -316,12 +305,14 @@ void ReadZIPDirectory_t(CTFileName *pfnmZip)
// if the file is real file
} else {
ctFiles++;
// convert filename
ConvertSlashes(strBuffer);
// create a new entry
CZipEntry &ze = _azeFiles.Push();
// remember the file's data
ze.ze_fnm = CTString(strBuffer);

// Convert slashes
ze.ze_fnm.ReplaceChar('/', '\\');

ze.ze_pfnmArchive = pfnmZip;
ze.ze_slCompressedSize = fh.fh_slCompressedSize;
ze.ze_slUncompressedSize = fh.fh_slUncompressedSize;
Expand Down
2 changes: 1 addition & 1 deletion Sources/GameMP/CompMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ CTString CCompMessage::GetLine(INDEX iLine)
}
// find end of line
CTString strLine = strText;
char *pchEndOfLine = (char *)strchr(strLine.ConstData(), '\n');
char *pchEndOfLine = strLine.FindChar('\n');
// if found
if (pchEndOfLine!=NULL) {
// cut there
Expand Down
4 changes: 2 additions & 2 deletions Sources/SeriousSam/CmdLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ CTString GetNextParam(void)
// if the first char is quote
if (_strCmd[0]=='"') {
// find first next quote
const char *pchClosingQuote = strchr(_strCmd+1, '"');
const char *pchClosingQuote = _strCmd.FindChar('"', 1);
// if not found
if (pchClosingQuote==NULL) {
// error in command line
Expand Down Expand Up @@ -122,7 +122,7 @@ void ParseCommandLine(CTString strCmd)
cmd_strPassword = GetNextParam();
} else if (strWord=="+connect") {
cmd_strServer = GetNextParam();
const char *pcColon = strchr(cmd_strServer, ':');
const char *pcColon = cmd_strServer.FindChar(':');
if (pcColon!=NULL) {
CTString strServer;
CTString strPort;
Expand Down
2 changes: 1 addition & 1 deletion Sources/WorldEditor/WorldEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ static CTString GetNextParam(void)
// if the first char is quote
if (_strCmd[0]=='"') {
// find first next quote
const char *pchClosingQuote = strchr(_strCmd+1, '"');
const char *pchClosingQuote = _strCmd.FindChar('"', 1);
// if not found
if (pchClosingQuote==NULL) {
// error in command line
Expand Down
29 changes: 8 additions & 21 deletions Sources/WorldEditor/WorldEditorDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4732,20 +4732,6 @@ void CWorldEditorDoc::OnExportPlacements()
}
}

CTFileName CorrectSlashes(const CTFileName &fnmFile)
{
char afnmSlash[1024];
for(INDEX iChar=0; iChar<fnmFile.Length(); iChar++) {
afnmSlash[iChar] = fnmFile[iChar];
if(afnmSlash[iChar]=='\\') {
afnmSlash[iChar] = '/';
}
}
// end the string
afnmSlash[fnmFile.Length()] = 0;
return CTString(afnmSlash);
}

// Detects detail texture and replaces it with normal map texture
CTFileName RemapDetailTexturePath(CTFileName &fnmFile)
{
Expand Down Expand Up @@ -5163,7 +5149,7 @@ void ExportLayer_t(CWorldEditorDoc *pDoc, CEntity &en, ExportType etExportType,
// export first layer data
CTFileName strPath;
strPath = bpo.bpo_abptTextures[0].bpt_toTexture.GetName();
strPath = CorrectSlashes(strPath);
strPath.ReplaceChar('\\', '/');
strPath = RemapDetailTexturePath(strPath);
if(bFieldBrush) {
strmAmf.FPrintF_t(" \"base color\" Color %d;\n", C_GREEN|128);
Expand All @@ -5181,13 +5167,13 @@ void ExportLayer_t(CWorldEditorDoc *pDoc, CEntity &en, ExportType etExportType,
strmAmf.FPrintF_t(" \"base color\" Color %d;\n", bpo.bpo_abptTextures[0].s.bpt_colColor);
// export second layer data
//strPath = bpo.bpo_abptTextures[1].bpt_toTexture.GetName();
//strPath = CorrectSlashes(strPath);
//strPath.ReplaceChar('\\', '/');
//strPath = RemapDetailTexturePath(strPath);
//strmAmf.FPrintF_t(" \"blend mask\" Texture \"%s\";\n", strPath);
//strmAmf.FPrintF_t(" \"mask uvmap\" UVMap \"Texture 2\";\n");
// export third layer data
//strPath = bpo.bpo_abptTextures[2].bpt_toTexture.GetName();
//strPath = CorrectSlashes(strPath);
//strPath.ReplaceChar('\\', '/');
//strPath = RemapDetailTexturePath(strPath);
//strmAmf.FPrintF_t(" \"detail normalmap\" Texture \"%s\";\n", strPath);
//strmAmf.FPrintF_t(" \"detail uvmap\" UVMap \"Texture 3\";\n");
Expand All @@ -5203,19 +5189,19 @@ void ExportLayer_t(CWorldEditorDoc *pDoc, CEntity &en, ExportType etExportType,
// export first layer data
CTFileName strPath;
strPath = bpo.bpo_abptTextures[0].bpt_toTexture.GetName();
strPath = CorrectSlashes(strPath);
strPath.ReplaceChar('\\', '/');
strPath = RemapDetailTexturePath(strPath);
strmAmf.FPrintF_t(" \"diffuse 1 texture\" Texture \"%s\";\n", strPath);
strmAmf.FPrintF_t(" \"diffuse 1 uvmap\" UVMap \"Texture 1\";\n");
// export second layer data
strPath = bpo.bpo_abptTextures[1].bpt_toTexture.GetName();
strPath = CorrectSlashes(strPath);
strPath.ReplaceChar('\\', '/');
strPath = RemapDetailTexturePath(strPath);
strmAmf.FPrintF_t(" \"shade\" Texture \"%s\";\n", strPath);
strmAmf.FPrintF_t(" \"shade uvmap\" UVMap \"Texture 2\";\n");
// export third layer data
strPath = bpo.bpo_abptTextures[2].bpt_toTexture.GetName();
strPath = CorrectSlashes(strPath);
strPath.ReplaceChar('\\', '/');
strPath = RemapDetailTexturePath(strPath);
strmAmf.FPrintF_t(" \"normal map 1\" Texture \"%s\";\n", strPath);
strmAmf.FPrintF_t(" \"normal 1 uvmap\" UVMap \"Texture 3\";\n");
Expand Down Expand Up @@ -5556,7 +5542,8 @@ void CWorldEditorDoc::OnExportEntities()
// file name
if( pepProperty->ep_eptType == CEntityProperty::EPT_FILENAME ||
pepProperty->ep_eptType == CEntityProperty::EPT_FILENAMENODEP) {
CTFileName fnmFile = CorrectSlashes(ENTITYPROPERTY( &en, pepProperty->ep_slOffset, CTFileName));
CTFileName fnmFile = ENTITYPROPERTY( &en, pepProperty->ep_slOffset, CTFileName);
fnmFile.ReplaceChar('\\', '/');
strLine.PrintF(" \"%s\" = string(\"%s\");", pepProperty->ep_strName, fnmFile);
strmFile.PutLine_t(strLine);
}
Expand Down

0 comments on commit bfa8bcc

Please sign in to comment.