Advertisement
Guest User

colorize.cpp

a guest
Nov 30th, 2015
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdarg.h>
  5.  
  6. #include <vector>
  7. #include <string>
  8.  
  9. #define MAXMSG 512
  10.  
  11. #define COLOR_GREEN    "\x1B[32m"
  12. #define COLOR_RESET    "\033[0m"
  13.  
  14. typedef std::vector<std::string> MyArray;
  15.  
  16. char* change_color(char* str, ...)
  17. {
  18.     static char output[MAXMSG];
  19.     char string[MAXMSG];
  20.  
  21.     va_list args;
  22.     va_start(args, str);
  23.     vsprintf(string, str, args);
  24.     va_end(args);
  25.  
  26.     sprintf(output, "%s%s%s", COLOR_GREEN, string, COLOR_RESET);
  27.  
  28.     return output;
  29. }
  30.  
  31. void search_in_array(MyArray& pArray, const char *key)
  32. {
  33.     int i, n = pArray.size();
  34.     bool bFound = false;
  35.  
  36.     for (i = 0; i < n; i++)
  37.     {
  38.         if (strstr(pArray[i].c_str(), key) != NULL)
  39.         {
  40.             printf("String found: %s\n", pArray[i].c_str());
  41.             char *colored = change_color("%s", pArray[i].c_str());
  42.             printf("Changed color: %s\n", colored);
  43.             bFound = true;
  44.         }
  45.     }
  46.  
  47.     if (!bFound) printf("Can not find string with given key: %s\n", key);
  48. }
  49.  
  50. int main(int argc, char *argv[])
  51. {
  52.     MyArray sArray;
  53.     sArray.clear();
  54.  
  55.     if (argc < 2)
  56.     {
  57.         printf("Usage: %s [name]\n", argv[0]);
  58.         printf("Example: %s lectus\n", argv[0]);
  59.         exit(EXIT_FAILURE);
  60.     }
  61.  
  62.     sArray.push_back("lorem ipsum dolor sit amet");
  63.     sArray.push_back("consectetur adipiscing elit");
  64.     sArray.push_back("donec a diam lectus");
  65.     sArray.push_back("sed sit amet ipsum mauris");
  66.  
  67.     search_in_array(sArray, argv[1]);
  68.  
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement