#include "irfiles.h"
Boolean loggingUser = FALSE;
FILE* userLog = NULL;
char *log_file_name = NULL;

FILE *logfile; /* the logfile */

main(argc, argv)
char *argv[];
int argc;
 {
	database *db;
	long dictionary_value;

	if(argc != 2) {
		fprintf(stderr, "dump_dict database name\n");
		exit(1);
	}
	db=openDatabase(argv[1], false, true);

	if(db==NULL) {
		fprintf(stderr,"db not found\n");
		exit(2);
	}
	
	print_dictionary(db);
#if 0
	dictionary_value = 
		look_up_word_in_dictionary(string_downcase("chemistry"),db); 
	if(dictionary_value > 0) {
		printf("Dict value = %d\n", dictionary_value);
		if(0 != search_word1("techinfo", 0, 0, 1, 0, dictionary_value, db)) {
			fprintf(stderr, "Some error looking up word\n");
		} else {
			finished_search_word(db);
		}
	}
#endif
	disposeDatabase(db);
}

#include "cdialect.h"
#include "irfiles.h"
#include "irsearch.h"
#include "irext.h"
#include <string.h>

extern long current_best_hit;
extern int document_score_array_len;
extern unsigned char *document_score_array;
extern long document_score_array_len ;

/* make_document_score_array insures that the document_score_array
   array is long enough, if not it makes it long enough */
static void make_document_score_array(length)
long length;
{
  if(length <= document_score_array_len)
    return;
  /* we have to make a new one.  free the old one first (if any) */
  if(document_score_array != 0){
    s_free(document_score_array);
  }
  document_score_array = (unsigned char*)s_malloc
    ((size_t)(length * sizeof(unsigned char)));
  document_score_array_len = length;
}




long search_word1(word,char_pos, line_pos, weight, doc_id, dictionary_value,
		 db)
     char *word; /* the word to be searched for */
     long char_pos;		/* the position of the start of the word */
     long line_pos;		/* is this needed? not for signature system */
     long weight;		/* how important the word looks syntactically,
				   such as is it bold */
     long doc_id;		/* current document, seed words is 0,
				   then it increments into the relevant 
				   document */
     long dictionary_value;	/* this is from the disk dictionary,
				   a signature system would use weight,
				   inverted file systems would put
				   position information */
     database *db;
{
  /* this side effects the document_score_array,
   * and downcases the word.
   * Returns 0 if successful or word not present, 
   * returns non-0 if an error.
   *
   */
  
  long not_full_flag = INDEX_BLOCK_FULL_FLAG; /*start out full so it will go on looking */
  long count, index_block_size;
  long internal_document_id, internal_weight, number_of_valid_entries;
  long index_file_block_number = dictionary_value;
  
  FILE *stream = NULL;
  current_best_hit = 0;  /* so that the best hits willstart from 0 */

  if(index_file_block_number >= 0){
    stream = db->index_stream;
    
    while((not_full_flag != INDEX_BLOCK_NOT_FULL_FLAG) && 
	  (index_file_block_number != 0)){	
      /* read the index block */
      if (0 != fseek(stream, (long)index_file_block_number, 
		     SEEK_SET))	
	{ 
	  fprintf(stderr,"fseek failed into the inverted file to position %ld\n",
		  (long)index_file_block_number); 
	  return(-1);
	}

      not_full_flag = read_bytes(INDEX_BLOCK_FLAG_SIZE, stream);
      index_file_block_number = read_bytes(NEXT_INDEX_BLOCK_SIZE, stream);
      index_block_size = read_bytes(INDEX_BLOCK_SIZE_SIZE, stream);
      if(EOF == index_block_size) 
	{ fprintf(stderr,"reading from the index file failed\n");
	  return(-1);
	}
      
      if(not_full_flag == INDEX_BLOCK_NOT_FULL_FLAG){
	/* not full */
	number_of_valid_entries = index_file_block_number;
      }
      else if(not_full_flag == INDEX_BLOCK_FULL_FLAG){
	/* full */
	number_of_valid_entries = index_block_size - INDEX_BLOCK_HEADER_SIZE;
      }
      else{			/* bad news, file is corrupted. */
	fprintf(stderr,
		"Expected the flag in the inverted file to be valid.  it is %ld",
		not_full_flag);
	return(-1);
      }
      printf("number of valid bytes: %ld entries: %ld\n",
	     number_of_valid_entries, 
	     number_of_valid_entries/INDEX_ELEMENT_SIZE); 
      
      /* add the array to the document_score_array */
    }
    return(0); 
  }
  else if(0 == index_file_block_number){
    /* an error occurred on looking up the word */
    return(-1);
  }
  else				/* index_file_block_number is negative */
    return(0);		/* word not present */
}

