Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void createCharData(Map<ext_char, string> &data, Node* head, string result) {
- if (head -> one == NULL && head -> zero == NULL) {
- data.put(head -> character, result);
- }
- if (head -> one != NULL) {
- result = result + "1";
- createCharData(data, head -> one, result);
- }
- if (head -> zero != NULL) {
- result = result + "0";
- createCharData(data, head -> zero, result);
- }
- }
- /* Function: encodeFile
- * Usage: encodeFile(source, encodingTree, output);
- * --------------------------------------------------------
- * Encodes the given file using the encoding specified by the
- * given encoding tree, then writes the result one bit at a
- * time to the specified output file.
- *
- * This function can assume the following:
- *
- * - The encoding tree was constructed from the given file,
- * so every character appears somewhere in the encoding
- * tree.
- *
- * - The output file already has the encoding table written
- * to it, and the file cursor is at the end of the file.
- * This means that you should just start writing the bits
- * without seeking the file anywhere.
- */
- void encodeFile(istream& infile, Node* encodingTree, obstream& outfile) {
- Map<ext_char, string> data;
- string result = "";
- createCharData(data, encodingTree, result);
- char ch;
- while(true){
- if(infile.fail()) break;
- ch = infile.get();
- string str = data.get(ch);
- for (int i = 0; i < str.length(); i++) {
- if (str[i] == '0')
- outfile.writeBit(0);
- else
- outfile.writeBit(1);
- }
- }
- string tmp = data.get(PSEUDO_EOF);
- for (int i = 0; i < tmp.length(); i++) {
- if (tmp[i] == '0')
- outfile.writeBit(0);
- else
- outfile.writeBit(1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement