private void mAddStringsButton_Click(object sender, EventArgs e) { //public static string ShowMessage(string title, string message, Icon icon, bool editable, bool showCancelButton) string result = MessageForm.ShowMessage("Paste in strings to add", "", SystemIcons.Question, true, true); if (result != null) { result = result.Replace("\r\n", "\n"); //public static string Replace(string input, string pattern, string replacement); result = Regex.Replace(result, "[ ]+\n", "\n"); List <string> stringsToAdd = new List <string>(result.Split(new char[] { '\n' })); int numberAdded = HashHelper.AddStringsToDictionary(stringsToAdd); txt_output.Text = string.Format("Added {0} previously unknown strings\n", numberAdded); } }
/// <summary> /// Adds/replaces the strings in the current loc file. /// </summary> /// <param name="text">The text to apply</param> /// <param name="addOnly">When true, do not modify existing strings; just add the new ones.</param> public void ApplyText(string text, bool addOnly, bool skipEmptyStrings) { GetAllStrings(); int pos = 0; string key = ""; string value = ""; Dictionary <string, string> stringsToAdd = new Dictionary <string, string>(); Dictionary <string, string> stringsToSet = new Dictionary <string, string>(); uint stringId = 0; while (pos < text.Length) { key = GetKey(pos, text); if (String.IsNullOrEmpty(key)) { pos++; continue; } if (key.Length > 2 && key[0] == '0' && key[1] == 'x') { try { stringId = UInt32.Parse(key.Substring(2), System.Globalization.NumberStyles.AllowHexSpecifier); } catch (Exception e) { Console.WriteLine("Error! invalid string key:'{0}'", key); throw e; } } else { stringId = HashHelper.HashString(key); } pos += (key.Length); value = ParseStringValue(pos, text); if (value == null) { pos++; Console.WriteLine("Warning! No value found for key:'{0}'", key); continue; } if (mStringSet.ContainsKey(stringId)) { if (mStringSet[stringId] != value) { if (stringsToSet.ContainsKey(key)) { stringsToSet[key] = value; } else { stringsToSet.Add(key, value); } } } else { if (stringsToAdd.ContainsKey(key)) { stringsToAdd[key] = value; } else { stringsToAdd.Add(key, value); } } pos += value.Length; // advance to next line pos = text.IndexOf('\n', pos); if (pos == -1) { break; } pos++;// advance past new line } if (!addOnly) { foreach (string k in stringsToSet.Keys) { SetString(k, stringsToSet[k]); } } List <string> hashThese = new List <string>(); foreach (string k in stringsToAdd.Keys) { if (skipEmptyStrings && stringsToAdd[k] == String.Empty) { // do not include empty strings } else { hashThese.Add(k); AddString(k, stringsToAdd[k]); if (Program.Verbose) { Console.WriteLine("adding string:'[{0}]: '{1}'", k, stringsToAdd[k]); } } } if (hashThese.Count > 0) { HashHelper.AddStringsToDictionary(hashThese); } }