public static string AddCoreToState(string text, CountryMergeHelper cmh, string prefix, string suffix) { int locationOfSection = 0; string frontline = "history={"; string endline = "}\r\n\r\n\tprovinces={"; string newlinePattern = "\r\n\t"; string historyPattern = endline; Regex historyFinder = new Regex(historyPattern); try { if (historyFinder.IsMatch(text)) { Match locationOfHistory = historyFinder.Match(text); locationOfSection = locationOfHistory.Index; int lengthofSection = locationOfHistory.Length; if (cmh.retainCore != "y" && cmh.addCore != "y") { return(text.Remove(locationOfSection, (prefix + cmh.tagFrom + suffix + newlinePattern).Length)); } else if (cmh.retainCore == "y" && cmh.addCore == "y") { return(text.Insert(locationOfSection, prefix + cmh.tagTo + suffix + newlinePattern)); } else if (cmh.retainCore != "y" && cmh.addCore == "y") { return(SwapTag(text, cmh.tagFrom, cmh.tagTo, prefix, suffix)); } else { return(text); } } else { return(text); } } catch (Exception e) { Console.WriteLine("regex matching in history->state file f****d up.\n\tLeaving it unchanged" + "\n\t" + e.Message); return(text); } }
public static void ChangeStateOwners(FileInfo fi, CountryMergeHelper cmh, List <Tuple <string, string> > prefixSuffixes) { Console.WriteLine("changing state owner for stateId " + fi.Name); string file = File.ReadAllText(fi.FullName, Encoding.UTF8); int counter = 0; foreach (Tuple <string, string> prefixSuffix in prefixSuffixes) { if (counter == 1) { file = AddCoreToState(file, cmh, prefixSuffix.Item1, prefixSuffix.Item2); } if (counter == 0) { file = SwapTag(file, cmh.tagFrom.ToUpperInvariant(), cmh.tagTo.ToUpperInvariant(), prefixSuffix.Item1, prefixSuffix.Item2); } counter++; } File.WriteAllText(fi.FullName, file); }