
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "trie.h"

#define LINESZ 1024

static int total_words = 0;
int total_values=0;

const void *value;
Trie trie;

/* Methods called when serialing trie data */
static int _write_to_handle(const void *towrite, const int length, void *handle)
{  
    fwrite(&towrite, length, 1, (FILE *) handle); 
    // here I could check if success or not
    return 1;	
}

static int _write_value_to_handle(const void *towrite, void *handle)
{
    fwrite(&towrite, sizeof(int), 1, handle);  
    return 1;
}

/* Methods called when deserialing trie data */
static int _read_from_handle(void *wasread, const int length, void *handle)
{   
     fread(&wasread, length, 1, (FILE *) handle);  
    return 1;	
}

static void * _read_value_from_handle(void *handle)   //returns a pointer to the value
{
   int *value;
   printf("_read_value_handle called."); //if this function is called, the key was deserialized properly 
    fread(&value, sizeof(int), 1, handle);  
    return value;
}

int trie_save(){   

  FILE *out = fopen("savefile.txt", "wb"); 
  int success=Trie_serialize(trie, _write_to_handle, _write_value_to_handle, (void *)out); 
  fclose(out);		   
 return success;
}

int trie_load(){  
  int success=1;
  FILE *in = fopen("savefile.txt", "rb"); 
  trie = Trie_deserialize(_read_from_handle, _read_value_from_handle,in);
  if(!trie) {
   success=0;
   printf("Error: trie could not be created from data.\n");
  }
  fclose(in);		   
 return success;
}

int main(int argc, char **argv) {
  char s[LINESZ];
  int myval;
  
  /* Uncomment next 2 lines to test to deserialize trie data from file */
  int success=trie_load();
  printf("load sucess is: %d\n",success);
  
  /* Comment the below block to test to deserialize trie data from file */
  //BEGIN BLOCK
  /*FILE *in = fopen(argv[1], "r");  // e.g. /tmp/keys.txt 
  trie=Trie_new();
  	
  if (!in) {
    perror(argv[1]);
    return 1;
  }
  while (fgets(s, LINESZ, in)) {
    int len = strlen(s);
    if (len) {
      s[len-1] = '\0';  // chop newline 
       total_words++; 
      Trie_set(trie, (const unsigned char*) s, (const void*) total_words); // arg3 is the value for a key. I use a dummy value
    }
  }
  fclose(in);
  
  int success=trie_save();
  printf("serialized trie success: %d\n",success);
  */
  //END BLOCK
  
  /* load file with keys to search in the trie */
  /*
  in = fopen(argv[2], "r");  // e.g. /tmp/query.txt 

  if (!in) {
    perror(argv[2]);
    return 1;
  }
  while (fgets(s, LINESZ, in)) {
    int len = strlen(s);
    if (len) {
      s[len-1] = '\0';  // chop newline 
      //total_words++;  //words searched in trie
      myval=(int) Trie_get(trie, (const unsigned char*) s); //it returns void*
      //printf("%d |",myval);
      //total_values +=myval;
    }
  }
  fclose(in);
*/
 
  fprintf(stderr, "Trie length is: %d .\n", Trie_len(trie));
  
  Trie_del(trie);
  return 0;
}