/// <summary> /// save patch file to disk /// </summary> /// <param name="filename"></param> public void SavePatchFile(string filename) { //save all patch task int j = patchTasks.Count; var output = new SourceFile(filename, true); //first line of patch file is original file name output.AddLine(_ORIGINAL + " " + this.OriginalFileName); for (int i = 0; i < j; ++i) { PatchTask ptask = patchTasks[i]; List <PatchCommand> cmds = ptask.GetCommands(); //each task begin with start command output.AddLine(PatchCommand.START + " " + ptask.TaskId); output.AddLine(ptask.LandMark); int cmdCount = cmds.Count; for (int n = 0; n < cmdCount; ++n) { PatchWriter.WriteCommand(output, cmds[n], true); } } output.Save(); }
public void PatchContent() { //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"); } else { //can patch **** //input patch code with original filename output.AddLine(patchCode + " " + originalFilename); } output.IsCMakeFile = input.IsCMakeFile; //----------------------------------------------------------------- //can't patch again int j = patchTasks.Count; for (int i = 0; i < j; ++i) { patchTasks[i].PatchFile(input, output); } int linecount = input.LineCount; for (int i = input.CurrentLine; i < linecount; ++i) { output.AddLine(input.GetLine(i)); input.CurrentLine++; } output.Save(); }
public void PatchFile(SourceFile input, SourceFile output) { //------------------------------------------------------------ if (this.IsPatchBlock) { throw new NotSupportedException(); } //------------------------------------------------------------ //find start position of this task int curLine = input.CurrentLine; int lineCount = input.LineCount; bool foundLandMark = false; int cur_line_id = curLine; for (; cur_line_id < lineCount; ++cur_line_id) { string line = input.GetLine(cur_line_id); input.CurrentLine++; if (line.TrimStart().StartsWith(this.LandMark)) { //found land mark foundLandMark = true; string newStartLine = PatchCommand.START + " " + this.TaskId; if (!string.IsNullOrEmpty(this.PatchStartCmd)) { newStartLine += " " + this.PatchStartCmd; } if (!output.IsCMakeFile) { output.AddLine(newStartLine); } if (this.PatchStartCmd == "-X") { //replace the original line with the land mark output.AddLine(this.LandMark); } else { output.AddLine(line); } break; } else { //just add to output output.AddLine(line); } } if (!foundLandMark) { //report System.Diagnostics.Debug.WriteLine("not completed :" + input.Filename + ", landmark: " + this.LandMark); return; throw new NotSupportedException(); } //------------------------------------------------------------ //do actions... int j = commands.Count; for (int c = 0; c < j; ++c) { PatchCommand cmd = commands[c]; //find start position switch (cmd.comandKind) { case PatchCommandKind.FindNextLandMark: { //from current position find next until found curLine = input.CurrentLine; bool foundNextLandMark = false; for (int i = curLine; i < lineCount; ++i) { string line = input.GetLine(i); input.CurrentLine++; if (line.TrimStart().StartsWith(cmd.String)) { //found land mark foundNextLandMark = true; AddControlLine(output, cmd); //*** output.AddLine(line); break; } else { //just add to output output.AddLine(line); } } if (!foundNextLandMark) { throw new NotSupportedException("next land mark not found"); } } break; case PatchCommandKind.AppendStringStart: { AddControlLine(output, cmd); //*** } break; case PatchCommandKind.FollowBy: { //just 1 line must match string nextLine = input.GetLine(input.CurrentLine); input.CurrentLine++; if (nextLine.TrimStart().StartsWith(cmd.String)) { //match AddControlLine(output, cmd); //*** output.AddLine(nextLine); } else { throw new NotSupportedException(); } } break; case PatchCommandKind.SkipUntilPass: { curLine = input.CurrentLine; bool foundNextLandMark = false; for (int i = curLine; i < lineCount; ++i) { string line = input.GetLine(i); input.CurrentLine++; if (line.TrimStart().StartsWith(cmd.String)) { AddControlLine(output, cmd); //*** //found land mark foundNextLandMark = true; //not add this //just skip break; } else { //skip } } if (!foundNextLandMark) { throw new NotSupportedException("next land mark not found"); } } break; case PatchCommandKind.SkipUntilAndAccept: { curLine = input.CurrentLine; bool foundNextLandMark = false; for (int i = curLine; i < lineCount; ++i) { string line = input.GetLine(i); input.CurrentLine++; if (line.TrimStart().StartsWith(cmd.String)) { //found land mark foundNextLandMark = true; //accept this line AddControlLine(output, cmd); //*** output.AddLine(line); break; } else { //skip } } if (!foundNextLandMark) { throw new NotSupportedException("next land mark not found"); } } break; default: throw new NotSupportedException(); } } }
/// <summary> /// save patch file to disk /// </summary> /// <param name="filename"></param> public void SavePatchFile(string filename) { //save all patch task int j = patchTasks.Count; var output = new SourceFile(filename, true); //first line of patch file is original file name output.AddLine(_ORIGINAL + " " + this.OriginalFileName); for (int i = 0; i < j; ++i) { PatchTask ptask = patchTasks[i]; List <PatchCommand> cmds = ptask.GetCommands(); //each task begin with start command if (ptask.IsPatchBlock) { //this is patch block output.AddLine(PatchCommand.BEGIN + " 0"); //pre-notes/ post-notes int noteCount = ptask.preNotes.Count; for (int m = 0; m < noteCount; ++m) { //write each line of pre note output.AddLine(PatchCommand.PRE); output.AddLine(ptask.preNotes[m]); } //--------- int contentLineCount = ptask.ContentLines.Count; for (int m = 0; m < contentLineCount; ++m) { output.AddLine(ptask.ContentLines[m]); } //--------- noteCount = ptask.postNotes.Count; for (int m = 0; m < noteCount; ++m) { //write each line of post note output.AddLine(PatchCommand.POST); output.AddLine(ptask.postNotes[m]); } output.AddLine(PatchCommand.END + " 0"); } else { if (!string.IsNullOrEmpty(ptask.PatchStartCmd)) { output.AddLine(PatchCommand.START + " " + ptask.TaskId + " " + ptask.PatchStartCmd); } else { output.AddLine(PatchCommand.START + " " + ptask.TaskId); } output.AddLine(ptask.LandMark); int cmdCount = cmds.Count; for (int n = 0; n < cmdCount; ++n) { PatchWriter.WriteCommand(output, cmds[n], true); } } } output.Save(); }
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(); }
public static void WriteCommand(SourceFile outputFile, PatchCommand cmd, bool withValue) { string perfix_comment = outputFile.IsCMakeFile ? "# " : ""; switch (cmd.comandKind) { case PatchCommandKind.AppendStringStart: { outputFile.AddLine(perfix_comment + PatchCommand.APPPEND_START + " " + cmd.TaskId); var strReader = new StringReader(cmd.String); string appendLine = strReader.ReadLine(); while (appendLine != null) { outputFile.AddLine(appendLine); appendLine = strReader.ReadLine(); } outputFile.AddLine(perfix_comment + PatchCommand.APPPEND_STOP); } break; case PatchCommandKind.AppendStringStop: outputFile.AddLine(perfix_comment + PatchCommand.APPPEND_STOP + " " + cmd.TaskId); if (withValue) { outputFile.AddLine(cmd.String); } break; case PatchCommandKind.FindNextLandMark: outputFile.AddLine(perfix_comment + PatchCommand.FIND_NEXT_LANDMARK + " " + cmd.TaskId); if (withValue) { outputFile.AddLine(cmd.String); } break; case PatchCommandKind.FollowBy: outputFile.AddLine(perfix_comment + PatchCommand.FOLLOW_BY + " " + cmd.TaskId); if (withValue) { outputFile.AddLine(cmd.String); } break; case PatchCommandKind.SkipUntilAndAccept: outputFile.AddLine(perfix_comment + PatchCommand.SKIP_UNTIL_AND_ACCEPT + " " + cmd.TaskId); if (withValue) { outputFile.AddLine(cmd.String); } break; case PatchCommandKind.SkipUntilPass: outputFile.AddLine(perfix_comment + PatchCommand.SKIP_UNTIL_PASS + " " + cmd.TaskId + " " + cmd.String); //value is add in the same line *** break; default: throw new NotSupportedException(); } }
public void PatchFile(SourceFile input, SourceFile output) { //find start position of this task int curLine = input.CurrentLine; int lineCount = input.LineCount; bool foundLandMark = false; for (int i = curLine; i < lineCount; ++i) { string line = input.GetLine(i); input.CurrentLine++; if (line.TrimStart().StartsWith(this.LandMark)) { //found land mark foundLandMark = true; if (!output.IsCMakeFile) { output.AddLine(PatchCommand.START + " " + this.TaskId); } output.AddLine(line); break; } else { //just add to output output.AddLine(line); } } if (!foundLandMark) { return; throw new NotSupportedException(); } //------------------------------------------------------------ //do actions... int j = commands.Count; for (int c = 0; c < j; ++c) { PatchCommand cmd = commands[c]; //find start position switch (cmd.comandKind) { case PatchCommandKind.FindNextLandMark: { //from current position find next until found curLine = input.CurrentLine; bool foundNextLandMark = false; for (int i = curLine; i < lineCount; ++i) { string line = input.GetLine(i); input.CurrentLine++; if (line.TrimStart().StartsWith(cmd.String)) { //found land mark foundNextLandMark = true; AddControlLine(output, cmd); //*** output.AddLine(line); break; } else { //just add to output output.AddLine(line); } } if (!foundNextLandMark) { throw new NotSupportedException("next land mark not found"); } } break; case PatchCommandKind.AppendStringStart: { AddControlLine(output, cmd); //*** } break; case PatchCommandKind.FollowBy: { //just 1 line must match string nextLine = input.GetLine(input.CurrentLine); input.CurrentLine++; if (nextLine.TrimStart().StartsWith(cmd.String)) { //match AddControlLine(output, cmd); //*** output.AddLine(nextLine); } else { throw new NotSupportedException(); } } break; case PatchCommandKind.SkipUntilPass: { curLine = input.CurrentLine; bool foundNextLandMark = false; for (int i = curLine; i < lineCount; ++i) { string line = input.GetLine(i); input.CurrentLine++; if (line.TrimStart().StartsWith(cmd.String)) { AddControlLine(output, cmd); //*** //found land mark foundNextLandMark = true; //not add this //just skip break; } else { //skip } } if (!foundNextLandMark) { throw new NotSupportedException("next land mark not found"); } } break; case PatchCommandKind.SkipUntilAndAccept: { curLine = input.CurrentLine; bool foundNextLandMark = false; for (int i = curLine; i < lineCount; ++i) { string line = input.GetLine(i); input.CurrentLine++; if (line.TrimStart().StartsWith(cmd.String)) { //found land mark foundNextLandMark = true; //accept this line AddControlLine(output, cmd); //*** output.AddLine(line); break; } else { //skip } } if (!foundNextLandMark) { throw new NotSupportedException("next land mark not found"); } } break; default: throw new NotSupportedException(); } } }