private bool verifyIntegrity(string userstr) { char[] sep = { ' ', '\t' }; string[] tokens = userstr.Split(sep, StringSplitOptions.RemoveEmptyEntries); if (tokens.Length == 0) { return(true); } int mid = userStrToMainCmdID(tokens[0]); if (mid == -1) { return(false); } Console.WriteLine("verify integrity : token[0] = " + tokens[0] + " len = " + tokens.Length); if (tokens.Length == 1) { return(true); } SubCommand subcmd = userStrToSubCmdClass(mid, tokens[1]); Console.WriteLine("verify integrity : token[1] = " + tokens[1] + " sub=null? = " + (subcmd == null)); if (subcmd == null) { return(false); } if (tokens.Length == 2) { return(true); } /* * Now start working with token pairs */ for (int i = 2; i < tokens.Length; i += 2) { string ukey = tokens[i]; string uvalue = ((i + 1) > (tokens.Length - 1)) ? null : tokens[i + 1]; if (subcmd.checkIfKeyValueIsValid(false, ukey, uvalue) == false) { return(false); } else if (uvalue == null) { return(true); } } return(true); }
private string[] autofill_useroptions(string userstr) { char[] sep = { ' ', '\t' }; string[] tokens = userstr.Split(sep, StringSplitOptions.RemoveEmptyEntries); if (tokens.Length == 0) { string[] retval = new string[maincmdcnt]; for (int i = 0; i < maincmdcnt; i++) { retval[i] = "\t" + maincmd[i]; } return(retval); } else if (tokens.Length == 1) { int mid = userStrToMainCmdID(tokens[0]); //populate all the subcommands that can go with this one. string[] supportedsubcmds = new string[100]; int counter = 0; for (int s = 0; s < subcommandscnt; s++) { if (subcommands[s].is_valid_for_maincmd(mid)) { supportedsubcmds[counter++] = subcommands[s].subcmd_name; } } string[] retval = new string[counter]; for (int i = 0; i < counter; i++) { retval[i] = "\t" + supportedsubcmds[i]; } return(retval); } else if (tokens.Length >= 2) { if (tokens.Length % 2 == 0) { int mid = userStrToMainCmdID(tokens[0]); SubCommand subcmd = userStrToSubCmdClass(mid, tokens[1]); return(subcmd.get_all_user_options()); } else { //odd number, so we must be looking at a key, so show the values to user for that key. string key = tokens[tokens.Length - 1]; int mid = userStrToMainCmdID(tokens[0]); SubCommand subcmd = userStrToSubCmdClass(mid, tokens[1]); Console.WriteLine(">>>>>>> key = " + key); return(subcmd.get_possible_values(key)); } } //no reach return(null); }
private string autofill_string(string userstr) { Console.WriteLine("::: In autofill_string = " + userstr); char[] sep = { ' ', '\t' }; string[] tokens = userstr.Split(sep, StringSplitOptions.RemoveEmptyEntries); if (tokens.Length == 1) { //try to match maincmd for (int i = 0; i < maincmdcnt; i++) { if (maincmd[i].IndexOf(tokens[0]) != -1) { return(maincmd[i] + " "); } } return(null); } else if (tokens.Length == 2) { int mid = userStrToMainCmdID(tokens[0]); if (mid == -1) { return(null); } //try to match maincmd for (int i = 0; i < subcommandscnt; i++) { if (subcommands[i].subcmd_name.IndexOf(tokens[1]) == 0 && subcommands[i].is_valid_for_maincmd(mid)) { return(maincmd[mid] + " " + subcommands[i].subcmd_name + " "); } } return(null); } else if (tokens.Length >= 3) //3 or greater. { //we have a token, so we have to search for autofill option. Console.WriteLine("tokenlen 3+ " + userstr); string finalretval = ""; int mid = userStrToMainCmdID(tokens[0]); if (mid == -1) { return(null); } //try to match maincmd SubCommand subcmd = null; for (int i = 0; i < subcommandscnt; i++) { if (subcommands[i].subcmd_name == tokens[1] && subcommands[i].is_valid_for_maincmd(mid)) { subcmd = subcommands[i]; } } if (subcmd == null) { return(null); } finalretval += maincmd[mid] + " " + subcmd.subcmd_name + " "; for (int i = 2; i < tokens.Length; i += 2) { Console.WriteLine("I=" + i + " t=" + tokens[i] + " final=" + finalretval); string ukey = tokens[i]; string uvalue = ((i + 1) > (tokens.Length - 1)) ? null : tokens[i + 1]; //below must be modified to autofill value if the key is correct!. - todo. if (subcmd.checkIfKeyValueIsValid(true, ukey, uvalue) == false) { string str2 = subcmd.autofill_key(tokens[i]); Console.WriteLine("^^^^ failed verificatioin, now do autofill ^^^ str2 = " + str2); if (str2 == null) { return(null); } return(finalretval + str2 + ((uvalue == null) ? " " : " " + uvalue)); } else { finalretval += (ukey + " " + uvalue + " "); } } } //cannot reach return(null); }
public AutoFill(string fpath) { FileStream fs = new FileStream(fpath, FileMode.Open); StreamReader sr = new StreamReader(fs); string line = ""; bool subcmdoptions = false; SubCommand currsubcmd = null; while ((line = sr.ReadLine()) != null) { line = line.Trim(); Console.WriteLine("loop " + line); if (line.Length == 0) { if (subcmdoptions) { subcmdoptions = false; subcommands[subcommandscnt++] = currsubcmd; currsubcmd = null; Console.WriteLine(" saved subcommand full"); } else { Console.WriteLine(" ignore blank line"); } continue; } char[] array = line.ToCharArray(0, 1); if (array[0] == '#') { Console.WriteLine(" ignore comment line"); continue; } if (line.IndexOf("maincmd", 0) == 0) { char[] sep = { ' ', '\t' }; string[] tokens = line.Split(sep, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < tokens.Length; i++) { tokens[i] = tokens[i].Trim(); } //int idx = Int32.Parse(tokens[2]); maincmd[maincmdcnt++] = tokens[1]; Console.WriteLine(" parsed maincmd line"); continue; } if (subcmdoptions) { currsubcmd.parse_cmdoption_string(line); Console.WriteLine(" parse key-value line"); continue; } if (line.IndexOf("subcmd", 0) == 0) { char[] sep = { ' ', '\t' }; string[] tokens = line.Split(sep, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < tokens.Length; i++) { tokens[i] = tokens[i].Trim(); } string[] midstr = tokens[2].Split(','); currsubcmd = new SubCommand(tokens[1]); for (int xi = 0; xi < midstr.Length; xi++) { currsubcmd.set_maincmd_id(Int32.Parse(midstr[xi])); } Console.WriteLine(" Create new subcmd entry"); subcmdoptions = true; continue; } } if (subcmdoptions) { subcmdoptions = false; subcommands[subcommandscnt++] = currsubcmd; currsubcmd = null; Console.WriteLine(" saved subcommand full"); } Console.WriteLine("------------------------------------"); fs.Close(); }