Created
March 11, 2015 18:06
-
-
Save Suavac/e2326ac1cff951a08783 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
char *array[5]; // Array of tokens | |
void readCommand(char *commandString){ // A utility function used for splitting given command on parts - string token, use of space as a divider | |
int i = 0; char *token; | |
// Array[i] holds the following | |
array[0] = NULL; // Operation (encode. decode) | |
array[1] = NULL; // Target (file, console) | |
array[2] = NULL; // Input file path | |
array[3] = NULL; // Optional operation ('write' to the file) | |
array[4] = NULL; // Output file path | |
token = strtok(commandString, " "); | |
while (token != NULL){ | |
array[i++] = token; | |
token = strtok (NULL, " "); | |
} | |
} | |
char *processFileName(char *filename, int fileType){ | |
/* The utility function for validating file name provided by the user. | |
It allowes for entering the file names with or without their extenstions, | |
and helps to avoid mistakes in naming files e.g. file.dat.dat */ | |
char extension[][5]={".dat",".txt"}; // Files extension | |
char ext[]={".ext"}; // Holds the extension provided by user | |
int len = strlen(filename); // Save length of path | |
char* pos; // Position of the character | |
pos = (filename+len-4); // Make pos point to the 4'th last char of string '.' | |
strcpy(ext, pos); // Get last 4 characters '.ext' | |
if(!strcmp(ext,extension[fileType])){ // Compare to the standard extensions types | |
*pos='\0'; // If standard, delete extension provided by the user | |
} | |
char filepath[50]; // Holds the file path | |
strcpy(filepath, filename); // Copy file path | |
strcat(filepath, extension[fileType]);// Add standard extension | |
return _strdup(filepath); | |
} | |
/* Used in writing to file operations. Function checks if file already exists in the given location, | |
displays the warning message and prompts for decision on overwriting/terminating operation. | |
It returns an integer value used as condition within runCommand() function. This function also | |
validates and manage the files extensions distribution */ | |
int validatePath(char *path, int fileType){ | |
char choice; // Holds user choice on overwriting existing file | |
int write; // Holds the decision on further action | |
char filepath[50]; // Holds the file path | |
strcpy(filepath, processFileName(path, fileType)); // manage the file extension | |
FILE *fptr = fopen(filepath, "rb"); // Check if file exists | |
if ( fptr != NULL ) { // If exists | |
fclose(fptr); | |
printf("\n -- WARNING ! : File already exists! 'y' - yes, else - no \n"); | |
printf("\n Overwrite? "); | |
scanf("%c", &choice); // Ask for decision | |
if(choice=='y'){ | |
write = 1; // Store the decision | |
} | |
else { | |
write = 0; | |
} | |
} | |
else if ( fptr == NULL ){ // If doesn't exist continue writing file | |
write=1; | |
} | |
return write; | |
} | |
// PRINTING FUNCTIONS---------------------------------------------------------------------------------------------------------------------------- | |
/* Prints the table of the unique characters,their frequencies and binary prefixes (Huffman Codes) | |
Function is performing additional sort of characterBINARY array created in HuffmanEncoding.h | |
in order to match codes with their corresponding letters | |
*/ | |
void print_Table(char character[], int frequency[], char string[], int size ){ | |
// Attach the binary codes to the corresponding letters in order to print the table correctly | |
char temp2[250][20]; // Temp array used for printing codes in the table | |
for (int i = 0; i < strlen(character); ++i){ // For each of unique characters in 'character' array | |
for(int j=0; j < strlen(characterASCII); ++j ){ // Scan through 'characterASCII' array | |
if(character[i]==characterASCII[j]){ // If match found | |
strcpy(temp2[i], characterBINARY[j]); // Copy 'characterBinary' data in the same index what 'character' is located | |
} | |
} | |
} | |
printf("\n\n Table >>> BINARY prefixes:\n\n"); | |
printf(" ID | Char | Freq | Binary Code\n"); | |
printf(" -------------------------------- \n"); | |
for (int i=0; i < strlen(character); i++){ | |
printf(" %5d | %5c | %5d | %s \n", (i+1), character[i], frequency[i],temp2[i]); | |
} | |
printf(" ________________________________ \n"); | |
printf("\n Total characters: %d ; Unique characters: %d", strlen(string), size); | |
printf("\n\n"); | |
} | |
/* Prints the error messages based on given circumstance. It indicates the reason of error */ | |
void printErrorCode(int errorCode, char *filePath){ // Function prints the error messages | |
switch(errorCode){ | |
case 1:printf("\n -- syntax error 1 : The command was not recognised! | type help for the commands list\n\n");break; | |
case 2:printf("\n -- syntax error 2 : Missing input file path \n");break; | |
case 3:printf("\n -- syntax error 3 : Missing output file path \n");break; | |
case 4:printf("\n -- access error 4 : The '%s' file does not exist at this location !\n\n",filePath);break; | |
case 5:printf("\n -- limit error : There must be atleast 2 unique characters !\n\n"); | |
printf(" --q to Quit , --help for Help\n\n");break; | |
case 6:printf("\n -- syntax error 5 : No target specyfied! \n");break; | |
case 7:printf("\n\n\n -- access error : Can't save '%s' file : ACCES DENIED!\n\n",filePath);break; | |
} | |
} | |
/* Prints the 'Help' informations */ | |
void print_Help(){ // | |
printf("\n\n COMMAND LIST \n\n"); | |
printf(" ----------------------------------------------------------- \n\n"); | |
printf(" encode | run encoding operation \n\n"); | |
printf(" decode | run decoding operation \n\n"); | |
printf(" file <path> | reads file from the given location \n\n"); | |
printf(" console | reads text from the console \n\n"); | |
printf(" write <path> | writes file to the given location\n\n\n"); | |
printf(" clear | clear the screen \n\n"); | |
printf(" help | Help \n\n"); | |
printf(" exit | Exit program \n\n"); | |
printf(" ----------------------------------------------------------- \n\n"); | |
printf(" Use of the commands:\n\n"); | |
printf(" 1. encode file <file path>.txt \n"); | |
printf(" 2. encode file <file path>.txt write file.dat \n"); | |
printf(" 3. encode console \n"); | |
printf(" 4. encode console <file path>.dat \n"); | |
printf(" 5. decode <file path>.dat \n"); | |
printf(" 6. decode <file path>.dat write file.txt \n"); | |
printf(" NOTE: The files extensions are assigned automaticly\n\n"); | |
} | |
/* Prints header of the program */ | |
void print_Header(){ | |
printf("\n HUFFMAN ENCODING-DECODING\n"); | |
printf("_____________________________________________________\n\n"); | |
printf(" ----------------------\n"); | |
printf(" Type 'help' for Help \n"); | |
printf(" ----------------------\n"); | |
} | |
// END OF PRINTING FUNCTIONS--------------------------------------------------------------------------------------------------------------------- | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment