Code:
/* Usage:
#test -save /tmp/words.txt read strings, store in trie, and save trie to disk.
#test -load load data from disk and builds a trie from it.
*/
#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_handle(const void *towrite, const int length, void *handle)
{
fwrite(&towrite, length, 1, (FILE *) handle);
return 1;
}
static int _write_value_handle(const void *towrite, void *handle)
{
fwrite(&towrite, sizeof(int), 1, handle);
return 1;
}
/* Methods called when deserialing trie data */
static int _read_handle(void *wasread, const int length, void *handle)
{
printf("_read_handle called. length: %d\n",length);
fread(wasread, length, 1, (FILE *) handle);
printf("wasread is: %s\n",wasread);
return 1;
}
static void * _read_value_handle(void *handle) //returns a pointer to the value
{
//In my tests, this method never gets called, because a problem happens with
// the data read in _read_handle(). No error given, deserialize() function just quits.
int *value;
printf("_read_value_handle called.");
fread(&value, sizeof(int), 1, handle);
printf("value read is: %d",value);
return value;
}
/* Methods to serialize and deserialize the trie */
int trie_save(){
FILE *out = fopen("savefile.txt", "wb");
int success=Trie_serialize(trie, _write_handle, _write_value_handle, (void *)out);
fclose(out);
return success;
}
int trie_load(){
int success=1;
FILE *in = fopen("savefile.txt", "rb");
trie = Trie_deserialize(_read_handle, _read_value_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;
if(!strcmp(argv[1], "-load")) {
int success=trie_load();
printf("load sucess is: %d\n",success);
}
if(!strcmp(argv[1], "-save")) {
FILE *in = fopen(argv[2], "r"); // e.g. /tmp/words.txt
trie=Trie_new();
value=(const void *)1;
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++;
// add to trie //arg3 is the value
Trie_set(trie, (const unsigned char*) s, (const void*) total_words);
}
}
fclose(in);
int success=trie_save();
printf("serialized trie success: %d\n",success);
}
fprintf(stderr, "Checked %d total words.\n", total_words);
fprintf(stderr, "Trie length is: %d .\n", Trie_len(trie));
Trie_del(trie);
return 0;
}