Splux

Command line argument testing + Win32 API testing

Mar 20th, 2013
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <Windows.h>
  4. #include <ctime>
  5.  
  6. void setColor(int);
  7. void makeItColorful(int);
  8. bool validArgument(char*);
  9. void handleArgument(char*, int);
  10. void sleep(int);
  11. int getNumber(char*);
  12.  
  13. int main(int argc, char* args[])
  14. {
  15.     for (int i = 0; i < argc; ++i)
  16.     {
  17.         std::cout << args[i] << std::endl;
  18.     }
  19.  
  20.     for(int i = 1; i < argc; ++i)
  21.     {
  22.         if (validArgument(args[i]))
  23.         {
  24.             if(!((i + 1) >= argc))
  25.             {
  26.                 int ii = i + 1;
  27.                 if(args[ii][0] >= '0' && args[ii][0] <='9')
  28.                 {
  29.                     handleArgument(args[i], getNumber(args[ii]));
  30.                     ++i;
  31.                 }
  32.                 else
  33.                 {
  34.                     handleArgument(args[i], 0);
  35.                 }
  36.             }
  37.             else
  38.             {
  39.                 handleArgument(args[i], 0);
  40.             }
  41.         }
  42.         else
  43.         {
  44.             std::cout << "invalid command\n\nsee -help for more info\n\n";
  45.         }
  46.     }
  47.  
  48.     //std::cin.ignore(80, '\n');
  49.  
  50. #ifdef _DEBUG
  51.     std::cout << "Press [ENTER] to end\n";
  52.     std::cin.ignore(80, '\n');
  53. #endif // debug
  54.  
  55.     return 0;
  56. }
  57.  
  58. int getNumber(char* arg)
  59. {
  60.     std::string str(arg);
  61.     std::stringstream ss;
  62.     ss << str;
  63.     int nr = 0;
  64.     ss >> nr;
  65.  
  66.     return nr;
  67. }
  68.  
  69. bool validArgument(char* arg)
  70. {
  71.     if(strcmp(arg, "-loop\0") == 0 || strcmp(arg, "-reverse\0") == 0 || strcmp(arg, "-colorful\0") == 0 ||
  72.         strcmp(arg, "-setcolor\0") == 0 || strcmp(arg, "-clear\0") == 0 || strcmp(arg, "-sleep\0") == 0)
  73.         return true;
  74.     else if(strcmp(arg, "-help\0") == 0)
  75.     {
  76.         std::cout << "Command line arguments:\n -loop nr\twhere \"nr\" is an integer\n -reverse nr\twhere nr is an integer\n";
  77.         std::cout << "-colorful nr\twhere \"nr\" is number of colorchanges to make\n\n";
  78.     }
  79.     return false;
  80. }
  81.  
  82. void clrscrn()
  83. {
  84.     HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  85.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  86.     if(!GetConsoleScreenBufferInfo(hConsole, &csbi))
  87.         return;
  88.     DWORD cellCount = csbi.dwSize.X * csbi.dwSize.Y;
  89.  
  90.     DWORD count;
  91.     COORD homeCoord = {0, 0};
  92.     if(!FillConsoleOutputCharacter(hConsole, (TCHAR)' ', cellCount, homeCoord, &count))
  93.         return;
  94.     if(!FillConsoleOutputAttribute(hConsole, csbi.wAttributes, cellCount, homeCoord, &count))
  95.         return;
  96.     SetConsoleCursorPosition(hConsole, homeCoord);
  97. }
  98.  
  99. void reverseNumber(int nr)
  100. {
  101.     std::stringstream ss;
  102.     ss << nr;
  103.     std::string str_nr;
  104.     ss >> str_nr;
  105.     const char* ch_nr = str_nr.c_str();
  106.  
  107.     for(int i = str_nr.length() - 1; i >= 0; --i)
  108.         std::cout << ch_nr[i];
  109.     std::cout << std::endl;
  110. }
  111.  
  112. void loop(int times)
  113. {
  114.     for(int i = 0; i < times; ++i)
  115.     {
  116.         std::cout << "Looping!\n";
  117.     }
  118. }
  119.  
  120. void handleArgument(char* arg, int nr)
  121. {
  122.     if(strcmp(arg, "-loop\0") == 0)
  123.     {
  124.         loop(nr);
  125.     }
  126.     else if(strcmp(arg, "-reverse\0") == 0)
  127.         reverseNumber(nr);
  128.     else if(strcmp(arg, "-colorful") == 0)
  129.         makeItColorful(nr);
  130.     else if(strcmp(arg, "-setcolor") == 0)
  131.         setColor(nr);
  132.     else if(strcmp(arg, "-clear") == 0)
  133.         clrscrn();
  134.     else if(strcmp(arg, "-sleep") == 0)
  135.         sleep(nr);
  136. }
  137.  
  138. void sleep(int ms)
  139. {
  140.     clock_t start = clock();
  141.     clock_t end = clock() + ms;
  142.     do
  143.     {
  144.         start = clock();
  145.     } while (start <= end);
  146. }
  147.  
  148. void makeItColorful(int maxColor)
  149. {
  150.     HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  151.     if(hConsole == INVALID_HANDLE_VALUE)
  152.         return;
  153.     DWORD written;
  154.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  155.     GetConsoleScreenBufferInfo(hConsole, &csbi);
  156.     DWORD len = csbi.dwSize.X * csbi.dwSize.Y;
  157.     COORD home = {0, 0};
  158.     for(int i = 0; i < maxColor; ++i)
  159.     {
  160.         SetConsoleTextAttribute(hConsole, i);
  161.         FillConsoleOutputAttribute(hConsole, i, len, home, &written);
  162.         sleep(50);
  163.         std::cout << "WOHOO COLORS!!!\tcolor: " << i << std::endl;
  164.     }
  165.     SetConsoleTextAttribute(hConsole, 15);
  166. }
  167.  
  168. void setColor(int color)
  169. {
  170.     HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  171.     if(hConsole == INVALID_HANDLE_VALUE)
  172.         return;
  173.     if(color < 0)
  174.     {
  175.         SetConsoleTextAttribute(hConsole, 15);
  176.     }
  177.     else
  178.         SetConsoleTextAttribute(hConsole, color);
  179.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  180.     DWORD wr;
  181.    
  182.     COORD coord = {0, 0};
  183.     GetConsoleScreenBufferInfo(hConsole, &csbi);
  184.     DWORD sz = csbi.dwSize.X * csbi.dwSize.Y;
  185.     FillConsoleOutputAttribute(hConsole, color, sz, coord, &wr);
  186. }
Advertisement
Add Comment
Please, Sign In to add comment