                       /* printnum.c, 12 Apr 10 */

#include <stdio.h>
#include <string.h>

#include "global.h"
#include "fltval.h"
#include "hashtab.h"
#include "../hash/hashtable_itr.h"
#include "matchnum.h"
#include "printnum.h"

BOOL do_print_all,
     do_print_no_match,
     do_summary_match = TRUE,
     do_summary_no_match;


void print_digit_seq(DIGIT_CHAR digit_seq[MAX_NUM_DIGITS],
                     int num_digits)
{
int c;

for (c=0; c < num_digits; c++)
   printf("%c", digit_seq[c]);
}


void print_number(DIGIT_CHAR mantissa[MAX_NUM_DIGITS],
                  int len_mantissa,
		  DIGIT_CHAR exponent[MAX_NUM_DIGITS])
{
print_digit_seq(mantissa, len_mantissa);
if (strlen(exponent) != 0)
   printf("e%s", exponent);
}


static void print_diff_plus_num(fp_t seen_num, fp_t ndb_num)
{
size_t seen_len,
       ndb_len,
       min_len;
int nindex;
DIGIT_CHAR seen_str[MAX_NUM_DIGITS];
DIGIT_CHAR ndb_str[MAX_NUM_DIGITS];

fp_to_string(seen_str, seen_num, MAX_NUM_DIGITS-1);
fp_to_string(ndb_str, ndb_num, MAX_NUM_DIGITS-1);
seen_len=strlen(seen_str);
ndb_len=strlen(ndb_str);
if (seen_len < ndb_len)
   min_len=seen_len;
else
   min_len=ndb_len;

for (nindex=0; nindex < min_len; nindex++)
   if (seen_str[nindex] == ndb_str[nindex])
      printf(".");
   else
      break;

for (; nindex < seen_len; nindex++)
   printf("%c", seen_str[nindex]);

printf("   ");

for (nindex=0; nindex < ndb_len; nindex++)
   printf("%c", ndb_str[nindex]);
}


void print_summary(void)
{
struct num_seen_rec *seen_num_ptr;
struct num_seen_keys *key;
struct hashtable_itr *itr;

if (do_summary_match)
   {
   printf("Matched number summary\n");

   // Iterator only returns a valid iterator if the hashtable is not empty
   itr = hashtable_iterator(saved_htable);
   if (hashtable_count(saved_htable) > 0)
      {
      do
         {
          key = hashtable_iterator_key(itr);
          seen_num_ptr = hashtable_iterator_value(itr);
          switch (seen_num_ptr->match_kind)
	     {
	     case LEV_SIGNIF:
	     case EXACT_SIGNIF: printf("%6d ", seen_num_ptr->file_occurrences);
                                print_digit_seq(seen_num_ptr->mantissa,
						   seen_num_ptr->len_mantissa);
			        printf("  ");
			        fp_print(seen_num_ptr->matched_num->num_valu);
			        printf("  %s", seen_num_ptr->matched_num->msg);
			        break;

	     case VALUE_MATCH: printf("%6d ", seen_num_ptr->file_occurrences);
			       print_diff_plus_num(seen_num_ptr->seen_valu,
		                          seen_num_ptr->matched_num->num_valu);
		               printf("  %s", seen_num_ptr->matched_num->msg);
			       break;

	     case NEED_MATCH: printf("%6d ", seen_num_ptr->file_occurrences);
			      printf("value sequence ");
		              printf("  %s", seen_num_ptr->matched_num->msg);

	     case NO_MATCH:
	     case NDB_MATCH: break;
	     }
          }
      while (hashtable_iterator_advance(itr));
      }
   }

if (do_summary_no_match)
   {
   printf("Unmatched number summary\n");

   itr = hashtable_iterator(saved_htable);
   if (hashtable_count(saved_htable) > 0)
      {
      do
	 {
         key = hashtable_iterator_key(itr);
         seen_num_ptr = hashtable_iterator_value(itr);
         if ((seen_num_ptr->file_occurrences != 0) &&
	     (seen_num_ptr->match_kind == NO_MATCH))
	    {
	    printf("%6d ", seen_num_ptr->file_occurrences);
            print_digit_seq(seen_num_ptr->mantissa,
					      seen_num_ptr->len_mantissa);
	    if (strlen(seen_num_ptr->exponent) == 0)
	       printf("\n");
	    else
	       printf("e%s\n", seen_num_ptr->exponent);
	    }
         }
      while (hashtable_iterator_advance(itr));
      }
   }
}

