[2.0] Process escape sequences for MOTD for issue #23 #96

Merged
merged 2 commits into from May 3, 2012

Projects

None yet

2 participants

@Justasic

Added C/C++ style escape codes for color codes in the MOTD along with @SaberUK's \x, \u, \b, \c codes.

@rburchell rburchell commented on the diff May 2, 2012
src/commands/cmd_motd.cpp
+ * color code chars using C++ style escape sequences. You
+ * can append other chars to replace if you like (such as %U
+ * being underline). -- Justasic
+ */
+void ProcessColors(ConfigFileCache::iterator &file)
+{
+ static struct special_chars
+ {
+ std::string character;
+ std::string replace;
+ special_chars(const std::string &c, const std::string &r) : character(c), replace(r) { }
+ }
+
+ special[] = {
+ special_chars("\\002", "\002"), // Bold
+ special_chars("\\037", "\037"), // underline
@rburchell
rburchell May 2, 2012

indentation here is off

@rburchell rburchell commented on the diff May 2, 2012
src/commands/cmd_motd.cpp
+ * Replace all color codes from the special[] array to actual
+ * color code chars using C++ style escape sequences. You
+ * can append other chars to replace if you like (such as %U
+ * being underline). -- Justasic
+ */
+void ProcessColors(ConfigFileCache::iterator &file)
+{
+ static struct special_chars
+ {
+ std::string character;
+ std::string replace;
+ special_chars(const std::string &c, const std::string &r) : character(c), replace(r) { }
+ }
+
+ special[] = {
+ special_chars("\\002", "\002"), // Bold
@rburchell
rburchell May 2, 2012

hmm.. why are you using std::string for character/replace? can't you just use 'char' instead? that should also make your replacing a bit tidier too I guess

while (pos != npos)
{
str[pos] = repl;
pos = str.find(....)
}

you also avoid string copies there (no need for new_str) - I guess you did it that way to allow for multiple-character replaces, but since you're not doing that, I don't think there's any need for the complexity?

@Justasic
Justasic May 2, 2012

I am doing a multi-character replace, I am interpreting the literal \002 from the MOTD file as the ascii \002 char (hence the double slash), I also made it so it could have future expansion in case someone likes %C instead of using \003.
The function it's self can be cleaned up and I'll work on that when I get home but ultimately I copied this from anope (Though I use this same function in many of my other projects)

@rburchell
rburchell May 2, 2012

Right, good point... I guess it shows I hadn't had coffee when I was reading that

@Justasic
Justasic May 3, 2012

grr. I was going to remove the replace_all function so it was like a microsecond faster at doing this but I guess it works, I didn't get around to it yesterday because I did an all-nighter (we all know how we love those).. I guess I can stick it in with a future pull request.

@rburchell rburchell merged commit fcacc8e into inspircd:insp20 May 3, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment