//generate one string from random template public string NoiceGen1() { if (templates.lst.Count == 0) { return(""); } string s = "?", sReplc; int i, j; i = rnd.Next(templates.lst.Count); s = templates.lst[i]; int sex = 0; //0-neutral, 1-male, 2-female if (s.IndexOf(NAME) >= 0) { sex = 1 + rnd.Next(2); if (scNeutral != null) { sReplc = ParamValue(scNeutral, sex); } else { if (sex == 1) { sReplc = ParamValue(scMale, sex); } else { sReplc = ParamValue(scFem, sex); } } s = s.Replace(NAME, sReplc); } else if (s.IndexOf(NAME_M) >= 0) { sex = 1; sReplc = ParamValue(scMale, sex); s = s.Replace(NAME_M, sReplc); } else if (s.IndexOf(NAME_F) >= 0) { sex = 2; sReplc = ParamValue(scFem, sex); s = s.Replace(NAME_F, sReplc); } for (j = 0; j < lstSect.Count; j++) { IniSect ii = lstSect[j]; if (ii.name == NAME || ii.name == NAME_M || ii.name == NAME_F) { continue; } sReplc = ParamValue(ii, sex); s = s.Replace(ii.name, sReplc); } return(s); }
//find value string FindValue(IniSect sc, string key) { int i; if (sc == null || sc.lst.Count == 0) { return(""); } for (i = 0; i < sc.lst.Count; i++) { string s = sc.lst[i]; int ip = s.IndexOf("="); if (ip > 0 && s.Substring(0, ip).Trim().ToLower() == key.Trim().ToLower() && ip < s.Length - 1) { return(s.Substring(ip + 1).Trim()); } } return(""); }
//get random value from section string ParamValue(IniSect ii, int sex) { if (ii == null || ii.lst.Count == 0) { return(""); } //generate unique in array int j, i, k = 0; bool bGood = false; int nchk = Math.Min(ii.done.Length, lstSect.Count); do { j = rnd.Next(ii.lst.Count); for (i = 0; i < nchk - 1; i++) { if (ii.done[i] == j) { break; } } if (i == nchk - 1) { bGood = true; } k++; if (k > 10) { break; } } while (!bGood); //shift for (i = nchk - 1; i > 0; i--) { ii.done[i] = ii.done[i - 1]; } ii.done[0] = j; string s = ii.lst[j]; return(s.Replace("(а)", sex == 1 ? "" : "а")); }
//load templates and other sections from ini-file public bool LoadNoiceIni(string sFile) { int i; string[] lines = File.ReadAllLines(sFile, Encoding.UTF8); bool bParseTempl = false; templates.name = "[TEMPLATE]"; for (i = 0; i < lines.Length; i++) { string s = lines[i].TrimEnd(); //Console.WriteLine(s); if (s.Length == 0 || s.Substring(0, 1) == "#") { continue; } if (s == templates.name) { bParseTempl = true; continue; } if (s.Substring(0, 1) == "[") { bParseTempl = false; IniSect ii = new IniSect(); ii.name = s; lstSect.Add(ii); if (s == NAME) { scNeutral = ii; } else if (s == NAME_M) { scMale = ii; } else if (s == NAME_F) { scFem = ii; } continue; } if (bParseTempl) { templates.lst.Add(s.Trim()); } else if (lstSect.Count > 0) { IniSect ii = lstSect[lstSect.Count - 1]; ii.lst.Add(s); } } //test ini load /*for (j = -1; j < lstSect.Count; j++) * { * IniSect ii = j == -1 ? templates : lstSect[j]; * Console.WriteLine(ii.name); * for (i = 0; i < ii.lst.Count; i++) * Console.WriteLine(" " + ii.lst[i]); * }*/ return(lstSect.Count != 0); }
//transform init public Transf TransformInit(string sectname) { int i; IniSect scTransf = FindSect(sectname); if (scTransf != null && scTransf.lst.Count != 0) { Transf tf = new Transf(); tf.lstInt = new List <int>(); string ord = FindValue(scTransf, "order"); if (ord != "") { string[] arr = ord.Split(','); for (i = 0; i < arr.Length; i++) { tf.lstInt.Add(int.Parse(arr[i])); } } ord = FindValue(scTransf, "sp_in"); if (ord != "") { tf.sp_in = ord.ToCharArray()[0]; } ord = FindValue(scTransf, "sp_out"); if (ord != "") { tf.sp_out = ord.ToCharArray()[0]; } ord = FindValue(scTransf, "all"); if (ord == "1") { tf.all = 1; } ord = FindValue(scTransf, "regexp"); if (ord != "") { tf.regexp = new Regex(ord, RegexOptions.Singleline); tf.myEvaluator = new MatchEvaluator(tf.MyReplace); } ord = FindValue(scTransf, "regout"); if (ord != "") { tf.regout = ord; } ord = FindValue(scTransf, "modifonly"); if (ord != "") { int.TryParse(ord, out tf.modifonly); } ord = FindValue(scTransf, "file"); if (ord != "") { tf.file = ord; } ord = FindValue(scTransf, "enc"); if (ord != "") { tf.enc = ord; } ord = FindValue(scTransf, "dir"); if (ord != "") { tf.dir = ord; } ord = FindValue(scTransf, "regrep"); if (ord != "") { tf.regrep = ord; } ord = FindValue(scTransf, "ng"); if (ord != "") { int.TryParse(ord, out tf.ng); } ord = FindValue(scTransf, "connection"); if (ord != "") { tf.connection = ord; } return(tf); } return(null); }