/// <summary> /// create patch file from a patch file in disk /// </summary> /// <param name="filename"></param> /// <returns></returns> public static PatchFile BuildPatchFile(string filename) { //create patch command for specific filename PatchFile patchFile = new PatchFile(filename); SourceFile sourceFile = new SourceFile(filename, false); sourceFile.ReadAllLines(); int j = sourceFile.LineCount; if (j == 0) { return(null); } // 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 line = line.TrimEnd(); string cmdline; int taskId; string additionalInfo; ParseCommand(line, out cmdline, out taskId, out additionalInfo); switch (cmdline) { case PatchCommand.START: { //start new patch task //read next line for info i++; //read next line for land mark string cmd_value = sourceFile.GetLine(i); //create new task ptask = new PatchTask(cmd_value, taskId); ptask.Owner = patchFile; if (additionalInfo == "-X") //special cmd { ptask.PatchStartCmd = additionalInfo; } // patchFile.AddTask(ptask); } break; case PatchCommand.BEGIN: { //begin block *** //create new patch block ptask = new PatchTask("", taskId); //we will set land mark later ptask.Owner = patchFile; ptask.IsPatchBlock = true; patchFile.AddTask(ptask); ParseAutoContextPatchBlock(ptask, sourceFile, ref i); //parse auto context patch block } 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(); } } } if (patchFile.TaskCount > 0) { return(patchFile); } else { return((ptask != null && ptask.CommandCount > 0) ? patchFile : null); } }
/// <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); }