Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- #include <Windows.h>
- #include <ctime>
- void setColor(int);
- void makeItColorful(int);
- bool validArgument(char*);
- void handleArgument(char*, int);
- void sleep(int);
- int getNumber(char*);
- int main(int argc, char* args[])
- {
- for (int i = 0; i < argc; ++i)
- {
- std::cout << args[i] << std::endl;
- }
- for(int i = 1; i < argc; ++i)
- {
- if (validArgument(args[i]))
- {
- if(!((i + 1) >= argc))
- {
- int ii = i + 1;
- if(args[ii][0] >= '0' && args[ii][0] <='9')
- {
- handleArgument(args[i], getNumber(args[ii]));
- ++i;
- }
- else
- {
- handleArgument(args[i], 0);
- }
- }
- else
- {
- handleArgument(args[i], 0);
- }
- }
- else
- {
- std::cout << "invalid command\n\nsee -help for more info\n\n";
- }
- }
- //std::cin.ignore(80, '\n');
- #ifdef _DEBUG
- std::cout << "Press [ENTER] to end\n";
- std::cin.ignore(80, '\n');
- #endif // debug
- return 0;
- }
- int getNumber(char* arg)
- {
- std::string str(arg);
- std::stringstream ss;
- ss << str;
- int nr = 0;
- ss >> nr;
- return nr;
- }
- bool validArgument(char* arg)
- {
- if(strcmp(arg, "-loop\0") == 0 || strcmp(arg, "-reverse\0") == 0 || strcmp(arg, "-colorful\0") == 0 ||
- strcmp(arg, "-setcolor\0") == 0 || strcmp(arg, "-clear\0") == 0 || strcmp(arg, "-sleep\0") == 0)
- return true;
- else if(strcmp(arg, "-help\0") == 0)
- {
- std::cout << "Command line arguments:\n -loop nr\twhere \"nr\" is an integer\n -reverse nr\twhere nr is an integer\n";
- std::cout << "-colorful nr\twhere \"nr\" is number of colorchanges to make\n\n";
- }
- return false;
- }
- void clrscrn()
- {
- HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- if(!GetConsoleScreenBufferInfo(hConsole, &csbi))
- return;
- DWORD cellCount = csbi.dwSize.X * csbi.dwSize.Y;
- DWORD count;
- COORD homeCoord = {0, 0};
- if(!FillConsoleOutputCharacter(hConsole, (TCHAR)' ', cellCount, homeCoord, &count))
- return;
- if(!FillConsoleOutputAttribute(hConsole, csbi.wAttributes, cellCount, homeCoord, &count))
- return;
- SetConsoleCursorPosition(hConsole, homeCoord);
- }
- void reverseNumber(int nr)
- {
- std::stringstream ss;
- ss << nr;
- std::string str_nr;
- ss >> str_nr;
- const char* ch_nr = str_nr.c_str();
- for(int i = str_nr.length() - 1; i >= 0; --i)
- std::cout << ch_nr[i];
- std::cout << std::endl;
- }
- void loop(int times)
- {
- for(int i = 0; i < times; ++i)
- {
- std::cout << "Looping!\n";
- }
- }
- void handleArgument(char* arg, int nr)
- {
- if(strcmp(arg, "-loop\0") == 0)
- {
- loop(nr);
- }
- else if(strcmp(arg, "-reverse\0") == 0)
- reverseNumber(nr);
- else if(strcmp(arg, "-colorful") == 0)
- makeItColorful(nr);
- else if(strcmp(arg, "-setcolor") == 0)
- setColor(nr);
- else if(strcmp(arg, "-clear") == 0)
- clrscrn();
- else if(strcmp(arg, "-sleep") == 0)
- sleep(nr);
- }
- void sleep(int ms)
- {
- clock_t start = clock();
- clock_t end = clock() + ms;
- do
- {
- start = clock();
- } while (start <= end);
- }
- void makeItColorful(int maxColor)
- {
- HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
- if(hConsole == INVALID_HANDLE_VALUE)
- return;
- DWORD written;
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- GetConsoleScreenBufferInfo(hConsole, &csbi);
- DWORD len = csbi.dwSize.X * csbi.dwSize.Y;
- COORD home = {0, 0};
- for(int i = 0; i < maxColor; ++i)
- {
- SetConsoleTextAttribute(hConsole, i);
- FillConsoleOutputAttribute(hConsole, i, len, home, &written);
- sleep(50);
- std::cout << "WOHOO COLORS!!!\tcolor: " << i << std::endl;
- }
- SetConsoleTextAttribute(hConsole, 15);
- }
- void setColor(int color)
- {
- HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
- if(hConsole == INVALID_HANDLE_VALUE)
- return;
- if(color < 0)
- {
- SetConsoleTextAttribute(hConsole, 15);
- }
- else
- SetConsoleTextAttribute(hConsole, color);
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- DWORD wr;
- COORD coord = {0, 0};
- GetConsoleScreenBufferInfo(hConsole, &csbi);
- DWORD sz = csbi.dwSize.X * csbi.dwSize.Y;
- FillConsoleOutputAttribute(hConsole, color, sz, coord, &wr);
- }
Advertisement
Add Comment
Please, Sign In to add comment