Изменил(а) на 'sk2a/compressor.c'

This commit is contained in:
Oleksandr Hryshchenko 2022-01-09 21:48:11 +00:00
parent 42bd39bb6e
commit c3907f8bba

View File

@ -6,43 +6,34 @@
#include "compressor.h" #include "compressor.h"
#define BUFSIZE 1024 #define BUFSIZE 1024
int basic_string = 0;
char* leftRotatebyOne(char arr[BUFSIZE * 2]){
char *rotateString(char *str, int count) { char* result = (char*) calloc(strlen(arr), sizeof(char));
char temp = arr[0];
size_t len = strlen(str); for (int i = 0; i < strlen(arr) - 1; i++)
int counter; arr[i] = arr[i + 1];
arr[strlen(arr)-1] = temp;
for(counter=0;counter < len;counter++) { strcpy(result, arr);
int value = (int)str[counter]; return result;
if((char)value != ' ') {
value = value + count;
str[counter] = value;
}
}
return str;
} }
char* bwt_compress(char buffer[BUFSIZE * 2]){ char* bwt_compress(char buffer[BUFSIZE * 2]){
char* encoded = (char*) calloc(BUFSIZE *2, sizeof(char)); char* encoded = (char*) calloc(BUFSIZE * 2, sizeof(char));
char rotate_temp[BUFSIZE * 2];
for(int i = 0; i < strlen(buffer); i++)
rotate_temp[i] = buffer[i];
rotate_temp[strlen(buffer)] = '\0';
char* rotation[strlen(buffer)]; 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++)
strcpy(rotation[i], leftRotatebyOne(rotate_temp));
char* temp = (char*) calloc(BUFSIZE * 2, sizeof(char)); char* temp = (char*) calloc(BUFSIZE * 2, sizeof(char));
for(int i = 0; i < strlen(buffer); i++){ for(int i = 0; i < strlen(buffer); i++){
rotation[i] = (char*) calloc(BUFSIZE * 2, sizeof(char)); for(int j = 0; j < strlen(buffer) - i - 1; j++){
}
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){ if(strcmp(rotation[j], rotation[j+1]) > 0){
strcpy(temp, rotation[j]); strcpy(temp, rotation[j]);
strcpy(rotation[j], rotation[j+1]); strcpy(rotation[j], rotation[j+1]);
@ -51,16 +42,23 @@ char* bwt_compress(char buffer[BUFSIZE * 2]){
} }
} }
for(int i = 0; i < strlen(buffer); i++){
strncat(encoded, &rotation[i][strlen(rotation[i])], 1); for(int i = 0; i < strlen(buffer); i++) {
if(strcmp(rotation[i], buffer) == 0){ strncat(encoded, &rotation[i][strlen(rotation[i]) - 1], 1);
basic_string = i; if(strcmp(rotation[i], buffer) == 0) {
char str[100];
for(int j = 0; j < 100; j++)
str[j] = '\0';
sprintf(str, "%d", i);
FILE* num = fopen("num.txt", "w");
fwrite(str, sizeof(char), strlen(str), num);
} }
} }
return encoded; return encoded;
} }
char* run_length(char buffer[BUFSIZE]){ char* run_length(char buffer[BUFSIZE]){
char* encoded = (char*) calloc(BUFSIZE * 2, sizeof(char)); char* encoded = (char*) calloc(BUFSIZE * 2, sizeof(char));
int counter = 1; int counter = 1;
@ -106,34 +104,47 @@ void compress(FILE* infile,FILE* outfile){
} }
} }
char* bwt_decompress(char buffer[BUFSIZE * 2]){
char* rotation[strlen(buffer)];
for(int i = 0; i < strlen(buffer); i++){ char* bwt_decompress(char buffer[BUFSIZE * 2]){
rotation[i] = (char*) calloc(BUFSIZE * 2, sizeof(char)); char* decoded = (char*) calloc(BUFSIZE * 2, sizeof(char));
char rotation[strlen(buffer)][BUFSIZE * 2];
for(int i = 0; i < strlen(buffer); i++) {
for (int j = 0; j < strlen(buffer) + 1; j++) {
if(j != strlen(buffer))
rotation[i][j] = ' ';
else
rotation[i][j] = '\0';
}
} }
for(int i = 0; i < strlen(buffer); i++){ for(int i = 0; i < strlen(buffer); i++) {
for(int j = 0; j < strlen(buffer); j++){ for (int j = 0; j < strlen(buffer) + 1; j++) {
rotation[j][i] = buffer[strlen(buffer) - 1 -j]; rotation[j][strlen(buffer) - 1 - i] = buffer[j];
} }
char* temp = (char*) calloc(BUFSIZE * 2, sizeof(char)); char *temp = (char *) calloc(BUFSIZE * 2, sizeof(char));
for (int iterator = 0; iterator < strlen(buffer); iterator++) {
for(int iterator = 0; iterator < strlen(buffer); iterator++){ for (int j = 0; j < strlen(buffer) - 1 - iterator; j++) {
for(int j = 0; j < strlen(buffer); j++){ if (strcmp(rotation[j], rotation[j + 1]) > 0) {
if(strcmp(rotation[j], rotation[j+1]) > 0){
strcpy(temp, rotation[j]); strcpy(temp, rotation[j]);
strcpy(rotation[j], rotation[j+1]); strcpy(rotation[j], rotation[j + 1]);
strcpy(rotation[j+1], temp); strcpy(rotation[j + 1], temp);
} }
} }
} }
} }
return rotation[basic_string]; FILE* num = fopen("num.txt", "r");
char number[100];
for(int i = 0; i < 100; i++)
number[i] = '\0';
fread(number, sizeof(char), 100, num);
strcpy(decoded, rotation[atoi(number)]);
return decoded;
} }
char* run_length_decompress(char buffer[BUFSIZE * 2]){ char* run_length_decompress(char buffer[BUFSIZE * 2]){
char* decoded = (char*) calloc(BUFSIZE, sizeof(char)); char* decoded = (char*) calloc(BUFSIZE, sizeof(char));
int i = 0; int i = 0;
@ -157,6 +168,7 @@ char* run_length_decompress(char buffer[BUFSIZE * 2]){
return decoded; return decoded;
} }
void decompress(FILE* infile,FILE* outfile){ void decompress(FILE* infile,FILE* outfile){
char buffer[BUFSIZE * 2]; char buffer[BUFSIZE * 2];
memset(buffer, 0, BUFSIZE*2); memset(buffer, 0, BUFSIZE*2);
@ -174,4 +186,4 @@ void decompress(FILE* infile,FILE* outfile){
fwrite(outbuf,sizeof(char), strlen(outbuf),outfile); fwrite(outbuf,sizeof(char), strlen(outbuf),outfile);
} }
} }