From 999fec5bce9557715a65de026a400f0fb3b84909 Mon Sep 17 00:00:00 2001 From: sk-5 Date: Tue, 21 Jul 2015 09:30:52 -0700 Subject: [PATCH] Fixed a bug that was skipping codes if they had already been used --- examples/example.cc | 11 ++++---- src/libpipecolors.cc | 65 +++++++++++++++++++++----------------------- 2 files changed, 37 insertions(+), 39 deletions(-) diff --git a/examples/example.cc b/examples/example.cc index 036101d..7ed5de9 100644 --- a/examples/example.cc +++ b/examples/example.cc @@ -1,10 +1,11 @@ -#include -#include +#include +#include +#include #include - using namespace pipecolors; using namespace std; + int main(void) { char buffer[100]; @@ -23,5 +24,5 @@ int main(void) { pcprintf("\t|02Length of |15pcsprintf |02is : |10|30%d|39 : We count escape characters just like sprintf\n\n", len2); return 0; - -} + +} \ No newline at end of file diff --git a/src/libpipecolors.cc b/src/libpipecolors.cc index 0f934de..76077bb 100644 --- a/src/libpipecolors.cc +++ b/src/libpipecolors.cc @@ -49,36 +49,37 @@ namespace pipecolors { } switch(pc) { - case 0: return "\x1b[0;30m"; break; // FG_BLACK - case 1: return "\x1b[0;34m"; break; // FG_BLUE - case 2: return "\x1b[0;32m"; break; // FG_GREEN - case 3: return "\x1b[0;36m"; break; // FG_CYAN - case 4: return "\x1b[0;31m"; break; // FG_RED - case 5: return "\x1b[0;35m"; break; // FG_MAGENTA - case 6: return "\x1b[0;33m"; break; // FG_YELLOW - case 7: return "\x1b[0;37m"; break; // FG_GRAY - case 8: return "\x1b[0;90m"; break; // FG_GRAY_D - case 9: return "\x1b[0;94m"; break; // FG_BLUE_L - case 10: return "\x1b[0;92m"; break; // FG_GREEN_L - case 11: return "\x1b[0;96m"; break; // FG_CYAN_L - case 12: return "\x1b[0;91m"; break; // FG_RED_L - case 13: return "\x1b[0;95m"; break; // FG_MAGENTA_L - case 14: return "\x1b[0;93m"; break; // FG_YELLOW_L - case 15: return "\x1b[0;97m"; break; // FG_WHITE + case 0: return "\033[0;30m"; break; // FG_BLACK + case 1: return "\033[0;34m"; break; // FG_BLUE + case 2: return "\033[0;32m"; break; // FG_GREEN + case 3: return "\033[0;36m"; break; // FG_CYAN + case 4: return "\033[0;31m"; break; // FG_RED + case 5: return "\033[0;35m"; break; // FG_MAGENTA + case 6: return "\033[0;33m"; break; // FG_YELLOW + case 7: return "\033[0;37m"; break; // FG_GRAY + case 8: return "\033[0;90m"; break; // FG_GRAY_D + case 9: return "\033[0;94m"; break; // FG_BLUE_L + case 10: return "\033[0;92m"; break; // FG_GREEN_L + case 11: return "\033[0;96m"; break; // FG_CYAN_L + case 12: return "\033[0;91m"; break; // FG_RED_L + case 13: return "\033[0;95m"; break; // FG_MAGENTA_L + case 14: return "\033[0;93m"; break; // FG_YELLOW_L + case 15: return "\033[0;97m"; break; // FG_WHITE - case 16: return "\x1b[1;40m"; break; // BG_DEFAULT - case 17: return "\x1b[1;44m"; break; // BG_BLUE - case 18: return "\x1b[1;42m"; break; // BG_GREEN - case 19: return "\x1b[1;46m"; break; // BG_CYAN - case 20: return "\x1b[1;41m"; break; // BG_RED - case 21: return "\x1b[1;45m"; break; // BG_MAGENTA - case 22: return "\x1b[1;43m"; break; // BG_YELLOW - case 23: return "\x1b[1;47m"; break; // BG_WHITE + case 16: return "\033[1;40m"; break; // BG_DEFAULT + case 17: return "\033[1;44m"; break; // BG_BLUE + case 18: return "\033[1;42m"; break; // BG_GREEN + case 19: return "\033[1;46m"; break; // BG_CYAN + case 20: return "\033[1;41m"; break; // BG_RED + case 21: return "\033[1;45m"; break; // BG_MAGENTA + case 22: return "\033[1;43m"; break; // BG_YELLOW + case 23: return "\033[1;47m"; break; // BG_WHITE - case 30: return "\x1b[1m"; break; // Bold On - case 31: return "\x1b[0m"; break; // Bold Off - case 39: return "\x1b[0;39m"; break; // FG_DEFAULT + case 30: return "\033[1m"; break; // Bold On + case 31: return "\033[0m"; break; // Bold Off + case 39: return "\033[0;39m"; break; // FG_DEFAULT default: return "nocode"; break; + //x1b[ } } @@ -103,12 +104,9 @@ namespace pipecolors { str.first.erase(index, pipe.length()); } else { index += 3; - goto skip; } - skip:; - index += std::string::npos; } - if(strlen > 0) str.second.insert(str.second.end(), strlen, '\0'); + if(strlen > 0) str.second.insert(str.second.end(), strlen, '\n'); } std::pair replace_colors( std::string s ) { @@ -119,7 +117,7 @@ namespace pipecolors { for(int c = 0; c<=99; c++) { int len = ( c < 10 ? asprintf(&buf, "%c0%d", '|', c) : asprintf(&buf, "%c%d", '|', c) ); - strcpy(pcodes[c], buf); + memcpy(pcodes[c], buf, strlen(buf)+1); } for(int i=0; i<=99; i++) { @@ -177,10 +175,9 @@ namespace pipecolors { delete[] *str1; std::pair result = replace_colors(s); - const char * s2 = (const char *)result.first.c_str(); - strcpy(str, s2); + memcpy(str, s2, strlen(s2)+1); return result.second; }