Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <cstdlib>
- #include <windows.h>
- #include <vector>
- #include <conio.h>
- #include <dirent.h>
- #include <list>
- #include <algorithm>
- #include <map>
- using namespace std;
- namespace Util{
- template<typename T>
- string toString(T value){
- stringstream s;
- s << value;
- return s.str();
- }
- template<typename T>
- int getLen(T value){
- return Util::toString<T>(value).length();
- }
- float toFloat(string s){
- return ::atof(s.c_str());
- }
- void setCurPos(COORD pos){
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
- }
- int getConsoleWidth(){
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
- return csbi.srWindow.Right - csbi.srWindow.Left + 1;
- }
- void clearConRow(uint32_t y, int num = 1){
- for(int yp = y; yp < y+num; yp++){
- setCurPos({X: 0, Y: yp});
- cout << string(getConsoleWidth(), ' ');
- }
- }
- COORD getCurPos(){
- CONSOLE_SCREEN_BUFFER_INFO info;
- GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
- return info.dwCursorPosition;
- }
- bool isCtrlDown(){
- return (GetKeyState(VK_CONTROL) & 0x8000);
- }
- void setColor(int c){
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);
- }
- }
- //Podstawowy szarawy kolor tekstu w konsoli
- enum COLOR{
- DEFAULT = FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_RED
- };
- //ID PRZYCISKÓW
- enum KEYS{
- ARROW = 224,
- UP = 72,
- DOWN = 80,
- LEFT = 75,
- RIGHT = 77,
- CTRLUP = 141,
- CTRLDOWN = 145,
- CTRLLEFT = 115,
- CTRLRIGHT = 116,
- ENTER = 13,
- BACKSPACE = 8,
- SPACE = 32,
- DOT = 46,
- MINUS = 45,
- ZERO = 48,
- NINE = 57
- };
- //CA£A KLASA DO TWORZENIA MENU
- //ze strza³kami!
- class Menu{
- vector<string> options;
- bool backgroundColorFull = false;
- string margin = "";
- public:
- Menu(){}
- Menu(vector<string> options){
- this->setOptions(options);
- }
- void modifyOption(int index, string newText){
- this->options[index] = newText;
- }
- void setOptions(vector<string> o){
- options = o;
- }
- void setMargin(string margin){
- this->margin = margin;
- }
- void setBackgroundFull(bool flag){
- this->backgroundColorFull = flag;
- }
- void setColor(int c){
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);
- }
- int getMaxOptLen(){
- int len = this->options[0].length();
- for(int i = 1; i < options.size(); i++){
- if(options[i].length() > len){len = options[i].length();}
- }
- return len;
- }
- int activate(int def = 0, int buffSize = 15){
- if(options.size() >= buffSize){
- system("cls");
- }
- bool run = true;
- int c = def;
- int y = Util::getCurPos().Y;
- int maxLen = this->getMaxOptLen();
- while(run){
- int viewPos = y;
- if(c >= buffSize){
- viewPos = 0;
- }
- Util::setCurPos({X:0, Y:viewPos});
- int i = 0;
- int showCount = options.size();
- if(showCount >= buffSize){
- showCount = buffSize+c;
- if(c >= buffSize){
- i = c-(buffSize-1);
- }
- }
- for(; i < showCount; i++){
- if(c == i){setColor(BACKGROUND_GREEN|BACKGROUND_INTENSITY);}
- cout << this->margin << options[i];
- if(this->backgroundColorFull){ //pe³ny kolor wype³nienia
- cout << string(maxLen - options[i].length(), ' ');
- }
- cout << endl;
- if(c == i){setColor(COLOR::DEFAULT);}
- }
- switch(getch()){
- case KEYS(ARROW):{
- int k = getch();
- if(k == KEYS(UP)){
- if(c > 0){c--;}
- }
- if(k == KEYS(DOWN)){
- if(c < options.size()-1){
- c++;
- }
- }
- break;
- }
- case KEYS(ENTER):{
- return c;
- }
- }
- }
- }
- };
- class Directory{
- private:
- DIR* dir;
- struct dirent *ent;
- bool matchesExt(string file, string ext){
- int extLen = ext.length();
- int fileLen = file.length();
- int begin = fileLen - extLen;
- if(fileLen <= extLen){return false;}
- if(file.substr(begin) == ext){return true;}
- return false;
- }
- public:
- bool open(string path){
- if((this->dir = opendir(path.c_str())) != NULL){
- return true;
- }
- return false;
- }
- vector<string> getList(string matchExt = ""){
- bool all = matchExt.length() == 0;
- vector<string> ret;
- while((ent = readdir(this->dir)) != NULL){
- string name = ent->d_name;
- if(all || this->matchesExt(name, matchExt)){
- ret.push_back(name);
- }
- }
- return ret;
- }
- };
- class CSV{
- public:
- map<int, string> values;
- void read(string name){
- fstream file(name, ios::in);
- while(!file.eof()){
- string key;
- string v1, v2;
- getline(file, key, ';');
- if(key.empty()){continue;}
- getline(file, v1, ';');
- getline(file, v2);
- this->values[atoi(key.c_str())] = v1+"_"+v2;
- }
- file.close();
- }
- vector<string> getVec(){
- vector<string> ret;
- for(auto& x: this->values){
- ret.push_back(Util::toString(x.first)+" => "+x.second);
- }
- return ret;
- }
- };
- ostream& operator<<(ostream& str, const CSV& f){
- for(auto& x: f.values){
- str << x.first << " => " << x.second << endl;
- }
- return str;
- }
- int main()
- {
- vector<string> fileList;
- Directory d;
- d.open(".");
- fileList = d.getList("csv");
- Menu m;
- m.setOptions(fileList);
- m.setBackgroundFull(true);
- string file;
- if(fileList.size() == 1){
- file = fileList[0];
- }else{
- m.activate();
- }
- CSV csvFile;
- csvFile.read(file);
- m.setOptions({"Usun pozycje","Dodaj pozycje", "Wyszukaj po id", "Wyswietl", "Koniec"});
- bool run = true;
- while(run){
- switch(m.activate()){
- case 0:{ //usuwanie
- int id;
- Menu m;
- m.setBackgroundFull(true);
- m.setOptions(csvFile.getVec());
- id = m.activate();
- //cout << "Podaj id do usuniecia: ";
- //cin >> id;
- //std::map<int,string>::iterator it;
- //it = csvFile.values.find(id);
- //csvFile.values.erase(it);
- system("cls");
- break;
- }
- case 1:{ //dodawanie
- cout << "Podaj wartosc do dopisania: ";
- string val;
- cin >> val;
- csvFile.values[csvFile.values.rbegin()->first+1] = val;
- system("cls");
- break;
- }
- case 2:{ //szukanie
- int k;
- cout << "Podaj id do wyszukania: ";
- cin >> k;
- std::map<int,string>::iterator it;
- it = csvFile.values.find(k);
- cout << "Odnaleziono: " << it->second << endl;
- getch();
- system("cls");
- break;
- }
- case 3:{ //szukanie
- cout << csvFile << endl;
- getch();
- system("cls");
- break;
- }
- case 4:{
- run = false;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement