Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define DEBUG 1
- void dict_print(unsigned char **dictionary, int patLength[])
- {
- int i, j;
- printf("Dictionary at this point: \n");
- for(i=0;i<1000;i++)
- {
- if(patLength[i]!=0)
- {
- printf("Entry #%d: ", i);
- for(j=0;j<patLength[i];j++)
- printf("%d ",(int)dictionary[i][j]);
- printf(" - PatLength: %d\n",patLength[i]);
- }
- }
- printf("End Dictionary Print\n");
- }
- int contains(unsigned char *string, unsigned char **dictionary, int *patLength)
- {
- int i, j;
- int foundMatch = 0;
- for(i=0;i<1000;i++)
- {
- foundMatch = 1;
- for(j=0;j<strlen((char *)string);j++)
- {
- if(string[j] != dictionary[i][j])
- {
- foundMatch = 0;
- break;
- }
- }
- if(foundMatch) break;
- }
- if(foundMatch) {
- printf("Found match for %s, index in dictionary is %d.\n", string, i);
- return 1;
- }
- printf("Did not find match for %s.\n", string);
- return 0;
- }
- unsigned char *my_concat(unsigned char *str, unsigned char ch)
- {
- size_t sizeOfChar = sizeof(unsigned char);
- size_t sizeOfStr = sizeof(unsigned char) * strlen((char *)str);
- size_t sizeOfNewStr = sizeof(unsigned char) * (strlen((char *)str) + 2);
- unsigned char *newStr = malloc(sizeOfNewStr);
- memset(newStr, 0, sizeOfStr);
- memcpy(newStr, str, sizeOfStr);
- memcpy(newStr + sizeOfStr, &ch, 1);
- memcpy(newStr + sizeOfStr + sizeOfChar, "\0", 1);
- return newStr;
- }
- int main(int argc, char *argv[])
- {
- FILE *fptInput, *fptOutput;
- int i, j;
- unsigned char *dictionary[1000]; // contains patterns
- int patLength[1000]; // contains corresponding length of patterns
- unsigned char *string; // most recent pattern
- unsigned char character; // current character being read
- unsigned char *newString; // string + character, used to check dictionary
- // make sure correct command line arguments
- if(argc != 3)
- {
- printf("Error, not enough command line arguments.\n");
- printf("Usage: ./rle [-c or -d] [filename]\n");
- // exit(0);
- }
- // open input file from command line and check for correctness
- if((fptInput=fopen(argv[2], "rb"))==NULL)
- {
- printf("Error opening file %s \n",argv[2]);
- // exit(0);
- }
- // initialize patLength and dictionary
- for(i=0;i<1000;i++)
- {
- dictionary[i] = NULL;
- patLength[i] = 0;
- }
- // initialize alphabet of dictionary
- for(i=0;i<256;i++)
- {
- dictionary[i] = (unsigned char *)malloc(sizeof(char));
- dictionary[i][0] = i;
- patLength[i] = 1;
- }
- // print initialization
- if(DEBUG) dict_print(dictionary, patLength);
- i = contains((unsigned char *)"1", dictionary, patLength);
- // if we are in compress mode
- if(!strcmp(argv[1],"-c"))
- {
- string = NULL;
- while((i=fread(&character,1,1,fptInput))==1)
- {
- printf("Character read in from file: %c\n",character);
- newString = my_concat(string, character);
- if(contains(newString, dictionary, patLength))
- {
- printf("Found match for %s\n", newString);
- string = newString;
- }
- else {
- printf("We need to encode %s to output and add %s to dictionary\n",
- string, newString);
- }
- }
- }
- // if we are in decompress mode
- else if(!strcmp(argv[1],"-d"))
- {
- }
- // if we aren't in compress or decompress mode, we got an incorrect input flag
- else
- {
- printf("Error. Incorrect input flags, must be -c or -d.\n");
- printf("Usage: ./rle [-c or -d] [filename]\n");
- }
- // close files
- fclose(fptInput);
- fclose(fptOutput);
- // free all the memory
- for(i=0;i<1000;i++)
- {
- if(dictionary[i]!=NULL) free(dictionary[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement