Advertisement
daniil_mironoff

Untitled

Mar 4th, 2020
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. ////////////////////////////////////////////////////////////////////////
  7.  
  8. // Типы path
  9. // 1) /homework/skyrim.exe        - относительный путь | до файла
  10. // 2) C:/abc/homework/skyrim.exe  - абсолютный    путь | до файла
  11. // 3) /homework/                  - относительный путь | до католога
  12. // 4) C:/abc/                     - абсолютный    путь | до католога
  13.  
  14. // ДОПУСТИМЫЕ символы
  15. // 1) Буквы
  16. // 2) Цифры
  17. // 3) Символы: ~ @ # $ % ^ - _ ( ) { } ' ` .
  18.  
  19. ////////////////////////////////////////////////////////////////////////
  20.  
  21. class classPath {
  22.     private:
  23.         string path;   // Путь
  24.  
  25.  
  26.     public:
  27.         classPath(string strPath) : path(strPath) { /* ... */ }
  28.  
  29.         bool itCoorectPath() const;
  30.         void catalog_or_path() const;
  31.         string return_name_file() const;
  32.         classPath operator+(const classPath&) const;
  33.         string operator[] (unsigned int) const;
  34.  
  35. };
  36.  
  37. //----------------------------------------------------------------------
  38.  
  39. bool classPath::itCoorectPath() const {
  40.     bool absolute = false;
  41.     bool coorect = true;
  42.     bool file = false;
  43.  
  44.     // Проверка на абсолютность путя
  45.     if ( (path.size() > 2) & (path[0] >= 'A') & ('Z' >= path[0]) & (path[1] == ':') ) {
  46.         absolute = true;
  47.     }
  48.  
  49.     unsigned int i; if (absolute) { i = 2; } else { i = 0; }
  50.  
  51.     while (path.size() > i) {
  52.         char ch = path[i];
  53.  
  54.         // Проверка на допустимые символы (парсинг 2000лвл)
  55.         if ( ((ch >= 'A')  & (ch <= 'Z'))  |
  56.                 ((ch >= 'a')  & (ch <= 'z'))  |
  57.                 ((ch >= '.')  & (ch <= '9'))  |
  58.                 ((ch >= '#')  & (ch <= '%'))  |
  59.                 ((ch >= '\'') & (ch <= ')'))  |
  60.  
  61.                 (ch == '{')   |   (ch == '}') |
  62.                 (ch == '~')   |   (ch == '@') |
  63.                 (ch == '^')   |   (ch == '-') |
  64.                 (ch == '_')   |   (ch == '`')    )
  65.                
  66.         {
  67.            
  68.             // Проверка на корректность окончания пути файла
  69.             if ( (path.size() + 1 == i) & (file) & (path[i] != '.') ) {
  70.                 coorect = false; break;
  71.             }
  72.  
  73.             // Проверка на название файла
  74.             if ( path[i] == '.' ) {
  75.                 file = true;
  76.             }
  77.  
  78.             // Проверка на корректность окончания пути каталога
  79.             if ( (path.size() - 1 == i) & (!file) & (path[i] != '/') ) {
  80.                 coorect = false; break;
  81.             }
  82.  
  83.             // Проверка на корректность окончания пути файла
  84.             if ( (path.size() - 1 == i) & (file) & (path[i] == '.') ) {
  85.                 coorect = false; break;
  86.             }
  87.  
  88.             // Проверка на корректность слэша
  89.             if ( (path[i] == '/') & (i != 0) ) {
  90.                 if (file) {
  91.                     coorect = false; break;
  92.                 }
  93.  
  94.                 if (path[i - 1] == '/') {
  95.                     coorect = false; break;
  96.                 }
  97.             }
  98.  
  99.             // CЮДА МОЖНО ДОБАВИТЬ НУЖНЫЕ ПРОВЕРКИ
  100.             // CЮДА МОЖНО ДОБАВИТЬ НУЖНЫЕ ПРОВЕРКИ
  101.             // CЮДА МОЖНО ДОБАВИТЬ НУЖНЫЕ ПРОВЕРКИ
  102.         }
  103.  
  104.         // Если символ не допустим
  105.         else { coorect = false; break; }
  106.  
  107.         // Итерация
  108.         i++;
  109.     }
  110.  
  111.     // cout-отладка
  112.     // cout << "Path: "      << path     << endl
  113.     //      << "coorect = "  << coorect  << endl
  114.     //      << "absolute = " << absolute << endl
  115.     //      << "file = "     << file     << endl
  116.     // << endl;
  117.    
  118.  
  119.  
  120.     return coorect;
  121. }
  122.  
  123. //----------------------------------------------------------------------
  124.  
  125. void classPath::catalog_or_path() const {
  126.     if (itCoorectPath()) {
  127.         if (find(path.begin(), path.end(), '.') != path.end()) {
  128.             cout << "Its path file" << endl;
  129.         } else {
  130.             cout << "Its path catalog" << endl;
  131.         }
  132.     }
  133.  
  134.     else {
  135.         cout << "Path dont coorect" << endl;
  136.     }
  137. }
  138.  
  139. //----------------------------------------------------------------------
  140.  
  141. string classPath::return_name_file() const {
  142.     if (itCoorectPath()) {
  143.         if (find(path.begin(), path.end(), '.') == path.end()) {
  144.             cout << "Its path catalog" << endl;
  145.             return string("");
  146.         } else {
  147.             return string( find(path.rbegin(), path.rend(), '/').base() , path.end() );
  148.         }
  149.     }
  150.  
  151.     else {
  152.         cout << "Path dont coorect" << endl;
  153.         return string("");
  154.     }
  155. }
  156.  
  157. //----------------------------------------------------------------------
  158.  
  159. classPath classPath::operator+(const classPath& obj) const {
  160.     // Возможно суммирование
  161.     // 1. Если оба path корректны
  162.     // 2. Правый path - относительный
  163.     // 3. Левый path - не файл
  164.  
  165.     // Первая проверка
  166.     if (!(itCoorectPath() & obj.itCoorectPath())) {
  167.         return classPath("error path");
  168.     }
  169.  
  170.     // Вторая проверка
  171.     if (((obj.path.size() > 2) & (obj.path[0] >= 'A')
  172.             & ('Z' >= obj.path[0]) & (obj.path[1] == ':'))) {
  173.                     return classPath("error path");
  174.     }
  175.  
  176.     // Третья проверка
  177.     if (find(path.begin(), path.end(), '.') != path.end()) {
  178.         return classPath("error path");
  179.     }
  180.  
  181.     return classPath(string(path.begin(), path.end() - 1) + obj.path);
  182. }
  183.  
  184. //----------------------------------------------------------------------
  185.  
  186. string classPath::operator[] (unsigned int index) const {
  187.     auto iter_left = path.begin();
  188.     unsigned int k = 1;
  189.  
  190.     while ((k++ != index) & (iter_left != path.end())) {
  191.         iter_left = find(iter_left, path.end(), '/');
  192.         iter_left++;
  193.     }
  194.  
  195.     return string(iter_left, find(iter_left, path.end(), '/'));
  196. }
  197.  
  198. ////////////////////////////////////////////////////////////////////////
  199.  
  200. int main() {
  201.     classPath test1("/homework/skyrim.exe");
  202.     classPath test2("C:/abc/homework/skyrim.exe/");
  203.     classPath test3("/homework/L()L");
  204.     classPath test4("/homework/skyrim.exe");
  205.  
  206.     cout << "Name file test1: " << test1.return_name_file() << endl;
  207.     cout << "Coorect test1: " << test1.itCoorectPath() << endl;
  208.     cout << "test1 "; test1.catalog_or_path(); cout << endl;
  209.  
  210.     cout << "Coorect test2: " << test2.itCoorectPath() << endl;
  211.  
  212.     cout << "Coorect test3: " << test3.itCoorectPath() << endl;
  213.  
  214.     cout << "Name file test4: " << test4.return_name_file() << endl;
  215.  
  216.     cout << endl;
  217.  
  218.     classPath testLeft("C:/Desktop/");
  219.     classPath testRight("/labs/Adobe_PS.exe");
  220.     classPath newPath(testLeft + testRight);
  221.     cout << "Name file newPath: " << newPath.return_name_file() << endl;
  222.     cout << "Coorect newPath: " << newPath.itCoorectPath() << endl;
  223.     cout << "newPath "; newPath.catalog_or_path();
  224.     cout << "newPath[2]: " << newPath[2] << endl;
  225.     cout << "newPath[3]: " << newPath[3] << endl;
  226.     cout << "newPath[4]: " << newPath[4] << endl;
  227.  
  228.  
  229.  
  230.     return 0;
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement