35657

Untitled

May 9th, 2023
810
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <string>
  4. #include <array>
  5. #include <fstream>
  6. #include <string_view>
  7.  
  8. using namespace std::literals;
  9.  
  10.  
  11. // напишите эту функцию
  12. inline bool DecodeRLE(const std::string& src_name, const std::string& dst_name) {
  13.  
  14.     std::ifstream in_file(src_name, std::ios::binary);
  15.     if (!in_file) {
  16.         std::cerr << "Can't open input file"sv << std::endl;
  17.         return false;
  18.     }
  19.  
  20.     std::ofstream out_file(dst_name, std::ios::binary);
  21.     if (!out_file) {
  22.         std::cerr << "Can't open output file"sv << std::endl;
  23.         return false;
  24.     }
  25.  
  26.     std::array<char, 1024> buffer;
  27.  
  28.     do {
  29.         int c = in_file.get();
  30.         unsigned char header = static_cast<char>(c);
  31.         int block_type = (header & 1);
  32.         int data_size = (header >> 1) + 1;
  33.         if (!block_type) {
  34.             in_file.read(buffer.data(), data_size);
  35.             out_file.write(buffer.data(), in_file.gcount());
  36.         }
  37.         else {
  38.             c = in_file.get();
  39.             unsigned char ch = static_cast<char>(c);
  40.             out_file << std::string(data_size, ch);
  41.         }
  42.  
  43.     } while (in_file);
  44.  
  45.     return true;
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment