Advertisement
radmickey

Untitled

Sep 12th, 2022
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <filesystem>
  5.  
  6. int optionsChecker(std::string opt){
  7.     if (opt == "-l" or opt == "--lines"){
  8.         return 1;
  9.     } else if (opt == "-c" or opt == "-bytes"){
  10.         return 2;
  11.     } else if (opt == "-w" or opt == "--words"){
  12.         return 3;
  13.     } else if (opt == "-m" or opt == "--chars"){
  14.         return 4;
  15.     } else {
  16.         std::cout << "There's no " << opt << " option." << std::endl;
  17.         return 5;
  18.     }
  19. }
  20.  
  21.  
  22. int main(int argc, char** argv){
  23.     bool options[6] = {false, false, false, false, false, false};
  24.     /*
  25.      * 0 - Хотя бы одна опция задана
  26.      * 1 - -l, --lines вывод количества строк
  27.      * 2 - -c, --bytes вывод размера файла в байтах
  28.      * 3 - -w, --words вывод количества слов
  29.      * 4 - -m, –chars вывод количества букв
  30.      * 5 - error
  31.      */
  32.     double counters[4];
  33.     /*
  34.      * 0 - кол-во строк
  35.      * 1 - размер файла
  36.      * 2 - кол-во слов
  37.      * 3 - кол-во букв
  38.      */
  39.     std::string pathFile;
  40.     std::string strFile;
  41.  
  42.     for (int i = 1; i < argc; i++){
  43.         if (argv[i][0] == '-'){
  44.             options[optionsChecker(argv[i])] = true;
  45.             options[0] = true;
  46.         } else {
  47.             for (int j = 0; j < 4; j++){
  48.                 counters[j] = 0;
  49.             }
  50.             pathFile = argv[i];
  51.             std::ifstream file(pathFile);
  52.  
  53.             if (!file.is_open()){
  54.                 std::cout << "File can't be opened." << std::endl;
  55.             } else {
  56.                 while (std::getline(file, strFile)) {
  57.                     counters[0] += 1;
  58.                     for (int t = 0; t < strFile.size(); t++) {
  59.                         if (int(strFile[t]) == 32 or int(strFile[t]) == 127) {
  60.                             counters[2] += 1;
  61.                         } else if (int(strFile[t]) > 32 or int(strFile[t]) < 127) {
  62.                             counters[3] += 1;
  63.                         }
  64.                     }
  65.                     counters[2] += 1;
  66.                 }
  67.                 //counters[2] += 1;
  68.  
  69.                 std::filesystem::path p{argv[i]};
  70.                 counters[1] = std::filesystem::file_size(p);
  71.                 file.close();
  72.  
  73.                 if (!options[0]) {
  74.                     for (int t = 0; t < 4; t++) {
  75.                         std::cout << counters[t] << " ";
  76.                     }
  77.                     std::cout << argv[i] << std::endl;
  78.                 } else {
  79.                     for (int t = 1; t < 5; t++) {
  80.                         if (options[t]) {
  81.                             std::cout << counters[t - 1] << " ";
  82.                         }
  83.                     }
  84.                     std::cout << argv[i] << std::endl;
  85.                 }
  86.             }
  87.         }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement