diff --git a/Makefile b/Makefile index 42dd0d67c0919bab4eec90d5e05e2719fe9f5ac8..505c047c4339d15a39c604d9c8cef9f06a6d1213 100644 --- a/Makefile +++ b/Makefile @@ -6,18 +6,14 @@ LIB=./lib INCLUDE=./include SRC=./src OBJ=./obj -FLAGS = -Wall +FLAGS = -Wall -lm -main: connectionRawSocket messages - $(CC) $(SRC)/main.cpp $(OBJ)/connectionRawSocket.o $(OBJ)/messages.o $(FLAGS) -I$(INCLUDE) -L$(LIB) -o connection +main: lzw + $(CC) $(SRC)/main.cpp $(OBJ)/lzw.o $(FLAGS) -I$(INCLUDE) -L$(LIB) -o lzw -connectionRawSocket: - $(CC) -c $(SRC)/connectionRawSocket.cpp $(FLAGS) -I$(INCLUDE) -o $(OBJ)/connectionRawSocket.o - ar -cru $(LIB)/connectionRawSocket.a $(OBJ)/connectionRawSocket.o - -messages: - $(CC) -c $(SRC)/messages.cpp $(FLAGS) -I$(INCLUDE) -o $(OBJ)/messages.o - ar -cru $(LIB)/messages.a $(OBJ)/messages.o +lzw: + $(CC) -c $(SRC)/lzw.cpp $(FLAGS) -I$(INCLUDE) -o $(OBJ)/lzw.o + ar -cru $(LIB)/lzw.a $(OBJ)/lzw.o clean: - rm connection $(SRC)/*~ $(OBJ)/*o $(LIB)/*a + rm lzw $(SRC)/*~ $(OBJ)/*o $(LIB)/*a diff --git a/include/lzw.h b/include/lzw.h index e45e61474a3271a65ef36ba6c06f278e54ff6995..63472dd273e68efcd437fefe2610839657648d81 100644 --- a/include/lzw.h +++ b/include/lzw.h @@ -1,8 +1,14 @@ +// Implementado por Eduardo Machado e Victor Perszel +// 2015 + #include <iostream> #include <string> +#include <fstream> +#include <map> +#include <cstdlib> +#include <cmath> + +using namespace std; -class LZW{ - public: - int* pack(string &input); - string unpack(int input[]); -}; \ No newline at end of file +void pack(string input, int* intOutput); +string unpack(int* input); \ No newline at end of file diff --git a/src/lzw.cpp b/src/lzw.cpp index 885f21b495718afca87c53e7819436b9540e406d..c3ca61d2b074f2360ca14757bf5b93773e34c493 100644 --- a/src/lzw.cpp +++ b/src/lzw.cpp @@ -1,40 +1,44 @@ // Implementado por Eduardo Machado e Victor Perszel +// 2015 #include "lzw.h" using namespace std; -int* LZW::pack(string &input){ +void pack(string input, int* intOutput){ map<string, int> dictionary; string output = ""; string nextChar, currentChar = ""; - int i, temp, n, lastI, cont=0, dictionarySize = 256; - int *intOutput; - intOutput = new int[dictionarySize-1]; + int i, j, temp, n = 0, size, lastI, cont=0, dictionarySize = 256; //inicializa o dicionário for(i = 0; i < 256; i++){ dictionary[string(1, i)] = i; } - for(i = 0; i < input.size(); i++){ + size = input.size(); + for(i = 0; i < size; i++){ nextChar = input[i]; if(dictionary[currentChar + nextChar] != 0){ currentChar = currentChar + nextChar; }else{ output += dictionary[currentChar] + " "; +cout << dictionary[currentChar] << endl; +cout << output << endl; dictionary[currentChar+nextChar] = dictionarySize; currentChar = nextChar; dictionarySize++; cont++; } +//cout << output << endl; } output += dictionary[currentChar] + " "; cont++; // --------------------- CONVERSAO -------------------------- + intOutput = new int[dictionarySize-1]; do{ if(output[i] != ' ' && output[i] != '\0'){ temp++; @@ -49,15 +53,15 @@ int* LZW::pack(string &input){ i++; }while (output[i-1] != '\0'); - //----------------------------------------------------------- +cout << intOutput << endl; - return &intOutput; + //----------------------------------------------------------- } -string LZW::unpack(int input[]){ +string unpack(int* input){ map<int, string> dictionary; string currentChar, nextChar, output = ""; - int i, dictionarySize = 256; + int i, size, dictionarySize = 256; int currentWord, nextWord; //inicializa o dicionário @@ -67,7 +71,8 @@ string LZW::unpack(int input[]){ nextWord = input[0]; output += dictionary[nextWord]; - for(i = 0; i < sizeof(input)/sizeof(int); i++){ + size = sizeof(input)/sizeof(int); + for(i = 0; i < size; i++){ currentWord = nextWord; nextWord = input[i+1]; if(dictionary[nextWord] != ""){ diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c168a4d823859ca7d2e862cee3f375a919a440d2 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,70 @@ +// Implementado por Eduardo Machado e Victor Perszel +// 2015 + +#include "lzw.h" + +using namespace std; + +int main(int argc, char *argv[]) +{ + ifstream fileIn; + ofstream fileOut; + string parameter, temp, nameFileIn, nameFileOut, inputPack="", outputUnpack; + int *outputPack=NULL, *inputUnpack, length; + + // Leitura de parâmetros. + if(argc != 4){ + cout << "Arquivo não encontrado!" << endl; + return 0; + } + nameFileIn=argv[1]; + parameter=argv[2]; + nameFileOut=argv[3]; + + + if(parameter == "pack"){ + // Abertura do arquivo de entrada. + fileIn.open(nameFileIn.c_str(), ios::in); + fileIn.seekg (0); + + // Leitura do arquivo. + while(getline(fileIn, temp)) { + inputPack += temp + "\n"; + } + + // Compactação. + pack(inputPack, outputPack); + + // Escrita no arquivo de saída. + fileOut.open(nameFileOut.c_str(), ios::binary); + fileOut << outputPack; + }else if(parameter == "unpack"){ + // Abertura do arquivo de entrada. + fileIn.open(nameFileIn.c_str(), ios::binary); + + // Contagem do tamanho do arquivo. + fileIn.seekg (0, ios::end); + length = fileIn.tellg(); + fileIn.seekg (0, ios::beg); + + // Alocação do vetor para a leitura do arquivo. + inputUnpack = new int[length/4]; + + // Leitura. + fileIn.read((char*)inputUnpack, sizeof(inputUnpack)); + + // Descompactação. + outputUnpack = unpack(inputUnpack); + + // Escrita no arquivo de saída. + fileOut.open(nameFileOut.c_str(), ios::out); + fileOut << outputUnpack; + }else{ + cout << "Parâmetro inválido!" << endl; + } + + // Fecha os arquivos + fileIn.close(); + fileOut.close(); + return 0; +} \ No newline at end of file diff --git a/teste.txt b/teste.txt new file mode 100644 index 0000000000000000000000000000000000000000..41a270e5c7beef3de3d8d62c32773efbf8bbac34 --- /dev/null +++ b/teste.txt @@ -0,0 +1,3 @@ +apenas uma frase +ou duas +apenas para testar.