static public DictBuilder dictBuilder() { if (dctBuilder == null) { dctBuilder = new DictBuilder(); } return(dctBuilder); }
public int Execute(string input) { if (input.All(x => x == ' ') || input == "") { return(0); } Object[] fields = CParser.Parse(input); string command = (string)fields[0]; if (!cmds.Keys.Contains(command)) { Console.WriteLine("command not found"); return(-1); } List <string> flags = (List <string>)fields[1]; List <string> paramss = (List <string>)fields[2]; /* ------------------- * READ command * -------------------*/ if (command == READ) { if (paramss == null) { dictManger.read("../../input1.txt"); } else if (paramss.Count == 1) { dictManger.read(paramss[0]); } else { Console.WriteLine("error: too many params"); } return(0); } /* ------------------- * SAVE command * -------------------*/ if (command == SAVE) { if (paramss == null) { dictManger.save("../../input1.txt"); } else if (paramss.Count == 1) { dictManger.save(paramss[0]); } else { Console.WriteLine("error: too many params"); } return(0); } /* ------------------- * ADD command * -------------------*/ if (command == ADD) { if (flags.Count == 0) { Console.WriteLine("error: choose -w / -d / -n / -e"); return(0); } if (flags[0] == "-z") { if (paramss == null || paramss.Count != 2) { Console.WriteLine("ERROR USAGE: add -dct DICT_NAME LANG"); return(-1); } dictManger.addDict( DictBuilder.dictBuilder(). setName(paramss[0]). setLanguage(paramss[1]). build() ); return(0); } /* ------------------- * ADD DEFINITION command * -------------------*/ if (flags[0] == "-d") { if (paramss == null || paramss.Count == 1) { Console.WriteLine("USAGE: add -w WORD DEFINITION"); return(0); } Word wrd = dictManger.CurDict().findWord(paramss[0]); if (wrd == null) { Console.WriteLine("Error: Word not found"); return(-1); } wrd.addDefinition(new Definition(paramss[1].Replace("\'", "").Replace("\"", ""))); return(0); } /*----------------------- * ADD SYNONYM OR CONTRARY * ------------------------*/ if (flags[0] == "-s") { if (paramss == null || (paramss.Count != 3 && paramss.Count != 2)) { Console.WriteLine("USAGE: add -s WORD SYNONYM [N.DEFINITION]"); return(0); } Word wrd = dictManger.CurDict().findWord(paramss[0]); if (wrd == null) { Console.WriteLine("Error: Word not found"); return(-1); } Definition def; def = wrd.getDefinition(paramss.Count == 3 ? Int32.Parse(paramss[2]) : 0); def.addSynonym(dictManger.CurDict().findWord(paramss[1])); } if (flags[0] == "-c") { if (paramss == null || (paramss.Count != 3 && paramss.Count != 2)) { Console.WriteLine("USAGE: add -s WORD CONTRARY [N.DEFINITION]"); return(0); } Word wrd = dictManger.CurDict().findWord(paramss[0]); if (wrd == null) { Console.WriteLine("Error: Word not found"); return(-1); } Definition def; def = wrd.getDefinition(paramss.Count == 3 ? Int32.Parse(paramss[2]) : 0); def.addContrary(dictManger.CurDict().findWord(paramss[1])); } /*----------------------- * ADD TRANSLATION need to be re-thought * ------------------------*/ if (flags[0] == "-t") { if (paramss == null || paramss.Count > 4 || paramss.Count < 2) { Console.WriteLine("USAGE: add -t WORD TRANSLATED LANG [N.DEFINITION]"); return(0); } Word wrd = dictManger.CurDict().findWord(paramss[0]); if (wrd == null) { Console.WriteLine("Error: Word not found"); return(-1); } // Texts found , List of indexes of dictionaries Tuple <List <Text>, List <int> > tuple = dictManger.find(paramss[1], Dictionary.FLAG_W); int idx = tuple.Item2[0]; if (tuple.Item1.Count != 1) { for (int i = 0; i < tuple.Item2.Count; i++) { if (dictManger.getDict(tuple.Item2[i]).Language == paramss[2]) { idx = i; break; } } } Word translated = (Word)tuple.Item1[idx]; Definition def = wrd.getDefinition(paramss.Count == 4 ? Int32.Parse(paramss[3]) : 0); wrd.addTranslation(new Translation(paramss[2], translated), wrd.indexOfDef(def)); } Text txt = null; string title = ""; string origin = ""; string note = ""; /* ------------------- * ADD WORD command * -------------------*/ if (flags[0] == "-w") { string type = ""; if (paramss == null) { title = DShell.input("word:"); type = DShell.input("tipo:"); origin = DShell.multiline("origin:"); note = DShell.multiline("note:"); } if (paramss != null && paramss.Count == 2) { title = paramss[0]; type = paramss[1]; } else if (paramss.Count == 1) { title = paramss[0]; } else { return(-1); } wrdBuilder.setTitle(title); wrdBuilder.setOrigin(origin); wrdBuilder.setNote(note); wrdBuilder.setType(type); txt = wrdBuilder.build(); } /* ------------------- * ADD EXPRESSION command * -------------------*/ if (flags[0] == "-e") { string text = ""; string explanation = ""; title = DShell.input("title:"); text = DShell.multiline("text:"); txt = new Expression(title, origin, note, text, explanation); } /* ------------------- * ADD NOVEL command * -------------------*/ if (flags[0] == "-n") { string text = ""; string author = ""; title = DShell.input("title:"); author = DShell.input("author:"); text = DShell.multiline("text:"); txt = new Novel(title, origin, note, text, author); } if (txt == null) { Console.WriteLine("Invalid flag"); } dictManger.CurDict().add(txt); return(0); } /* ------------------- * CLEAR command * -------------------*/ if (command == CLEAR) { Console.Clear(); Console.WriteLine(HEAD); return(0); } /* ------------------- * EXIT command * -------------------*/ if (command == EXIT) { return(0); } /* ------------------- * HELP command * -------------------*/ if (command == HELP) { Console.WriteLine("commands:"); foreach (var var in cmds) { Console.WriteLine(" " + var.Key + ":" + "\t" + var.Value); } return(0); } /* ------------------- * LIST command * -------------------*/ if (command == LIST) { var dict = dictManger.CurDict(); /* ------------------- * LIST all TEXT command * -------------------*/ if (flags == null) { Console.WriteLine(dict.list()); return(0); } /* ---------------------------- * LIST DICTIONARIES command * --------------------------*/ if (flags[0] == "-z") { Console.WriteLine(dictManger.listDicts()); return(0); } /* ------------------- * LIST WORDS command * -------------------*/ //show flag int sFLAG = 0; if (flags[0] == "-w") { sFLAG = sFLAG | Dictionary.FLAG_W; } /* ------------------- * LIST EXPRESSIONS command * -------------------*/ if (flags[0] == "-e") { sFLAG = sFLAG | Dictionary.FLAG_E; } /* ------------------- * LIST NOVELS command * -------------------*/ if (flags[0] == "-n") { sFLAG = sFLAG | Dictionary.FLAG_N; } Console.WriteLine(dict.list(sFLAG)); return(0); } /* ------------------- * DELETE command * -------------------*/ if (command == REMOVE) { if (paramss == null) { Console.WriteLine("ERROR: params missing"); return(-1); } else if ((paramss.Count != 2 && paramss.Count != 1)) { Console.WriteLine("USAGE: remove -d WORD N*DEFINITION"); return(-1); } Text txt = dictManger.CurDict().find(paramss[0]); if (flags.Count != 0 && flags[0] == "-d") { if (((Word)txt).removeDefinition(Int32.Parse(paramss[1]) - 1) == null) { Console.WriteLine("ERROR: index of definition out of bound"); return(-1); } return(0); } if (flags.Count == 0 && dictManger.CurDict().remove(txt)) { Console.WriteLine("ERROR: word/expression/novel not found"); return(-1); } return(0); } /*---------------------- * FIND * -------------------*/ if (command == FIND) { if (flags.Count == 0) { Text txt = dictManger.CurDict().find(paramss[0]); if (txt != null) { Console.WriteLine("found:" + txt.title); } else { Console.WriteLine("not found :" + paramss[0]); } return(0); } if (flags[0] == "-w") { if (paramss == null) { Console.WriteLine("ERROR: params missing"); return(-1); } Word wrd = dictManger.CurDict().findWord(paramss[0]); if (wrd != null) { Console.WriteLine("found word:" + wrd.title); } else { Console.WriteLine("not found word:" + paramss[0]); } return(0); } if (flags[0] == "-e") { if (paramss == null) { Console.WriteLine("ERROR: params missing"); return(-1); } Expression exp = dictManger.CurDict().findExp(paramss[0]); if (exp != null) { Console.WriteLine("found word:" + exp.title); } else { Console.WriteLine("not found word:" + paramss[0]); } return(0); } if (flags[0] == "-n") { if (paramss == null) { Console.WriteLine("ERROR: params missing"); return(-1); } Novel nvl = dictManger.CurDict().findNovel(paramss[0]); if (nvl != null) { Console.WriteLine("found novel:" + nvl.title); } else { Console.WriteLine("not found word:" + paramss[0]); } return(0); } if (flags[0] == "-p") { if (paramss == null) { Console.WriteLine("ERROR: params missing"); return(-1); } List <Text> texts = dictManger.CurDict().PBS(paramss[0]); foreach (Text text in texts) { if (text != null) { Console.WriteLine("found text:" + text.title); } else { Console.WriteLine("not found word:" + paramss[0]); } } return(0); } } /*---------------------- * REMIND * -------------------*/ if (command == REMIND) { int n = 5; if (paramss != null) { n = int.Parse(paramss[0]); } List <string> list_wrd = new List <string>(); List <Word> words = dictManger.CurDict().wrdRemind(n); foreach (Word w in words) { list_wrd.Add(w.title); } Console.WriteLine(list_wrd); return(0); } /*---------------------- * SHOW * -------------------*/ if (command == SHOW) { if (paramss == null || paramss.Count != 1) { Console.WriteLine("USAGE: show -w WORD"); return(0); } int find_flags = 0; if (flags.Count != 0) { if (flags[0] == "-w") { find_flags |= Dictionary.FLAG_W; } else if (flags[0] == "-n") { find_flags |= Dictionary.FLAG_N; } else if (flags[0] == "-e") { find_flags |= Dictionary.FLAG_E; } } Text txtFound = dictManger.CurDict().find(paramss[0], find_flags); if (txtFound == null) { Console.WriteLine("error: word/expression/novel not found"); return(-1); } else { Console.WriteLine(txtFound.toString()); } return(0); } /*---------------------- * CHANGE * -------------------*/ if (command == CHANGE) { if (paramss == null || paramss.Count != 1) { Console.WriteLine("ERROR, USAGE: change INDEX"); return(-1); } dictManger.setCurDict(Int32.Parse(paramss[0])); } Console.WriteLine($"{input} not found"); return(1); }