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.
It sounds like you are trying to do a very complicated map lookup. Wouldn’t this work?
Of course, this does not address the issue of supposedly equal strings not comparing equal. You will need to post a more complete example for us to be able to answer that, I think. But the first answer about embedded null characters is mistaken. What you have is embedded escape characters, which is fully correct. The only thing I would do different there is to use const char * RED = etc. instead of #defines, since variables are easier to deal with than macros.
Edit: Seems like the answer I was referring to here disappeared.
Edit2: How does this compare to what you are trying to do? It works for me, at least:
The input
should produce the colors indicated.
Edit3: In light of your new information, it seems like the real problem is that your config file contains does not contain actual escape characters, just the characters ‘\’ and ‘e’. Make sure your file actually contains real escape characters. How to generate these varies from editor to editor. In vim, you would type an escape by doing ctrl-V followed by escape. In the worst case, you can generate this by printing them from your own C program – you already have the defines, after all:
fprintf(conffile, "bold %s\n", BOLD).