Advertisement
Mike_be

Test me (file system)

Aug 5th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.23 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <fstream>
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6. #include <algorithm>
  7. #include <regex>
  8. #include <streambuf>
  9. #include <filesystem>
  10.  
  11. int matrix[3][10] =
  12. { //S, A1, A2, A3, A4, A5, A6, A7, A8, A9
  13.     1, 2,  0,  4,  5,  0,  7,  8,  9,  10, //A for 0-9
  14.     0, 0,  3,  0,  0,  6,  0,  0,  0,  0,  //A for "-./"
  15.     0, 0,  0,  0,  0,  0,  0,  0,  0,  0,  //S for everything else
  16. };
  17.  
  18. int row(char& a)
  19. {
  20.     if (a >= '0' && a <= '9')
  21.         return 0;
  22.     if (a == '.' || a == '-' || a == '/')
  23.         return 1;
  24.     return 2;
  25. }
  26.  
  27. void auto_rifle(std::string& str, std::string& str_out)
  28. {
  29.     std::string date = "";
  30.     int state = 0;
  31.     for (unsigned long long k = 0; k < str.size(); k++)
  32.     {
  33.         if (state == 0)
  34.             date.clear();
  35.         state = matrix[(row(str[k]))][state];
  36.         date.push_back(str[k]);
  37.         if (state == 10)
  38.         {
  39.             str_out += date + " ";
  40.             state = 0;
  41.         }
  42.     }
  43.     return;
  44. }
  45.  
  46. template <typename T>
  47. typename T::size_type levenshtein_distance(const T & src, const T & dst)
  48. {
  49.     const typename T::size_type m = src.size();
  50.     const typename T::size_type n = dst.size();
  51.     if (m == 0)
  52.     {
  53.         return n;
  54.     }
  55.     if (n == 0)
  56.     {
  57.         return m;
  58.     }
  59.  
  60.     std::vector<std::vector<typename T::size_type>> matrix(m + 1);
  61.  
  62.     for (typename T::size_type i = 0; i <= m; ++i)
  63.     {
  64.         matrix[i].resize(n + 1);
  65.         matrix[i][0] = i;
  66.     }
  67.     for (typename T::size_type i = 0; i <= n; ++i)
  68.     {
  69.         matrix[0][i] = i;
  70.     }
  71.  
  72.     typename T::size_type above_cell, left_cell, diagonal_cell, cost;
  73.  
  74.     for (typename T::size_type i = 1; i <= m; ++i)
  75.     {
  76.         for (typename T::size_type j = 1; j <= n; ++j)
  77.         {
  78.             cost = src[i - 1] == dst[j - 1] ? 0 : 1;
  79.             above_cell = matrix[i - 1][j];
  80.             left_cell = matrix[i][j - 1];
  81.             diagonal_cell = matrix[i - 1][j - 1];
  82.             matrix[i][j] = std::min(std::min(above_cell + 1, left_cell + 1), diagonal_cell + cost);
  83.         }
  84.     }
  85.  
  86.     return matrix[m][n];
  87. }
  88.  
  89. #include <Windows.h>
  90.  
  91. using namespace std;
  92. using namespace experimental::filesystem;
  93.  
  94. HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  95.  
  96. inline bool print_str(string str, int k, int n)
  97. {
  98.     SetConsoleTextAttribute(hConsole, k);
  99.     for (int l = 0; l < int(str.size()); l++)
  100.     {
  101.         cout << str[l];
  102.         Sleep(n);
  103.     }
  104.     SetConsoleTextAttribute(hConsole, 14);
  105.     return true;
  106. }
  107.  
  108. bool yes_no()
  109. {
  110.     bool err = false;
  111.     string temp;
  112.     getline(cin, temp);
  113.     do
  114.     {
  115.         err = false;
  116.         if (temp == "Yes" || temp == "yes" || temp == "y" || temp == "Y") return true; // Returning result for yes answer
  117.         else if (temp == "No" || temp == "no" || temp == "N" || temp == "n") return false; // For no answer
  118.         else
  119.         {
  120.             // If input is incorrect
  121.             print_str("Type [Yes] or [No]: ", 15, 15);
  122.             getline(cin, temp);
  123.             err = true;
  124.         }
  125.     }
  126.     while (err);
  127.     cout << "An error occurred!" << endl;
  128.     return true;
  129. }
  130.  
  131. bool yes_no(const string& str)
  132. {
  133.     bool err = false;
  134.     print_str(str, 15, 10);
  135.     string temp;
  136.     getline(cin, temp);
  137.     do
  138.     {
  139.         err = false;
  140.         if (temp == "Yes" || temp == "yes" || temp == "y" || temp == "Y") return true; // Returning result for yes answer
  141.         else if (temp == "No" || temp == "no" || temp == "N" || temp == "n") return false; // For no answer
  142.         else
  143.         {
  144.             // If input is incorrect
  145.             print_str("Type [Yes] or [No]: ", 15, 15);
  146.             getline(cin, temp);
  147.             err = true;
  148.         }
  149.     }
  150.     while (err);
  151.     cout << "An error occurred!" << endl;
  152.     return true;
  153. }
  154.  
  155. string make_path(string file, string& directory)
  156. {
  157.     string extension;
  158.     for (unsigned int i = file.size() - 4; i < file.size(); i++)
  159.     {
  160.         extension.push_back(file[i]);
  161.     }
  162.     if (extension == ".txt")
  163.         file = directory + "\\" + file;
  164.     else
  165.         file = directory + "\\" + file + ".txt";
  166.     return file;
  167. }
  168.  
  169. void filling_strings(vector<string>& vec)
  170. {
  171.     unsigned int max_l = 0;
  172.     for (unsigned int i = 0; i < vec.size(); i++)
  173.     {
  174.         if (vec[i].length() > max_l)
  175.             max_l = vec[i].size();
  176.     }
  177.     for (unsigned int i = 0; i < vec.size(); i++)
  178.     {
  179.         while (vec[i].length() < max_l)
  180.             vec[i].push_back(' ');
  181.     }
  182. }
  183.  
  184. string shorting_string(string str)
  185. {
  186.     while (str[str.length() - 1] == ' ')
  187.         str.pop_back();
  188.     return str;
  189. }
  190.  
  191. string vec_search(const vector<string>& vec, const string& str)
  192. {
  193.     unsigned int i = 0, k = 0, dist = 100;
  194.     while (k < vec.size())
  195.     {
  196.         if (levenshtein_distance(vec[k], str) < dist)
  197.         {
  198.             dist = levenshtein_distance(vec[k], str);
  199.             i = k;
  200.         }
  201.         k++;
  202.     }
  203.     return vec[i];
  204. }
  205.  
  206. void regex_result(string& str, vector<string>& vec, const regex& rgx)
  207. {
  208.     regex_token_iterator<string::iterator> rend;
  209.     regex_token_iterator<string::iterator> a(str.begin(), str.end(), rgx);
  210.     while (a != rend) vec.push_back(*a++);
  211. }
  212.  
  213. bool empty(const string& str)
  214. {
  215.     bool empty = true;
  216.     if (!str.empty())
  217.     {
  218.         for (unsigned int i = 0; i < str.length(); i++)
  219.         {
  220.             if (str[i] != ' ')
  221.             {
  222.                 empty = false;
  223.             }
  224.         }
  225.     }
  226.     return empty;
  227. }
  228.  
  229. /*
  230. namespace fs = std::filesystem;
  231.  
  232. int main()
  233. {
  234.     fs::create_directories("sandbox/a/b");
  235.     std::ofstream("sandbox/file1.txt");
  236.     std::ofstream("sandbox/file2.txt");
  237.     for(auto& p: fs::directory_iterator("sandbox"))
  238.         std::cout << p << '\n';
  239.     fs::remove_all("sandbox");
  240. }
  241. */
  242.  
  243. void memory(string& directory)
  244. {
  245.     ifstream in;
  246.     string temp;
  247.     in.open("Logs.txt");
  248.     if (in.is_open())
  249.     {
  250.         temp.assign((istreambuf_iterator<char>(in)),
  251.             istreambuf_iterator<char>());
  252.         print_str("Do you want to continue from \"", 13, 10); cout << temp; print_str("\"? ", 13, 10);
  253.         if (yes_no())
  254.         {
  255.             directory = temp;
  256.             return;
  257.         }
  258.     }
  259.     print_str("Write path: ", 11, 10);
  260.     getline(cin, directory);
  261. }
  262.  
  263. void files(vector<string>& vec, string& directory)
  264. {
  265.     bool err, no_txt = false;
  266.     do {
  267.         err = false, no_txt = true;
  268.         memory(directory);
  269.         for (auto p : directory_iterator(directory))
  270.         {
  271.             if (p.path().string().length() > directory.length() + 5)
  272.                 vec.push_back(p.path().string());
  273.         }
  274.         if (vec.size() == 0)
  275.         {
  276.             print_str("This path doesn't exist or it's empty.\n", 12, 10);
  277.             err = true;
  278.             continue;
  279.         }
  280.         for (unsigned int i = 0; i < vec.size(); i++)
  281.         {
  282.             string extension;
  283.             for (unsigned int j = vec[i].size() - 4; j < vec[i].size(); j++)
  284.             {
  285.                 extension.push_back(vec[i][j]);
  286.             }
  287.             if (extension == ".txt")
  288.             {
  289.                 no_txt = false;
  290.                 break;
  291.             }
  292.         }
  293.         if (no_txt)
  294.         {
  295.             print_str("There is no txt file.\n", 12, 10);
  296.         }
  297.     } while (err || no_txt);
  298.     filling_strings(vec);
  299.     ofstream logs("Logs.txt");
  300.     logs << directory;
  301.     logs.close();
  302. }
  303.  
  304. void check_files(vector<string>& vec, string& directory)
  305. {
  306.     unsigned int i;
  307.     vector<string> vec_2;
  308.     for (auto p : directory_iterator(directory))
  309.     {
  310.         vec_2.push_back(p.path().string());
  311.     }
  312.     filling_strings(vec_2);
  313.     if (vec_2.size() != vec.size())
  314.     {
  315.         vec.clear();
  316.         vec = vec_2;
  317.         filling_strings(vec);
  318.     }
  319.     else
  320.     {
  321.         i = vec.size();
  322.         for (unsigned j = 0; j < i; j++)
  323.         {
  324.             if (vec[j] != vec_2[j])
  325.             {
  326.                 vec = vec_2;
  327.                 return;
  328.             }
  329.         }
  330.     }
  331. }
  332.  
  333. int main()
  334. {
  335.     print_str(
  336.         "Hello user!\
  337.     \nThis is a program for loading files and searching for earliest date in them. \
  338.     I am your personal assistant, AI Cortana.\
  339.     \nI have few featerues like: \n",
  340.         3, 5);
  341.     print_str(
  342.         "1. Finding existing date (within 32 days and month and more than 12 months) \nincluding leap years;\
  343.     \n2. Suggesting file names if input was incorrect (Pretty unstable, needs \nto be modified);\
  344.     \n3. Looping for easy multiple requests.\
  345.     \nHave fun!\n",
  346.         10, 5);
  347.     //smatch match;
  348.     //regex date(
  349.     //  "(((0[1-9]|[12][0-9]|3[01])[-/.](0[13578]|1[02])|(0[1-9]|[12][0-9]|30)[-/.](0[469]|11)|(0[1-9]|1\\d|2[0-8])[-/.]02)[-/.]\\d{4}|29[-/.]02[-/.](\\d{2}(0[48]|[2468][048]|[13579][26])|([02468][048]|[1359][26])00))",
  350.     //  regex_constants::nosubs);
  351.     vector<string> books;// , dates;
  352.     string file, read, directory;
  353.     files(books, directory);
  354.     do
  355.     {
  356.         print_str("Write file's name: ", 15, 10);
  357.         do
  358.         {
  359.             getline(cin, file);
  360.         }
  361.         while (empty(file) && cout << "Why would you write nothing? Type again: ");
  362.         ifstream in;
  363.         check_files(books, directory);
  364.         //dates.clear();
  365.         in.open(make_path(file, directory));
  366.         if (!in.is_open())
  367.         {
  368.             print_str("Did you mean \"", 15, 10);
  369.             cout << shorting_string(vec_search(books, make_path(file, directory)));
  370.             print_str("\"?\n", 15, 10);
  371.             if (yes_no())
  372.                 in.open(vec_search(books, make_path(file, directory)));
  373.         }
  374.         read.assign((istreambuf_iterator<char>(in)),
  375.                     istreambuf_iterator<char>());
  376.         //string auto_dates = "";
  377.         cout << read;
  378.         //auto_rifle(read, auto_dates);
  379.         //regex_result(auto_dates, dates, date);
  380.         if (!in.is_open())
  381.             print_str("Couldn't load the file.\n", 7, 10);
  382.         //else if (dates.empty())
  383.         //  print_str("There is no dates in this text file.\n", 15, 10);
  384.         /*else
  385.         {
  386.             string final_date = dates[0];
  387.             for (unsigned int i = 1; i < dates.size(); i++)
  388.             {
  389.                 int k = 0;
  390.                 int j = dates[i].length() - 4;
  391.                 while (final_date[j] == dates[i][j])
  392.                 {
  393.                     if (j == dates[i].length() - 1)
  394.                     {
  395.                         j -= 7;
  396.                     }
  397.                     if (j == dates[i].length() - 6)
  398.                     {
  399.                         j -= 4;
  400.                         k++;
  401.                         if (k == 2)
  402.                             break;
  403.                     }
  404.                     j++;
  405.                 }
  406.                 if (final_date[j] > dates[i][j])
  407.                     final_date = dates[i];
  408.             }
  409.             print_str("The earliest date is ", 15, 15);
  410.             cout << final_date << endl;
  411.         }*/
  412.         in.close();
  413.     }
  414.     while (yes_no("\nDo you want to continue?\n"));
  415. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement