From e3929f72b8a52851b5a65d5c5acd342663720670 Mon Sep 17 00:00:00 2001 From: Oleksandr Hryshchenko Date: Sun, 9 Jan 2022 20:19:57 +0000 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB(?= =?UTF-8?q?=D0=B0)=20=D0=BD=D0=B0=20'sk2a/compressor.c'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sk2a/compressor.c | 178 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 177 insertions(+), 1 deletion(-) diff --git a/sk2a/compressor.c b/sk2a/compressor.c index 0045d93..782305b 100644 --- a/sk2a/compressor.c +++ b/sk2a/compressor.c @@ -1 +1,177 @@ -wwww \ No newline at end of file +#include +#include +#include +#include +#include +#include "compressor.h" +#define BUFSIZE 1024 + +int basic_string = 0; + + +char *rotateString(char *str, int count) { + + size_t len = strlen(str); + int counter; + + for(counter=0;counter < len;counter++) { + int value = (int)str[counter]; + + if((char)value != ' ') { + value = value + count; + str[counter] = value; + } + } + + return str; +} + +char* bwt_compress(char buffer[BUFSIZE * 2]){ + char* encoded = (char*) calloc(BUFSIZE *2, sizeof(char)); + char* rotation[strlen(buffer)]; + + char* temp = (char*) calloc(BUFSIZE * 2, sizeof(char)); + + for(int i = 0; i < strlen(buffer); i++){ + rotation[i] = (char*) calloc(BUFSIZE * 2, sizeof(char)); + } + + for(int i = 0; i < strlen(buffer); i++){ + strcpy(rotation[i], rotateString(buffer, -1)); + } + + + for(int i = 0; i < strlen(buffer); i++){ + for(int j = 0; j < strlen(buffer); j++){ + if(strcmp(rotation[j], rotation[j+1]) > 0){ + strcpy(temp, rotation[j]); + strcpy(rotation[j], rotation[j+1]); + strcpy(rotation[j+1], temp); + } + } + } + + for(int i = 0; i < strlen(buffer); i++){ + strncat(encoded, &rotation[i][strlen(rotation[i])], 1); + if(strcmp(rotation[i], buffer) == 0){ + basic_string = i; + } + } + + return encoded; +} + +char* run_length(char buffer[BUFSIZE]){ + char* encoded = (char*) calloc(BUFSIZE * 2, sizeof(char)); + int counter = 1; + int i = 1; + + while(i < strlen(buffer)+1){ + for(; i < strlen(buffer)+1; i++){ + if(buffer[i] == buffer[i-1]) + counter++; + else { + char* temp = (char*) calloc(5, sizeof(char)); + sprintf(temp, "%d", counter); + strncat(encoded, temp, strlen(temp)); + strncat(encoded, &buffer[i-1], 1); + i++; + counter = 1; + break; + } + } + } + + return encoded; +} + + +void compress(FILE* infile,FILE* outfile){ + char buffer[BUFSIZE]; + memset(buffer,0,BUFSIZE); + while(1) { + int insize = (int) fread(buffer, sizeof(char), BUFSIZE, infile); + if (insize == 0) { + if (feof(infile)) { + // end of file + break; + } + assert(!ferror(infile)); + } + + char outbuf[BUFSIZE*2]; + strncpy(outbuf, run_length(bwt_compress(buffer)), BUFSIZE*2-1); + + fwrite(outbuf,sizeof(char), strlen(outbuf),outfile); + } +} + +char* bwt_decompress(char buffer[BUFSIZE * 2]){ + char* rotation[strlen(buffer)]; + + for(int i = 0; i < strlen(buffer); i++){ + rotation[i] = (char*) calloc(BUFSIZE * 2, sizeof(char)); + } + + for(int i = 0; i < strlen(buffer); i++){ + for(int j = 0; j < strlen(buffer); j++){ + rotation[j][i] = buffer[strlen(buffer) - 1 -j]; + } + + char* temp = (char*) calloc(BUFSIZE * 2, sizeof(char)); + + for(int iterator = 0; iterator < strlen(buffer); iterator++){ + for(int j = 0; j < strlen(buffer); j++){ + if(strcmp(rotation[j], rotation[j+1]) > 0){ + strcpy(temp, rotation[j]); + strcpy(rotation[j], rotation[j+1]); + strcpy(rotation[j+1], temp); + } + } + } + } + + return rotation[basic_string]; +} + +char* run_length_decompress(char buffer[BUFSIZE * 2]){ + char* decoded = (char*) calloc(BUFSIZE, sizeof(char)); + int i = 0; + + while(i < strlen(buffer)){ + char* temp = (char*) calloc(5, sizeof(char)); + for(; i < strlen(buffer); i++){ + if(isdigit(buffer[i])){ + strncat(temp, &buffer[i], 1); + } + else{ + for(int j = 0; j < atoi(temp); j++) + strncat(decoded, &buffer[i], 1); + i++; + break; + } + } + memset(temp, '\0', 5); + } + + return decoded; +} + +void decompress(FILE* infile,FILE* outfile){ + char buffer[BUFSIZE * 2]; + memset(buffer, 0, BUFSIZE*2); + while(1){ + int insize = (int)fread(buffer,sizeof(char),BUFSIZE * 2,infile); + if (insize == 0){ + if (feof(infile)){ + // end of file + break; + } + assert(!ferror(infile)); + } + char outbuf[BUFSIZE]; + strncpy(outbuf, bwt_decompress(run_length_decompress(buffer)), BUFSIZE-1); + + fwrite(outbuf,sizeof(char), strlen(outbuf),outfile); + } +} \ No newline at end of file