I suppose I wasn't entirely clear. Worstje did actually help me a little.
I've been away from coding for awhile so I was forgetting some basic things. I assumed tolower was modifying the original, instead of returning another. Newbie mistake!
Originally I couldn't see why the code wasn't working. I posted a working copy (the first code snip)
First Snip
messagemap [tolower (sMessageCode)] = FindAndReplace (sMessageText, "%r", "\r\n");
messagemap [tolower (sMessageCode)] = FindAndReplace (messagemap [tolower (sMessageCode)], "[/color]", "\e[0m");
then I posted a second snip and hoped for an explanation as to why it didn't work as the first did.
Second Snip
messagemap [tolower (sMessageCode)] = FindAndReplace (sMessageText, "%r", "\r\n");
messagemap [tolower (sMessageCode)] = FindAndReplace (sMessageText, "[/color]", "\e[0m");
The second part of both snips was where the question was.
The answer, of course, was that the original string wasn't being modified so I just kept reassigning values to the messagemap.
Since then I've been working to find a way to write this in a better way, slimming it down as much as possible. This is what I have.
void LoadHelps ()
{
// format is: <code> <message>
// eg. motd Hi there!
// Imbedded %r sequences will becomes new lines.
ifstream fHelps (HELPS_FILE, ios::in);
if (!fHelps)
{
cerr << "Could not open helps file: " << HELPS_FILE << endl;
return;
}
while (!(fHelps.eof ()))
{
string sMessageCode, sMessageText;
fHelps >> sMessageCode >> ws;
getline (fHelps, sMessageText);
if (!(sMessageCode.empty ()))
sMessageText = FindAndReplace (sMessageText, "%r", "\r\n");
sMessageText = FindAndReplace (sMessageText, "[/color]", "\e[0m");
sMessageText = FindAndReplace (sMessageText, "[red]", "\e[0;31m");
sMessageText = FindAndReplace (sMessageText, "[green]", "\e[0;32m");
sMessageText = FindAndReplace (sMessageText, "[yellow]", "\e[0;33m");
sMessageText = FindAndReplace (sMessageText, "[blue]", "\e[0;34m");
sMessageText = FindAndReplace (sMessageText, "[magenta]", "\e[0;35m");
sMessageText = FindAndReplace (sMessageText, "[cyan]", "\e[0;36m");
sMessageText = FindAndReplace (sMessageText, "[white]", "\e[0;37m");
sMessageText = FindAndReplace (sMessageText, "[grey]", "\e[1;30m");
sMessageText = FindAndReplace (sMessageText, "[RED]", "\e[1;31m");
sMessageText = FindAndReplace (sMessageText, "[GREEN]", "\e[1;32m");
sMessageText = FindAndReplace (sMessageText, "[YELLOW]", "\e[1;33m");
sMessageText = FindAndReplace (sMessageText, "[BLUE]", "\e[1;34m");
sMessageText = FindAndReplace (sMessageText, "[MAGENTA]", "\e[1;35m");
sMessageText = FindAndReplace (sMessageText, "[CYAN]", "\e[1;36m");
sMessageText = FindAndReplace (sMessageText, "[WHITE]", "\e[1;37m");
messagemap [tolower (sMessageCode)] = sMessageText;
} // end of read loop
} // end of LoadHelps
The above shows the context, the other snips were simply the bottom half.
[EDIT] Edited by Nick to put in some backslashes where required. |