add all
This commit is contained in:
parent
8e53fe8bac
commit
317af25699
8
sk2a/Makefile
Normal file
8
sk2a/Makefile
Normal file
@ -0,0 +1,8 @@
|
||||
all: program
|
||||
|
||||
program: compressor.c main.c
|
||||
gcc -Wall -g compressor.c main.c -o program
|
||||
|
||||
clean:
|
||||
rm program
|
||||
|
136
sk2a/compressor.c
Normal file
136
sk2a/compressor.c
Normal file
@ -0,0 +1,136 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "compressor.h"
|
||||
#define BUFSIZE 2000000
|
||||
#define BSIZE 9
|
||||
|
||||
int mycompress(char* buff,int size,char* outbuf){
|
||||
//memcpy(outbuf,buff,size);
|
||||
int k = 0;
|
||||
int head = 0;
|
||||
while(head < size && buff[head] > 0 ){
|
||||
|
||||
int bufferSize = BSIZE;
|
||||
if (head < bufferSize){
|
||||
bufferSize = head;
|
||||
}
|
||||
|
||||
char* searchBuffer = buff + head - bufferSize;
|
||||
|
||||
char* window = buff + head;
|
||||
|
||||
int lookAheadSize = BSIZE;
|
||||
if((size - head) < lookAheadSize){
|
||||
lookAheadSize = BSIZE - head;
|
||||
}
|
||||
//printf("Buffer: %s\n",buff);
|
||||
//printf("window: %s\n", window);
|
||||
//printf("head: %d\n", head);
|
||||
int wsz = BSIZE;
|
||||
int msz = wsz;
|
||||
if (bufferSize < wsz){
|
||||
msz = bufferSize;
|
||||
}
|
||||
int offset = -1;
|
||||
|
||||
int size = -1;
|
||||
|
||||
for(int i = 1; i<msz; i++){
|
||||
char* r = memmem(searchBuffer, bufferSize, window, i);
|
||||
if(r > 0){
|
||||
offset = bufferSize - ( r - searchBuffer );
|
||||
size = i+1;
|
||||
}else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(size > 0){
|
||||
//printf("%d",offset);
|
||||
//printf("%d",size-1);
|
||||
//printf("%c\n",buff[head+size-1]);
|
||||
sprintf(&outbuf[k],"%d%d%c",offset,size-1,buff[head+size-1]);
|
||||
k+=3;
|
||||
head+=size;
|
||||
|
||||
}else {
|
||||
//printf("00%c\n",buff[head]);
|
||||
sprintf(&outbuf[k],"00%c",buff[head]);
|
||||
k+=3;
|
||||
head+=1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
return k;
|
||||
}
|
||||
|
||||
|
||||
void compress(FILE* infile,FILE* outfile){
|
||||
char buffer[BUFSIZE];
|
||||
memset(buffer,0,BUFSIZE);
|
||||
while(1){
|
||||
int insize = fread(buffer,sizeof(char),BUFSIZE,infile);
|
||||
if (insize == 0){
|
||||
if (feof(infile)){
|
||||
// end of file
|
||||
break;
|
||||
}
|
||||
assert(!ferror(infile));
|
||||
}
|
||||
char outbuf[BUFSIZE*2];
|
||||
// Doplnte implementaciu kompresie.
|
||||
int outsize = mycompress(buffer,insize,outbuf);
|
||||
if (outsize > 0){
|
||||
fwrite(outbuf,sizeof(char),outsize,outfile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void decompress(FILE* infile,FILE* outfile){
|
||||
char code[3];
|
||||
char buffer[BUFSIZE];
|
||||
int size = 0;
|
||||
int k = 0;
|
||||
fseek(infile, 0, SEEK_END);
|
||||
long fileSize = ftell(infile);
|
||||
fseek(infile, 0, SEEK_SET);
|
||||
printf("size: %ld\n", fileSize);
|
||||
char letter;
|
||||
while(!feof(infile)){
|
||||
size = 0;
|
||||
memset(buffer,0,BUFSIZE);
|
||||
for(int j = 0; j < 4096; j++){
|
||||
fread(code, sizeof(char), 3, infile);
|
||||
int offset = atoi(code);
|
||||
int length = atoi(&code[1]);
|
||||
letter = code[2];
|
||||
offset=(offset-length)/10;
|
||||
if(offset == 0){
|
||||
buffer[size] = letter;
|
||||
size++;
|
||||
}else {
|
||||
for(int i = 0; i < length; i++){
|
||||
buffer[size] = buffer[size-offset];
|
||||
size++;
|
||||
}
|
||||
buffer[size] = letter;
|
||||
size++;
|
||||
}
|
||||
printf("%d\n",k);
|
||||
k+=3;
|
||||
}
|
||||
|
||||
fwrite(buffer, sizeof(char), size-1, outfile);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
18
sk2a/compressor.h
Normal file
18
sk2a/compressor.h
Normal file
@ -0,0 +1,18 @@
|
||||
#ifndef _COMPRESSORH
|
||||
#define _COMPRESSORH
|
||||
#include <stdio.h>
|
||||
|
||||
/**
|
||||
* Skomprimuje súbor in a zapíše do súboru out.
|
||||
* @arg in smerník na otvorený vstupný súbor (na čítanie)
|
||||
* @arg out smerník na otvorený výstupný súbor (na zápis)
|
||||
*/
|
||||
void compress(FILE* in, FILE* out);
|
||||
/**
|
||||
* Dekomprimuje súbor in a zapíše do súboru out.
|
||||
* @arg in smerník na otvorený vstupný súbor (na čítanie)
|
||||
* @arg out smerník na otvorený výstupný súbor (na zápis)
|
||||
*/
|
||||
void decompress(FILE* in, FILE* out);
|
||||
|
||||
#endif
|
24
sk2a/main.c
Normal file
24
sk2a/main.c
Normal file
@ -0,0 +1,24 @@
|
||||
#include "compressor.h"
|
||||
|
||||
int main(int argc,char** argv){
|
||||
if (argc != 4 || (argv[1][1] != 'c' && argv[1][1] != 'd')){
|
||||
printf("Usage: \n");
|
||||
printf(" Compress ./compress -c infile.txt outfile.compress\n");
|
||||
printf(" decompress ./compress -d outfile.compress infile.txt\n");
|
||||
return 0;
|
||||
}
|
||||
char* action = argv[1];
|
||||
char* infile = argv[2];
|
||||
char* outfile = argv[3];
|
||||
FILE* inf = fopen(infile,"r");
|
||||
FILE* outf = fopen(outfile,"w");
|
||||
if (action[1] == 'c'){
|
||||
compress(inf,outf);
|
||||
}
|
||||
else if (action[1] == 'd'){
|
||||
decompress(inf,outf);
|
||||
}
|
||||
fclose(inf);
|
||||
fclose(outf);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user