コード例 #1
0
ファイル: huffman.c プロジェクト: rozaxe/huffman
// Close files
static void destroy() {
    // Close files
    delete_reader();
    delete_writer();
}
コード例 #2
0
ファイル: deflate.c プロジェクト: TvoroG/deflate_compression
int main(int argc, char **argv)
{
	get_args(argc, argv);
	get_files_name();
	
	do_tests();

	FILE *output = fopen(global_args.output_name, "w");
	if (output == NULL)
		die(NULL);

	struct stat input_stat;
	stat(global_args.input_name, &input_stat);
	size_t st_size = input_stat.st_size;

	if (global_args.isdecompress) {
		reader_t *reader;
		init_reader(&reader);
		inflate(reader);
		delete_reader(&reader);
	} else {
		int rc1, rc2;
		pthread_t thread_static, thread_dynamic;

		size_t last_size, size = 0;
		size_t size_static, size_dynamic;

		io *io_static, *io_dynamic, *io_nocom;
		init_io(&io_static);
		init_io(&io_dynamic);
		init_io(&io_nocom);
		io_nocom->output_file = output;

		int k = 1;
		while (size < st_size) {
			printf("k = %d\n", k++);
			if (st_size - size >= BLOCK_SIZE) {
				last_size = BLOCK_SIZE;
			} else {
				last_size = st_size - size;
				io_static->isfinal = true;
				io_dynamic->isfinal = true;
				io_nocom->isfinal = true;
			}

			io_static->offset = size;
			io_static->block_size = last_size;
			io_dynamic->offset = size;
			io_dynamic->block_size = last_size;
			io_nocom->offset = size;
			io_nocom->block_size = last_size;

			rc1 = pthread_create(&thread_static, NULL, 
								 &static_deflate, io_static);
			rc2 = pthread_create(&thread_dynamic, NULL, 
								 &dynamic_deflate, io_dynamic);

			if (rc1 || rc2)
				die("thread creation failed");
			pthread_join(thread_static, NULL);
			pthread_join(thread_dynamic, NULL);
			size_static = io_static->result;
			size_dynamic = io_dynamic->result;

			printf("ss = %d, sd = %d, sn = %d\n", size_static, size_dynamic, last_size);

			if (size_dynamic <= size_static && 
				size_dynamic < last_size) {
				write_to_output(io_dynamic, output);
				copy_last_byte(io_static, io_dynamic);
				copy_last_byte(io_nocom, io_dynamic);
				size += io_dynamic->block_size;
				printf("block_size = %d, size = %d\n", io_dynamic->block_size, size);
			} else if (size_static <= size_dynamic && 
					   size_static < last_size) {
				write_to_output(io_static, output);
				copy_last_byte(io_dynamic, io_static);
				copy_last_byte(io_nocom, io_static);
				size += io_static->block_size;
				printf("block_size = %d, size = %d\n", io_static->block_size, size);
			} else {
				nocompress_deflate(io_nocom);
				copy_last_byte(io_static, io_nocom);
				copy_last_byte(io_dynamic, io_nocom);
				size += io_nocom->block_size;
				printf("block_size = %d, size = %d\n", io_nocom->block_size, size);
			}
		}

		delete_io(&io_static);
		delete_io(&io_dynamic);
		delete_io(&io_nocom);
	}

	free(global_args.output_name);
	fclose(output);
	exit(0);
}