How could this be improved?

Newbie Spellweaver
Joined
Oct 10, 2015
Messages
32
Reaction score
0
Aight i'm new to C++, so I have to start with the basics. What I wanted to do:

- Add "[characterName]" to the /system message, for example:

my char name = boom
/system blabla => [boom] blabla

So basicly people see who used the /system command.

I did it on the following method, but I guess its kinda messy :blushing: So I'd love to get some feedback on how this easy little thing could be improved.

BOOL TextCmd_System( CScanner & scanner ){
#ifdef __WORLDSERVER

CHAR szString[512] = "";
CHAR a[30] = "";
CUser* pUser = (CUser*)scanner.dwValue;

std::string str; std::string str2= pUser->GetName(); str.append("[");
str.append(str2);
str.append("] ");
scanner.GetLastFull();

if( strlen( scanner.token ) >= 512 )
return TRUE;
strcpy(a, str.c_str());
strcpy( szString, scanner.token );

StringTrimRight( szString );

strcat(a, szString);
g_DPCoreClient.SendSystem( a );

#endif // __WORLDSERVER return TRUE;
}
 
BOOL TextCmd_System( CScanner & scanner )
{
#ifdef __WORLDSERVER
CHAR szString[512] = "";
CUser* pUser = (CUser*)scanner.dwValue;
CString strName;
strName = pUser->GetName();


scanner.GetLastFull();
if( strlen( scanner.token ) >= 512 )
return TRUE;
strcpy( szString, scanner.token );
StringTrimRight( szString );


g_DPCoreClient.SendSystem( "[" + strName + "]" + szString );


#endif // __WORLDSERVER
return TRUE;
}
 
BOOL TextCmd_System( CScanner & scanner )
{
#ifdef __WORLDSERVER
CHAR szString[512] = "";
CUser* pUser = (CUser*)scanner.dwValue;
CString strName;
strName = pUser->GetName();


scanner.GetLastFull();
if( strlen( scanner.token ) >= 512 )
return TRUE;
strcpy( szString, scanner.token );
StringTrimRight( szString );


g_DPCoreClient.SendSystem( "[" + strName + "]" + szString );


#endif // __WORLDSERVER
return TRUE;
}

They asked how to improve it, not how to copy it from a virt source.
 
BOOL TextCmd_System( CScanner & scanner )
{
#ifdef __WORLDSERVER
CHAR szString[512] = "";
CUser* pUser = (CUser*)scanner.dwValue;
CString strName;
strName = pUser->GetName();


scanner.GetLastFull();
if( strlen( scanner.token ) >= 512 )
return TRUE;
strcpy( szString, scanner.token );
StringTrimRight( szString );


g_DPCoreClient.SendSystem( "[" + strName + "]" + szString );


#endif // __WORLDSERVER
return TRUE;
}
THANK YOU !!! You made my day... I'm used to program in Java and a bit in C# but I'm not used to the syntax of C++ yet :/ I'm learning it by myself and I knew there was a neater way then what I did. Thanks for this.



They asked how to improve it, not how to copy it from a virt source.

Meh still, I got the improvement from it, there were just some kind of ways of declaration and usages I couldn't figure out. But its all fine now. He did still help me ^^
 
Code:
BOOL TextCmd_System( CScanner & scanner ){#ifdef __WORLDSERVER    CHAR szString[512] = "";    CHAR a[30] = "";    CUser* pUser    = (CUser*)scanner.dwValue; 
   std::string str;
   std::string str2= pUser->GetName();
   str.append("[");    str.append(str2);    str.append("] ");   scanner.GetLastFull(); 
   if( strlen( scanner.token ) >= 512 )        return TRUE; 
   strcpy(a, str.c_str());    strcpy( szString, scanner.token ); 
   StringTrimRight( szString ); 
   strcat(a, szString);    g_DPCoreClient.SendSystem( a ); #endif    // __WORLDSERVER    return TRUE;}

Well it all depends on code preference. As JhonasGarcia posted a version below that works too. Personally, if you want to optimize the current version of yours, using std::string, you could use one std::string rather than two. std::string's += operator would be faster compared to using .append and we can allocate the capacity of the string before adding to the string. Then comes the question, do you want to trim the string of multiple whitespaces or not (using std::'s functions)? And do you want to be able to send an empty string, or not?

Here is what I use for trimming whitespace for std::strings.
Code:
static void stdltrim(std::string &s) {    s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));}static void stdrtrim(std::string &s) {    s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());}




Even then, that code runs pretty much the same speed as the code you presented. My only suggestion is to check the size of the string before and after the increase of the players name to make sure it doesn't exceed the cap, that is if it is actually used via client. If the limiter isn't used via the client and sending the text over, you could remove the limiter if you switch to char* or std::string.
Code:
std::string wew = "[";
wew += pUser->GetName();
wew += "]: ";
wew += scanner.token;




Another alternative is using like a CString and the CString.Format function.
Code:
CString wew;
wew.Format("[%s]: %s", pUser->GetName(), scanner.token);
g_DPCoreClient.SendSystem(wew);




Either way, everything should work fine and the only optimization you could ever get would be very pointless micro optimizations with someways compared to others. You should just focus on what you want it to look like at this point.
 
Well it all depends on code preference. As JhonasGarcia posted a version below that works too. Personally, if you want to optimize the current version of yours, using std::string, you could use one std::string rather than two. std::string's += operator would be faster compared to using .append and we can allocate the capacity of the string before adding to the string. Then comes the question, do you want to trim the string of multiple whitespaces or not (using std::'s functions)? And do you want to be able to send an empty string, or not?

Here is what I use for trimming whitespace for std::strings.
Code:
static void stdltrim(std::string &s) {    s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));}static void stdrtrim(std::string &s) {    s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());}




Even then, that code runs pretty much the same speed as the code you presented. My only suggestion is to check the size of the string before and after the increase of the players name to make sure it doesn't exceed the cap, that is if it is actually used via client. If the limiter isn't used via the client and sending the text over, you could remove the limiter if you switch to char* or std::string.
Code:
std::string wew = "[";
wew += pUser->GetName();
wew += "]: ";
wew += scanner.token;




Another alternative is using like a CString and the CString.Format function.
Code:
CString wew;
wew.Format("[%s]: %s", pUser->GetName(), scanner.token);
g_DPCoreClient.SendSystem(wew);




Either way, everything should work fine and the only optimization you could ever get would be very pointless micro optimizations with someways compared to others. You should just focus on what you want it to look like at this point.
Thanks for this huge reply, its been very informative. The biggest reason I made the topic wasn't to optimize it honestly. I'm new to C++ and want to see more experienced programmers there idea's on it. Its something small and easy but its a good learning source.
 
I'd like to have this type of feature, too. I think its best that we (on our end) know who used the "/system" command. Sorry for this considered easy question but what's the name of the file that you changed it with? Let me also know if the code provided by iisayboom is compatible with Cuvvvie's files. Thanks!
 
I'd like to have this type of feature, too. I think its best that we (on our end) know who used the "/system" command. Sorry for this considered easy question but what's the name of the file that you changed it with? Let me also know if the code provided by @iisayboom is compatible with @Cuvvvie's files. Thanks!

i'll let you know once I get on my pc. The feature is compatible with those files of cuvvy yes. Also try to search in the source for the function you need to adjust.. you will 99% sure find it. Also, credits for me are allowed but not nessesary haha, its a super easy source edit.
 
functextcmd.cpp. You can use CTRL(strg)+SHIFT+F to do a project or solution wide find for the function in question.
 
Back