BellTar.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #pragma once
  2. #include <iostream> // for istream, ostream
  3. #include <string> // for string
  4. namespace bell::BellTar {
  5. typedef long long unsigned file_size_t;
  6. ////////////////////////////////////////
  7. // Writing raw data
  8. ////////////////////////////////////////
  9. class writer {
  10. std::ostream& _dst;
  11. public:
  12. writer(std::ostream& dst) : _dst(dst) {}
  13. ~writer() { finish(); }
  14. // Append the data specified by |data| and |file_size| into the
  15. // tar file represented by |dst|.
  16. // In the tar file, the data will be available at |path_in_tar|.
  17. void put(std::string path_in_tar, char const* const data,
  18. const file_size_t data_size);
  19. // Write empty folder at |path_in_tar|.
  20. // NOTE: to specify folders for files, just use / in the path
  21. // passed to |put()|.
  22. void put_directory(std::string path_in_tar);
  23. // Call after everything has been added to the tar represented
  24. // by |dst| to make it a valid tar file.
  25. void finish();
  26. };
  27. ////////////////////////////////////////
  28. // Reading raw data
  29. ////////////////////////////////////////
  30. class reader {
  31. std::istream& _inp;
  32. struct {
  33. std::string file_name;
  34. file_size_t file_size;
  35. char file_type;
  36. } _cached_header_data;
  37. bool _cached_header_data_valid;
  38. void _cache_header();
  39. int _number_of_files;
  40. public:
  41. // Constructor, pass input stream |inp| pointing to a tar file.
  42. reader(std::istream& inp)
  43. : _inp(inp), _cached_header_data_valid(false), _number_of_files(-1) {}
  44. // Returns true if another file can be read from |inp|.
  45. bool contains_another_file();
  46. // Returns file name of next file in |inp|.
  47. std::string get_next_file_name();
  48. // Returns file size of next file in |inp|. Use to allocate
  49. // memory for the |read_next_file()| call.
  50. file_size_t get_next_file_size();
  51. // Read next file in |inp| to |data|.
  52. void read_next_file(char* const data);
  53. char get_next_file_type();
  54. void extract_all_files(std::string output_dir);
  55. // Skip next file in |inp|.
  56. void skip_next_file();
  57. // Returns number of files in tar at |inp|.
  58. int number_of_files();
  59. };
  60. } // namespace bell::BellTar