Thursday, August 1, 2013

File Compressor in C++

Hello friends, Here is the code of File Compressor...

The main features of this program are:

  1. Works like note pad (i.e create, open, save , text document )
  2. Can generate random bits of data
  3. Can compress binary data (i.e If there are 10 or more 1’s consecutively replace them +10+, +11+, ... +20+, …..  OR in case of 0’s replace them -10-, -11-,-12-, ....... -20-, ....
  4. Can uncompressed data (i..e replace +10+ with '1111111111' , -10-, '000000000' , ....... )
  5. GUI interface,........
Pictorial View:






Codding:
void CompressFile(HWND hWnd, HWND hEdit)
{
int len = GetWindowTextLength(hEdit);
if(len <= 0)
{
MessageBox(hWnd,"THERE IS NO TEXT TO COMPRESS",ERROR,MB_OK);
return;
}
else{
int iChars = len+1;
CHAR* pstrText;
CHAR* compText;
CHAR* buff;
if ((pstrText = (CHAR*) malloc (sizeof (CHAR) * iChars)) != NULL) {
compText = (CHAR*) malloc (sizeof (CHAR) * (iChars));
buff = (CHAR*) malloc (sizeof (CHAR) * iChars);
compText[0] = buff[0] = '\0';
GetWindowText (hEdit, pstrText, iChars);
int i,count_0,count_1;
i = count_0 = count_1 = 0;
char value[10];
while(i<=len)
{
if(pstrText[i] == '1')
{
int l = 0;
while(pstrText[i] == '1')
buff[l++] = pstrText[i++];
buff[l] = '\0';
if(l>=10)
{
strcat(compText,"+");
itoa(l,value,10);
strcat(compText,value);
strcat(compText,"+");
}
else
strcat(compText,buff);
}
else if(pstrText[i] == '0')
{
int l = 0;
while(pstrText[i] == '0')
buff[l++] = pstrText[i++];
buff[l] = '\0';
if(l>=10)
{
strcat(compText,"-");
itoa(l,value,10);
strcat(compText,value);
strcat(compText,"-");
}
else
strcat(compText,buff);
}
else
{
buff[0]=pstrText[i++];
buff[1]='\0';
strcat(compText,buff);
}
}
SetWindowText(hEdit, compText);
free (buff);
free (pstrText);
free (compText);
}
}
}
void UncompressFile(HWND hWnd, HWND hEdit)
{
int len = GetWindowTextLength(hEdit);
if(len <= 0)
{
MessageBox(hWnd,"THERE IS NO TEXT TO UNCOMPRESS",ERROR,MB_OK);
return;
}
else{
int iChars = len+1;
CHAR* pstrText;
CHAR* uncompText;
CHAR* buff;
if ((pstrText = (CHAR*) malloc (sizeof (CHAR) * iChars)) != NULL) {
buff = (CHAR*) malloc (sizeof (CHAR) * iChars);
buff[0] = '\0';
int t_size = 0;
GetWindowText (hEdit, pstrText, iChars);
char str[100];
int i=0;
while(i<=len)
{
if(pstrText[i] == '-'){
int l = 0;
while(pstrText[++i] != '-')
buff[l++] = pstrText[i];
buff[l] = '\0';
t_size = t_size + atoi(buff);
}
else if(pstrText[i] == '+')
{
int l = 0;
while(pstrText[++i] != '+')
buff[l++] = pstrText[i];
buff[l] = '\0';
t_size = t_size + atoi(buff);
}
else
t_size++;
i++;
}
if ((uncompText = (CHAR*) malloc (sizeof (CHAR) * t_size)) != NULL) {
uncompText[0]='\0';
int j=0;
i=0;
while(i
{
if(pstrText[i] == '-')
{
int l = 0;
while(pstrText[++i] != '-')
buff[l++] = pstrText[i];
buff[l] = '\0';
t_size = atoi(buff);
int k = 0;
while(k < t_size)
{uncompText[j++] = '0';k++;}
}
else if(pstrText[i] == '+')
{
int l = 0;
while(pstrText[++i] != '+')
buff[l++] = pstrText[i];
buff[l] = '\0';
t_size = atoi(buff);
int k = 0;
while(k < t_size)
{uncompText[j++] = '1';k++;}
}
else
uncompText[j++] = pstrText[i];
i++;
}
uncompText[j]='\0';
SetWindowText(hEdit, uncompText);
free (uncompText);
}
free(pstrText);
free(buff);
}
}
}
//Genrate Random Bits
void GenrateRandomBits(HWND hWnd, HWND hEdit)
{
char randomData[10000];
randomData[0] = '\0';
char Buff[2];
int randomBit;
for (int i = 0; i < 8000; i++)
{
randomBit = rand()%2;
itoa(randomBit,Buff,10);
strcat(randomData,Buff);
}
SetWindowText(hEdit, randomData);
}
view raw filecompressor hosted with ❤ by GitHub
   

No comments:

Post a Comment