Advertisement
daniil_mironoff

Untitled

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