BellTar.h 2.0 KB

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