Example #1
0
void getCharCount(ibstream& infile, map<int, int>& charMap){

	int ch = infile.get(); 
	while (ch != EOF){
		if (charMap.count(ch) > 0){
			charMap[ch] += 1;
		}
		else
			charMap[ch] = 1;
		ch = infile.get();
	}
}
/* Function: getFrequencyTable
 * Usage: Map<ext_char, int> freq = getFrequencyTable(file);
 * --------------------------------------------------------
 * Given an input stream containing text, calculates the
 * frequencies of each character within that text and stores
 * the result as a Map from ext_chars to the number of times
 * that the character appears.
 *
 * This function will also set the frequency of the PSEUDO_EOF
 * character to be 1, which ensures that any future encoding
 * tree built from these frequencies will have an encoding for
 * the PSEUDO_EOF character.  */
Map<ext_char, int> getFrequencyTable(ibstream& infile) {
    Map<ext_char, int> resultMap;
    ext_char nextChar;
    while ((nextChar = infile.get()) != EOF) {
        modifyMap(resultMap, nextChar);
    }
    resultMap.add(EOF, 1);
    return resultMap;
}
Example #3
0
bool Encoding::compress(ibstream &inStream, obstream &outStream){
	buildEncodingForInput(inStream);
	int input;
	string code;

	map<int, string>::iterator it;
	outStream.put(mp.size());
	outStream.put('|');
	for (it = mp.begin(); it != mp.end(); it++){
		outStream.put(it->first);
		//cout << it->first << " has the code ";
		for (int i = 0; i < it->second.size(); i++){
			outStream.put(it->second[i]);
			//cout << it->second[i];
		}
		outStream.put('|');
		//cout << endl;
	}

	inStream.rewind();
	string temp;
	int letter;
	int temps;
	for (int i = 0; i < inStream.size(); i++){
		temps = inStream.size();
		input = inStream.get();
		code = mp[input];
		for (int i = 0; i < code.length(); i++){
			temp = code[i];
			outStream.writebit(StringToInteger(temp));
			//cout << code[i];
		}
		//cout << endl;
	}
	outStream.put(_EOF_);
	outStream.close();


	return true;

}
/* Function: readFileHeader
 * Reads header of cyphered file and reconstructs huffman tree
 * --------------------------------------------------------
 * Reads header of cyphered file and reconstructs huffman tree
 * for this cypher file
 */
void readFileHeader(ibstream& infileStream, Node* root) {
    /* Recursive header decoding */
    int bit = 0;
    if ((bit = infileStream.readBit()) > -1) {
        if (bit == 1) {//It's a tree leaf
            /* We write followed symbol into leaf node */
            ext_char nodeSymb = infileStream.get();
            root->symbol = nodeSymb;
        }else if (bit == 0) {//It's a tree knot
            /* We have to create children for it */
            root->symbol = NOT_A_CHAR;
            Node* leftChild = new Node;
            Node* rightChild = new Node;
            root->leftChild = leftChild;
            root->rightChild = rightChild;
            /* Move to children */
            readFileHeader(infileStream, leftChild);
            readFileHeader(infileStream, rightChild);
        }
    }
}
/* Function: decodeFileToFile
 * --------------------------
 * Main cyphered text decoding process.
 */
void decodeFileToFile(ibstream& infileStream, Node* root, obstream& outfileStream) {
    int bit;
    Node* currentNode = root;
    /* Runs through every cypher bit and, at the same time, move
     * through tree to symbols leafs */
    while ((bit = infileStream.readBit()) > -1) {
        if (bit == 0) {
            currentNode = currentNode->leftChild;
        }else{
            currentNode = currentNode->rightChild;
        }
        if ((currentNode->symbol) < NOT_A_CHAR) {
            if (((char)currentNode->symbol) == EOF) {
                break;
            }else{
                outfileStream.put((char)(currentNode->symbol));
            }
            currentNode = root;
        }
    }
}
/* Function: encodeMainTextToFile
 * Usage: encodeMainTextToFile(sourceFile, encodingTree, cypheredFile);
 * --------------------------------------------------------
 * 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 encodeMainTextToFile(ibstream& infile,
                        Node* root,
                        obstream& ofbs){
    /* Cyphers table - [symb][Huffman code] */
    Map<ext_char, string> cypherTable;
    string currentCypher = "";//start code to concantenate
    /* Fills cypherTable by entries [symb][Huffman code] */
    buildCypherTable(currentCypher , root, cypherTable);

    /* Infile stream translation process  */
    char textChar;
    string symbCode = "";
    while ((textChar = infile.get()) != EOF) {
        symbCode = cypherTable[((ext_char)textChar)];
        writeCodeToStream(symbCode, ofbs);
    }
    /* Write EOF */
    symbCode = cypherTable[EOF];
    writeCodeToStream(symbCode, ofbs);//Main EOF
    writeCodeToStream(symbCode, ofbs);//Fix problem of EOF decoding
}
/* Function: encodeMainTextToFile
 * Usage: encodeMainTextToFile(sourceFile, encodingTree, cypheredFile);
 * --------------------------------------------------------
 * 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 encodeMainTextToFile(ibstream& infileStream,
                          Node* root,
                          obstream& outfileStream) {
    /* Cyphers table - [symb][Huffman code] */
    MyMap<ext_char, string> charsCodesTable;
    string currentCypher = "";//start code to concantenate
    /* Fills cypherTable by entries [symb][Huffman code] */
    buildCodesTableForTree(currentCypher, root, charsCodesTable);

    /* Infile stream translation process  */
    char textChar;
    string symbCode = "";
    while ((textChar = infileStream.get()) != EOF) {
        symbCode = getCodeForChar(textChar, charsCodesTable);
        writeCodeToStream(symbCode, outfileStream);
    }

    /* Write EOF */
    symbCode = getCodeForChar(EOF, charsCodesTable);
    writeCodeToStream(symbCode, outfileStream);//Main EOF
    writeCodeToStream(symbCode, outfileStream);//Fix problem of EOF decoding
}
/* Function: compress
 * Usage: compress(infile, outfile);
 * --------------------------------------------------------
 * Main entry point for the Huffman compressor.  Compresses
 * the file whose contents are specified by the input
 * ibstream, then writes the result to outfile. */
void compress(ibstream& infile, obstream& outfile) {
    /* Calculates the frequencies of each character within text */
    Map<ext_char, int> frequenciesTable = getFrequencyTable(infile);

    /* Buffer-vector for cypher tree creation */
    Vector<Node*> vec;
    /* Add nodes for each sumbol and put them to vec */
    buildNodesVector(vec, frequenciesTable);
    /* Main Huffman tree building */
    Node* root = buildEncodingTree(vec);

    /* ENCODE TREE INTO CYPHER FILE HEADER */
    encodeTreeToFileHeader(root, outfile);
    outfile.put(' ');//put some char to divide header from main text cypher

    /* ENCODE FILE MAIN TEXT INTO CYPHER FILE */
    infile.rewind();
    encodeMainTextToFile(infile, root, outfile);

    /* Memory cleaning */
    deleteTree(root);
}
Example #9
0
bool Encoding::decompress(ibstream &inStream, obstream &outStream){
	int mapSize = inStream.get();
	char kill = inStream.get(); //get rid of pipe

	map<string, int> dmap; //remove this after testing
	for (int i = 0; i < mapSize; i++){
		int code;
		string key;
		code = inStream.get();
		int val = inStream.get();
		while (val != '|'){
			key += val;
			val = inStream.get();
		}
		dmap[key] = code;
	}

		int input;
		string code;

	while (true){
			code += IntegerToString(inStream.readbit());
			if (dmap.count(code) > 0){
				input = dmap[code];
				//cout << input << endl;
				outStream.put(input);
				code.clear();
				if (inStream.peek() == _EOF_)
				break;
			}
	}


	cout << " made it out of the loop";
		outStream.close();


	return true;
}