public void DemoDiffOfAdditionToLargeText() { var text1 = @"I am the very model of a modern Major-General, I've information vegetable, animal, and mineral, I know the kings of England, and I quote the fights historical, From Marathon to Waterloo, in order categorical. I am the very model of a cartoon individual, My animation's comical, unusual, and whimsical, I'm quite adept at funny gags, comedic theory I have read, From wicked puns and stupid jokes to anvils that drop on your head."; var text2 = @"I am the very model of a modern Major-General, I've information vegetable, animal, and mineral, I know the kings of England, and I quote the fights historical, From Marathon to Waterloo, in order categorical. I am the very model of a cartoon individual, My animation's comical, unusual, and whimsical, I'm quite adept at funny gags, comedic theory I have read, From wicked puns and stupid jokes to anvils that drop on your head. And that is all I have to say about that."; var sut = new DiffMatchPatch(); sut.DiffTimeout = 0; var diffs = sut.DiffMain(text1, text2); sut.DiffCleanupEfficiency(diffs); var patches = sut.PatchMake(diffs); var patch = sut.PatchToText(patches); Approvals.Verify(patch); }
public static void Patch(this TableOperations <MeterConfiguration> meterConfigurationTable, MeterConfiguration meterConfiguration, string newConfigText) { string unpatchedText = Unpatch(meterConfigurationTable, meterConfiguration); DiffMatchPatch patchProvider = new DiffMatchPatch(); List <Patch> patches = patchProvider.PatchMake(newConfigText, unpatchedText); if (patches.Count > 0) { MeterConfiguration newConfiguration = new MeterConfiguration(); newConfiguration.MeterID = meterConfiguration.MeterID; newConfiguration.ConfigKey = meterConfiguration.ConfigKey; newConfiguration.ConfigText = newConfigText; newConfiguration.RevisionMajor = newConfiguration.RevisionMajor + 1; newConfiguration.RevisionMinor = 0; meterConfigurationTable.AddNewRecord(newConfiguration); meterConfiguration.DiffID = meterConfigurationTable.Connection.ExecuteScalar <int>("SELECT @@IDENTITY"); meterConfiguration.ConfigText = patchProvider.PatchToText(patches); meterConfigurationTable.UpdateRecord(meterConfiguration); } }
public bool Execute(MeterDataSet meterDataSet) { if (meterDataSet.Type != DataSetType.BENConfig) { return(false); } using (AdoDataConnection connection = new AdoDataConnection("systemSettings")) { FileInfo fi = new FileInfo(meterDataSet.FilePath); DateTime lastWriteTime = DateTime.ParseExact(string.Join(",", fi.Name.Split(',').Take(2)), "yyMMdd,HHmmssfff", CultureInfo.InvariantCulture); // see if there is already a record that matches this file ConfigFileChanges configFileChanges = new TableOperations <ConfigFileChanges>(connection).QueryRecordWhere("MeterID = {0} AND FileName = {1} AND LastWriteTime = {2}", meterDataSet.Meter.ID, $"{meterDataSet.Meter.AssetKey}.cfg", lastWriteTime); // if a record already exists for this file skip it. There was probably an error. if (configFileChanges != null) { return(false); } configFileChanges = new ConfigFileChanges(); // create new record configFileChanges.MeterID = meterDataSet.Meter.ID; configFileChanges.FileName = $"{meterDataSet.Meter.AssetKey}.cfg"; configFileChanges.LastWriteTime = lastWriteTime; configFileChanges.Text = meterDataSet.Text.Replace("\r", ""); string[] data = File.ReadAllLines(meterDataSet.FilePath); int[] channelCounts = data[1].Split(',').Select(x => int.Parse(x.Replace("A", "").Replace("D", ""))).ToArray(); int totalChannels = channelCounts[0]; // get portion of cfg file that contains channel mappings string relevantPortion = string.Join("\n", data.Take(2 + totalChannels)); // get the previous record for this file ConfigFileChanges lastChanges = new TableOperations <ConfigFileChanges>(connection).QueryRecord("LastWriteTime DESC", new RecordRestriction("MeterID = {0} AND FileName = {1} AND LastWriteTime < {2}", meterDataSet.Meter.ID, $"{meterDataSet.Meter.AssetKey}.cfg", lastWriteTime)); // if there were no previous records for this file if (lastChanges == null) { lastChanges = new ConfigFileChanges(); lastChanges.Text = configFileChanges.Text.Replace("\r", ""); // make diffs DiffMatchPatch dmp = new DiffMatchPatch(); List <Diff> diff = dmp.DiffMain(relevantPortion, relevantPortion); dmp.DiffCleanupSemantic(diff); configFileChanges.Html = dmp.DiffPrettyHtml(diff).Replace("¶", ""); configFileChanges.Changes = 0; } else { string[] data2 = lastChanges.Text.Split('\n'); int[] channelCounts2 = data2[1].Split(',').Select(x => int.Parse(x.Replace("A", "").Replace("D", ""))).ToArray(); int totalChannels2 = channelCounts2[0]; // get portion of cfg file that contains channel mappings string relevantPortion2 = string.Join("\n", data2.Take(2 + totalChannels2)); // make diffs DiffMatchPatch dmp = new DiffMatchPatch(); List <Diff> diff = dmp.DiffMain(relevantPortion2, relevantPortion); List <Patch> patch = dmp.PatchMake(relevantPortion2, relevantPortion); dmp.DiffCleanupSemantic(diff); configFileChanges.Html = dmp.DiffPrettyHtml(diff).Replace("¶", ""); configFileChanges.Changes = patch.Count; if (patch.Count == 0) { return(false); } } // write new record to db meterDataSet.ConfigChanges = configFileChanges.Changes; new TableOperations <ConfigFileChanges>(connection).AddNewRecord(configFileChanges); return(true); } }
public bool Execute(MeterDataSet meterDataSet) { if (meterDataSet.Type != DataSetType.Config) { return(false); } using (AdoDataConnection connection = new AdoDataConnection("systemSettings")) { FileInfo fi = new FileInfo(meterDataSet.FilePath); // see if there is already a record that matches this file ConfigFileChanges configFileChanges = new TableOperations <ConfigFileChanges>(connection).QueryRecordWhere("MeterID = {0} AND FileName = {1} AND LastWriteTime = {2}", meterDataSet.Meter.ID, fi.Name, fi.LastWriteTime); // if a record already exists for this file skip it. There was probably an error. if (configFileChanges != null) { return(false); } configFileChanges = new ConfigFileChanges(); // create new record configFileChanges.MeterID = meterDataSet.Meter.ID; configFileChanges.FileName = fi.Name; configFileChanges.LastWriteTime = fi.LastWriteTime; configFileChanges.Text = meterDataSet.Text; // get the previous record for this file ConfigFileChanges lastChanges = new TableOperations <ConfigFileChanges>(connection).QueryRecord("LastWriteTime DESC", new RecordRestriction("MeterID = {0} AND FileName = {1} AND LastWriteTime < {2}", meterDataSet.Meter.ID, fi.Name, fi.LastWriteTime)); // if there were no previous records for this file, just diff it against itself because we need an intial record. if (lastChanges == null) { lastChanges = new ConfigFileChanges(); lastChanges.Text = meterDataSet.Text; DiffMatchPatch dmp = new DiffMatchPatch(); List <Diff> diff = dmp.DiffMain(lastChanges.Text, configFileChanges.Text); List <Patch> patch = dmp.PatchMake(lastChanges.Text, configFileChanges.Text); dmp.DiffCleanupSemantic(diff); configFileChanges.Html = dmp.DiffPrettyHtml(diff).Replace("¶", ""); configFileChanges.Changes = patch.Count; // write new record to db meterDataSet.ConfigChanges = configFileChanges.Changes; } else { // make diffs DiffMatchPatch dmp = new DiffMatchPatch(); List <Diff> diff = dmp.DiffMain(lastChanges.Text, configFileChanges.Text); List <Patch> patch = dmp.PatchMake(lastChanges.Text, configFileChanges.Text); if (patch.Count == 0) { return(false); } dmp.DiffCleanupSemantic(diff); configFileChanges.Html = dmp.DiffPrettyHtml(diff).Replace("¶", ""); configFileChanges.Changes = patch.Count; // write new record to db meterDataSet.ConfigChanges = configFileChanges.Changes; } new TableOperations <ConfigFileChanges>(connection).AddNewRecord(configFileChanges); return(true); } }