/**
 * Display functions used only for debugging
 *
 * Author: Ramkumar Natarajan (nrk@cs.purdue.edu)
 **/

#include "printfuncs.h"

#ifdef DEBUG_
void printMap(int **patmap, int patpos, int *patbuf, int alpha_seen, int wordlen) {

    int i, j, thismap;

    for ( i = 0; i < MAX_WORDLEN_; ++i ) {
        if ( ! (patpos & (1 << i)) )
            continue;
        printf("Char at pos: %d\n", i);
        thismap = *patmap[i];
        printf("Map is: " );
        for ( j = 0; j < wordlen; ++j ) {
            if ( (thismap & (1 << j)) )
                printf("1");
            else
                printf("0");
        }
        printf("\n");
    }

    for ( i = 0; i < ALPHA_SZ_; ++i ) {
        if ( alpha_seen & aleph_codes[i] ) {
            printf("Char: %c, Map: ", INDEX_TO_CHAR(i));
            for ( j = 0; j < wordlen; ++j ) {
                if ( patbuf[i] & (1 << j) )
                    printf("1");
                else
                    printf("0");
            }
            printf("\n");
        }
    }
}

void printWord(char *cptr, int len) {
    int i;

    for ( i = 0; i < len; ++i )
        fprintf(stderr, "%c", cptr[i]);
}

void printDict(Dictionary *dptr, Bool printwords) {
    int   i, j;
    char *cptr;

    printf("dptr->wordlen = %d\n", dptr->wordlen);
    if ( !dptr->isRead ) {
        printf("Not read\n");
        return;
    }
    printf("dptr->nwords  = %d\n", dptr->nwords);
    if ( !printwords ) return;

    cptr = dptr->words;
    for ( i = 0; i < dptr->nwords; ++i ) {
        for ( j = 0; j < dptr->wordlen; ++j ) {
            putchar((int) *cptr++);
        }
        putchar('\n');
    }
}

void printCipherWord(CipherWord *wptr) {
    int i;

    printf("Word: %s\n", wptr->word);
    printf("Len:  %d\n", wptr->len);
    if ( wptr->npossibles ) {
        char **wordlist = wptr->possiblewords;
        int    nprinted = 0;

        printf("Possible Words:\n");
        for ( i = 0; i < wptr->npossibles; ++i ) {
            char *word = wordlist[i];
            int j;
            if ( !word ) continue;
            for ( j = 0; j < wptr->len; ++j ) {
                printf("%c", word[j]); 
            }
            if ( nprinted && !(nprinted % 8) )
                printf("\n");
            else printf(", ");
        }
        printf("\n");
    }
    else {
        printf("No Possible Matches\n");
    }
}

void printCipherChar(CipherChar *ccptr) {
    int i;
    
    if ( !ccptr->isPresent )
        return;

    printf("Maps to: ");
    if ( ccptr->plainmap == ALEPH_ALL )
        printf("All\n");
    else {
        for ( i = 0; i < ALPHA_SZ_; ++i ) {
            if ( ccptr->plainmap & aleph_codes[i] ) {
                printf("%c, ", INDEX_TO_CHAR(i));
            }
        }
        printf("\n");
    }
}

void printCipherText(CipherText *ctptr) {
    int i;

    printf("========================= printCipherText =============================\n");
    printf("Original Ciphertext: %s\n", ctptr->ociphertxt);
    for ( i = 0; i < ctptr->ncipherwords; ++i ) {
        CipherWord *wptr = ctptr->cipherwords[i];
        printf("------ Cipherword %d ------\n", i+1);
        printCipherWord(wptr);
    }
    printf("-----------------------------------------------------------------------\n");
    printf("---- CipherMap ----\n");
    for ( i = 0; i < ALPHA_SZ_; ++i ) {
        if ( ctptr->ciphermap[i].isPresent ) {
            printf("---- Cipherchar: %c ----\n", toupper((int) INDEX_TO_CHAR(i)));
            printCipherChar(ctptr->ciphermap + i);
            printf("\n\n");
        }
    }
}
#endif
