public void PatchContent() { PatchingMsg = null; //1. check if file is exist string originalFilename = this.OriginalFileName; if (!File.Exists(originalFilename)) { throw new NotSupportedException("file not found!"); } SourceFile input = new SourceFile(originalFilename, false); input.ReadAllLines(); SourceFile output = new SourceFile(originalFilename, true); string patchCode; if (CheckIfFileWasPatched(input, out patchCode)) { //can't patch //throw new NotSupportedException("not patch again in this file"); PatchingMsg = this.OriginalFileName + " => has be patched, so skip this file"; return; } else { //can patch **** //input patch code with original filename output.AddLine(patchCode + " " + originalFilename); } output.IsCMakeFile = input.IsCMakeFile; //----------------------------------------------------------------- //other patch that is not block-patch int j = patchTasks.Count; for (int i = 0; i < j; ++i) { PatchTask ptask = patchTasks[i]; if (!ptask.IsPatchBlock) { ptask.PatchFile(input, output); } else { ptask.PatchBlockSouldStartAt = output.LineCount; } } int linecount = input.LineCount; for (int i = input.CurrentLine; i < linecount; ++i) { output.AddLine(input.GetLine(i)); input.CurrentLine++; } //----------------------------------------------------------------- //only block-patch for (int i = 0; i < j; ++i) { PatchTask ptask = patchTasks[i]; if (ptask.IsPatchBlock) { int newLineCount = ptask.PatchBlockFile(output); if (newLineCount == 1) { for (int n = i + 1; n < j; ++n) { //adjust new offset PatchTask p2 = patchTasks[n]; if (p2.IsPatchBlock) { p2.PatchBlockSouldStartAt += 1; } } } else { System.Diagnostics.Debug.WriteLine("output is not saved: " + ptask.Owner.OriginalFileName); return; } } } output.Save(); }