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(); } }
void AddControlLine(SourceFile outputFile, PatchCommand cmd) { PatchWriter.WriteCommand(outputFile, cmd, false); }
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(); } } }
/// <summary> /// create patch file from a patch file in disk /// </summary> /// <param name="filename"></param> /// <returns></returns> public static PatchFile BuildPatchFile(string filename) { //if (filename == "d:\\projects\\CefBridge\\cef3\\cefclient\\browser\\client_handler.cc") //{ //} //create patch command for specific filename PatchFile patchFile = new PatchFile(filename); SourceFile sourceFile = new SourceFile(filename, false); sourceFile.ReadAllLines(); int j = sourceFile.LineCount; PatchTask ptask = null; int i = 0; string line = sourceFile.GetLine(0); //first line is original filename string originalFilename; if (GetOriginalFilename(line, out originalFilename)) { //change origial file name for patch file patchFile.OriginalFileName = originalFilename; i++; //next line } for (; i < j; ++i) { line = sourceFile.GetLine(i).TrimStart(); if (line.StartsWith("//###_")) { //what is the comamnd int pos0 = line.IndexOf(' '); //first (must have if (pos0 < 0) { throw new NotSupportedException("pos 0"); } else { string additionalInfo; int taskId = GetTaskId(line, pos0 + 1, out additionalInfo); string cmdline = line.Substring(0, pos0); switch (cmdline) { case PatchCommand.START: //start new patch task //read next line for info { i++; string cmd_value = sourceFile.GetLine(i); //create new task ptask = new PatchTask(cmd_value, taskId); patchFile.AddTask(ptask); } break; case PatchCommand.APPPEND_START: //start collect append string //until find append_stop { var collectAppendStBuilder = new StringBuilder(); i++; string cmd_value = sourceFile.GetLine(i); line = cmd_value.TrimStart(); //eval do { if (line.StartsWith(PatchCommand.APPPEND_STOP)) { //stop here break; } else { if (line.StartsWith("//###_")) { //other command throw new NotSupportedException(); } else { //collect this line collectAppendStBuilder.AppendLine(line); //read next line i++; line = sourceFile.GetLine(i).TrimStart(); } } } while (true); //finish command ptask.Append(collectAppendStBuilder.ToString()); } break; case PatchCommand.APPPEND_STOP: throw new NotSupportedException(); case PatchCommand.FIND_NEXT_LANDMARK: { i++; string cmd_value = sourceFile.GetLine(i); if (taskId != ptask.TaskId) { throw new NotSupportedException(); } ptask.FindNext(cmd_value); } break; case PatchCommand.FOLLOW_BY: { i++; string cmd_value = sourceFile.GetLine(i); if (taskId != ptask.TaskId) { throw new NotSupportedException(); } ptask.FollowBy(cmd_value); } break; case PatchCommand.SKIP_UNTIL_AND_ACCEPT: { i++; string cmd_value = sourceFile.GetLine(i); if (taskId != ptask.TaskId) { throw new NotSupportedException(); } ptask.SkipUntilAndAccept(cmd_value); } break; case PatchCommand.SKIP_UNTIL_PASS: { //has 2 parameters if (additionalInfo == null) { throw new NotSupportedException(); } // ptask.SkipUntilPass(additionalInfo); } break; default: throw new NotSupportedException(); } } } } return((ptask != null && ptask.CommandCount > 0) ? patchFile : null); }