/*
  crossword -- a crossword game
  Copyright (C) 2000 Falk Hueffner

  This program is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by the Free
  Software Foundation; either version 2 of the License, or (at your option)
  any later version.
  
  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  more details.
  
  You should have received a copy of the GNU General Public License along with
  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  Place, Suite 330, Boston, MA 02111-1307 USA

  $Id: trie.cc,v 1.7 2000/11/30 21:43:41 falk Exp $
*/

#include <iostream>
#include <string>

#include "WordListTrie.hh"
#include "Trie.hh"

void dumpNode(const TrieNode* node, std::string start) {
    if (node->isTerminal())
	std::cout << start << std::endl;

    for (const TrieNode* child = node->firstChild(); child != NULL;
	 child = child->nextSibling())
	dumpNode(child, start + child->letter());
}

int main(int argc, char* argv[]) {
    if (argc == 1) {
	// Build trie from sorted word list on stdin and dump to stdout
	WordListTrie t(std::cin);
	t.dump(std::cout);

	std::cerr << t.numNodes() << " nodes\n";
    } else if (argc == 2) {
	// Read trie from stdin and dump word list stdout
	Trie t(argv[1]);
	t.print(std::cout);
    } else if (argc == 3) {
	// Look up word
	Trie t(argv[1]);
	std::string word = argv[2];
	const TrieNode* node = t.root();
	for (unsigned i = 0; i < word.length(); ++i) {
	    node = node->child(word[i]);
	    if (node == NULL) {
		std::cout << word << " not in dictionary\n";
		return 0;
	    }
	}
	dumpNode(node, word);
    }

    return 0;
}
