Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions Core/GameEngine/Source/Common/ReplaySimulation.cpp
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file just has whitespace changes now.

Original file line number Diff line number Diff line change
Expand Up @@ -215,19 +215,18 @@
AsciiString dir1 = TheRecorder->getReplayDir();
AsciiString dir2 = *filename;
AsciiString wildcard = *filename;
const char* lastSep = dir2.reverseFind('\\');
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach here assumes that there's not a mix of separator characters in a file name.
As an example, c:\\some\\path/here would set dir2 to c:\\some\\ instead of c:\\some\\path/.

Is this a plausible possibility?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid such problem you can search for both and then take the address that is larger.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this string contains userinput from commandline, so it can contain /. I think we can just use the old code here. This will get replaced anyway as soon as we have a helper function for this stuff.

if (lastSep == NULL)
{
int len = dir2.getLength();
while (len)
{
char c = dir2.getCharAt(len-1);
if (c == '/' || c == '\\')
{
wildcard.set(wildcard.str()+dir2.getLength());
break;
}
dir2.removeLastChar();
len--;
}
lastSep = dir2.reverseFind('/');
}

if (lastSep != NULL)
{
// include one extra char so the separator itself gets included
int lenToLastSep = lastSep - dir2.str() + sizeof(char);
dir2.truncateTo(lenToLastSep);

Check failure on line 228 in Core/GameEngine/Source/Common/ReplaySimulation.cpp

View workflow job for this annotation

GitHub Actions / Build Generals / Preset vc6-debug+t+e

'truncateTo' : is not a member of 'AsciiString'

Check failure on line 228 in Core/GameEngine/Source/Common/ReplaySimulation.cpp

View workflow job for this annotation

GitHub Actions / Build Generals / Preset vc6-profile+t+e

'truncateTo' : is not a member of 'AsciiString'

Check failure on line 228 in Core/GameEngine/Source/Common/ReplaySimulation.cpp

View workflow job for this annotation

GitHub Actions / Build Generals / Preset vc6+t+e

'truncateTo' : is not a member of 'AsciiString'

Check failure on line 228 in Core/GameEngine/Source/Common/ReplaySimulation.cpp

View workflow job for this annotation

GitHub Actions / Build Generals / Preset win32-debug+t+e

'truncateTo': is not a member of 'AsciiString'

Check failure on line 228 in Core/GameEngine/Source/Common/ReplaySimulation.cpp

View workflow job for this annotation

GitHub Actions / Build Generals / Preset win32+t+e

'truncateTo': is not a member of 'AsciiString'

Check failure on line 228 in Core/GameEngine/Source/Common/ReplaySimulation.cpp

View workflow job for this annotation

GitHub Actions / Build Generals / Preset win32-profile+t+e

'truncateTo': is not a member of 'AsciiString'
wildcard.set(wildcard.str() + lenToLastSep);
}

FilenameList files;
Expand Down
13 changes: 13 additions & 0 deletions GeneralsMD/Code/GameEngine/Include/Common/AsciiString.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,19 @@ class AsciiString
*/
void removeLastChar();

/**
Remove the final charCount characters in the string. If the string is empty,
do nothing.
*/
void truncateBy(const UnsignedInt charCount);

/**
Truncate the string to a length of maxLength characters, not including null termination,
by removing from the end.
If the string is empty or shorter than maxLength, do nothing.
*/
void truncateTo(const UnsignedInt maxLength);

/**
Analogous to sprintf() -- this formats a string according to the
given sprintf-style format string (and the variable argument list)
Expand Down
13 changes: 13 additions & 0 deletions GeneralsMD/Code/GameEngine/Include/Common/UnicodeString.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,19 @@ class UnicodeString
*/
void removeLastChar();

/**
Remove the final charCount characters in the string. If the string is empty,
do nothing.
*/
void truncateBy(const UnsignedInt charCount);

/**
Truncate the string to a length of maxLength characters, not including null termination,
by removing from the end.
If the string is empty or shorter than maxLength, do nothing.
*/
void truncateTo(const UnsignedInt maxLength);

/**
Analogous to sprintf() -- this formats a string according to the
given sprintf-style format string (and the variable argument list)
Expand Down
3 changes: 3 additions & 0 deletions GeneralsMD/Code/GameEngine/Include/GameClient/DisplayString.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ class DisplayString : public MemoryPoolObject
virtual void setClipRegion( IRegion2D *region ); ///< clip text in this region

virtual void removeLastChar( void ); ///< remove the last character
virtual void truncateBy(const UnsignedInt charCount); ///< remove the last charCount characters
virtual void truncateTo(const UnsignedInt maxLength); ///< remove characters, if needed, until the string is maxLength long excluding null terminator

virtual void appendChar( WideChar c ); ///< append character to end

DisplayString *next( void ); ///< return next string
Expand Down
5 changes: 1 addition & 4 deletions GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,7 @@ static void ConvertShortMapPathToLongMapPath(AsciiString &mapName)
DEBUG_CRASH(("Invalid map name %s", mapName.str()));
}
// remove the .map from the end.
token.removeLastChar();
token.removeLastChar();
token.removeLastChar();
token.removeLastChar();
token.truncateBy(4);

actualpath.concat(token);
actualpath.concat('\\');
Expand Down
4 changes: 1 addition & 3 deletions GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -957,9 +957,7 @@ void updateTGAtoDDS()
}

// replace tga with dds
filenameDDS.removeLastChar(); // a
filenameDDS.removeLastChar(); // g
filenameDDS.removeLastChar(); // t
filenameDDS.truncateBy(3); // tga
filenameDDS.concat("dds");

Bool needsToBeUpdated = FALSE;
Expand Down
4 changes: 1 addition & 3 deletions GeneralsMD/Code/GameEngine/Source/Common/Recorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,7 @@ void RecorderClass::cleanUpReplayFile( void )
return;

AsciiString debugFname = fname;
debugFname.removeLastChar();
debugFname.removeLastChar();
debugFname.removeLastChar();
debugFname.truncateBy(3);
debugFname.concat("txt");
UnsignedInt fileSize = 0;
FILE *fp = fopen(logFileName, "rb");
Expand Down
5 changes: 1 addition & 4 deletions GeneralsMD/Code/GameEngine/Source/Common/StatsCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,7 @@ void StatsCollector::createFileName( void )
const char *fname = name.reverseFind('\\');
if (fname)
name = fname+1;
name.removeLastChar(); // p
name.removeLastChar(); // a
name.removeLastChar(); // m
name.removeLastChar(); // .
name.truncateBy(4); // ".map"
m_statsFileName.clear();
#if defined(RTS_DEBUG)
if (TheGlobalData->m_saveStats)
Expand Down
46 changes: 34 additions & 12 deletions GeneralsMD/Code/GameEngine/Source/Common/System/AsciiString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,16 +293,15 @@ void AsciiString::trim()
{
// Clip trailing white space from the string.
int len = strlen(peek());
for (int index = len-1; index >= 0; index--)
int index = len - 1;
while (index >= 0 && isspace(getCharAt(index)))
{
if (isspace(getCharAt(index)))
{
removeLastChar();
}
else
{
break;
}
--index;
}

if (index < len - 1)
{
truncateTo(index + 1);
}
}
}
Expand Down Expand Up @@ -331,15 +330,38 @@ void AsciiString::toLower()

// -----------------------------------------------------
void AsciiString::removeLastChar()
{
truncateBy(1);
}

// -----------------------------------------------------
void AsciiString::truncateBy(const UnsignedInt charCount)
{
validate();
if (m_data)
if (m_data && charCount > 0)
{
int len = strlen(peek());
size_t len = strlen(peek());
if (len > 0)
{
ensureUniqueBufferOfSize(len+1, true, NULL, NULL);
peek()[len - 1] = 0;
size_t count = min(charCount, len);
peek()[len - count] = 0;
}
}
validate();
}

// -----------------------------------------------------
void AsciiString::truncateTo(const UnsignedInt maxLength)
{
validate();
if (m_data)
{
size_t len = strlen(peek());
if (len > maxLength)
{
ensureUniqueBufferOfSize(len + 1, true, NULL, NULL);
peek()[maxLength] = 0;
}
}
validate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,7 @@ static void findHighFileNumber( AsciiString filename, void *userData )

// strip off the extension at the end of the filename
AsciiString nameOnly = filename;
for( size_t count = 0; count < strlen( SAVE_GAME_EXTENSION ); count++ )
nameOnly.removeLastChar();
nameOnly.truncateBy( strlen( SAVE_GAME_EXTENSION ) );

// convert filename (which is only numbers) to a number
Int fileNumber = atoi( nameOnly.str() );
Expand Down
46 changes: 34 additions & 12 deletions GeneralsMD/Code/GameEngine/Source/Common/System/UnicodeString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,16 +245,15 @@ void UnicodeString::trim()
{
// Clip trailing white space from the string.
int len = wcslen(peek());
for (int index = len-1; index >= 0; index--)
int index = len - 1;
while (index >= 0 && iswspace(getCharAt(index)))
{
if (iswspace(getCharAt(index)))
{
removeLastChar();
}
else
{
break;
}
--index;
}

if (index < len - 1)
{
truncateTo(index + 1);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct? index + 1?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so; index at this point contains the index of the last character that is not a space.
As an example, if the index is 2 then m_data[2] is the character we want the string to stop after. That translates to a total string length of 3, and truncateTo will then set m_data[3] to '\0'.

Is this perhaps an early warning sign of confusing naming/description of the function and its parameter?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the - + 1 stuff can be simplified by starting looping at index = len and then look at index - 1 to test the whitespace char.

}
}
}
Expand All @@ -263,15 +262,38 @@ void UnicodeString::trim()

// -----------------------------------------------------
void UnicodeString::removeLastChar()
{
truncateBy(1);
}

// -----------------------------------------------------
void UnicodeString::truncateBy(const UnsignedInt charCount)
{
validate();
if (m_data)
if (m_data && charCount > 0)
{
int len = wcslen(peek());
size_t len = wcslen(peek());
if (len > 0)
{
ensureUniqueBufferOfSize(len+1, true, NULL, NULL);
peek()[len - 1] = 0;
size_t count = min(charCount, len);
peek()[len - count] = 0;
}
}
validate();
}

// -----------------------------------------------------
void UnicodeString::truncateTo(const UnsignedInt maxLength)
{
validate();
if (m_data)
{
size_t len = wcslen(peek());
if (len > maxLength)
{
ensureUniqueBufferOfSize(len + 1, true, NULL, NULL);
peek()[maxLength] = 0;
}
}
validate();
Expand Down
11 changes: 2 additions & 9 deletions GeneralsMD/Code/GameEngine/Source/Common/UserPreferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -899,11 +899,7 @@ Bool LadderPreferences::loadProfile( Int profileID )
continue;

p.port = atoi( ptr + 1 );
Int i=0;
for (; i<strlen(ptr); ++i)
{
ladName.removeLastChar();
}
ladName.truncateBy(strlen(ptr));
p.address = QuotedPrintableToAsciiString(ladName);

ptr = ladData.reverseFind(':');
Expand All @@ -912,10 +908,7 @@ Bool LadderPreferences::loadProfile( Int profileID )
continue;

p.lastPlayDate = atoi( ptr + 1 );
for (i=0; i<strlen(ptr); ++i)
{
ladData.removeLastChar();
}
ladData.truncateBy(strlen(ptr));
p.name = QuotedPrintableToUnicodeString(ladData);

m_ladders[p.lastPlayDate] = p;
Expand Down
25 changes: 25 additions & 0 deletions GeneralsMD/Code/GameEngine/Source/GameClient/DisplayString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,31 @@ void DisplayString::removeLastChar( void )

} // end removeLastChar

// DisplayString::truncateBy ==================================================
/** Remove the last charCount characters from the string text */
//=============================================================================
void DisplayString::truncateBy( const UnsignedInt charCount )
{
m_textString.truncateBy(charCount);

// our text has now changed
notifyTextChanged();

} // end truncateBy

// DisplayString::truncateTo ==================================================
/** Remove the last characters from the string text so it's at the most
* maxLength characters long */
//=============================================================================
void DisplayString::truncateTo( const UnsignedInt maxLength )
{
m_textString.truncateTo(maxLength);

// our text has now changed
notifyTextChanged();

} // end truncateTo

// DisplayString::appendChar ==================================================
/** Append character to the end of the string */
//=============================================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,17 +464,16 @@ void LanLobbyMenuInit( WindowLayout *layout, void *userData )
//txtInput.translate(IPs.getMachineName());
LANPreferences prefs;
defaultName = prefs.getUserName();
while (defaultName.getLength() > g_lanPlayerNameLength)
defaultName.removeLastChar();
defaultName.truncateTo(g_lanPlayerNameLength);

GadgetTextEntrySetText( textEntryPlayerName, defaultName);
// Clear the text entry line
GadgetTextEntrySetText(textEntryChat, UnicodeString::TheEmptyString);

GadgetListBoxReset(listboxPlayers);
GadgetListBoxReset(listboxGames);

while (defaultName.getLength() > g_lanPlayerNameLength)
defaultName.removeLastChar();
defaultName.truncateTo(g_lanPlayerNameLength);
TheLAN->RequestSetName(defaultName);
TheLAN->RequestLocations();

Expand Down Expand Up @@ -857,8 +856,7 @@ WindowMsgHandledType LanLobbyMenuSystem( GameWindow *window, UnsignedInt msg,
else
txtInput = UnicodeString::TheEmptyString;

while (txtInput.getLength() > g_lanPlayerNameLength)
txtInput.removeLastChar();
txtInput.truncateTo(g_lanPlayerNameLength);

if (!txtInput.isEmpty() && txtInput.getCharAt(txtInput.getLength()-1) == L',')
txtInput.removeLastChar(); // we use , for strtok's so we can't allow them in names. :(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ void HostDirectConnectGame()
prefs["UserName"] = UnicodeStringToQuotedPrintable(name);
prefs.write();

while (name.getLength() > g_lanPlayerNameLength)
name.removeLastChar();
name.truncateTo(g_lanPlayerNameLength);
TheLAN->RequestSetName(name);
TheLAN->RequestGameCreate(localIPString, TRUE);
}
Expand Down Expand Up @@ -241,8 +240,7 @@ void JoinDirectConnectGame()
UpdateRemoteIPList();
PopulateRemoteIPComboBox();

while (name.getLength() > g_lanPlayerNameLength)
name.removeLastChar();
name.truncateTo(g_lanPlayerNameLength);
TheLAN->RequestSetName(name);

TheLAN->RequestGameJoinDirectConnect(ipaddress);
Expand Down Expand Up @@ -500,8 +498,7 @@ WindowMsgHandledType NetworkDirectConnectSystem( GameWindow *window, UnsignedInt
prefs["UserName"] = UnicodeStringToQuotedPrintable(name);
prefs.write();

while (name.getLength() > g_lanPlayerNameLength)
name.removeLastChar();
name.truncateTo(g_lanPlayerNameLength);
TheLAN->RequestSetName(name);

buttonPushed = true;
Expand Down
Loading
Loading