Ejemplo n.º 1
0
int main(int argc, char** argv)
{
	FILE* fp = fopen("MOBY DICK.txt","r");

	if(fp == NULL)
	{
		std::cout<<"The file is not found"<<argv[1]<<"\n";
		return 0;
	}

	unsigned int* counted_Chars = count_From_File(fp);
	int charactersUsed = 0;
	int i;
	for(i=0;i<256;i++)
	{
		if(counted_Chars[i]>0)
		{
			charactersUsed++;
		}
		
	}

	std::vector <huffmanTree*> huffmanHeap = createHuffmanHeap(counted_Chars);


	std::make_heap (huffmanHeap.begin(),huffmanHeap.end(),&compareHuffman );
	for(i=0;i<charactersUsed;i++)
	{
		printNode(huffmanHeap[i]);
	}

 	std::make_heap (huffmanHeap.begin(),huffmanHeap.end(),&compareHuffman );


 	huffmanTree* combinedTree = combineNodes(huffmanHeap, charactersUsed);
	//might not need

	assignDepth(combinedTree);

	charTranslation *translation;

	//memset(translation, 256, sizeof(charTranslation));

	translation = translateTree(combinedTree);

	FILE*output = fopen("compressed.txt", "w");

	inputToOutputFile(fp, output, translation);

	fclose(fp);

 	return 0;

}
Ejemplo n.º 2
0
int backend(AST *tree, char *filename)
{
	FILE *fp = NULL;
	u8 *bin = NULL;
	u32 binlen;
	u32 p2mlen;

	printf("*** Compiler back end ***\n");

	if (mode & MODE_SORT) {
		printf("Sorting by game title... ");
		sortTree(tree);
		printf("Done.\n");
	}

	printf("Translating to BIN... ");

	if ((bin = translateTree(tree)) == NULL) {
		fprintf(stderr, "Failed.\n");
		return -1;
	}

	binlen = *(u32*) bin + 8;

	printf("Done (%s).\n", sizeToStr(binlen));
	printf("%s file '%s'... ", mode & MODE_BIN ? "Writing to" : "Creating P2M", filename);

	if ((fp = fopen(filename, "wb")) == NULL) {
		fprintf(stderr, "Failed.\n");
		return -2;
	}

	if ((mode & MODE_BIN ? writeToFile(fp, bin, 0, binlen) : createP2mFile(fp, bin, binlen, &p2mlen)) < 0) {
		fprintf(stderr, "Failed.\n");
		return -3;
	}

	free(bin);
	fclose(fp);

	if (mode & MODE_BIN) printf("Done.\n");
	else printf("Done (%s).\n", sizeToStr(p2mlen));

	return 0;
}