#ifndef TEXT_H
#define TEXT_H


#include <String.h>
#include <stream.h>
#include <fstream.h>
#include <ctype.h>        
#include "perm.hh"
#include "vign.hh"
            
//============================================================
// TEXT - The TEXT class represents a cipher-text which must
//        only contain space and lower case letters.    
//        PrintVign allows you to print TEXT after
//        decrypting it with a Vigenere key
//        PrintPerm allows you to print TEXT after
//        decrypting with a permutation
//============================================================


class TEXT
{
public:
    int  length;
    char *data;

    static int char2num(int c)
        {
            if( isalpha(c) )
                return tolower(c) - 'a' + 1;
            else
                return 0;
        }

    static int num2char(int c)
        {
            if( c == 0)
                return ' ';
            else
                return c - 1 + 'a';
        }


    TEXT(char *filename)
        {
            int c;
            
            ifstream input(filename);
            input.seekg(0,ios::end);
            length = input.tellg();
            input.seekg(0,ios::beg);

            data = new( char[length+1] );

            length = 0;
            
            while( EOF != (c = input.get()) )
            {
                if( c == '\n' )
                {
                    continue;
                }
                else
                {
                    data[length++] = char2num(c);
                }
            }

        }
    

    ~TEXT()
        {
            delete(data);
        }
    
    int Length()
        {
            return length;
        }
    
    int Char(int i)
        {
            return data[i];
        }

    void PrintVign(ostream& o, int linelen, VIGN *v)
        {
            
            for( int i=0, count=0; i < Length(); i++ )
            {
                char c = num2char(v->Translate(Char(i),i));
                if( c == ' ' && count >= linelen)
                {
                    count = 0;
                    o << endl;
                }
                else
                {
                    count++;
                    o << c;
                }
            }

            o << endl;
  
        }
    
    void PrintPerm(ostream& o, PERM *p, int linelen)
        {
            
            for( int i=0, count=0; i < Length(); i++ )
            {
                char c = num2char(p->Translate(Char(i)));
                if( c == ' ' && count >= linelen)
                {
                    count = 0;
                    o << endl;
                }
                else
                {
                    count++;
                    o << c;
                }
            }

            o << endl;
        }

        void PrintPerm(ostream& o,PERM *p, int start, int end)
        {
            
            while( start < end )
            {
                o << char(num2char(p->Translate(Char(start++))));
            }
        }

    
};


#endif



