/* * Dictionary.cs - Handles dictionaries of Level 9 games. * * Copyright (C) 2004 - 2011 Andreas Scherrer * * This 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 software 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 software; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, * USA. */ using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Windows.Forms; namespace Level9 { /// /// Handles dictionaries of Level 9 games. /// public sealed class Dictionary { // -- Attributes ----------------------------------------------------- /// /// The one and only Dictionary instance (singleton). /// static Dictionary instance = null; /// /// The dictionary hash table (one bucket per character). /// Dictionary dictionary; /// /// The dictionary hash table keys. /// char[] keys = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; /// /// This property indicates whether the dictionary has no entries. /// bool empty = true; /// /// The split characters for strings containing dictionary entries. /// char[] split = new char[] {' ', '\r', '\n'}; /// /// The Dictionary representation as a string. /// string output = null; /// /// The text viewer component that displays the dictionary. /// TextForm dictionaryView = null; // -- Constructors --------------------------------------------------- /// /// Initializes a new instance of the Dictionary class. /// private Dictionary () { dictionary = new Dictionary(); for (int i = 0; i < keys.Length; i++) { dictionary.Add(keys[i], new ArrayList()); } dictionaryView = new TextForm(false,Config.Instance.DictViewerSize); dictionaryView.Text = "Dictionary"; Interpreter.NewGameFile += new NewGameFileEventHandler(InterpreterNewGameFile); } // -- Event handling ------------------------------------------------- /// /// Invoked when a new game file has been loaded. /// /// /// a NewGameFileEventArgs that contains the event data /// void InterpreterNewGameFile(NewGameFileEventArgs e) { IEnumerator it = dictionary.Values.GetEnumerator(); while (it.MoveNext()) { ((ArrayList)it.Current).Clear(); } empty = true; } // -- Methods -------------------------------------------------------- /// /// Imports dictionary entries from the given string. /// /// a string with dictionary entries public void Add(string words) { if (words == null || (words != null && words.Length == 0)) { return; } Logger.Log(this, words); string[] tokens = words.Split(split); for (int i = 0; i < tokens.Length; i++) { if (tokens[i].Length == 0) { continue; } char key = Char.ToLower(tokens[i][0]); if (!dictionary.ContainsKey(key)) { continue; } ArrayList bucket = dictionary[key]; if (!bucket.Contains(tokens[i])) { bucket.Add(tokens[i]); } } empty = false; } /// /// Initiates the dictionary import, displays a progress form and opens /// a text viewer for dictionary examination. /// /// /// the dialog owner window to be set for the text viewer /// public void Show(IWin32Window owner) { if (empty) { Interpreter.AddInputRequest(Interpreter.Commands.Dictionary); GameThread.Resume(); DictionaryProgressForm dpf = new DictionaryProgressForm(); dpf.ShowDialog(); output = CreateOutput(); dictionaryView.Clear(); dictionaryView.AppendText(output); } dictionaryView.SetCaretPosition(0); dictionaryView.BringToFront(); dictionaryView.Show(); } /// /// Creates a string representation of the dictionary. /// /// a string representation of the dictionary string CreateOutput() { StringBuilder output = new StringBuilder(); output.Append("# LEVEL 9 GAME DICTIONARY"); output.Append("\n# Game file: <" + Interpreter.GameFilePath + ">"); IEnumerator it = dictionary.Values.GetEnumerator(); while (it.MoveNext()) { ((ArrayList)it.Current).Sort(); } ArrayList keys = new ArrayList(dictionary.Keys); keys.Sort(); ArrayList bucket; char key; bool first = true; // // 0 - 9 // for (int i = 0; i < keys.Count; i++) { key = (char)keys[i]; if (key >= 'a' && key <= 'z') { continue; } bucket = dictionary[key]; if (bucket.Count == 0) { continue; } if (first) { output.Append("\n\n- 0-9 -\n\n"); } for (int j = 0; j < bucket.Count; j++) { if (first) { first = false; } else { output.Append(", "); } output.Append(bucket[j]); } } // // a - z // for (int i = 0; i < keys.Count; i++) { key = (char)keys[i]; if (key >= '0' && key <= '9') { continue; } bucket = dictionary[key]; if (bucket.Count == 0) { continue; } output.Append("\n\n- " + Char.ToUpper(key) + " -\n\n"); for (int j = 0; j < bucket.Count; j++) { output.Append(bucket[j]); if (j < bucket.Count - 1) { output.Append(", "); } } } return output.ToString(); } // -- Accessors ------------------------------------------------------ /// /// Gets the one and only Dictionary instance (singleton). /// public static Dictionary Instance { get { if (instance == null) { instance = new Dictionary(); } return instance; } } } }