/*
\n
01100100
============================================================================
Name : Oblig1-2.c
Author : Shahab Faghihi Moghaddam
Version : 0.1
Copyright : Shahab Faghihi Moghaddam
Description : Oblig1- Del 2
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Constants
#define SUCCESS 0
#define FAILED 0
//Defining Variables
struct _ReadLine{
struct _ReadLine *next;
char line[80];
};
typedef struct _ReadLine ReadLine;
//Defining functions
void checkit(int);
ReadLine readFile(char *);
int printFile(char *);
int commandAnalysorAndPerformer(char *,char *, char*);
int _performEncode(char*, char*);
char* _performDecode(char*);
void printOut(char*);
//end of decleration
/*
**** Main() FUNCTION *****************************************
*/
int main(int argc, char *argv[])
{
//Variables
//Clearing the SCREEN
system("clear");
//Code :
//Check if there are enough & valid parameters
checkit(argc);
/* Analyse and perform the command
* The first paramtere take the command, the 2nd take the output file
* and the last take the first node in the link list of the read file
*/
commandAnalysorAndPerformer(argv[1], argv[2], argv[3]);
//END of code
return SUCCESS;
}//end of main()
//*****************************************************************************
//Method check
void checkit(int _counter){
//Checking the input
if (_counter > 4){
printf("Too many parameters");
exit(0);
}//end of if
else if (_counter < 3){
printf("Too little parameters");
exit(0);
}//end of else if
}//end of check()
//*****************************************************************************
ReadLine readFile(char *_txtFileAddress){
FILE *file;
char strtemp[80];
ReadLine *temp = (struct _ReadLine*) malloc(sizeof(struct _ReadLine));
ReadLine *temp2;
ReadLine *FElement = (struct _ReadLine*) malloc(sizeof(struct _ReadLine));
file = fopen(_txtFileAddress, "r");
if(file==NULL) {
printf("\nError: can't open file.\n");
return *FElement;
}//end of if
else{
printf("\nReading phase, File Opened, operation in progress ... \n");
int i=0;
while(fgets(strtemp, 80, file) != 0){// keep looping until NULL pointer...
strcpy(temp->line, strtemp);
if (i==0) {FElement = temp; i++;}
temp2 = (struct _ReadLine*) malloc(sizeof(struct _ReadLine));
temp->next = temp2;
temp = temp2;
}//end of while reading file
//CLOSING THE FILE
fclose(file);
return *FElement;
}//end of else
}//end of readfile
//*****************************************************************************
int commandAnalysorAndPerformer(char *_command, char *_secondFileAddress, char *_thirdFileAddress){
char *_result = "";
//VARIABLES char *_result = "";
char _textFile[30];
char _compressedFile[30];
//char _result[100];
/*
* One of my problems is that I dont check exactly if the file names are right and allowded or not !!!
* the order of the input and output files should be carefully written if not the program can crash ...
* the program doesnt accept for example txt files for printing neithere for decoding ...
* Also the program has weakness to read all the characters at the end of the file, for example if total
* number of characters in the file %4 are more than 0, then (lenghtofchar%4) wont be read from the
* end of the file
*/
if (_secondFileAddress != 0){
if (strstr(_secondFileAddress,".txt") != 0)
strcpy(_textFile,_secondFileAddress);
else
strcpy(_compressedFile,_secondFileAddress);
}//end of _secondFileAddress
if (_thirdFileAddress != 0){
if (strstr(_thirdFileAddress,".compressed") != 0)
strcpy(_compressedFile,_thirdFileAddress);
else
strcpy(_compressedFile,_secondFileAddress);
}//end of if
//************************************************
if (!strcmp(_command,"p")){
printf("\nPRINTING\n");
//strcpy(_result,_performDecode(_compressedFile));
_result = _performDecode(_compressedFile);
printf("Hellllooooo");
printOut(_result);
}//end of if
else if (!strcmp(_command,"e")){
printf("\nENCODING ...\n Please Wait ...\n");
_performEncode(_textFile, _compressedFile);
}//end of else if (!strcmp(_command,"e"))
else if (!strcmp(_command,"d")){
printf("\nDECODING ...\n");
strcpy(_result,_performDecode(_compressedFile));
printOut(_result);
}//end of else if (!strcmp(_command,"d"))
else {
printf("\nCommand Not Recogonized, Please try again later ...\n");
exit(0);
}//end of else
return 0;
/*
if (!strcmp(_command,"p")){
printf("\nPRINTING\n");
_result = _performDecode(_secondFileAddress);
printOut(_result);
}//end of if
else if (!strcmp(_command,"e")){
printf("\nENCODING ...\n Please Wait ...\n");
_performEncode(_secondFileAddress, _thirdFileAddress);
}//end of else if (!strcmp(_command,"e"))
else if (!strcmp(_command,"d")){
printf("\nDECODING ...\n");
_result = _performDecode(_secondFileAddress);
printOut(_result);
}//end of else if (!strcmp(_command,"d"))
else {
printf("\nCommand Not Recogonized, Please try again later ...\n");
exit(0);
}//end of else
return 0;
*/
}//end of commandAnalysor
//*****************************************************************************
/*
* The problem in encoding is that we cant manage to read all the
* last characters at the end. For example if we have 33 character
* to read and encode, we just manage to do 32 and 33%4 will remain
* unread.
*/
int _performEncode(char *_txtFileAddress, char *_compressedFileAddress){
//Variables
FILE *file;
int i = 0, j=0;
ReadLine *fEl = (ReadLine*) malloc(sizeof(ReadLine));
char _result[5000]="";
unsigned char buffer;
int count = 0;
int encode[4];
//reading from the txt file
*fEl = readFile(_txtFileAddress);
while (fEl != 0){
strcat(_result, fEl->line);
fEl = fEl->next;
}//end of while and the whole text from the input file is in the _result variable
//opening file for write operation
file = fopen(_compressedFileAddress, "wb");
if (file == NULL){
printf("Error creating output file ..."); exit(0);}
else
{
printf("output file created ...\n"
"Encoding in progress ...\n"
"Please wait ...\n");
for (i=0; i < strlen(_result); i++){
switch (_result
){
case ' ' : encode[count] = 0; break;
case ':' : encode[count] = 1; break;
case '@' : encode[count] = 2; break;
case '\n': encode[count] = 3; break;
default : printf("Illegal character found = %c. in number %d\n", _result,i);exit(0);
}//end of SWITCH CASE
if ( (i+1)%4 == 0 && (count+1 == 4)){
for (j=0; j<4; j++)
buffer = buffer | (encode[j]<<((3-j)*2));
fwrite(&buffer,1,1,file);
buffer = 0;
count = 0;
}//end of if i%4
else
count++;
}//end of for
}//end of else
fclose(file);
printf("\nFinished encoding ...\n");
}//_performEncode(ReadLine);
//*****************************************************************************
char* _performDecode(char *_compressedFileAddress){
//VARIABLES
FILE *file;
char _result[5000];
unsigned char c[5];
unsigned char temp;
int j;
int tempint[4];
printf("Trying to open compressed file : %s \n",_compressedFileAddress);
file = fopen(_compressedFileAddress, "rb");
if(file==NULL) {
printf("\nError: can't open file.\n");
exit(0);
return NULL;
}//end of if
else{
printf("\nFile Opened, operation in progress ... \n\n");
//*c = 240;
//int i = 10;
while(fread(c,1,1,file) != 0){// keep looping until NULL pointer...
temp = *c;
int RValue = 192;
for (j=0;j<4;j++){
tempint[j] = temp & (RValue);
if (j!=3)
RValue = RValue / 4;
tempint[j] = tempint[j] >> ((3-j)*2);
}
for (j=0;j<4;j++){
switch ( tempint[j] ){
case 0 : strcat(_result," ");break;
case 1 : strcat(_result,":");break;
case 2 : strcat(_result,"@");break;
case 3 : strcat(_result,"\n");break;
default : printf("***** Illegal character found *****\nThe illegal character code is : %d\nExiting the program ...\n",tempint[j]);exit(0);
}//end of SWITCH CASE
}//end of for
temp = *c;
}//end of while which is for reading file
//CLOSING THE FILE
fclose(file);
}//end of else
char *strtemp = _result;
return strtemp;
}//end of _performDecode(ReadLine *fEl)
//*****************************************************************************
void printOut(char *_result){
printf("The result of decoding is :\n %s\n\n\n", _result);
}//end of printOut
//*****************************************************************************