Advertisement
Guest User

Untitled

a guest
Jan 24th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <cstdlib>
  5. #include <windows.h>
  6. #include <vector>
  7. #include <conio.h>
  8. #include <dirent.h>
  9. #include <list>
  10. #include <algorithm>
  11. #include <map>
  12.  
  13. using namespace std;
  14.  
  15. namespace Util{
  16.  
  17.     template<typename T>
  18.     string toString(T value){
  19.         stringstream s;
  20.         s << value;
  21.         return s.str();
  22.     }
  23.  
  24.     template<typename T>
  25.     int getLen(T value){
  26.         return Util::toString<T>(value).length();
  27.     }
  28.  
  29.     float toFloat(string s){
  30.         return ::atof(s.c_str());
  31.     }
  32.  
  33.     void setCurPos(COORD pos){
  34.         SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
  35.     }
  36.  
  37.     int getConsoleWidth(){
  38.         CONSOLE_SCREEN_BUFFER_INFO csbi;
  39.         GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
  40.         return csbi.srWindow.Right - csbi.srWindow.Left + 1;
  41.     }
  42.  
  43.     void clearConRow(uint32_t y, int num = 1){
  44.         for(int yp = y; yp < y+num; yp++){
  45.             setCurPos({X: 0, Y: yp});
  46.             cout << string(getConsoleWidth(), ' ');
  47.         }
  48.     }
  49.  
  50.     COORD getCurPos(){
  51.         CONSOLE_SCREEN_BUFFER_INFO info;
  52.         GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
  53.         return info.dwCursorPosition;
  54.     }
  55.  
  56.     bool isCtrlDown(){
  57.         return (GetKeyState(VK_CONTROL) & 0x8000);
  58.     }
  59.  
  60.     void setColor(int c){
  61.         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);
  62.     }
  63. }
  64.  
  65. //Podstawowy szarawy kolor tekstu w konsoli
  66. enum COLOR{
  67.     DEFAULT = FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_RED
  68. };
  69.  
  70. //ID PRZYCISKÓW
  71. enum KEYS{
  72.     ARROW = 224,
  73.     UP = 72,
  74.     DOWN = 80,
  75.     LEFT = 75,
  76.     RIGHT = 77,
  77.     CTRLUP = 141,
  78.     CTRLDOWN = 145,
  79.     CTRLLEFT = 115,
  80.     CTRLRIGHT = 116,
  81.     ENTER = 13,
  82.     BACKSPACE = 8,
  83.     SPACE = 32,
  84.     DOT = 46,
  85.     MINUS = 45,
  86.     ZERO = 48,
  87.     NINE = 57
  88. };
  89.  
  90. //CA£A KLASA DO TWORZENIA MENU
  91. //ze strza³kami!
  92. class Menu{
  93.     vector<string> options;
  94.     bool backgroundColorFull = false;
  95.     string margin = "";
  96. public:
  97.     Menu(){}
  98.     Menu(vector<string> options){
  99.         this->setOptions(options);
  100.     }
  101.  
  102.     void modifyOption(int index, string newText){
  103.         this->options[index] = newText;
  104.     }
  105.  
  106.     void setOptions(vector<string> o){
  107.         options = o;
  108.     }
  109.  
  110.     void setMargin(string margin){
  111.         this->margin = margin;
  112.     }
  113.  
  114.     void setBackgroundFull(bool flag){
  115.         this->backgroundColorFull = flag;
  116.     }
  117.  
  118.     void setColor(int c){
  119.         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);
  120.     }
  121.  
  122.     int getMaxOptLen(){
  123.         int len = this->options[0].length();
  124.         for(int i = 1; i < options.size(); i++){
  125.             if(options[i].length() > len){len = options[i].length();}
  126.         }
  127.         return len;
  128.     }
  129.  
  130.     int activate(int def = 0, int buffSize = 15){
  131.         if(options.size() >= buffSize){
  132.             system("cls");
  133.         }
  134.         bool run = true;
  135.         int c = def;
  136.         int y = Util::getCurPos().Y;
  137.         int maxLen = this->getMaxOptLen();
  138.         while(run){
  139.             int viewPos = y;
  140.             if(c >= buffSize){
  141.                 viewPos = 0;
  142.             }
  143.             Util::setCurPos({X:0, Y:viewPos});
  144.             int i = 0;
  145.             int showCount = options.size();
  146.             if(showCount >= buffSize){
  147.                 showCount = buffSize+c;
  148.                 if(c >= buffSize){
  149.                     i = c-(buffSize-1);
  150.                 }
  151.             }
  152.             for(; i < showCount; i++){
  153.                 if(c == i){setColor(BACKGROUND_GREEN|BACKGROUND_INTENSITY);}
  154.                 cout << this->margin << options[i];
  155.                 if(this->backgroundColorFull){ //pe³ny kolor wype³nienia
  156.                     cout << string(maxLen - options[i].length(), ' ');
  157.                 }
  158.                 cout << endl;
  159.                 if(c == i){setColor(COLOR::DEFAULT);}
  160.             }
  161.             switch(getch()){
  162.                 case KEYS(ARROW):{
  163.                     int k = getch();
  164.                     if(k == KEYS(UP)){
  165.                         if(c > 0){c--;}
  166.                     }
  167.                     if(k == KEYS(DOWN)){
  168.                         if(c < options.size()-1){
  169.                             c++;
  170.                         }
  171.                     }
  172.                     break;
  173.                 }
  174.                 case KEYS(ENTER):{
  175.                     return c;
  176.                 }
  177.             }
  178.         }
  179.     }
  180. };
  181.  
  182. class Directory{
  183. private:
  184.  
  185.     DIR* dir;
  186.     struct dirent *ent;
  187.  
  188.     bool matchesExt(string file, string ext){
  189.         int extLen = ext.length();
  190.         int fileLen = file.length();
  191.         int begin = fileLen - extLen;
  192.         if(fileLen <= extLen){return false;}
  193.         if(file.substr(begin) == ext){return true;}
  194.         return false;
  195.     }
  196. public:
  197.  
  198.     bool open(string path){
  199.         if((this->dir = opendir(path.c_str())) != NULL){
  200.             return true;
  201.         }
  202.         return false;
  203.     }
  204.  
  205.     vector<string> getList(string matchExt = ""){
  206.         bool all = matchExt.length() == 0;
  207.         vector<string> ret;
  208.         while((ent = readdir(this->dir)) != NULL){
  209.             string name = ent->d_name;
  210.             if(all || this->matchesExt(name, matchExt)){
  211.                 ret.push_back(name);
  212.             }
  213.         }
  214.         return ret;
  215.     }
  216. };
  217.  
  218. class CSV{
  219.  
  220.  
  221. public:
  222.     map<int, string> values;
  223.  
  224.     void read(string name){
  225.         fstream file(name, ios::in);
  226.         while(!file.eof()){
  227.             string key;
  228.             string v1, v2;
  229.             getline(file, key, ';');
  230.             if(key.empty()){continue;}
  231.             getline(file, v1, ';');
  232.             getline(file, v2);
  233.             this->values[atoi(key.c_str())] = v1+"_"+v2;
  234.         }
  235.         file.close();
  236.     }
  237.  
  238.     vector<string> getVec(){
  239.         vector<string> ret;
  240.         for(auto& x: this->values){
  241.             ret.push_back(Util::toString(x.first)+" => "+x.second);
  242.         }
  243.         return ret;
  244.     }
  245.  
  246.  
  247. };
  248.  
  249. ostream& operator<<(ostream& str, const CSV& f){
  250.     for(auto& x: f.values){
  251.         str << x.first << " => " << x.second << endl;
  252.     }
  253.     return str;
  254. }
  255.  
  256. int main()
  257. {
  258.     vector<string> fileList;
  259.     Directory d;
  260.     d.open(".");
  261.     fileList = d.getList("csv");
  262.     Menu m;
  263.     m.setOptions(fileList);
  264.     m.setBackgroundFull(true);
  265.     string file;
  266.     if(fileList.size() == 1){
  267.         file = fileList[0];
  268.     }else{
  269.         m.activate();
  270.     }
  271.     CSV csvFile;
  272.     csvFile.read(file);
  273.     m.setOptions({"Usun pozycje","Dodaj pozycje", "Wyszukaj po id", "Wyswietl", "Koniec"});
  274.     bool run = true;
  275.     while(run){
  276.         switch(m.activate()){
  277.             case 0:{ //usuwanie
  278.                 int id;
  279.                 Menu m;
  280.                 m.setBackgroundFull(true);
  281.                 m.setOptions(csvFile.getVec());
  282.                 id = m.activate();
  283.                 //cout << "Podaj id do usuniecia: ";
  284.                 //cin >> id;
  285.                 //std::map<int,string>::iterator it;
  286.                 //it = csvFile.values.find(id);
  287.                 //csvFile.values.erase(it);
  288.                 system("cls");
  289.                 break;
  290.             }
  291.             case 1:{ //dodawanie
  292.                 cout << "Podaj wartosc do dopisania: ";
  293.                 string val;
  294.                 cin >> val;
  295.                 csvFile.values[csvFile.values.rbegin()->first+1] = val;
  296.                 system("cls");
  297.                 break;
  298.             }
  299.             case 2:{ //szukanie
  300.                 int k;
  301.                 cout << "Podaj id do wyszukania: ";
  302.                 cin >> k;
  303.                 std::map<int,string>::iterator it;
  304.                 it = csvFile.values.find(k);
  305.                 cout << "Odnaleziono: " << it->second << endl;
  306.                 getch();
  307.                 system("cls");
  308.                 break;
  309.             }
  310.             case 3:{ //szukanie
  311.                 cout << csvFile << endl;
  312.                 getch();
  313.                 system("cls");
  314.                 break;
  315.             }
  316.             case 4:{
  317.                 run = false;
  318.             }
  319.         }
  320.     }
  321.     return 0;
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement