public Dal.DalResult<bool> AddShorthandExpansionEntry(string shorthand, string expansion) { Dal.DalResult<bool> retResult = null; try { if (!ShorthandExpansionEntries.Ton.ShorthandExists(shorthand)) { var entryToAdd = new ShorthandExpansionEntry() { ShorthandText = shorthand, ExpansionText = expansion }; ShorthandExpansionEntries.Ton.Add(entryToAdd); SaveTextFileEntries(); retResult = new Dal.DalResult<bool>(true); } else { //ALREADY EXISTS retResult = new Dal.DalResult<bool>(false); } } catch (Exception ex) { retResult = new Dal.DalResult<bool>(ex); } return retResult; }
/// <summary> /// Loads the ShorthandExpansionEntries from the data text file. /// </summary> private void LoadTextFileEntries() { using (StreamReader reader = new StreamReader(_TextFileNameFullPath)) { var text = reader.ReadToEnd(); if (string.IsNullOrEmpty(text)) return; var lines = text.Split('\n'); if (string.IsNullOrEmpty(lines[lines.Length - 1])) lines = (lines.Take(lines.Length - 1)).ToArray(); var linesCount = lines.Count(); var entriesCount = linesCount / 2; int remainder = -1; Math.DivRem(linesCount, 2, out remainder); if (remainder != 0) throw new Exception("Error Loading Shorthands-Expansions Text File \n\n" + "TextFile does not have an even number of lines. \n" + "Each shorthand is its own line, and the line following is the expansion. \n" + "So, there should be an even number of lines."); for (int i = 0; i < entriesCount; i++) { var shorthandIndex = ((i+1)* 2) - 2; var expansionIndex = shorthandIndex + 1; var shorthandText = lines[shorthandIndex]; if (shorthandText[shorthandText.Length - 1] == '\r') shorthandText = shorthandText.Substring(0, shorthandText.Length - 1); var expansionText = lines[expansionIndex]; if (expansionText[expansionText.Length - 1] == '\r') expansionText = expansionText.Substring(0, expansionText.Length - 1); ShorthandExpansionEntry entry = new ShorthandExpansionEntry() { ShorthandText = shorthandText, ExpansionText = expansionText }; ShorthandExpansionEntries.Ton.Add(entry); } } }