I'm trying to compare two strings
One string is a normal string that I got from an istringstream:
string command;
iss >> command;
and the other is a string that I get from my map.
But it does not seem to be working.
The idea is that I have a map<string, string>
, the value is to be an
escape code to change the terminal output colour.
I have a map in my main
that I put all the keys and values into
(which works, I've tested that) so I pass the map into this function.
I also have defined colours at the top of my program:
#define BLACK "\033[22;30m"
#define RED "\033[22;31m"
#define GREEN "\033[22;32m"
#define BROWN "\033[22;33m"
#define BLUE "\033[22;34m"
#define PURPLE "\033[22;35m"
#define CYAN "\033[22;36m"
#define GREY "\033[22;37m"
#define DARK_GREY "\033[01;30m"
#define LIGHT_RED "\033[01;31m"
#define LIGHT_GREEN "\033[01;32m"
#define YELLOW "\033[01;33m"
#define LIGHT_BLUE "\033[01;34m"
#define LIGHT_PURPLE "\033[01;35m"
#define LIGHT_CYAN "\033[01;36m"
#define WHITE "\033[01;37m"
This is the part that seems not to be working and confusing me:
string getColor(string command, map<string, string> &m)
{
string color;
if((m.find(command)->second).compare(RED) == 0)
{
color = RED;
return color;
}
// ...and so on for all the other colors
}
I would have made this a switch
, but C++ doesn't allow switches on strings.
So the problem is, even if the both strings are the same, it's not working like that.
I realize that m.find()
is returning an iterator at the position of what we are looking for.
But then doing m.find()->second
should get to the value right?
And the value is a string, isn't it?
So in short, I'm not sure why the comparison isn't working.
EDIT:
Ok, here is the config file I was talking about:
bold \e[0;31m
italic \e[0;34m
underline \e[0;32m
default \e[0;37m
so the user input would be something like:
(default this is a(bold simple) example.)
so when I get the command "default", I will search in my map for that string, and then get the colour code associated with it.
then I will change the output colour to the colour in the map associated with default. then I will change the output colour to the colour associated in the map with "bold" to print out the word "simple", and then I will go back to the previous colour to print out the rest of the sentence.
but because when I do this:
cout << config.find("bold")->second << " hi" << endl;
it doesn't change the colour, it prints out:
\e[0;31m hi
instead of changing the output colour :/
so I thought I'd do a comparison, because something like this:
cout << RED << "hi" << endl;
will print out hi in the colour red.