Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <sstream>
  4.  
  5. #include "FileUtils.hpp"
  6. #include "HCNode.hpp"
  7. #include "HCTree.hpp"
  8. #include "cxxopts.hpp"
  9.  
  10. #define SECOND_ARG 2
  11. #define NUM_CHARS 256
  12.  
  13. /**
  14. * Pseudo-decompression method that decompresses files based on ASCII and naive
  15. * header.
  16. * Params: infileName - input file, outFileName - output file
  17. * Returns: void
  18. */
  19. void pseudoDecompression(string inFileName, string outFileName) {
  20. ifstream readFile;
  21. unsigned int nextFreq;
  22. string num;
  23. vector<unsigned int> freqs(NUM_CHARS, 0);
  24. readFile.open(inFileName, ios::binary);
  25. int i = 0;
  26. // while file still has bytes to read
  27. while (std::getline(readFile, num) && i < NUM_CHARS) {
  28. istringstream freq(num);
  29. freq >> nextFreq;
  30. freqs[i] = nextFreq;
  31. i++;
  32. }
  33.  
  34. // build HCTree
  35. HCTree tree;
  36. tree.build(freqs);
  37.  
  38. // write to output file
  39. ofstream writeFile;
  40. writeFile.open(outFileName);
  41. // decoded bits
  42. readFile.clear();
  43. readFile.seekg(0, readFile.beg);
  44. int y = 0;
  45. while (y < NUM_CHARS) {
  46. readFile.seekg(1, readFile.cur);
  47. if (readFile.peek() == '\n') {
  48. y++;
  49. }
  50. }
  51. readFile.seekg(1, readFile.cur);
  52. while (true) {
  53. char c = tree.decode(readFile);
  54. if (readFile.eof()) {
  55. break;
  56. }
  57. writeFile << c;
  58. }
  59. readFile.close();
  60. writeFile.close();
  61. }
  62.  
  63. /**
  64. * True decompression method that decompresses files based on bitwise i/o and
  65. * small header.
  66. * Params: infileName - input file, outFileName - output file
  67. * Returns: void
  68. */
  69. void trueDecompression(string inFileName, string outFileName) {
  70. ifstream readFile;
  71. unsigned int nextFreq;
  72. string num;
  73. vector<unsigned int> freqs(NUM_CHARS, 0);
  74. readFile.open(inFileName, ios::binary);
  75. int i = 0;
  76. // while file still has bytes to read
  77. while (std::getline(readFile, num) && i < NUM_CHARS) {
  78. istringstream freq(num);
  79. freq >> nextFreq;
  80. freqs[i] = nextFreq;
  81. i++;
  82. }
  83.  
  84. // build HCTree
  85. HCTree tree;
  86. tree.build(freqs);
  87.  
  88. // write to output file
  89. ofstream writeFile;
  90. writeFile.open(outFileName, ios::binary);
  91. // decoded bits
  92. readFile.clear();
  93. readFile.seekg(0, readFile.beg);
  94. int y = 0;
  95. while (y < NUM_CHARS) {
  96. readFile.seekg(1, readFile.cur);
  97. if (readFile.peek() == '\n') {
  98. y++;
  99. }
  100. }
  101. readFile.seekg(1, readFile.cur);
  102.  
  103. string fsizeStr;
  104. std::getline(readFile, fsizeStr);
  105. istringstream fsizess(fsizeStr);
  106. int fsize;
  107. fsizess >> fsize;
  108.  
  109. readFile.clear();
  110. readFile.seekg(0, readFile.beg);
  111. y = 0;
  112. while (y < NUM_CHARS + 1) {
  113. readFile.seekg(1, readFile.cur);
  114. if (readFile.peek() == '\n') {
  115. y++;
  116. }
  117. }
  118. readFile.seekg(1, readFile.cur);
  119.  
  120. BitInputStream bis(readFile);
  121. int decodeCount = 0;
  122. while (decodeCount < fsize) {
  123. char c = tree.decode(bis);
  124. if (readFile.eof()) {
  125. break;
  126. }
  127. writeFile << c;
  128. decodeCount++;
  129. }
  130. readFile.close();
  131. writeFile.close();
  132. }
  133.  
  134. /**
  135. * Main program for running decompression, handles parsing and checking files.
  136. * Params: argc - # of args, argv - command-line args
  137. * Returns: 0 if successful exit, 1 otherwise
  138. */
  139. int main(int argc, char* argv[]) {
  140. cxxopts::Options options("./uncompress",
  141. "Uncompresses files using Huffman Encoding");
  142. options.positional_help("./path_to_input_file ./path_to_output_file");
  143.  
  144. bool isAsciiOutput = false;
  145. string inFileName, outFileName;
  146. options.allow_unrecognised_options().add_options()(
  147. "ascii", "Write output in ascii mode instead of bit stream",
  148. cxxopts::value<bool>(isAsciiOutput))(
  149. "input", "", cxxopts::value<string>(inFileName))(
  150. "output", "", cxxopts::value<string>(outFileName))(
  151. "h,help", "Print help and exit");
  152.  
  153. options.parse_positional({"input", "output"});
  154. auto userOptions = options.parse(argc, argv);
  155.  
  156. if (userOptions.count("help") || !FileUtils::isValidFile(inFileName) ||
  157. outFileName.empty()) {
  158. cout << options.help({""}) << std::endl;
  159. exit(0);
  160. }
  161.  
  162. // check empty input
  163. if (FileUtils::isEmptyFile(inFileName)) {
  164. ofstream output(outFileName);
  165. } else { // handle decompressing
  166. if (isAsciiOutput) {
  167. pseudoDecompression(inFileName, outFileName);
  168. } else {
  169. trueDecompression(inFileName, outFileName);
  170. }
  171. }
  172. return 0;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement