/* To normalize a given text file
ie, only a single blank to exist between 2 given words
calculate the hidden message of the given file
as a function of the input file
get another hidden message from the user
and create the output file with the combined hidden message (watermarking)
not yet:=>Tried and tested :: The required output file has been generated */


#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<math.h>
#include<string.h>
void main()
{
    FILE *fp,*ft;
    char ch,s[80],temp[20],message[10],base[10],aux[80],hidden_copy[20],hidden[20],hh;
    // remember we can't use ASCII 0 as it is NULL
    int i,j,k=0,flag=0,c,count=0,check,mess[10],m,base_8[10],t,arr[20],ch1=32,ch2=9,tt1,tt2,l;
    fp=fopen("c:\\plain.txt","r+");
    ft=fopen("c:\\text.txt","w+");
    clrscr();

    if(fp==NULL)
    {
        printf("\n Unable to open File ");
        getch();
        exit(0);
    }

    while(!feof(fp))
    {
        ch=fgetc(fp);
        if(ch!=' ' && (int)ch!=9)
        {
            fputc(ch,ft);
            flag=0;
        }
        else
            flag++;
        if(flag==1)
            putc(' ',ft);

    }
fclose(fp);
rewind(ft);
printf("\n\n\n \t My normalized file is ...\n\n\n");
/*while(!feof(ft))
{
        printf("%c",fgetc(ft));
} */

    while(fgets(s,79,ft)!=NULL)
    {
        printf("%s ",s,count);
        count++;
    }
    count--;
printf("\n\tNo. of lines =>%d",count);

fclose(ft);

printf("\nEnter the main hidden message in small letters (< than %d characters)",count);
    gets(hidden_copy);
    l=strlen(hidden_copy);

// calculate the hidden message

fp=fopen("c:\\text.txt","r+");
if(fp==NULL)
    {
        printf("\n Unable to open File ");
        getch();
        exit(0);
    }
    while(fgets(s,79,fp)!=NULL)
    {
        i=0;j=0;check=0;count=0;
        while(s[i]!=NULL)
        {
            if(s[i]!=' ' && check==0)
            {

                temp[j++]=s[i];
                count+=int(s[i]);
                //printf(" %d",count);
                check=1;
            }
            if(s[i]==' ')
                check=0;

        i++;
        }
        temp[j]=NULL;
        m=count%26;
        printf("\n\t %s =%d %d",temp,count,m);
        message[k]='a'+m;
        mess[k]=m;
        k++;
    }
    message[k-1]=NULL;
    printf("\n\tCalculated mesage=%s",message);

    i=0;
    while(hidden_copy[i]!=NULL)
        i++;
    printf("\n i =>%d k-1=>%d",i,k-1);
/*    for(j=i;j<k-1;j++)
        hidden_copy[j]=' ';
    */
    while(i<(k-1))
    {
        hidden_copy[i]=32;
        i++;
    }
    hidden_copy[k-1]=NULL;
    //printf("\n testing : %s",hidden_copy);
    printf("\n testing =>");
    for(i=0;i<k-1;i++)
        printf("%c",hidden_copy[i]);
    getch();

    for(i=0;i<k-1;i++)
    {
    if(hidden_copy[i]==' ')
            hidden[i]='?'; // (63) base 10 ie (77) base 8
    else
            hidden[i]=hidden_copy[i]+message[i]-2*'a';
    }
    hidden[k-1]=NULL;
    printf("\n\tfinal message =%s",hidden);

    // change the base of the message to 8
    for(i=0;i<20;i++)
        base_8[i]=0;
    i=0;
    while(i<l)
    {
        hh=hidden[i];

            t=hh;
                j=0;
                while(t!=0)
                {
                    base_8[i]+=((t%8)*(pow(10,j)));
                    t/=8;
                    j++;
                }
        i++;
    }
    // hidden message in base 8::
    printf("\n\tChange base of the encrypted hidden message to 8:\n\t");
    for(i=0;i<k-1;i++)
        printf(" %d",base_8[i]);
    getch();
/*    printf("\n\t hidden message to be watermarked into the output file: ");
    for(i=0;i<k-1;i++)
        base[i]=base_8[i]+'a';
    base[i]=NULL;
    printf("%s",base);
    */
    // Watermark the hidden message into the final file
    rewind(fp);// the original file should be rewinded
    ft=fopen("c:\\final.txt","w+"); /* we will write the final file with
                                    the hidden message in c:\final.txt */
        k=0;
        while(fgets(s,79,fp)!=NULL)
        {
        i=0;j=0;count=0;flag=0;
        tt2=base_8[k]%10;
        tt1=(base_8[k]-tt2)/10;
        printf("\n\t tt1=%d tt2=%d",tt1,tt2);
            while(s[i]!=NULL)
            {
                if(tt1==count && flag==0)
                    aux[j++]=ch1;
                if(tt2==count && flag==0)
                    aux[j++]=ch2;
                if(s[i]!=' ')
                    {
                        aux[j]=s[i];
                        flag=1;
                    }
                if(s[i]==' ')
                    {
                        flag=0;
                        count++;
                        aux[j]=s[i];
                    }
                j++;
                i++;
            }
        k++;

        aux[j]=NULL;
        fputs(aux,ft);

    }

    rewind(ft);
    printf("\n\n\t The final file is :\n");
    while(!feof(ft))
        printf("%c",fgetc(ft));

    fclose(fp);
    fclose(ft);
getch();
}