private static void List(List <string> args) { if (args.Count == 0) { if (currentBook == null) { Books.List(); } else { Phrases.List(); } } else if (args[0] == "books") { if (args.Count == 1) { Books.List(); } else { Console.WriteLine(strings.Get("WrongArgCount")); } } else if (args[0] == "phrases") { if (args.Count == 3) { if (args[1] == "in") { VersionedDictionary oldBook = currentBook; //TO-DO: 此处实现别扭,应改 if (books.TryGetValue(args[2], out currentBook)) { Phrases.List(); } else { Console.WriteLine(strings.Get("BookNotFound")); } currentBook = oldBook; } else { Console.WriteLine(strings.Get("WrongSyntax")); } } else if (args.Count == 1) { Phrases.List(); } else { Console.WriteLine(strings.Get("WrongSyntax")); } } else { Console.WriteLine(strings.Get("WrongSyntax")); } }
private static bool TryAdd( ref VersionedDictionary book, string phrase, string description) { if (book.ContainsKey(phrase)) { Console.WriteLine(strings.Get("PhraseAlreadyExists")); return(false); } else { book.Add(phrase, description); lastPhraseAndBook = new KeyValuePair <string, VersionedDictionary>(phrase, book); Console.WriteLine(strings.Get("AddPhraseSuccess"), phrase); return(true); } }
public static VersionedDictionary NDictFileConvert(FileInfo file, string newDirectory) { VersionedDictionary res = new VersionedDictionary( Path.Combine(newDirectory, Path.GetFileNameWithoutExtension(file.Name))); XmlDocument xml = new XmlDocument(); xml.Load(file.FullName); XmlNode root = xml.SelectSingleNode("book"); foreach (XmlElement node in root.ChildNodes) { res.Add(node.GetAttribute("key"), node.GetAttribute("val")); } res.Save(); return(res); }
private static void SaveBook(bool silent, string bookname, VersionedDictionary book) { XmlDocument xml = new XmlDocument(); xml.AppendChild(xml.CreateXmlDeclaration("1.0", "utf-8", null)); xml.AppendChild(xml.CreateElement("book")); foreach (KeyValuePair <string, string> phrase in book) { XmlElement node = xml.CreateElement("pair"); node.SetAttribute("key", phrase.Key); node.SetAttribute("val", phrase.Value); xml.DocumentElement.AppendChild(node); } xml.Save(myDirectory + "/" + bookname + ".ndict"); if (!silent) { Console.WriteLine(strings.Get("BookSaved"), bookname); } }
private static bool TryCreateContext( ref Dictionary <string, string> preps, string keyPrep, out VersionedDictionary context, bool askIfNull, bool allowGlobal = false) { context = currentBook; if (preps.TryGetValue(keyPrep, out var bookName)) { if (!books.TryGetValue(bookName, out context)) { Console.WriteLine(strings.Get("BookNotFound")); return(false); } } else { if (context == null) { if (askIfNull) { bookName = nconsole.Ask(strings.Get("EnterBookName")); if (!books.TryGetValue(bookName, out context)) { Console.WriteLine(strings.Get("BookNotFound")); return(false); } } else { if (!allowGlobal) { Console.WriteLine(strings.Get("NoBookOpen")); } return(false); } } } return(true); }